include/OffscreenGecko/scriptvariant.h

Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is OffscreenGecko code.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Frank Richter <frank.richter@gmail.com>.
00018  * Portions created by the Initial Developer are Copyright (C) 2007
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #ifndef __OFFSCREENGECKO_SCRIPTVARIANT_H__
00038 #define __OFFSCREENGECKO_SCRIPTVARIANT_H__
00039 
00043 #include "baseobj.h"
00044 #include "embedding.h"
00045 #include "string.h"
00046 
00047 OSGK_CLASSTYPE_DEF nsISupports;
00048 
00049 OSGK_DERIVEDTYPE(OSGK_ScriptVariant);
00050 
00051 typedef enum OSGK_ScriptVariantType
00052 {
00053   svtEmpty,
00054   svtInt,
00055   svtUint,
00056   svtFloat,
00057   svtDouble,
00058   svtBool,
00059   svtChar,
00060   svtString,
00061   svtISupports,
00062   svtScriptObject,
00063   svtArray
00064 } OSGK_ScriptVariantType;
00065 
00066 OSGK_EXTERN_C OSGK_API OSGK_ScriptVariantType osgk_variant_get_type (
00067   OSGK_ScriptVariant* variant);
00068 OSGK_EXTERN_C OSGK_API OSGK_ScriptVariant* osgk_variant_convert (
00069   OSGK_ScriptVariant* variant, OSGK_ScriptVariantType newType);
00070 
00071 OSGK_EXTERN_C OSGK_API int osgk_variant_get_int (OSGK_ScriptVariant* variant,
00072   int* val);
00073 OSGK_EXTERN_C OSGK_API int osgk_variant_get_uint (OSGK_ScriptVariant* variant,
00074   unsigned int* val);
00075 OSGK_EXTERN_C OSGK_API int osgk_variant_get_float (OSGK_ScriptVariant* variant,
00076   float* val);
00077 OSGK_EXTERN_C OSGK_API int osgk_variant_get_double (OSGK_ScriptVariant* variant,
00078   double* val);
00079 OSGK_EXTERN_C OSGK_API int osgk_variant_get_bool (OSGK_ScriptVariant* variant,
00080   int* val);
00081 OSGK_EXTERN_C OSGK_API int osgk_variant_get_char (OSGK_ScriptVariant* variant,
00082   unsigned int* val);
00083 // Does not increase ref count
00084 OSGK_EXTERN_C OSGK_API int osgk_variant_get_string (OSGK_ScriptVariant* variant,
00085   OSGK_String** val);
00086 // Does not increase ref count
00087 OSGK_EXTERN_C OSGK_API int osgk_variant_get_isupports (OSGK_ScriptVariant* variant,
00088   OSGK_CLASSTYPE_REF nsISupports** val);
00089 OSGK_EXTERN_C OSGK_API int osgk_variant_get_script_object (OSGK_ScriptVariant* variant,
00090   void** tag);
00091 OSGK_EXTERN_C OSGK_API int osgk_variant_get_array_size (OSGK_ScriptVariant* variant,
00092   size_t* size);
00093 OSGK_EXTERN_C OSGK_API int osgk_variant_get_array_item (OSGK_ScriptVariant* variant,
00094   OSGK_ScriptVariant** val);
00095 
00096 OSGK_EXTERN_C OSGK_API OSGK_ScriptVariant* osgk_variant_create_empty (
00097   OSGK_Embedding* embedding);
00098 OSGK_EXTERN_C OSGK_API OSGK_ScriptVariant* osgk_variant_create_int (
00099   OSGK_Embedding* embedding, int val);
00100 OSGK_EXTERN_C OSGK_API OSGK_ScriptVariant* osgk_variant_create_uint (
00101   OSGK_Embedding* embedding, unsigned int val);
00102 OSGK_EXTERN_C OSGK_API OSGK_ScriptVariant* osgk_variant_create_float (
00103   OSGK_Embedding* embedding, float val);
00104 OSGK_EXTERN_C OSGK_API OSGK_ScriptVariant* osgk_variant_create_double (
00105   OSGK_Embedding* embedding, double val);
00106 OSGK_EXTERN_C OSGK_API OSGK_ScriptVariant* osgk_variant_create_bool (
00107   OSGK_Embedding* embedding, int val);
00108 OSGK_EXTERN_C OSGK_API OSGK_ScriptVariant* osgk_variant_create_char (
00109   OSGK_Embedding* embedding, unsigned int val);
00110 OSGK_EXTERN_C OSGK_API OSGK_ScriptVariant* osgk_variant_create_string (
00111   OSGK_Embedding* embedding, OSGK_String* val);
00112 OSGK_EXTERN_C OSGK_API OSGK_ScriptVariant* osgk_variant_create_isupports (
00113   OSGK_Embedding* embedding, OSGK_CLASSTYPE_REF nsISupports* val);
00114 OSGK_EXTERN_C OSGK_API OSGK_ScriptVariant* osgk_variant_create_script_object (
00115   OSGK_Embedding* embedding, void* tag);
00116 OSGK_EXTERN_C OSGK_API OSGK_ScriptVariant* osgk_variant_create_array (
00117   OSGK_Embedding* embedding, size_t numItems, OSGK_ScriptVariant** items);
00118 
00119 #ifdef __cplusplus
00120 
00121 namespace OSGK
00122 {
00125   struct ScriptVariant : public CXXAPI::Wrap<OSGK_ScriptVariant>
00126   {
00127     ScriptVariant (Embedding& embedding) : 
00128       WrapperType (osgk_variant_create_empty (embedding.GetObject())) {}
00129     ScriptVariant (Embedding& embedding, int v) : 
00130       WrapperType (osgk_variant_create_int (embedding.GetObject(), v)) {}
00131     ScriptVariant (Embedding& embedding, unsigned int v) : 
00132       WrapperType (osgk_variant_create_uint (embedding.GetObject(), v)) {}
00133     ScriptVariant (Embedding& embedding, float v) : 
00134       WrapperType (osgk_variant_create_float (embedding.GetObject(), v)) {}
00135     ScriptVariant (Embedding& embedding, double v) : 
00136       WrapperType (osgk_variant_create_double (embedding.GetObject(), v)) {}
00137     ScriptVariant (Embedding& embedding, bool v) : 
00138       WrapperType (osgk_variant_create_bool (embedding.GetObject(), v ? 1 : 0)) {}
00139     ScriptVariant (Embedding& embedding, char v) : 
00140       WrapperType (osgk_variant_create_char (embedding.GetObject(), v)) {}
00141     ScriptVariant (Embedding& embedding, wchar_t v) : 
00142       WrapperType (osgk_variant_create_char (embedding.GetObject(), v)) {}
00143     ScriptVariant (Embedding& embedding, String& v) : 
00144       WrapperType (osgk_variant_create_string (embedding.GetObject(), v.GetObject())) {}
00145     ScriptVariant (Embedding& embedding, nsISupports* v) : 
00146       WrapperType (osgk_variant_create_isupports (embedding.GetObject(), v)) {}
00147     ScriptVariant (Embedding& embedding, void* tag) : 
00148       WrapperType (osgk_variant_create_script_object (embedding.GetObject(), tag)) {}
00149     ScriptVariant (Embedding& embedding, size_t numItems, ScriptVariant items[]) : 
00150       WrapperType (osgk_variant_create_array (embedding.GetObject(), numItems,
00151         reinterpret_cast<OSGK_ScriptVariant**> (items))) {}
00152     
00153     explicit ScriptVariant (WrappedType* obj) : WrapperType (obj) {}
00154     ScriptVariant& operator=(const ScriptVariant& other)
00155     {
00156       WrapperType::operator=(other);
00157       return *this;
00158     }
00159     
00160     OSGK_ScriptVariantType GetType()
00161     {
00162       return osgk_variant_get_type (GetObject());
00163     }
00164     ScriptVariant Convert (OSGK_ScriptVariantType newType)
00165     {
00166       ScriptVariant newVar (Null ());
00167       newVar.SetObject (osgk_variant_convert (GetObject(), newType));
00168       return newVar;
00169     }
00170 
00171     bool GetInt (int& val)
00172     {
00173       return osgk_variant_get_int (GetObject(), &val) != 0;
00174     }
00175     bool GetUint (unsigned int& val)
00176     {
00177       return osgk_variant_get_uint (GetObject(), &val) != 0;
00178     }
00179     bool GetFloat (float& val)
00180     {
00181       return osgk_variant_get_float (GetObject(), &val) != 0;
00182     }
00183     bool GetDouble (double& val)
00184     {
00185       return osgk_variant_get_double (GetObject(), &val) != 0;
00186     }
00187     bool GetBool (bool& val)
00188     {
00189       int v;
00190       if (osgk_variant_get_bool (GetObject(), &v) == 0) return false;
00191       val = v != 0;
00192       return true;
00193     }
00194     bool GetChar (unsigned int& val)
00195     {
00196       return osgk_variant_get_char (GetObject(), &val) != 0;
00197     }
00198     bool GetString (String& val)
00199     {
00200       OSGK_String* s;
00201       if (osgk_variant_get_string (GetObject(), &s) == 0) return false;
00202       osgk_addref (s);
00203       val.SetObject (s);
00204       return true;
00205     }
00206     bool GetISupports (nsISupports*& val)
00207     {
00208       return osgk_variant_get_isupports (GetObject(), &val) != 0;
00209     }
00210     bool GetScriptObject (void*& tag)
00211     {
00212       return osgk_variant_get_script_object (GetObject(), &tag) != 0;
00213     }
00214     bool GetArraySize (size_t& val)
00215     {
00216       return osgk_variant_get_array_size (GetObject(), &val) != 0;
00217     }
00218     bool GetArrayItem (ScriptVariant& val)
00219     {
00220       OSGK_ScriptVariant* v;
00221       if (osgk_variant_get_array_item (GetObject(), &v) == 0) return false;
00222       osgk_addref (v);
00223       val.SetObject (v);
00224       return true;
00225     }
00226   };
00227 } // namespace OSGK
00228 #endif
00229 
00230 #endif // __OFFSCREENGECKO_SCRIPTVARIANT_H__

Generated on Sun Nov 2 18:15:18 2008 for OffscreenGecko by  doxygen 1.5.4