summaryrefslogtreecommitdiffstats
path: root/o3d/gpu_plugin/np_utils/np_utils.h
blob: af1555c0b111e5010f0475268f31d00ec191d504 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef O3D_GPU_PLUGIN_NP_UTILS_NP_UTILS_H_
#define O3D_GPU_PLUGIN_NP_UTILS_NP_UTILS_H_

#include <string>

#include "o3d/gpu_plugin/np_utils/np_browser.h"
#include "o3d/gpu_plugin/np_utils/np_object_pointer.h"
#include "third_party/npapi/bindings/npapi.h"
#include "third_party/npapi/bindings/npruntime.h"

namespace o3d {
namespace gpu_plugin {

// Convert NPVariant to C++ type. Returns whether the conversion was successful.
bool NPVariantToValue(bool* value, const NPVariant& variant);
bool NPVariantToValue(int* value, const NPVariant& variant);
bool NPVariantToValue(float* value, const NPVariant& variant);
bool NPVariantToValue(double* value, const NPVariant& variant);
bool NPVariantToValue(std::string* value, const NPVariant& variant);

template <typename T>
bool NPVariantToValue(NPObjectPointer<T>* value,
                      const NPVariant& variant) {
  if (NPVARIANT_IS_NULL(variant)) {
    *value = NPObjectPointer<T>();
    return true;
  } else if (NPVARIANT_IS_OBJECT(variant)) {
    NPObject* object = NPVARIANT_TO_OBJECT(variant);
    if (object->_class == BaseNPObject::GetNPClass<T>()) {
      *value = NPObjectPointer<T>(static_cast<T*>(
          NPVARIANT_TO_OBJECT(variant)));
      return true;
    }
  }

  return false;
}

// Specialization for NPObject does not check for mismatched NPClass.
template <>
inline bool NPVariantToValue(NPObjectPointer<NPObject>* value,
                             const NPVariant& variant) {
  if (NPVARIANT_IS_NULL(variant)) {
    *value = NPObjectPointer<NPObject>();
    return true;
  } else if (NPVARIANT_IS_OBJECT(variant)) {
    *value = NPObjectPointer<NPObject>(NPVARIANT_TO_OBJECT(variant));
    return true;
  }

  return false;
}

// Convert C++ type to NPVariant.
void ValueToNPVariant(bool value, NPVariant* variant);
void ValueToNPVariant(int value, NPVariant* variant);
void ValueToNPVariant(float value, NPVariant* variant);
void ValueToNPVariant(double value, NPVariant* variant);
void ValueToNPVariant(const std::string& value, NPVariant* variant);

template <typename T>
void ValueToNPVariant(const NPObjectPointer<T>& value,
                      NPVariant* variant) {
  if (value.Get()) {
    NPBrowser::get()->RetainObject(value.Get());
    OBJECT_TO_NPVARIANT(value.Get(), *variant);
  } else {
    NULL_TO_NPVARIANT(*variant);
  }
}

// NPVariant that automatically manages lifetime of string and object variants.
class SmartNPVariant : public NPVariant {
 public:
  SmartNPVariant();
  SmartNPVariant(const SmartNPVariant& rhs);

  template <typename T>
  explicit SmartNPVariant(const T& v) {
    ValueToNPVariant(v, this);
  }

  ~SmartNPVariant();

  SmartNPVariant& operator=(const SmartNPVariant& rhs);

  template <typename T>
  bool GetValue(T* v) const {
    return NPVariantToValue(v, *this);
  }

  template <typename T>
  void SetValue(const T& v) {
    Release();
    ValueToNPVariant(v, this);
  }

  void Release();

