* Added missing bool type support.

* Added Bytes(), returning a pointer to the "raw" data, and Size(), returning
  the data size.
* Added SetToTypedData(), which initializes the object from a data buffer and a
  type code.
* Added SwapEndianess() to swap the endianess of the contained data (if
  possible).


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@31627 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold
2009-07-18 23:01:48 +00:00
parent 591eab0aba
commit b5d6fc7173
2 changed files with 173 additions and 5 deletions
+31 -3
View File
@@ -19,6 +19,7 @@ enum {
class BVariant {
public:
inline BVariant();
inline BVariant(bool value);
inline BVariant(int8 value);
inline BVariant(uint8 value);
inline BVariant(int16 value);
@@ -36,6 +37,7 @@ public:
~BVariant();
inline void SetTo(const BVariant& other);
inline void SetTo(bool value);
inline void SetTo(int8 value);
inline void SetTo(uint8 value);
inline void SetTo(int16 value);
@@ -49,17 +51,22 @@ public:
inline void SetTo(const void* value);
inline void SetTo(const char* value,
uint32 flags = 0);
status_t SetToTypedData(const void* data,
type_code type);
void Unset();
inline BVariant& operator=(const BVariant& other);
type_code Type() const { return fType; }
inline type_code Type() const { return fType; }
size_t Size() const;
const uint8* Bytes() const;
bool IsNumber() const;
bool IsInteger() const;
bool IsFloat() const;
// floating point, not just float
bool ToBool() const;
int8 ToInt8() const;
uint8 ToUInt8() const;
int16 ToInt16() const;
@@ -73,10 +80,15 @@ public:
void* ToPointer() const;
const char* ToString() const;
static size_t SizeOfType(uint32 type);
void SwapEndianess();
// has effect only on scalar types (pointer
// counting as scalar, not string, though)
static size_t SizeOfType(type_code type);
private:
void _SetTo(const BVariant& other);
void _SetTo(bool value);
void _SetTo(int8 value);
void _SetTo(uint8 value);
void _SetTo(int16 value);
@@ -88,7 +100,7 @@ private:
void _SetTo(float value);
void _SetTo(double value);
void _SetTo(const void* value);
void _SetTo(const char* value,
bool _SetTo(const char* value,
uint32 flags);
template<typename NumberType>
@@ -98,6 +110,7 @@ private:
type_code fType;
uint32 fFlags;
union {
bool fBool;
int8 fInt8;
uint8 fUInt8;
int16 fInt16;
@@ -110,6 +123,7 @@ private:
double fDouble;
void* fPointer;
char* fString;
uint8 fBytes[8];
};
};
@@ -122,6 +136,12 @@ BVariant::BVariant()
}
BVariant::BVariant(bool value)
{
_SetTo(value);
}
BVariant::BVariant(int8 value)
{
_SetTo(value);
@@ -217,6 +237,14 @@ BVariant::SetTo(const BVariant& other)
}
void
BVariant::SetTo(bool value)
{
Unset();
_SetTo(value);
}
void
BVariant::SetTo(int8 value)
{
+142 -2
View File
@@ -3,17 +3,22 @@
* Distributed under the terms of the MIT License.
*/
#include <Variant.h>
#include <stdlib.h>
#include <string.h>
#include <ByteOrder.h>
template<typename NumberType>
inline NumberType
BVariant::_ToNumber() const
{
switch (fType) {
case B_BOOL_TYPE:
return fBool ? 1 : 0;
case B_INT8_TYPE:
return (NumberType)fInt8;
case B_UINT8_TYPE:
@@ -46,6 +51,59 @@ BVariant::~BVariant()
}
status_t
BVariant::SetToTypedData(const void* data, type_code type)
{
Unset();
switch (type) {
case B_BOOL_TYPE:
fBool = *(bool*)data;
break;
case B_INT8_TYPE:
fInt8 = *(int8*)data;
break;
case B_UINT8_TYPE:
fUInt8 = *(uint8*)data;
break;
case B_INT16_TYPE:
fInt16 = *(int16*)data;
break;
case B_UINT16_TYPE:
fUInt16 = *(uint16*)data;
break;
case B_INT32_TYPE:
fInt32 = *(int32*)data;
break;
case B_UINT32_TYPE:
fUInt32 = *(uint32*)data;
break;
case B_INT64_TYPE:
fInt64 = *(int64*)data;
break;
case B_UINT64_TYPE:
fUInt64 = *(uint64*)data;
break;
case B_FLOAT_TYPE:
fFloat = *(float*)data;
break;
case B_DOUBLE_TYPE:
fDouble = *(double*)data;
break;
case B_POINTER_TYPE:
fPointer = *(void**)data;
break;
case B_STRING_TYPE:
return _SetTo((const char*)data, 0) ? B_OK : B_NO_MEMORY;
default:
return B_BAD_TYPE;
}
fType = type;
return B_OK;
}
void
BVariant::Unset()
{
@@ -64,6 +122,24 @@ BVariant::Unset()
}
size_t
BVariant::Size() const
{
if (fType == B_STRING_TYPE)
return fString != NULL ? strlen(fString) + 1 : 0;
return SizeOfType(fType);
}
const uint8*
BVariant::Bytes() const
{
if (fType == B_STRING_TYPE)
return (const uint8*)fString;
return fBytes;
}
bool
BVariant::IsNumber() const
{
@@ -117,6 +193,45 @@ BVariant::IsFloat() const
}
bool
BVariant::ToBool() const
{
switch (fType) {
case B_BOOL_TYPE:
return fBool;
case B_INT8_TYPE:
return fInt8 != 0;
case B_UINT8_TYPE:
return fUInt8 != 0;
case B_INT16_TYPE:
return fInt16 != 0;
case B_UINT16_TYPE:
return fUInt16 != 0;
case B_INT32_TYPE:
return fInt32 != 0;
case B_UINT32_TYPE:
return fUInt32 != 0;
case B_INT64_TYPE:
return fInt64 != 0;
case B_UINT64_TYPE:
return fUInt64 != 0;
case B_FLOAT_TYPE:
return fFloat != 0;
case B_DOUBLE_TYPE:
return fDouble != 0;
case B_POINTER_TYPE:
return fPointer != NULL;
case B_STRING_TYPE:
return fString != NULL;
// TODO: We should probably check for actual values like "true",
// "false", "on", "off", etc.
default:
return false;
}
}
int8
BVariant::ToInt8() const
{
@@ -220,10 +335,22 @@ BVariant::_SetTo(const BVariant& other)
}
void
BVariant::SwapEndianess()
{
if (!IsNumber() || fType == B_POINTER_TYPE)
return;
swap_data(fType, fBytes, Size(), B_SWAP_ALWAYS);
}
/*static*/ size_t
BVariant::SizeOfType(uint32 type)
BVariant::SizeOfType(type_code type)
{
switch (type) {
case B_BOOL_TYPE:
return 1;
case B_INT8_TYPE:
return 1;
case B_UINT8_TYPE:
@@ -252,6 +379,15 @@ BVariant::SizeOfType(uint32 type)
}
void
BVariant::_SetTo(bool value)
{
fType = B_BOOL_TYPE;
fFlags = 0;
fBool = value;
}
void
BVariant::_SetTo(int8 value)
{
@@ -351,7 +487,7 @@ BVariant::_SetTo(const void* value)
}
void
bool
BVariant::_SetTo(const char* value, uint32 flags)
{
fType = B_STRING_TYPE;
@@ -361,11 +497,15 @@ BVariant::_SetTo(const char* value, uint32 flags)
if ((flags & B_VARIANT_DONT_COPY_DATA) == 0) {
fString = strdup(value);
fFlags |= B_VARIANT_OWNS_DATA;
if (fString == NULL)
return false;
} else {
fString = (char*)value;
fFlags |= flags & B_VARIANT_OWNS_DATA;
}
} else
fString = NULL;
return true;
}