blob: 7823a73af5feeececa15e102ad60f88a57c3536d [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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.
15namespace pp {
16
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000017class Var;
Torne (Richard Coles)0f1bc082013-11-06 12:27:47 +000018
Torne (Richard Coles)58218062012-11-14 11:43:16 +000019/// A reference counted module resource.
20class Resource {
21 public:
22 /// The default constructor.
23 Resource();
24
25 /// A constructor for copying a resource.
26 ///
27 /// @param[in] other A <code>Resource</code>.
28 Resource(const Resource& other);
29
30 /// Destructor.
31 virtual ~Resource();
32
33 /// This function assigns one <code>Resource</code> to another
34 /// <code>Resource</code>.
35 ///
36 /// @param[in] other A Resource.
37 ///
38 /// @return A Resource containing the assigned Resource.
39 Resource& operator=(const Resource& other);
40
41 /// This functions determines if this resource is invalid or
42 /// uninitialized.
43 ///
44 /// @return true if this resource is invalid or uninitialized.
45 bool is_null() const { return !pp_resource_; }
46
47 PP_Resource pp_resource() const { return pp_resource_; }
48
49 /// This function releases ownership of this resource and returns it to the
50 /// caller.
51 ///
52 /// Note that the reference count on the resource is unchanged and the caller
53 /// needs to release the resource.
54 ///
55 /// @return The detached <code>PP_Resource</code>.
56 PP_Resource detach();
57
58 protected:
59 /// A constructor used when a <code>PP_Resource</code> is provided as a
60 /// return value whose reference count we need to increment.
61 ///
62 /// @param[in] resource A <code>PP_Resource</code> corresponding to a
63 /// resource.
64 explicit Resource(PP_Resource resource);
65
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);
69
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
74 /// current resource.
75 ///
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
80 /// increased).
81 ///
82 /// @param[in] resource A <code>PP_Resource</code> corresponding to a
83 /// resource.
84 void PassRefFromConstructor(PP_Resource resource);
85
Torne (Richard Coles)0f1bc082013-11-06 12:27:47 +000086 /// Sets this resource to null. This releases ownership of the resource.
87 void Clear();
88
Torne (Richard Coles)58218062012-11-14 11:43:16 +000089 private:
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000090 friend class Var;
Torne (Richard Coles)0f1bc082013-11-06 12:27:47 +000091
Torne (Richard Coles)58218062012-11-14 11:43:16 +000092 PP_Resource pp_resource_;
93};
94
95} // namespace pp
96
97inline bool operator==(const pp::Resource& lhs, const pp::Resource& rhs) {
98 return lhs.pp_resource() == rhs.pp_resource();
99}
100
101inline bool operator!=(const pp::Resource& lhs, const pp::Resource& rhs) {
102 return !(lhs == rhs);
103}
104
105#endif // PPAPI_CPP_RESOURCE_H_