1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef PPAPI_SHARED_IMPL_SCOPED_RESOURCE_H_
6#define PPAPI_SHARED_IMPL_SCOPED_RESOURCE_H_
7
8#include "ppapi/c/pp_resource.h"
9#include "ppapi/shared_impl/ppapi_shared_export.h"
10
11namespace ppapi {
12
13class Resource;
14
15// This is a version of scoped_refptr but for PP_Resources.
16class PPAPI_SHARED_EXPORT ScopedPPResource {
17 public:
18  struct PassRef {};
19
20  ScopedPPResource();
21
22  // Takes one reference to the given resource.
23  explicit ScopedPPResource(PP_Resource resource);
24
25  // Assumes responsibility for one ref that the resource already has.
26  explicit ScopedPPResource(const PassRef&, PP_Resource resource);
27
28  // Helper to get the PP_Resource out of the given object and take a reference
29  // to it.
30  explicit ScopedPPResource(Resource* resource);
31
32  // Implicit copy constructor allowed.
33  ScopedPPResource(const ScopedPPResource& other);
34
35  ~ScopedPPResource();
36
37  ScopedPPResource& operator=(PP_Resource resource);
38  ScopedPPResource& operator=(const ScopedPPResource& resource);
39
40  // Returns the PP_Resource without affecting the refcounting.
41  PP_Resource get() const { return id_; }
42  operator PP_Resource() const { return id_; }
43
44  // Returns the PP_Resource, passing the reference to the caller. This class
45  // will no longer hold the resource.
46  PP_Resource Release();
47
48 private:
49  // Helpers to addref or release the id_ if it's non-NULL. The id_ value will
50  // be unchanged.
51  void CallAddRef();
52  void CallRelease();
53
54  PP_Resource id_;
55};
56
57}  // namespace ppapi
58
59#endif  // PPAPI_SHARED_IMPL_SCOPED_RESOURCE_H_
60