update copyright date
[gnash.git] / plugin / npapi / GnashNPVariant.h
blobe79949ff8ccab21d70a4778f0c2592fe7b9ac273
1 //
2 // Copyright (C) 2010, 2011 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.
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
19 #ifndef GNASH_NPVARIANT_H
20 #define GNASH_NPVARIANT_H
22 #include "npapi.h"
23 #include "npruntime.h"
25 namespace gnash {
27 /// Makes a deep copy of a NPVariant.
28 /// @param from The source NPVariant to copy values from.
29 /// @param to The destination NPVariant.
30 inline void
31 CopyVariantValue(const NPVariant& from, NPVariant& to)
33 // First, we'll make a shallow copy, which is fine for most variant types.
34 to = from;
36 // For deep copies for strings we obviously have to duplicate the string,
37 // and object pointer copies need to have their reference count increased.
38 switch(from.type) {
39 case NPVariantType_String:
41 const NPString& fromstr = NPVARIANT_TO_STRING(from);
42 const uint32_t& len = fromstr.UTF8Length;
44 NPUTF8* tostr = static_cast<NPUTF8*>(NPN_MemAlloc(len));
45 std::copy(fromstr.UTF8Characters, fromstr.UTF8Characters+len, tostr);
47 STRINGN_TO_NPVARIANT(tostr, len, to);
48 break;
50 case NPVariantType_Object:
51 NPN_RetainObject(NPVARIANT_TO_OBJECT(to));
52 break;
53 default:
58 /// Construct a std::string from an NPString.
60 /// NPStrings are not guaranteed to be NULL-terminated.
61 inline std::string
62 NPStringToString(const NPString& str)
64 return std::string(str.UTF8Characters, str.UTF8Length);
68 /// This class holds ownership of (a copy of) an NPVariant.
70 /// The user of this class must keep in mind that it does not take
71 /// ownership of already-allocated resources. The user must supply
72 /// an NPVariant to construct a GnashNPVariant, and must subsequently
73 /// release any resources associated with the original NPVariant.
74 ///
75 /// When an object of type GnashNPVariant goes out of scope, the resources
76 /// associated with the copied NPVariant will be released.
77 class GnashNPVariant
79 public:
80 GnashNPVariant()
82 NULL_TO_NPVARIANT(_variant);
85 GnashNPVariant(const GnashNPVariant& var)
87 CopyVariantValue(var._variant, _variant);
90 /// Construct a GnashNPVariant by copying an NPVariant.
91 /// @param var The NPVariant to copy from.
92 GnashNPVariant(const NPVariant& var)
94 CopyVariantValue(var, _variant);
97 GnashNPVariant& operator= (const GnashNPVariant& var)
99 NPN_ReleaseVariantValue(&_variant);
101 CopyVariantValue(var._variant, _variant);
103 return *this;
106 /// Copy the contained NPVariant into another NPVariant.
108 /// This is useful to return a GnashNPVariant to an external API.
109 /// @param dest The NPVariant to copy the value into.
110 void
111 copy(NPVariant& dest) const
113 CopyVariantValue(_variant, dest);
116 /// Obtain a reference to the contained NPVariant.
118 /// This method returns a const reference to avoid the temptation
119 /// to modify ownership, which could lead to memory errors. Use copy() if
120 /// you want to alter the contained NPVariant.
121 const NPVariant& get() const { return _variant; }
123 ~GnashNPVariant()
125 NPN_ReleaseVariantValue(&_variant);
128 private:
129 NPVariant _variant;
132 } // namespace gnash
134 #endif // GNASH_NPVARIANT_H
136 // local Variables:
137 // mode: C++
138 // indent-tabs-mode: nil
139 // End: