Pepper_31_C++_interfaces
resource.h
Go to the documentation of this file.
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_CPP_RESOURCE_H_
6 #define PPAPI_CPP_RESOURCE_H_
7 
8 #include "ppapi/c/pp_resource.h"
9 #include "ppapi/cpp/instance_handle.h"
10 #include "ppapi/cpp/pass_ref.h"
11 
12 /// @file
13 /// This file defines a <code>Resource</code> type representing data associated
14 /// with the module.
15 namespace pp {
16 
17 /// A reference counted module resource.
18 class Resource {
19  public:
20  /// The default constructor.
21  Resource();
22 
23  /// A constructor for copying a resource.
24  ///
25  /// @param[in] other A <code>Resource</code>.
26  Resource(const Resource& other);
27 
28  /// Destructor.
29  virtual ~Resource();
30 
31  /// This function assigns one <code>Resource</code> to another
32  /// <code>Resource</code>.
33  ///
34  /// @param[in] other A Resource.
35  ///
36  /// @return A Resource containing the assigned Resource.
37  Resource& operator=(const Resource& other);
38 
39  /// This functions determines if this resource is invalid or
40  /// uninitialized.
41  ///
42  /// @return true if this resource is invalid or uninitialized.
43  bool is_null() const { return !pp_resource_; }
44 
45  PP_Resource pp_resource() const { return pp_resource_; }
46 
47  /// This function releases ownership of this resource and returns it to the
48  /// caller.
49  ///
50  /// Note that the reference count on the resource is unchanged and the caller
51  /// needs to release the resource.
52  ///
53  /// @return The detached <code>PP_Resource</code>.
55 
56  protected:
57  /// A constructor used when a <code>PP_Resource</code> is provided as a
58  /// return value whose reference count we need to increment.
59  ///
60  /// @param[in] resource A <code>PP_Resource</code> corresponding to a
61  /// resource.
62  explicit Resource(PP_Resource resource);
63 
64  /// Constructor used when a <code>PP_Resource</code> already has a ref count
65  /// assigned. Add additional refcount is not taken.
66  Resource(PassRef, PP_Resource resource);
67 
68  /// PassRefFromConstructor is called by derived class' constructors to
69  /// initialize this <code>Resource</code> with a <code>PP_Resource</code>
70  /// that has already had its reference count incremented by
71  /// <code>Core::AddRefResource</code>. It also assumes this object has no
72  /// current resource.
73  ///
74  /// The intended usage of this function that the derived class constructor
75  /// will call the default <code>Resource</code> constructor, then make a call
76  /// to create a resource. It then wants to assign the new resource (which,
77  /// since it was returned by the browser, already had its reference count
78  /// increased).
79  ///
80  /// @param[in] resource A <code>PP_Resource</code> corresponding to a
81  /// resource.
82  void PassRefFromConstructor(PP_Resource resource);
83 
84  private:
85  PP_Resource pp_resource_;
86 };
87 
88 } // namespace pp
89 
90 inline bool operator==(const pp::Resource& lhs, const pp::Resource& rhs) {
91  return lhs.pp_resource() == rhs.pp_resource();
92 }
93 
94 inline bool operator!=(const pp::Resource& lhs, const pp::Resource& rhs) {
95  return !(lhs == rhs);
96 }
97 
98 #endif // PPAPI_CPP_RESOURCE_H_
void PassRefFromConstructor(PP_Resource resource)
Resource()
The default constructor.
PP_Resource detach()
bool operator==(const pp::Resource &lhs, const pp::Resource &rhs)
Definition: resource.h:90
Resource & operator=(const Resource &other)
virtual ~Resource()
Destructor.
PassRef
Definition: pass_ref.h:17
PP_Resource pp_resource() const
Definition: resource.h:45
bool operator!=(const pp::Resource &lhs, const pp::Resource &rhs)
Definition: resource.h:94
bool is_null() const
Definition: resource.h:43
A reference counted module resource.
Definition: resource.h:18