• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List
  • File Members

GnashNPVariant.h

Go to the documentation of this file.
00001 // 
00002 //   Copyright (C) 2010 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 #include "npapi.h"
00023 #include "npruntime.h"
00024 
00025 namespace gnash {
00026 
00030 inline void
00031 CopyVariantValue(const NPVariant& from, NPVariant& to)
00032 {
00033     // First, we'll make a shallow copy, which is fine for most variant types.
00034     to = from;
00035 
00036     // For deep copies for strings we obviously have to duplicate the string,
00037     // and object pointer copies need to have their reference count increased.
00038     switch(from.type) {
00039         case NPVariantType_String:
00040         {
00041             const NPString& fromstr = NPVARIANT_TO_STRING(from);
00042             const uint32_t& len = fromstr.UTF8Length;
00043 
00044             NPUTF8* tostr = static_cast<NPUTF8*>(NPN_MemAlloc(len));
00045             std::copy(fromstr.UTF8Characters, fromstr.UTF8Characters+len, tostr);
00046 
00047             STRINGN_TO_NPVARIANT(tostr, len, to);
00048             break;
00049         }
00050         case NPVariantType_Object:
00051             NPN_RetainObject(NPVARIANT_TO_OBJECT(to));
00052             break;
00053         default:
00054         {}
00055     }
00056 }
00057 
00059 //
00061 inline std::string
00062 NPStringToString(const NPString& str)
00063 {
00064     return std::string(str.UTF8Characters, str.UTF8Length);
00065 }
00066 
00067 
00069 //
00077 class GnashNPVariant
00078 {
00079 public:
00080     GnashNPVariant()
00081     {
00082         NULL_TO_NPVARIANT(_variant);
00083     }
00084 
00085     GnashNPVariant(const GnashNPVariant& var)
00086     {
00087         CopyVariantValue(var._variant, _variant);
00088     }
00089 
00092     GnashNPVariant(const NPVariant& var)
00093     {
00094         CopyVariantValue(var, _variant);
00095     }
00096 
00097     GnashNPVariant& operator= (const GnashNPVariant& var)
00098     {
00099         NPN_ReleaseVariantValue(&_variant);
00100 
00101         CopyVariantValue(var._variant, _variant);
00102 
00103         return *this;
00104     }
00105 
00107     //
00110     void
00111     copy(NPVariant& dest) const
00112     {
00113         CopyVariantValue(_variant, dest);
00114     }
00115 
00117     //
00121     const NPVariant& get() const { return _variant; }
00122 
00123     ~GnashNPVariant()
00124     {
00125         NPN_ReleaseVariantValue(&_variant);
00126     }
00127 
00128 private:
00129     NPVariant _variant;
00130 };
00131 
00132 } // namespace gnash
00133 
00134 #endif // GNASH_NPVARIANT_H
00135 
00136 // local Variables:
00137 // mode: C++
00138 // indent-tabs-mode: nil
00139 // End:

Generated on Fri Mar 16 2012 15:46:10 for Gnash by  doxygen 1.7.1