Pepper_56_C++_interfaces
Pepper_56_C++_interfaces
 All Classes Namespaces Files Functions Typedefs Enumerations Macros Groups
var_private.h
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.
4 
5 #ifndef PPAPI_CPP_PRIVATE_VAR_PRIVATE_H_
6 #define PPAPI_CPP_PRIVATE_VAR_PRIVATE_H_
7 
8 #include <stdint.h>
9 
10 #include "ppapi/cpp/var.h"
11 
12 namespace pp {
13 
14 class InstanceHandle;
15 
16 namespace deprecated {
17 class ScriptableObject;
18 }
19 
20 // VarPrivate is a version of Var that exposes the private scripting API.
21 // It's designed to be mostly interchangeable with Var since most callers will
22 // be dealing with Vars from various places.
23 class VarPrivate : public Var {
24  public:
25  VarPrivate() : Var() {}
26  VarPrivate(Null) : Var(Null()) {}
27  VarPrivate(bool b) : Var(b) {}
28  VarPrivate(int32_t i) : Var(i) {}
29  VarPrivate(double d) : Var(d) {}
30  VarPrivate(const char* utf8_str) : Var(utf8_str) {}
31  VarPrivate(const std::string& utf8_str) : Var(utf8_str) {}
32  VarPrivate(PassRef, PP_Var var) : Var(PassRef(), var) {}
33  VarPrivate(DontManage, PP_Var var) : Var(DontManage(), var) {}
34  VarPrivate(const InstanceHandle& instance,
36  VarPrivate(const Var& other) : Var(other) {}
37 
38  virtual ~VarPrivate() {}
39 
40  // This assumes the object is of type object. If it's not, it will assert in
41  // debug mode. If it is not an object or not a ScriptableObject type, returns
42  // NULL.
43  deprecated::ScriptableObject* AsScriptableObject() const;
44 
45  bool HasProperty(const Var& name, Var* exception = NULL) const;
46  bool HasMethod(const Var& name, Var* exception = NULL) const;
47  VarPrivate GetProperty(const Var& name, Var* exception = NULL) const;
48  void GetAllPropertyNames(std::vector<Var>* properties,
49  Var* exception = NULL) const;
50  void SetProperty(const Var& name, const Var& value, Var* exception = NULL);
51  void RemoveProperty(const Var& name, Var* exception = NULL);
52  VarPrivate Call(const Var& method_name, uint32_t argc, Var* argv,
53  Var* exception = NULL);
54  VarPrivate Construct(uint32_t argc, Var* argv, Var* exception = NULL) const;
55 
56  // Convenience functions for calling functions with small # of args.
57  VarPrivate Call(const Var& method_name, Var* exception = NULL);
58  VarPrivate Call(const Var& method_name, const Var& arg1,
59  Var* exception = NULL);
60  VarPrivate Call(const Var& method_name, const Var& arg1, const Var& arg2,
61  Var* exception = NULL);
62  VarPrivate Call(const Var& method_name, const Var& arg1, const Var& arg2,
63  const Var& arg3, Var* exception = NULL);
64  VarPrivate Call(const Var& method_name, const Var& arg1, const Var& arg2,
65  const Var& arg3, const Var& arg4, Var* exception = NULL);
66 
67  // For use when calling the raw C PPAPI when using the C++ Var as a possibly
68  // NULL exception. This will handle getting the address of the internal value
69  // out if it's non-NULL and fixing up the reference count.
70  //
71  // Danger: this will only work for things with exception semantics, i.e. that
72  // the value will not be changed if it's a non-undefined exception. Otherwise,
73  // this class will mess up the refcounting.
74  //
75  // This is a bit subtle:
76  // - If NULL is passed, we return NULL from get() and do nothing.
77  //
78  // - If a undefined value is passed, we return the address of a undefined var
79  // from get and have the output value take ownership of that var.
80  //
81  // - If a non-undefined value is passed, we return the address of that var
82  // from get, and nothing else should change.
83  //
84  // Example:
85  // void FooBar(a, b, Var* exception = NULL) {
86  // foo_interface->Bar(a, b, VarPrivate::OutException(exception).get());
87  // }
88  class OutException {
89  public:
90  OutException(Var* v)
91  : output_(v),
92  originally_had_exception_(v && !v->is_undefined()) {
93  if (output_) {
94  temp_ = output_->pp_var();
95  } else {
96  temp_.padding = 0;
97  temp_.type = PP_VARTYPE_UNDEFINED;
98  }
99  }
100  ~OutException() {
101  if (output_ && !originally_had_exception_)
102  *output_ = Var(PassRef(), temp_);
103  }
104 
105  PP_Var* get() {
106  if (output_)
107  return &temp_;
108  return NULL;
109  }
110 
111  private:
112  Var* output_;
113  bool originally_had_exception_;
114  PP_Var temp_;
115  };
116 
117  private:
118  // Prevent an arbitrary pointer argument from being implicitly converted to
119  // a bool at Var construction. If somebody makes such a mistake, they will
120  // get a compilation error.
121  VarPrivate(void* non_scriptable_object_pointer);
122 };
123 
124 } // namespace pp
125 
126 #endif // PPAPI_CPP_PRIVATE_VAR_PRIVATE_H_
Definition: scriptable_object_deprecated.h:33
Special value passed to constructor to make NULL.
Definition: var.h:26
bool is_undefined() const
Definition: var.h:110
Definition: var.h:72
const PP_Var & pp_var() const
Definition: var.h:228
Definition: instance_handle.h:44
Definition: var_private.h:88
PassRef
Definition: pass_ref.h:17
A generic type used for passing data types between the module and the page.
Definition: var.h:23
Definition: var_private.h:23