Pepper_56_C++_interfaces
Pepper_56_C++_interfaces
 All Classes Namespaces Files Functions Typedefs Enumerations Macros Groups
var.h
Go to the documentation of this file.
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_CPP_VAR_H_
6 #define PPAPI_CPP_VAR_H_
7 
8 #include <stdint.h>
9 
10 #include <string>
11 #include <vector>
12 
13 #include "ppapi/c/pp_var.h"
14 #include "ppapi/cpp/pass_ref.h"
15 #include "ppapi/cpp/resource.h"
16 
20 namespace pp {
21 
23 class Var {
24  public:
26  struct Null {};
27 
30  Var();
31 
33  Var(Null);
34 
38  Var(bool b);
39 
43  Var(int32_t i);
44 
48  Var(double d);
49 
51  Var(const char* utf8_str); // Must be encoded in UTF-8.
52 
54  Var(const std::string& utf8_str); // Must be encoded in UTF-8.
55 
57  explicit Var(const pp::Resource& resource);
58 
64  Var(PassRef, const PP_Var& var) {
65  var_ = var;
66  is_managed_ = true;
67  }
68 
70  explicit Var(const PP_Var& var);
71 
72  struct DontManage {};
73 
80  Var(DontManage, const PP_Var& var) {
81  var_ = var;
82  is_managed_ = false;
83  }
84 
86  Var(const Var& other);
87 
89  virtual ~Var();
90 
96  virtual Var& operator=(const Var& other);
97 
105  bool operator==(const Var& other) const;
106 
110  bool is_undefined() const { return var_.type == PP_VARTYPE_UNDEFINED; }
111 
115  bool is_null() const { return var_.type == PP_VARTYPE_NULL; }
116 
120  bool is_bool() const { return var_.type == PP_VARTYPE_BOOL; }
121 
125  bool is_string() const { return var_.type == PP_VARTYPE_STRING; }
126 
130  bool is_object() const { return var_.type == PP_VARTYPE_OBJECT; }
131 
135  bool is_array() const { return var_.type == PP_VARTYPE_ARRAY; }
136 
140  bool is_dictionary() const { return var_.type == PP_VARTYPE_DICTIONARY; }
141 
145  bool is_resource() const { return var_.type == PP_VARTYPE_RESOURCE; }
146 
155  bool is_int() const { return var_.type == PP_VARTYPE_INT32; }
156 
165  bool is_double() const { return var_.type == PP_VARTYPE_DOUBLE; }
166 
171  bool is_number() const {
172  return var_.type == PP_VARTYPE_INT32 ||
173  var_.type == PP_VARTYPE_DOUBLE;
174  }
175 
177  bool is_array_buffer() const { return var_.type == PP_VARTYPE_ARRAY_BUFFER; }
178 
184  bool AsBool() const;
185 
197  int32_t AsInt() const;
198 
210  double AsDouble() const;
211 
216  std::string AsString() const;
217 
222  pp::Resource AsResource() const;
223 
228  const PP_Var& pp_var() const {
229  return var_;
230  }
231 
239  PP_Var Detach() {
240  PP_Var ret = var_;
241  var_ = PP_MakeUndefined();
242  is_managed_ = true;
243  return ret;
244  }
245 
252  std::string DebugString() const;
253 
277  class OutException {
278  public:
281  : output_(v),
282  originally_had_exception_(v && !v->is_undefined()) {
283  if (output_) {
284  temp_ = output_->var_;
285  } else {
286  temp_.padding = 0;
287  temp_.type = PP_VARTYPE_UNDEFINED;
288  }
289  }
290 
293  if (output_ && !originally_had_exception_)
294  *output_ = Var(PASS_REF, temp_);
295  }
296 
297  PP_Var* get() {
298  if (output_)
299  return &temp_;
300  return NULL;
301  }
302 
303  private:
304  Var* output_;
305  bool originally_had_exception_;
306  PP_Var temp_;
307  };
308 
309  protected:
310  PP_Var var_;
311 
312  // |is_managed_| indicates if the instance manages |var_|.
313  // You need to check if |var_| is refcounted to call Release().
314  bool is_managed_;
315 
316  private:
317  // Prevent an arbitrary pointer argument from being implicitly converted to
318  // a bool at Var construction. If somebody makes such a mistake, they will
319  // get a compilation error.
320  Var(void* non_scriptable_object_pointer);
321 };
322 
323 } // namespace pp
324 
325 #endif // PPAPI_CPP_VAR_H_
bool is_array_buffer() const
This function determines if this Var is an ArrayBuffer.
Definition: var.h:177
bool is_number() const
Definition: var.h:171
Definition: var.h:277
PP_Var Detach()
Definition: var.h:239
std::string DebugString() const
bool is_dictionary() const
Definition: var.h:140
bool is_object() const
Definition: var.h:130
bool is_double() const
Definition: var.h:165
std::string AsString() const
Special value passed to constructor to make NULL.
Definition: var.h:26
bool is_string() const
Definition: var.h:125
OutException(Var *v)
A constructor.
Definition: var.h:280
~OutException()
Destructor.
Definition: var.h:292
bool is_undefined() const
Definition: var.h:110
virtual ~Var()
Destructor.
Definition: var.h:72
bool is_array() const
Definition: var.h:135
Var(PassRef, const PP_Var &var)
Definition: var.h:64
double AsDouble() const
const PP_Var & pp_var() const
Definition: var.h:228
bool is_resource() const
Definition: var.h:145
bool is_int() const
Definition: var.h:155
bool AsBool() const
bool is_null() const
Definition: var.h:115
bool operator==(const Var &other) const
PassRef
Definition: pass_ref.h:17
int32_t AsInt() const
Var(DontManage, const PP_Var &var)
Definition: var.h:80
virtual Var & operator=(const Var &other)
A generic type used for passing data types between the module and the page.
Definition: var.h:23
pp::Resource AsResource() const
bool is_bool() const
Definition: var.h:120
A reference counted module resource.
Definition: resource.h:20