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 #include <Referenceable.h> 13 14 15 enum { 16 B_VARIANT_DONT_COPY_DATA = 0x01, 17 B_VARIANT_OWNS_DATA = 0x02, 18 B_VARIANT_REFERENCEABLE_DATA = 0x04 19 }; 20 21 22 class BVariant { 23 public: 24 inline BVariant(); 25 inline BVariant(bool value); 26 inline BVariant(int8 value); 27 inline BVariant(uint8 value); 28 inline BVariant(int16 value); 29 inline BVariant(uint16 value); 30 inline BVariant(int32 value); 31 inline BVariant(uint32 value); 32 inline BVariant(int64 value); 33 inline BVariant(uint64 value); 34 inline BVariant(float value); 35 inline BVariant(double value); 36 inline BVariant(const void* value); 37 inline BVariant(const char* value, 38 uint32 flags = 0); 39 inline BVariant(BReferenceable* value, type_code type); 40 // type must be a custom type 41 inline BVariant(const BVariant& other); 42 ~BVariant(); 43 44 inline void SetTo(const BVariant& other); 45 inline void SetTo(bool value); 46 inline void SetTo(int8 value); 47 inline void SetTo(uint8 value); 48 inline void SetTo(int16 value); 49 inline void SetTo(uint16 value); 50 inline void SetTo(int32 value); 51 inline void SetTo(uint32 value); 52 inline void SetTo(int64 value); 53 inline void SetTo(uint64 value); 54 inline void SetTo(float value); 55 inline void SetTo(double value); 56 inline void SetTo(const void* value); 57 inline void SetTo(const char* value, 58 uint32 flags = 0); 59 inline void SetTo(BReferenceable* value, type_code type); 60 // type must be a custom type 61 status_t SetToTypedData(const void* data, 62 type_code type); 63 void Unset(); 64 65 inline BVariant& operator=(const BVariant& other); 66 67 inline type_code Type() const { return fType; } 68 size_t Size() const; 69 const uint8* Bytes() const; 70 71 bool IsNumber() const; 72 bool IsInteger() const; 73 bool IsFloat() const; 74 // floating point, not just float 75 76 bool ToBool() const; 77 int8 ToInt8() const; 78 uint8 ToUInt8() const; 79 int16 ToInt16() const; 80 uint16 ToUInt16() const; 81 int32 ToInt32() const; 82 uint32 ToUInt32() const; 83 int64 ToInt64() const; 84 uint64 ToUInt64() const; 85 float ToFloat() const; 86 double ToDouble() const; 87 void* ToPointer() const; 88 const char* ToString() const; 89 BReferenceable* ToReferenceable() const; 90 91 void SwapEndianess(); 92 // has effect only on scalar types (pointer 93 // counting as scalar, not string, though) 94 95 static size_t SizeOfType(type_code type); 96 97 private: 98 void _SetTo(const BVariant& other); 99 void _SetTo(bool value); 100 void _SetTo(int8 value); 101 void _SetTo(uint8 value); 102 void _SetTo(int16 value); 103 void _SetTo(uint16 value); 104 void _SetTo(int32 value); 105 void _SetTo(uint32 value); 106 void _SetTo(int64 value); 107 void _SetTo(uint64 value); 108 void _SetTo(float value); 109 void _SetTo(double value); 110 void _SetTo(const void* value); 111 bool _SetTo(const char* value, 112 uint32 flags); 113 void _SetTo(BReferenceable* value, type_code type); 114 115 template<typename NumberType> 116 inline NumberType _ToNumber() const; 117 118 private: 119 type_code fType; 120 uint32 fFlags; 121 union { 122 bool fBool; 123 int8 fInt8; 124 uint8 fUInt8; 125 int16 fInt16; 126 uint16 fUInt16; 127 int32 fInt32; 128 uint32 fUInt32; 129 int64 fInt64; 130 uint64 fUInt64; 131 float fFloat; 132 double fDouble; 133 void* fPointer; 134 char* fString; 135 BReferenceable* fReferenceable; 136 uint8 fBytes[8]; 137 }; 138 }; 139 140 141 BVariant::BVariant() 142 : 143 fType(0), 144 fFlags(0) 145 { 146 } 147 148 149 BVariant::BVariant(bool value) 150 { 151 _SetTo(value); 152 } 153 154 155 BVariant::BVariant(int8 value) 156 { 157 _SetTo(value); 158 } 159 160 161 BVariant::BVariant(uint8 value) 162 { 163 _SetTo(value); 164 } 165 166 167 BVariant::BVariant(int16 value) 168 { 169 _SetTo(value); 170 } 171 172 173 BVariant::BVariant(uint16 value) 174 { 175 _SetTo(value); 176 } 177 178 179 BVariant::BVariant(int32 value) 180 { 181 _SetTo(value); 182 } 183 184 185 BVariant::BVariant(uint32 value) 186 { 187 _SetTo(value); 188 } 189 190 191 BVariant::BVariant(int64 value) 192 { 193 _SetTo(value); 194 } 195 196 197 BVariant::BVariant(uint64 value) 198 { 199 _SetTo(value); 200 } 201 202 203 BVariant::BVariant(float value) 204 { 205 _SetTo(value); 206 } 207 208 209 BVariant::BVariant(double value) 210 { 211 _SetTo(value); 212 } 213 214 215 BVariant::BVariant(const void* value) 216 { 217 _SetTo(value); 218 } 219 220 221 BVariant::BVariant(const char* value, uint32 flags) 222 { 223 _SetTo(value, flags); 224 } 225 226 227 BVariant::BVariant(BReferenceable* value, type_code type) 228 { 229 _SetTo(value, type); 230 } 231 232 233 BVariant::BVariant(const BVariant& other) 234 { 235 _SetTo(other); 236 } 237 238 239 BVariant& 240 BVariant::operator=(const BVariant& other) 241 { 242 Unset(); 243 _SetTo(other); 244 return *this; 245 } 246 247 248 void 249 BVariant::SetTo(const BVariant& other) 250 { 251 Unset(); 252 _SetTo(other); 253 } 254 255 256 void 257 BVariant::SetTo(bool value) 258 { 259 Unset(); 260 _SetTo(value); 261 } 262 263 264 void 265 BVariant::SetTo(int8 value) 266 { 267 Unset(); 268 _SetTo(value); 269 } 270 271 272 void 273 BVariant::SetTo(uint8 value) 274 { 275 Unset(); 276 _SetTo(value); 277 } 278 279 280 void 281 BVariant::SetTo(int16 value) 282 { 283 Unset(); 284 _SetTo(value); 285 } 286 287 288 void 289 BVariant::SetTo(uint16 value) 290 { 291 Unset(); 292 _SetTo(value); 293 } 294 295 296 void 297 BVariant::SetTo(int32 value) 298 { 299 Unset(); 300 _SetTo(value); 301 } 302 303 304 void 305 BVariant::SetTo(uint32 value) 306 { 307 Unset(); 308 _SetTo(value); 309 } 310 311 312 void 313 BVariant::SetTo(int64 value) 314 { 315 Unset(); 316 _SetTo(value); 317 } 318 319 320 void 321 BVariant::SetTo(uint64 value) 322 { 323 Unset(); 324 _SetTo(value); 325 } 326 327 328 void 329 BVariant::SetTo(float value) 330 { 331 Unset(); 332 _SetTo(value); 333 } 334 335 336 void 337 BVariant::SetTo(double value) 338 { 339 Unset(); 340 _SetTo(value); 341 } 342 343 344 void 345 BVariant::SetTo(const void* value) 346 { 347 Unset(); 348 _SetTo(value); 349 } 350 351 352 void 353 BVariant::SetTo(const char* value, uint32 flags) 354 { 355 Unset(); 356 _SetTo(value, flags); 357 } 358 359 360 void 361 BVariant::SetTo(BReferenceable* value, type_code type) 362 { 363 Unset(); 364 _SetTo(value, type); 365 } 366 367 368 #endif // _VARIANT_H 369