• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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_PP_VAR_H_
6 #define PPAPI_SHARED_IMPL_SCOPED_PP_VAR_H_
7 
8 #include <stdlib.h>
9 
10 #include "base/macros.h"
11 #include "ppapi/c/pp_var.h"
12 #include "ppapi/shared_impl/ppapi_shared_export.h"
13 
14 namespace ppapi {
15 
16 class PPAPI_SHARED_EXPORT ScopedPPVar {
17  public:
18   struct PassRef {};
19 
20   ScopedPPVar();
21 
22   // Takes one reference to the given var.
23   explicit ScopedPPVar(const PP_Var& v);
24 
25   // Assumes responsibility for one ref that the var already has.
26   ScopedPPVar(const PassRef&, const PP_Var& v);
27 
28   // Implicit copy constructor allowed.
29   ScopedPPVar(const ScopedPPVar& other);
30 
31   ~ScopedPPVar();
32 
33   ScopedPPVar& operator=(const PP_Var& r);
34   ScopedPPVar& operator=(const ScopedPPVar& other) {
35     return operator=(other.var_);
36   }
37 
get()38   const PP_Var& get() const { return var_; }
39 
40   // Returns the PP_Var, passing the reference to the caller. This class
41   // will no longer hold the var.
42   PP_Var Release();
43 
44  private:
45   PP_Var var_;
46 };
47 
48 // An array of PP_Vars which will be deallocated and have their references
49 // decremented when they go out of scope.
50 class PPAPI_SHARED_EXPORT ScopedPPVarArray {
51  public:
52   struct PassPPBMemoryAllocatedArray {};
53 
54   // Assumes responsibility for one ref of each of the vars in the array as
55   // well as the array memory allocated by PPB_Memory_Dev.
56   // TODO(raymes): Add compatibility for arrays allocated with C++ "new".
57   ScopedPPVarArray(const PassPPBMemoryAllocatedArray&,
58                    PP_Var* array,
59                    size_t size);
60 
61   explicit ScopedPPVarArray(size_t size);
62   ~ScopedPPVarArray();
63 
64   // Passes ownership of the vars and the underlying array memory to the caller.
65   // Note that the memory has been allocated with PPB_Memory_Dev.
66   PP_Var* Release(const PassPPBMemoryAllocatedArray&);
67 
get()68   PP_Var* get() { return array_; }
size()69   size_t size() { return size_; }
70 
71   // Takes a ref to |var|. The refcount of the existing var will be decremented.
72   void Set(size_t index, const ScopedPPVar& var);
73   const PP_Var& operator[](size_t index) { return array_[index]; }
74 
75  private:
76   // TODO(raymes): Consider supporting copy/assign.
77   DISALLOW_COPY_AND_ASSIGN(ScopedPPVarArray);
78 
79   PP_Var* array_;
80   size_t size_;
81 };
82 
83 }  // namespace ppapi
84 
85 #endif  // PPAPI_SHARED_IMPL_SCOPED_PP_VAR_H_
86