 private:
  void Copy(const NPVariant& rhs);
};

// These allow a method to be invoked with automatic conversion of C++
// types to variants for arguments and return values.

bool NPHasMethod(NPP npp,
                 const NPObjectPointer<NPObject>& object,
                 const NPUTF8* name);

inline bool NPInvokeVoid(NPP npp,
                         const NPObjectPointer<NPObject>& object,
                         const NPUTF8* name) {
  SmartNPVariant result;
  return NPBrowser::get()->Invoke(
      npp,
      object.Get(),
      NPBrowser::get()->GetStringIdentifier(name),
      NULL, 0,
      &result);
}

template<typename R>
bool NPInvoke(NPP npp,
              const NPObjectPointer<NPObject>& object,
              const NPUTF8* name,
              R* r) {
  SmartNPVariant result;
  if (NPBrowser::get()->Invoke(
      npp,
      object.Get(),
      NPBrowser::get()->GetStringIdentifier(name),
      NULL, 0,
      &result)) {
    return result.GetValue(r);
  }
  return false;
}

template<typename P0>
bool NPInvokeVoid(NPP npp,
                  const NPObjectPointer<NPObject>& object,
                  const NPUTF8* name,
                  P0 p0) {
  SmartNPVariant args[1];
  args[0].SetValue(p0);
  SmartNPVariant result;
  return NPBrowser::get()->Invoke(
      npp,
      object.Get(),
      NPBrowser::get()->GetStringIdentifier(name),
      &args[0], 1,
      &result);
}

template<typename R, typename P0>
bool NPInvoke(NPP npp,
              const NPObjectPointer<NPObject>& object,
              const NPUTF8* name,
              P0 p0,
              R* r) {
  SmartNPVariant args[1];
  args[0].SetValue(p0);
  SmartNPVariant result;
  if (NPBrowser::get()->Invoke(
      npp,
      object.Get(),
      NPBrowser::get()->GetStringIdentifier(name),
      &args[0], 1,
      &result)) {
    return result.GetValue(r);
  }
  return false;
}

template<typename P0, typename P1>
bool NPInvokeVoid(NPP npp,
                  const NPObjectPointer<NPObject>& object,
                  const NPUTF8* name,
                  P0 p0, P1 p1) {
  SmartNPVariant args[2];
  args[0].SetValue(p0);
  args[1].SetValue(p1);
  SmartNPVariant result;
  return NPBrowser::get()->Invoke(
      npp,
      object.Get(),
      NPBrowser::get()->GetStringIdentifier(name),
      &args[0], 2,
      &result);
}

template<typename R, typename P0, typename P1>
bool NPInvoke(NPP npp,
              const NPObjectPointer<NPObject>& object,
              const NPUTF8* name,
              P0 p0, P1 p1,
              R* r) {
  SmartNPVariant args[2];
  args[0].SetValue(p0);
  args[1].SetValue(p1);
  SmartNPVariant result;
  if (NPBrowser::get()->Invoke(
      npp,
      object.Get(),
      NPBrowser::get()->GetStringIdentifier(name),
      &args[0], 2,
      &result)) {
    return result.GetValue(r);
  }
  return false;
}

bool NPHasProperty(NPP npp,
                   const NPObjectPointer<NPObject>& object,
                   const NPUTF8* name);

template <typename T>
bool NPGetProperty(NPP npp,
                   const NPObjectPointer<NPObject>& object,
                   const NPUTF8* name,
                   T* value) {
  SmartNPVariant result;
  if (NPBrowser::get()->GetProperty(npp,
                                    object.Get(),
                                    NPBrowser::get()->GetStringIdentifier(name),
                                    &result)) {
    return result.GetValue(value);
  }
  return false;
}

template <typename NPObjectType>
NPObjectPointer<NPObjectType> NPCreateObject(NPP npp) {
  const NPClass* np_class = BaseNPObject::GetNPClass<NPObjectType>();
  NPObjectType* object = static_cast<NPObjectType*>(
      NPBrowser::get()->CreateObject(npp, np_class));
  return NPObjectPointer<NPObjectType>::FromReturned(object);
}

}  // namespace gpu_plugin
}  // namespace o3d

#endif  // O3D_GPU_PLUGIN_NP_UTILS_NP_UTILS_H_