1 /* 2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _VARIANT_H 6 #define _VARIANT_H 7 8 9 #include <SupportDefs.h> 10 #include <TypeConstants.h> 11 12 13 enum { 14 B_VARIANT_DONT_COPY_DATA = 0x01, 15 B_VARIANT_OWNS_DATA = 0x02 16 }; 17 18 19 class BVariant { 20 public: 21 inline BVariant(); 22 inline BVariant(int8 value); 23 inline BVariant(uint8 value); 24 inline BVariant(int16 value); 25 inline BVariant(uint16 value); 26 inline BVariant(int32 value); 27 inline BVariant(uint32 value); 28 inline BVariant(int64 value); 29 inline BVariant(uint64 value); 30 inline BVariant(float value); 31 inline BVariant(double value); 32 inline BVariant(const void* value); 33 inline BVariant(const char* value, 34 uint32 flags = 0); 35 inline BVariant(const BVariant& other); 36 ~BVariant(); 37 38 inline void SetTo(const BVariant& other); 39 inline void SetTo(int8 value); 40 inline void SetTo(uint8 value); 41 inline void SetTo(int16 value); 42 inline void SetTo(uint16 value); 43 inline void SetTo(int32 value); 44 inline void SetTo(uint32 value); 45 inline void SetTo(int64 value); 46 inline void SetTo(uint64 value); 47 inline void SetTo(float value); 48 inline void SetTo(double value); 49 inline void SetTo(const void* value); 50 inline void SetTo(const char* value, 51 uint32 flags = 0); 52 void Unset(); 53 54 inline BVariant& operator=(const BVariant& other); 55 56 type_code Type() const { return fType; } 57 58 bool IsNumber() const; 59 bool IsInteger() const; 60 bool IsFloat() const; 61 // floating point, not just float 62 63 int8 ToInt8() const; 64 uint8 ToUInt8() const; 65 int16 ToInt16() const; 66 uint16 ToUInt16() const; 67 int32 ToInt32() const; 68 uint32 ToUInt32() const; 69 int64 ToInt64() const; 70 uint64 ToUInt64() const; 71 float ToFloat() const; 72 double ToDouble() const; 73 void* ToPointer() const; 74 const char* ToString() const; 75 76 private: 77 void _SetTo(const BVariant& other); 78 void _SetTo(int8 value); 79 void _SetTo(uint8 value); 80 void _SetTo(int16 value); 81 void _SetTo(uint16 value); 82 void _SetTo(int32 value); 83 void _SetTo(uint32 value); 84 void _SetTo(int64 value); 85 void _SetTo(uint64 value); 86 void _SetTo(float value); 87 void _SetTo(double value); 88 void _SetTo(const void* value); 89 void _SetTo(const char* value, 90 uint32 flags); 91 92 template<typename NumberType> 93 inline NumberType _ToNumber() const; 94 95 private: 96 type_code fType; 97 uint32 fFlags; 98 union { 99 int8 fInt8; 100 uint8 fUInt8; 101 int16 fInt16; 102 uint16 fUInt16; 103 int32 fInt32; 104 uint32 fUInt32; 105 int64 fInt64; 106 uint64 fUInt64; 107 float fFloat; 108 double fDouble; 109 void* fPointer; 110 char* fString; 111 }; 112 }; 113 114 115 BVariant::BVariant() 116 : 117 fType(0), 118 fFlags(0) 119 { 120 } 121 122 123 BVariant::BVariant(int8 value) 124 { 125 _SetTo(value); 126 } 127 128 129 BVariant::BVariant(uint8 value) 130 { 131 _SetTo(value); 132 } 133 134 135 BVariant::BVariant(int16 value) 136 { 137 _SetTo(value); 138 } 139 140 141 BVariant::BVariant(uint16 value) 142 { 143 _SetTo(value); 144 } 145 146 147 BVariant::BVariant(int32 value) 148 { 149 _SetTo(value); 150 } 151 152 153 BVariant::BVariant(uint32 value) 154 { 155 _SetTo(value); 156 } 157 158 159 BVariant::BVariant(int64 value) 160 { 161 _SetTo(value); 162 } 163 164 165 BVariant::BVariant(uint64 value) 166 { 167 _SetTo(value); 168 } 169 170 171 BVariant::BVariant(float value) 172 { 173 _SetTo(value); 174 } 175 176 177 BVariant::BVariant(double value) 178 { 179 _SetTo(value); 180 } 181 182 183 BVariant::BVariant(const void* value) 184 { 185 _SetTo(value); 186 } 187 188 189 BVariant::BVariant(const char* value, uint32 flags) 190 { 191 _SetTo(value, flags); 192 } 193 194 195 BVariant::BVariant(const BVariant& other) 196 { 197 _SetTo(other); 198 } 199 200 201 BVariant& 202 BVariant::operator=(const BVariant& other) 203 { 204 Unset(); 205 _SetTo(other); 206 return *this; 207 } 208 209 210 void 211 BVariant::SetTo(const BVariant& other) 212 { 213 Unset(); 214 _SetTo(other); 215 } 216 217 218 void 219 BVariant::SetTo(int8 value) 220 { 221 Unset(); 222 _SetTo(value); 223 } 224 225 226 void 227 BVariant::SetTo(uint8 value) 228 { 229 Unset(); 230 _SetTo(value); 231 } 232 233 234 void 235 BVariant::SetTo(int16 value) 236 { 237 Unset(); 238 _SetTo(value); 239 } 240 241 242 void 243 BVariant::SetTo(uint16 value) 244 { 245 Unset(); 246 _SetTo(value); 247 } 248 249 250 void 251 BVariant::SetTo(int32 value) 252 { 253 Unset(); 254 _SetTo(value); 255 } 256 257 258 void 259 BVariant::SetTo(uint32 value) 260 { 261 Unset(); 262 _SetTo(value); 263 } 264 265 266 void 267 BVariant::SetTo(int64 value) 268 { 269 Unset(); 270 _SetTo(value); 271 } 272 273 274 void 275 BVariant::SetTo(uint64 value) 276 { 277 Unset(); 278 _SetTo(value); 279 } 280 281 282 void 283 BVariant::SetTo(float value) 284 { 285 Unset(); 286 _SetTo(value); 287 } 288 289 290 void 291 BVariant::SetTo(double value) 292 { 293 Unset(); 294 _SetTo(value); 295 } 296 297 298 void 299 BVariant::SetTo(const void* value) 300 { 301 Unset(); 302 _SetTo(value); 303 } 304 305 306 void 307 BVariant::SetTo(const char* value, uint32 flags) 308 { 309 Unset(); 310 _SetTo(value, flags); 311 } 312 313 314 #endif // _VARIANT_H 315