![]() |
Main Page
Class Hierarchy
Alphabetical List
Compound List
File List
Compound Members
![]() |
00001 /******************************************************************************** 00002 * * 00003 * Variant class - a container for any data type * 00004 * * 00005 ********************************************************************************* 00006 * Copyright (C) 2003 by Giancarlo Formicuccia. All Rights Reserved. * 00007 * Copyright (C) 2003 by Mathew Robertson. All Rights Reserved. * 00008 ********************************************************************************* 00009 * This library is free software; you can redistribute it and/or * 00010 * modify it under the terms of the GNU Lesser General Public * 00011 * License as published by the Free Software Foundation; either * 00012 * version 2.1 of the License, or (at your option) any later version. * 00013 * * 00014 * This library is distributed in the hope that it will be useful, * 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00017 * Lesser General Public License for more details. * 00018 * * 00019 * You should have received a copy of the GNU Lesser General Public * 00020 * License along with this library; if not, write to the Free Software * 00021 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * 00022 *********************************************************************************/ 00023 #ifndef FXVARIANT_H 00024 #define FXVARIANT_H 00025 00026 #ifndef FXBASEOBJECT_H 00027 #include "FXBaseObject.h" 00028 #endif 00029 namespace FXEX { 00030 00031 /** 00032 * Variant objects can (... its aim is to ...) be assignable to any data type. 00033 * They accept ID_GET/SET events so at to make connecting to widgets/datatargets easier. 00034 * FIXME: add support for FXWString 00035 * 00036 * Note: we only provide a single pair of operator==/operator!= for the FXVariant type 00037 * since you can always use the getType() and 'asXXXXX()' methods to get the type 00038 * you want. 00039 */ 00040 class FXAPI FXVariant : public FXBaseObject { 00041 FXDECLARE(FXVariant) 00042 00043 protected: 00044 FXint type; // type of variant 00045 union { // basic types 00046 bool boolean; 00047 FXuchar fxuchar; 00048 FXchar fxchar; 00049 FXushort fxushort; 00050 FXshort fxshort; 00051 FXuint fxuint; 00052 FXint fxint; 00053 FXulong fxulong; 00054 FXlong fxlong; 00055 FXfloat fxfloat; 00056 FXdouble fxdouble; 00057 } data; 00058 FXCharset fxcharset; // charset 00059 FXString fxstring; // string 00060 FXWString fxwstring; // wide string 00061 FXObject *fxobject; // point to FXObject type 00062 void *pointer; // assignment to pointer 00063 struct { // hold some raw data 00064 unsigned long size; 00065 void *pointer; 00066 } raw; 00067 00068 public: 00069 /** 00070 * Supported data types 00071 * - allows for subclassing (use your own data type) 00072 * - use the pointer types when you just want to point at anaother object 00073 */ 00074 enum { 00075 DT_UNASSIGNED=-1, 00076 DT_NULL, 00077 DT_BOOL, 00078 DT_FXUCHAR, 00079 DT_FXCHAR, 00080 DT_FXUSHORT, 00081 DT_FXSHORT, 00082 DT_FXUINT, 00083 DT_FXINT, 00084 DT_FXULONG, 00085 DT_FXLONG, 00086 DT_FXFLOAT, 00087 DT_FXDOUBLE, 00088 DT_FXCHARSET, 00089 DT_FXSTRING, 00090 DT_FXWSTRING, 00091 DT_FXOBJECT, 00092 DT_POINTER, 00093 DT_RAW, 00094 DT_LAST 00095 }; 00096 00097 public: 00098 enum { 00099 ID_VALUE=FXBaseObject::ID_LAST, 00100 ID_LAST 00101 }; 00102 00103 public: 00104 /* 00105 long onCmdValue(FXObject*,FXSelector,void*); 00106 long onUpdValue(FXObject*,FXSelector,void*); 00107 */ 00108 long onCmdSetValue(FXObject*,FXSelector,void*); 00109 long onCmdSetIntValue(FXObject*,FXSelector,void*); 00110 long onCmdSetRealValue(FXObject*,FXSelector,void*); 00111 long onCmdSetStringValue(FXObject*,FXSelector,void*); 00112 long onCmdGetIntValue(FXObject*,FXSelector,void*); 00113 long onCmdGetRealValue(FXObject*,FXSelector,void*); 00114 long onCmdGetStringValue(FXObject*,FXSelector,void*); 00115 00116 public: 00117 /** 00118 * constructors for all the known types 00119 * takes a deep copy of data type (where possible) 00120 */ 00121 FXVariant(FXObject *tgt=NULL,FXSelector sel=0); 00122 FXVariant(const FXVariant &v,FXObject *tgt=NULL,FXSelector sel=0); 00123 FXVariant(const bool &v,FXObject *tgt=NULL,FXSelector sel=0); 00124 FXVariant(const FXuchar &v,FXObject *tgt=NULL,FXSelector sel=0); 00125 FXVariant(const FXchar &v,FXObject *tgt=NULL,FXSelector sel=0); 00126 FXVariant(const FXushort &v,FXObject *tgt=NULL,FXSelector sel=0); 00127 FXVariant(const FXshort &v,FXObject *tgt=NULL,FXSelector sel=0); 00128 FXVariant(const FXuint &v,FXObject *tgt=NULL,FXSelector sel=0); 00129 FXVariant(const FXint &v,FXObject *tgt=NULL,FXSelector sel=0); 00130 FXVariant(const FXulong &v,FXObject *tgt=NULL,FXSelector sel=0); 00131 FXVariant(const FXlong &v,FXObject *tgt=NULL,FXSelector sel=0); 00132 FXVariant(const FXfloat &v,FXObject *tgt=NULL,FXSelector sel=0); 00133 FXVariant(const FXdouble &v,FXObject *tgt=NULL,FXSelector sel=0); 00134 FXVariant(const FXCharset &c,FXObject *tgt=NULL,FXSelector sel=0); 00135 FXVariant(const FXString &s,FXObject *tgt=NULL,FXSelector sel=0); 00136 FXVariant(const FXWString &s,FXObject *tgt=NULL,FXSelector sel=0); 00137 FXVariant(FXObject *o,FXObject *tgt=NULL,FXSelector sel=0); // point at FXObject 00138 FXVariant(void *p,FXObject *tgt=NULL,FXSelector sel=0); // pointer/reference 00139 FXVariant(const void *p,unsigned long size,FXObject *tgt=NULL,FXSelector sel=0); // raw data type 00140 00141 /** 00142 * Return the number of bytes needed to store hold the data for the current type 00143 * this will often be the natural size of the data type 00144 */ 00145 unsigned long getSize() const; 00146 00147 /// clears/resets the contents of the variant 00148 virtual void clear(); 00149 00150 /// set the date type, to a specific type 00151 void setType(FXint type); 00152 00153 /// return data type 00154 FXint getType() const { return type; } 00155 00156 /// test data type 00157 FXbool isUnassigned() const { return type==DT_UNASSIGNED; } 00158 FXbool isNull() const { return type==DT_NULL; } 00159 FXbool isBool() const { return type==DT_BOOL; } 00160 FXbool isFXuchar() const { return type==DT_FXUCHAR; } 00161 FXbool isFXchar() const { return type==DT_FXCHAR; } 00162 FXbool isFXushort() const { return type==DT_FXUSHORT; } 00163 FXbool isFXshort() const { return type==DT_FXSHORT; } 00164 FXbool isFXuint() const { return type==DT_FXUINT; } 00165 FXbool isFXint() const { return type==DT_FXINT; } 00166 FXbool isFXfloat() const { return type==DT_FXFLOAT; } 00167 FXbool isFXdouble() const { return type==DT_FXDOUBLE; } 00168 FXbool isFXCharset() const { return type==DT_FXCHARSET; } 00169 FXbool isFXString() const { return type==DT_FXSTRING; } 00170 FXbool isFXWString() const { return type==DT_FXWSTRING; } 00171 FXbool isFXObject() const { return type==DT_FXOBJECT; } 00172 FXbool isPointer() const { return type==DT_POINTER; } 00173 FXbool isRaw() const { return type==DT_RAW; } 00174 00175 /// assignment operators 00176 FXVariant& operator=(const FXVariant &v); 00177 FXVariant& operator=(const bool &v); 00178 FXVariant& operator=(const FXuchar &v); 00179 FXVariant& operator=(const FXchar &v); 00180 FXVariant& operator=(const FXushort &v); 00181 FXVariant& operator=(const FXshort &v); 00182 FXVariant& operator=(const FXuint &v); 00183 FXVariant& operator=(const FXint &v); 00184 FXVariant& operator=(const FXulong &v); 00185 FXVariant& operator=(const FXlong &v); 00186 FXVariant& operator=(const FXfloat &v); 00187 FXVariant& operator=(const FXdouble &v); 00188 FXVariant& operator=(const FXCharset &c); 00189 FXVariant& operator=(const FXString &s); 00190 FXVariant& operator=(const FXWString &s); 00191 FXVariant& operator=(FXObject *o); 00192 FXVariant& operator=(void *p); // set to new pointer 00193 00194 /// copy raw data, of specified length 00195 void setData(const void *p,unsigned long size); 00196 00197 /** 00198 * Try and convert the data to something useful; returns the result 00199 * Note: you may or may not get something useful out of this 00200 */ 00201 virtual bool asBool() const; 00202 virtual FXuchar asFXuchar() const; 00203 virtual FXchar asFXchar() const; 00204 virtual FXushort asFXushort() const; 00205 virtual FXshort asFXshort() const; 00206 virtual FXuint asFXuint() const; 00207 virtual FXint asFXint() const; 00208 virtual FXulong asFXulong() const; 00209 virtual FXlong asFXlong() const; 00210 virtual FXfloat asFXfloat() const; 00211 virtual FXdouble asFXdouble() const; 00212 virtual FXCharset asFXCharset() const; 00213 virtual FXString asFXString() const; 00214 virtual FXWString asFXWString() const; 00215 virtual FXObject* asFXObject() const; 00216 virtual void* asPointer() const; 00217 virtual void* asRaw() const; 00218 virtual FXuval asAddress() const; // special case for pointers 00219 00220 /** 00221 * return operator() (eg: as used in 'if' conditions) 00222 * FIXME: Should we throw an error if the required type is not the internal type, 00223 * rather than trying to convert it to that type? 00224 */ 00225 operator bool() const { return asBool(); } 00226 operator FXuchar() const { return asFXuchar(); } 00227 operator FXchar() const { return asFXchar(); } 00228 operator FXushort() const { return asFXushort(); } 00229 operator FXshort() const { return asFXshort(); } 00230 operator FXuint() const { return asFXuint(); } 00231 operator FXint() const { return asFXint(); } 00232 operator FXulong() const { return asFXulong(); } 00233 operator FXlong() const { return asFXlong(); } 00234 operator FXfloat() const { return asFXfloat(); } 00235 operator FXdouble() const { return asFXdouble(); } 00236 operator FXCharset() const { return asFXCharset(); } 00237 operator FXString() const { return asFXString(); } 00238 operator FXWString() const { return asFXWString(); } 00239 operator FXObject*() const { return asFXObject(); } 00240 operator void*() const { return asPointer(); } 00241 00242 /** 00243 * convert internal representation to different type, if possible 00244 * note: this may result in loss of data 00245 */ 00246 FXVariant& toBool() { *this = asBool(); return *this; } 00247 FXVariant& toFXuchar() { *this = asFXuchar(); return *this; } 00248 FXVariant& toFXchar() { *this = asFXchar(); return *this; } 00249 FXVariant& toFXushort() { *this = asFXushort(); return *this; } 00250 FXVariant& toFXshort() { *this = asFXshort(); return *this; } 00251 FXVariant& toFXuint() { *this = asFXuint(); return *this; } 00252 FXVariant& toFXint() { *this = asFXint(); return *this; } 00253 FXVariant& toFXulong() { *this = asFXulong(); return *this; } 00254 FXVariant& toFXlong() { *this = asFXlong(); return *this; } 00255 FXVariant& toFXfloat() { *this = asFXfloat(); return *this; } 00256 FXVariant& toFXdouble() { *this = asFXdouble(); return *this; } 00257 FXVariant& toFXCharset() { *this = asFXCharset(); return *this; } 00258 FXVariant& toFXString() { *this = asFXString(); return *this; } 00259 FXVariant& toFXWString() { *this = asFXWString(); return *this; } 00260 FXVariant& toFXObject() { *this = asFXObject(); return *this; } 00261 FXVariant& toPointer() { *this = asPointer(); return *this; } 00262 FXVariant& toRaw(); 00263 00264 /// convert to a specific type 00265 FXVariant& convertTo(FXint type); 00266 00267 /// equality... 00268 friend FXAPI FXbool operator==(const FXVariant &v1,const FXVariant &v2); 00269 00270 /// in-equality... 00271 friend FXAPI FXbool operator!=(const FXVariant &v1,const FXVariant &v2); 00272 00273 /// save to stream 00274 virtual void save(FXStream &store) const; 00275 00276 /// load from stream 00277 virtual void load(FXStream &store); 00278 00279 /// destructor (& cleanup) 00280 virtual ~FXVariant(); 00281 }; 00282 00283 } // namespace FXEX 00284 #endif // FXVARIANT_H