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