Gnash  0.8.10
GnashNPVariant.h
Go to the documentation of this file.
00001 // 
00002 //   Copyright (C) 2010, 2011, 2012 Free Software Foundation, Inc
00003 // 
00004 // This program is free software; you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation; either version 3 of the License, or
00007 // (at your option) any later version.
00008 // 
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017 //
00018 
00019 #ifndef GNASH_NPVARIANT_H
00020 #define GNASH_NPVARIANT_H
00021 
00022 #if NPAPI_VERSION == 190
00023 #include "npupp.h"
00024 #else
00025 #include "npapi.h"
00026 #include "npruntime.h"
00027 #endif
00028 
00029 namespace gnash {
00030 
00031 inline const uint32_t&
00032 GetNPStringLen(const NPString& str)
00033 {
00034 #if NPAPI_VERSION == 192
00035      return str.UTF8Length;
00036 #else
00037      return str.utf8length;
00038 #endif
00039 }
00040 
00041 inline const NPUTF8*
00042 GetNPStringChars(const NPString& str)
00043 {
00044 #if NPAPI_VERSION == 192
00045     return str.UTF8Characters;
00046 #else
00047     return str.utf8characters;
00048 #endif
00049 }
00050 
00054 inline void
00055 CopyVariantValue(const NPVariant& from, NPVariant& to)
00056 {
00057     // First, we'll make a shallow copy, which is fine for most variant types.
00058     to = from;
00059 
00060     // For deep copies for strings we obviously have to duplicate the string,
00061     // and object pointer copies need to have their reference count increased.
00062     switch(from.type) {
00063         case NPVariantType_String:
00064         {
00065             const NPString& fromstr = NPVARIANT_TO_STRING(from);
00066             const uint32_t& len = GetNPStringLen(fromstr);
00067 
00068             NPUTF8* tostr = static_cast<NPUTF8*>(NPN_MemAlloc(len));
00069             std::copy(GetNPStringChars(fromstr),
00070                       GetNPStringChars(fromstr)+ GetNPStringLen(fromstr), tostr);
00071 
00072             STRINGN_TO_NPVARIANT(tostr, len, to);
00073             break;
00074         }
00075         case NPVariantType_Object:
00076             NPN_RetainObject(NPVARIANT_TO_OBJECT(to));
00077             break;
00078         default:
00079         {}
00080     }
00081 }
00082 
00084 //
00086 inline std::string
00087 NPStringToString(const NPString& str)
00088 {
00089     return std::string(GetNPStringChars(str), GetNPStringLen(str));
00090 }
00091 
00092 
00094 //
00102 class GnashNPVariant
00103 {
00104 public:
00105     GnashNPVariant()
00106     {
00107         NULL_TO_NPVARIANT(_variant);
00108     }
00109 
00110     GnashNPVariant(const GnashNPVariant& var)
00111     {
00112         CopyVariantValue(var._variant, _variant);
00113     }
00114 
00117     GnashNPVariant(const NPVariant& var)
00118     {
00119         CopyVariantValue(var, _variant);
00120     }
00121 
00122     GnashNPVariant& operator= (const GnashNPVariant& var)
00123     {
00124         // Avoid destroying self
00125         if ( &var == this ) return *this; 
00126 
00127         NPN_ReleaseVariantValue(&_variant);
00128 
00129         CopyVariantValue(var._variant, _variant);
00130 
00131         return *this;
00132     }
00133 
00135     //
00138     void
00139     copy(NPVariant& dest) const
00140     {
00141         CopyVariantValue(_variant, dest);
00142     }
00143 
00145     //
00149     const NPVariant& get() const { return _variant; }
00150 
00151     ~GnashNPVariant()
00152     {
00153         NPN_ReleaseVariantValue(&_variant);
00154     }
00155 
00156 private:
00157     NPVariant _variant;
00158 };
00159 
00160 } // namespace gnash
00161 
00162 #endif // GNASH_NPVARIANT_H
00163 
00164 // local Variables:
00165 // mode: C++
00166 // indent-tabs-mode: nil
00167 // End: