Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(191)

Side by Side Diff: ppapi/cpp/private/var_private.h

Issue 6823016: Create a VarPrivate interface to contain the scripting helper functions of Var. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ppapi/cpp/dev/scriptable_object_deprecated.h ('k') | ppapi/cpp/private/var_private.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef PPAPI_CPP_VAR_H_ 5 #ifndef PPAPI_CPP_PRIVATE_VAR_PRIVATE_H_
6 #define PPAPI_CPP_VAR_H_ 6 #define PPAPI_CPP_PRIVATE_VAR_PRIVATE_H_
7 7
8 #include <string> 8 #include "ppapi/cpp/var.h"
9 #include <vector>
10
11 #include "ppapi/c/pp_module.h"
12 #include "ppapi/c/pp_var.h"
13 9
14 namespace pp { 10 namespace pp {
15 11
16 class Instance; 12 // VarPrivate is a version of Var that exposes the private scripting API.
13 // It's designed to be mostly interchangable with Var since most callers will
14 // be dealing with Vars from various places.
15 class VarPrivate : public Var {
16 public:
17 VarPrivate() : Var() {}
18 VarPrivate(Null) : Var(Null()) {}
19 VarPrivate(bool b) : Var(b) {}
20 VarPrivate(int32_t i) : Var(i) {}
21 VarPrivate(double d) : Var(d) {}
22 VarPrivate(const char* utf8_str) : Var(utf8_str) {}
23 VarPrivate(const std::string& utf8_str) : Var(utf8_str) {}
24 VarPrivate(PassRef, PP_Var var) : Var(PassRef(), var) {}
25 VarPrivate(DontManage, PP_Var var) : Var(DontManage(), var) {}
26 VarPrivate(Instance* instance, deprecated::ScriptableObject* object);
27 VarPrivate(const Var& other) : Var(other) {}
17 28
18 namespace deprecated { 29 virtual ~VarPrivate() {}
19 class ScriptableObject;
20 }
21
22 class Var {
23 public:
24 struct Null {}; // Special value passed to constructor to make NULL.
25
26 Var(); // PP_Var of type Undefined.
27 Var(Null); // PP_Var of type Null.
28 Var(bool b);
29 Var(int32_t i);
30 Var(double d);
31 Var(const char* utf8_str); // Must be encoded in UTF-8.
32 Var(const std::string& utf8_str); // Must be encoded in UTF-8.
33
34 // This magic constructor is used when we've gotten a PP_Var as a return
35 // value that has already been addref'ed for us.
36 struct PassRef {};
37 Var(PassRef, PP_Var var) {
38 var_ = var;
39 needs_release_ = true;
40 }
41
42 // TODO(brettw): remove DontManage when this bug is fixed
43 // http://code.google.com/p/chromium/issues/detail?id=52105
44 // This magic constructor is used when we've given a PP_Var as an input
45 // argument from somewhere and that reference is managing the reference
46 // count for us. The object will not be AddRef'ed or Release'd by this
47 // class instance..
48 struct DontManage {};
49 Var(DontManage, PP_Var var) {
50 var_ = var;
51 needs_release_ = false;
52 }
53
54 // Takes ownership of the given pointer.
55 Var(Instance* instance, deprecated::ScriptableObject* object);
56
57 Var(const Var& other);
58
59 virtual ~Var();
60
61 Var& operator=(const Var& other);
62
63 bool operator==(const Var& other) const;
64
65 bool is_undefined() const { return var_.type == PP_VARTYPE_UNDEFINED; }
66 bool is_null() const { return var_.type == PP_VARTYPE_NULL; }
67 bool is_bool() const { return var_.type == PP_VARTYPE_BOOL; }
68 bool is_string() const { return var_.type == PP_VARTYPE_STRING; }
69 bool is_object() const { return var_.type == PP_VARTYPE_OBJECT; }
70
71 // IsInt and IsDouble return the internal representation. The JavaScript
72 // runtime may convert between the two as needed, so the distinction may
73 // not be relevant in all cases (int is really an optimization inside the
74 // runtime). So most of the time, you will want to check IsNumber.
75 bool is_int() const { return var_.type == PP_VARTYPE_INT32; }
76 bool is_double() const { return var_.type == PP_VARTYPE_DOUBLE; }
77 bool is_number() const {
78 return var_.type == PP_VARTYPE_INT32 ||
79 var_.type == PP_VARTYPE_DOUBLE;
80 }
81
82 // Assumes the internal representation IsBool. If it's not, it will assert
83 // in debug mode, and return false.
84 bool AsBool() const;
85
86 // AsInt and AsDouble implicitly convert between ints and doubles. This is
87 // because JavaScript doesn't have a concept of ints and doubles, only
88 // numbers. The distinction between the two is an optimization inside the
89 // compiler. Since converting from a double to an int may be lossy, if you
90 // care about the distinction, either always work in doubles, or check
91 // !IsDouble() before calling AsInt().
92 //
93 // These functions will assert in debug mode and return 0 if the internal
94 // representation is not IsNumber().
95 int32_t AsInt() const;
96 double AsDouble() const;
97
98 // This assumes the object is of type string. If it's not, it will assert
99 // in debug mode, and return an empty string.
100 std::string AsString() const;
101 30
102 // This assumes the object is of type object. If it's not, it will assert in 31 // This assumes the object is of type object. If it's not, it will assert in
103 // debug mode. If it is not an object or not a ScriptableObject type, returns 32 // debug mode. If it is not an object or not a ScriptableObject type, returns
104 // NULL. 33 // NULL.
105 deprecated::ScriptableObject* AsScriptableObject() const; 34 deprecated::ScriptableObject* AsScriptableObject() const;
106 35
107 bool HasProperty(const Var& name, Var* exception = NULL) const; 36 bool HasProperty(const Var& name, Var* exception = NULL) const;
108 bool HasMethod(const Var& name, Var* exception = NULL) const; 37 bool HasMethod(const Var& name, Var* exception = NULL) const;
109 Var GetProperty(const Var& name, Var* exception = NULL) const; 38 VarPrivate GetProperty(const Var& name, Var* exception = NULL) const;
110 void GetAllPropertyNames(std::vector<Var>* properties, 39 void GetAllPropertyNames(std::vector<Var>* properties,
111 Var* exception = NULL) const; 40 Var* exception = NULL) const;
112 void SetProperty(const Var& name, const Var& value, Var* exception = NULL); 41 void SetProperty(const Var& name, const Var& value, Var* exception = NULL);
113 void RemoveProperty(const Var& name, Var* exception = NULL); 42 void RemoveProperty(const Var& name, Var* exception = NULL);
114 Var Call(const Var& method_name, uint32_t argc, Var* argv, 43 VarPrivate Call(const Var& method_name, uint32_t argc, Var* argv,
115 Var* exception = NULL); 44 Var* exception = NULL);
116 Var Construct(uint32_t argc, Var* argv, Var* exception = NULL) const; 45 VarPrivate Construct(uint32_t argc, Var* argv, Var* exception = NULL) const;
117 46
118 // Convenience functions for calling functions with small # of args. 47 // Convenience functions for calling functions with small # of args.
119 Var Call(const Var& method_name, Var* exception = NULL); 48 VarPrivate Call(const Var& method_name, Var* exception = NULL);
120 Var Call(const Var& method_name, const Var& arg1, Var* exception = NULL); 49 VarPrivate Call(const Var& method_name, const Var& arg1,
121 Var Call(const Var& method_name, const Var& arg1, const Var& arg2, 50 Var* exception = NULL);
122 Var* exception = NULL); 51 VarPrivate Call(const Var& method_name, const Var& arg1, const Var& arg2,
123 Var Call(const Var& method_name, const Var& arg1, const Var& arg2, 52 Var* exception = NULL);
124 const Var& arg3, Var* exception = NULL); 53 VarPrivate Call(const Var& method_name, const Var& arg1, const Var& arg2,
125 Var Call(const Var& method_name, const Var& arg1, const Var& arg2, 54 const Var& arg3, Var* exception = NULL);
126 const Var& arg3, const Var& arg4, Var* exception = NULL); 55 VarPrivate Call(const Var& method_name, const Var& arg1, const Var& arg2,
127 56 const Var& arg3, const Var& arg4, Var* exception = NULL);
128 // Returns a const reference to the PP_Var managed by this Var object.
129 const PP_Var& pp_var() const {
130 return var_;
131 }
132
133 // Detaches from the internal PP_Var of this object, keeping the reference
134 // count the same. This is used when returning a PP_Var from an API function
135 // where the caller expects the return value to be AddRef'ed for it.
136 PP_Var Detach() {
137 PP_Var ret = var_;
138 var_ = PP_MakeUndefined();
139 needs_release_ = false;
140 return ret;
141 }
142
143 // Prints a short description "Var<X>" that can be used for logging, where
144 // "X" is the underlying scalar or "UNDEFINED" or "OBJ" as it does not call
145 // into the browser to get the object description.
146 std::string DebugString() const;
147 57
148 // For use when calling the raw C PPAPI when using the C++ Var as a possibly 58 // For use when calling the raw C PPAPI when using the C++ Var as a possibly
149 // NULL exception. This will handle getting the address of the internal value 59 // NULL exception. This will handle getting the address of the internal value
150 // out if it's non-NULL and fixing up the reference count. 60 // out if it's non-NULL and fixing up the reference count.
151 // 61 //
152 // Danger: this will only work for things with exception semantics, i.e. that 62 // Danger: this will only work for things with exception semantics, i.e. that
153 // the value will not be changed if it's a non-undefined exception. Otherwise, 63 // the value will not be changed if it's a non-undefined exception. Otherwise,
154 // this class will mess up the refcounting. 64 // this class will mess up the refcounting.
155 // 65 //
156 // This is a bit subtle: 66 // This is a bit subtle:
157 // - If NULL is passed, we return NULL from get() and do nothing. 67 // - If NULL is passed, we return NULL from get() and do nothing.
158 // 68 //
159 // - If a undefined value is passed, we return the address of a undefined var 69 // - If a undefined value is passed, we return the address of a undefined var
160 // from get and have the output value take ownership of that var. 70 // from get and have the output value take ownership of that var.
161 // 71 //
162 // - If a non-undefined value is passed, we return the address of that var 72 // - If a non-undefined value is passed, we return the address of that var
163 // from get, and nothing else should change. 73 // from get, and nothing else should change.
164 // 74 //
165 // Example: 75 // Example:
166 // void FooBar(a, b, Var* exception = NULL) { 76 // void FooBar(a, b, Var* exception = NULL) {
167 // foo_interface->Bar(a, b, Var::OutException(exception).get()); 77 // foo_interface->Bar(a, b, VarPrivate::OutException(exception).get());
168 // } 78 // }
169 class OutException { 79 class OutException {
170 public: 80 public:
171 OutException(Var* v) 81 OutException(Var* v)
172 : output_(v), 82 : output_(v),
173 originally_had_exception_(v && v->is_null()) { 83 originally_had_exception_(v && v->is_null()) {
174 if (output_) { 84 if (output_) {
175 temp_ = output_->var_; 85 temp_ = output_->pp_var();
176 } else { 86 } else {
177 temp_.padding = 0; 87 temp_.padding = 0;
178 temp_.type = PP_VARTYPE_UNDEFINED; 88 temp_.type = PP_VARTYPE_UNDEFINED;
179 } 89 }
180 } 90 }
181 ~OutException() { 91 ~OutException() {
182 if (output_ && !originally_had_exception_) 92 if (output_ && !originally_had_exception_)
183 *output_ = Var(PassRef(), temp_); 93 *output_ = Var(PassRef(), temp_);
184 } 94 }
185 95
186 PP_Var* get() { 96 PP_Var* get() {
187 if (output_) 97 if (output_)
188 return &temp_; 98 return &temp_;
189 return NULL; 99 return NULL;
190 } 100 }
191 101
192 private: 102 private:
193 Var* output_; 103 Var* output_;
194 bool originally_had_exception_; 104 bool originally_had_exception_;
195 PP_Var temp_; 105 PP_Var temp_;
196 }; 106 };
197 107
198 private: 108 private:
199 // Prevent an arbitrary pointer argument from being implicitly converted to 109 // Prevent an arbitrary pointer argument from being implicitly converted to
200 // a bool at Var construction. If somebody makes such a mistake, (s)he will 110 // a bool at Var construction. If somebody makes such a mistake, (s)he will
201 // get a compilation error. 111 // get a compilation error.
202 Var(void* non_scriptable_object_pointer); 112 VarPrivate(void* non_scriptable_object_pointer);
203
204 PP_Var var_;
205 bool needs_release_;
206 }; 113 };
207 114
208 } // namespace pp 115 } // namespace pp
209 116
210 #endif // PPAPI_CPP_VAR_H_ 117 #endif // PPAPI_CPP_PRIVATE_VAR_PRIVATE_H_
OLDNEW
« no previous file with comments | « ppapi/cpp/dev/scriptable_object_deprecated.h ('k') | ppapi/cpp/private/var_private.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698