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.
5 #ifndef PPAPI_CPP_RESOURCE_H_
6 #define PPAPI_CPP_RESOURCE_H_
8 #include "ppapi/c/pp_resource.h"
9 #include "ppapi/cpp/instance_handle.h"
10 #include "ppapi/cpp/pass_ref.h"
13 /// This file defines a <code>Resource</code> type representing data associated
19 /// A reference counted module resource.
22 /// The default constructor.
25 /// A constructor for copying a resource.
27 /// @param[in] other A <code>Resource</code>.
28 Resource(const Resource
& other
);
33 /// This function assigns one <code>Resource</code> to another
34 /// <code>Resource</code>.
36 /// @param[in] other A Resource.
38 /// @return A Resource containing the assigned Resource.
39 Resource
& operator=(const Resource
& other
);
41 /// This functions determines if this resource is invalid or
44 /// @return true if this resource is invalid or uninitialized.
45 bool is_null() const { return !pp_resource_
; }
47 PP_Resource
pp_resource() const { return pp_resource_
; }
49 /// This function releases ownership of this resource and returns it to the
52 /// Note that the reference count on the resource is unchanged and the caller
53 /// needs to release the resource.
55 /// @return The detached <code>PP_Resource</code>.
59 /// A constructor used when a <code>PP_Resource</code> is provided as a
60 /// return value whose reference count we need to increment.
62 /// @param[in] resource A <code>PP_Resource</code> corresponding to a
64 explicit Resource(PP_Resource resource
);
66 /// Constructor used when a <code>PP_Resource</code> already has a ref count
67 /// assigned. Add additional refcount is not taken.
68 Resource(PassRef
, PP_Resource resource
);
70 /// PassRefFromConstructor is called by derived class' constructors to
71 /// initialize this <code>Resource</code> with a <code>PP_Resource</code>
72 /// that has already had its reference count incremented by
73 /// <code>Core::AddRefResource</code>. It also assumes this object has no
76 /// The intended usage of this function that the derived class constructor
77 /// will call the default <code>Resource</code> constructor, then make a call
78 /// to create a resource. It then wants to assign the new resource (which,
79 /// since it was returned by the browser, already had its reference count
82 /// @param[in] resource A <code>PP_Resource</code> corresponding to a
84 void PassRefFromConstructor(PP_Resource resource
);
86 /// Sets this resource to null. This releases ownership of the resource.
92 PP_Resource pp_resource_
;
97 inline bool operator==(const pp::Resource
& lhs
, const pp::Resource
& rhs
) {
98 return lhs
.pp_resource() == rhs
.pp_resource();
101 inline bool operator!=(const pp::Resource
& lhs
, const pp::Resource
& rhs
) {
102 return !(lhs
== rhs
);
105 #endif // PPAPI_CPP_RESOURCE_H_