Gnash  0.8.11dev
GnashNPVariant.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2010, 2011, 2012 Free Software Foundation, Inc
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 //
18 
19 #ifndef GNASH_NPVARIANT_H
20 #define GNASH_NPVARIANT_H
21 
22 #if NPAPI_VERSION == 190
23 #include "npupp.h"
24 #else
25 #include "npapi.h"
26 #include "npruntime.h"
27 #endif
28 
29 namespace gnash {
30 
31 inline const uint32_t&
32 GetNPStringLen(const NPString& str)
33 {
34 #if NPAPI_VERSION == 192
35  return str.UTF8Length;
36 #else
37  return str.utf8length;
38 #endif
39 }
40 
41 inline const NPUTF8*
42 GetNPStringChars(const NPString& str)
43 {
44 #if NPAPI_VERSION == 192
45  return str.UTF8Characters;
46 #else
47  return str.utf8characters;
48 #endif
49 }
50 
54 inline void
55 CopyVariantValue(const NPVariant& from, NPVariant& to)
56 {
57  // First, we'll make a shallow copy, which is fine for most variant types.
58  to = from;
59 
60  // For deep copies for strings we obviously have to duplicate the string,
61  // and object pointer copies need to have their reference count increased.
62  switch(from.type) {
63  case NPVariantType_String:
64  {
65  const NPString& fromstr = NPVARIANT_TO_STRING(from);
66  const uint32_t& len = GetNPStringLen(fromstr);
67 
68  NPUTF8* tostr = static_cast<NPUTF8*>(NPN_MemAlloc(len));
69  std::copy(GetNPStringChars(fromstr),
70  GetNPStringChars(fromstr)+ GetNPStringLen(fromstr), tostr);
71 
72  STRINGN_TO_NPVARIANT(tostr, len, to);
73  break;
74  }
75  case NPVariantType_Object:
76  NPN_RetainObject(NPVARIANT_TO_OBJECT(to));
77  break;
78  default:
79  {}
80  }
81 }
82 
84 //
86 inline std::string
87 NPStringToString(const NPString& str)
88 {
89  return std::string(GetNPStringChars(str), GetNPStringLen(str));
90 }
91 
93 //
96 inline std::string
97 NPVariantToString(const NPVariant& val)
98 {
99  if (!NPVARIANT_IS_STRING(val)) {
100  return std::string();
101  }
102 
103  return NPStringToString(NPVARIANT_TO_STRING(val));
104 }
105 
107 //
116 {
117 public:
119  {
120  NULL_TO_NPVARIANT(_variant);
121  }
122 
124  {
125  CopyVariantValue(var._variant, _variant);
126  }
127 
130  GnashNPVariant(const NPVariant& var)
131  {
132  CopyVariantValue(var, _variant);
133  }
134 
136  {
137  // Avoid destroying self
138  if ( &var == this ) return *this;
139 
140  NPN_ReleaseVariantValue(&_variant);
141 
142  CopyVariantValue(var._variant, _variant);
143 
144  return *this;
145  }
146 
148  //
151  void
152  copy(NPVariant& dest) const
153  {
154  CopyVariantValue(_variant, dest);
155  }
156 
158  //
162  const NPVariant& get() const { return _variant; }
163 
165  {
166  NPN_ReleaseVariantValue(&_variant);
167  }
168 
169 private:
170  NPVariant _variant;
171 };
172 
173 } // namespace gnash
174 
175 #endif // GNASH_NPVARIANT_H
176 
177 // local Variables:
178 // mode: C++
179 // indent-tabs-mode: nil
180 // End:
const NPUTF8 * GetNPStringChars(const NPString &str)
Definition: GnashNPVariant.h:42
GnashNPVariant(const GnashNPVariant &var)
Definition: GnashNPVariant.h:123
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
GnashNPVariant & operator=(const GnashNPVariant &var)
Definition: GnashNPVariant.h:135
GnashNPVariant()
Definition: GnashNPVariant.h:118
GnashNPVariant(const NPVariant &var)
Definition: GnashNPVariant.h:130
void copy(NPVariant &dest) const
Copy the contained NPVariant into another NPVariant.
Definition: GnashNPVariant.h:152
void NPN_ReleaseVariantValue(NPVariant *variant)
Definition: gshell.cpp:464
This class holds ownership of (a copy of) an NPVariant.
Definition: GnashNPVariant.h:115
void CopyVariantValue(const NPVariant &from, NPVariant &to)
Definition: GnashNPVariant.h:55
std::string NPVariantToString(const NPVariant &val)
Construct a std::string from an NPVariant.
Definition: GnashNPVariant.h:97
std::string NPStringToString(const NPString &str)
Construct a std::string from an NPString.
Definition: GnashNPVariant.h:87
NPObject * NPN_RetainObject(NPObject *obj)
Definition: gshell.cpp:513
~GnashNPVariant()
Definition: GnashNPVariant.h:164
const uint32_t & GetNPStringLen(const NPString &str)
Definition: GnashNPVariant.h:32
void * NPN_MemAlloc(uint32_t size)
Definition: gshell.cpp:188