2009-04-22 15:55:26 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
2009-06-20 03:00:25 +04:00
|
|
|
#ifndef _VARIANT_H
|
|
|
|
#define _VARIANT_H
|
2009-04-22 15:55:26 +04:00
|
|
|
|
|
|
|
|
|
|
|
#include <SupportDefs.h>
|
|
|
|
#include <TypeConstants.h>
|
|
|
|
|
2009-07-25 04:20:53 +04:00
|
|
|
#include <Referenceable.h>
|
|
|
|
|
2009-04-22 15:55:26 +04:00
|
|
|
|
|
|
|
enum {
|
2009-07-25 04:20:53 +04:00
|
|
|
B_VARIANT_DONT_COPY_DATA = 0x01,
|
|
|
|
B_VARIANT_OWNS_DATA = 0x02,
|
|
|
|
B_VARIANT_REFERENCEABLE_DATA = 0x04
|
2009-04-22 15:55:26 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-06-20 03:00:25 +04:00
|
|
|
class BVariant {
|
2009-04-22 15:55:26 +04:00
|
|
|
public:
|
2009-06-20 03:00:25 +04:00
|
|
|
inline BVariant();
|
2009-07-19 03:01:48 +04:00
|
|
|
inline BVariant(bool value);
|
2009-06-20 03:00:25 +04:00
|
|
|
inline BVariant(int8 value);
|
|
|
|
inline BVariant(uint8 value);
|
|
|
|
inline BVariant(int16 value);
|
|
|
|
inline BVariant(uint16 value);
|
|
|
|
inline BVariant(int32 value);
|
|
|
|
inline BVariant(uint32 value);
|
|
|
|
inline BVariant(int64 value);
|
|
|
|
inline BVariant(uint64 value);
|
|
|
|
inline BVariant(float value);
|
|
|
|
inline BVariant(double value);
|
|
|
|
inline BVariant(const void* value);
|
|
|
|
inline BVariant(const char* value,
|
2009-04-22 15:55:26 +04:00
|
|
|
uint32 flags = 0);
|
2009-07-25 04:20:53 +04:00
|
|
|
inline BVariant(BReferenceable* value, type_code type);
|
|
|
|
// type must be a custom type
|
2009-06-20 03:00:25 +04:00
|
|
|
inline BVariant(const BVariant& other);
|
|
|
|
~BVariant();
|
2009-04-22 15:55:26 +04:00
|
|
|
|
2009-06-20 03:00:25 +04:00
|
|
|
inline void SetTo(const BVariant& other);
|
2009-07-19 03:01:48 +04:00
|
|
|
inline void SetTo(bool value);
|
2009-04-22 15:55:26 +04:00
|
|
|
inline void SetTo(int8 value);
|
|
|
|
inline void SetTo(uint8 value);
|
|
|
|
inline void SetTo(int16 value);
|
|
|
|
inline void SetTo(uint16 value);
|
|
|
|
inline void SetTo(int32 value);
|
|
|
|
inline void SetTo(uint32 value);
|
|
|
|
inline void SetTo(int64 value);
|
|
|
|
inline void SetTo(uint64 value);
|
|
|
|
inline void SetTo(float value);
|
|
|
|
inline void SetTo(double value);
|
|
|
|
inline void SetTo(const void* value);
|
|
|
|
inline void SetTo(const char* value,
|
|
|
|
uint32 flags = 0);
|
2009-07-25 04:20:53 +04:00
|
|
|
inline void SetTo(BReferenceable* value, type_code type);
|
|
|
|
// type must be a custom type
|
2009-07-19 03:01:48 +04:00
|
|
|
status_t SetToTypedData(const void* data,
|
|
|
|
type_code type);
|
2009-04-22 15:55:26 +04:00
|
|
|
void Unset();
|
|
|
|
|
2009-06-20 03:00:25 +04:00
|
|
|
inline BVariant& operator=(const BVariant& other);
|
2009-04-22 15:55:26 +04:00
|
|
|
|
2009-09-27 08:08:45 +04:00
|
|
|
bool operator==(const BVariant& other) const;
|
|
|
|
inline bool operator!=(const BVariant& other) const;
|
|
|
|
|
2009-07-19 03:01:48 +04:00
|
|
|
inline type_code Type() const { return fType; }
|
|
|
|
size_t Size() const;
|
|
|
|
const uint8* Bytes() const;
|
2009-04-22 15:55:26 +04:00
|
|
|
|
2009-09-27 08:08:45 +04:00
|
|
|
inline bool IsNumber() const;
|
|
|
|
inline bool IsInteger(bool* _isSigned = NULL) const;
|
|
|
|
inline bool IsFloat() const;
|
2009-06-19 21:26:59 +04:00
|
|
|
// floating point, not just float
|
|
|
|
|
2009-07-19 03:01:48 +04:00
|
|
|
bool ToBool() const;
|
2009-04-22 15:55:26 +04:00
|
|
|
int8 ToInt8() const;
|
|
|
|
uint8 ToUInt8() const;
|
|
|
|
int16 ToInt16() const;
|
|
|
|
uint16 ToUInt16() const;
|
|
|
|
int32 ToInt32() const;
|
|
|
|
uint32 ToUInt32() const;
|
|
|
|
int64 ToInt64() const;
|
|
|
|
uint64 ToUInt64() const;
|
|
|
|
float ToFloat() const;
|
|
|
|
double ToDouble() const;
|
|
|
|
void* ToPointer() const;
|
|
|
|
const char* ToString() const;
|
2009-07-30 06:34:41 +04:00
|
|
|
BReferenceable* ToReferenceable() const;
|
2009-04-22 15:55:26 +04:00
|
|
|
|
2009-07-19 03:01:48 +04:00
|
|
|
void SwapEndianess();
|
|
|
|
// has effect only on scalar types (pointer
|
|
|
|
// counting as scalar, not string, though)
|
|
|
|
|
|
|
|
static size_t SizeOfType(type_code type);
|
2009-09-27 08:08:45 +04:00
|
|
|
static bool TypeIsNumber(type_code type);
|
|
|
|
static bool TypeIsInteger(type_code type,
|
|
|
|
bool* _isSigned = NULL);
|
|
|
|
static bool TypeIsFloat(type_code type);
|
2009-07-13 22:33:06 +04:00
|
|
|
|
2009-04-22 15:55:26 +04:00
|
|
|
private:
|
2009-06-20 03:00:25 +04:00
|
|
|
void _SetTo(const BVariant& other);
|
2009-07-19 03:01:48 +04:00
|
|
|
void _SetTo(bool value);
|
2009-04-22 15:55:26 +04:00
|
|
|
void _SetTo(int8 value);
|
|
|
|
void _SetTo(uint8 value);
|
|
|
|
void _SetTo(int16 value);
|
|
|
|
void _SetTo(uint16 value);
|
|
|
|
void _SetTo(int32 value);
|
|
|
|
void _SetTo(uint32 value);
|
|
|
|
void _SetTo(int64 value);
|
|
|
|
void _SetTo(uint64 value);
|
|
|
|
void _SetTo(float value);
|
|
|
|
void _SetTo(double value);
|
|
|
|
void _SetTo(const void* value);
|
2009-07-19 03:01:48 +04:00
|
|
|
bool _SetTo(const char* value,
|
2009-04-22 15:55:26 +04:00
|
|
|
uint32 flags);
|
2009-07-25 04:20:53 +04:00
|
|
|
void _SetTo(BReferenceable* value, type_code type);
|
2009-04-22 15:55:26 +04:00
|
|
|
|
|
|
|
template<typename NumberType>
|
|
|
|
inline NumberType _ToNumber() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
type_code fType;
|
|
|
|
uint32 fFlags;
|
|
|
|
union {
|
2009-07-19 03:01:48 +04:00
|
|
|
bool fBool;
|
2009-04-22 15:55:26 +04:00
|
|
|
int8 fInt8;
|
|
|
|
uint8 fUInt8;
|
|
|
|
int16 fInt16;
|
|
|
|
uint16 fUInt16;
|
|
|
|
int32 fInt32;
|
|
|
|
uint32 fUInt32;
|
|
|
|
int64 fInt64;
|
|
|
|
uint64 fUInt64;
|
|
|
|
float fFloat;
|
|
|
|
double fDouble;
|
|
|
|
void* fPointer;
|
|
|
|
char* fString;
|
2009-07-25 04:20:53 +04:00
|
|
|
BReferenceable* fReferenceable;
|
2009-07-19 03:01:48 +04:00
|
|
|
uint8 fBytes[8];
|
2009-04-22 15:55:26 +04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::BVariant()
|
2009-04-22 15:55:26 +04:00
|
|
|
:
|
|
|
|
fType(0),
|
|
|
|
fFlags(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-19 03:01:48 +04:00
|
|
|
BVariant::BVariant(bool value)
|
|
|
|
{
|
|
|
|
_SetTo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::BVariant(int8 value)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
_SetTo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::BVariant(uint8 value)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
_SetTo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::BVariant(int16 value)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
_SetTo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::BVariant(uint16 value)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
_SetTo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::BVariant(int32 value)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
_SetTo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::BVariant(uint32 value)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
_SetTo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::BVariant(int64 value)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
_SetTo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::BVariant(uint64 value)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
_SetTo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::BVariant(float value)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
_SetTo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::BVariant(double value)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
_SetTo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::BVariant(const void* value)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
_SetTo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::BVariant(const char* value, uint32 flags)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
_SetTo(value, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-25 04:20:53 +04:00
|
|
|
BVariant::BVariant(BReferenceable* value, type_code type)
|
|
|
|
{
|
|
|
|
_SetTo(value, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::BVariant(const BVariant& other)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
_SetTo(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant&
|
|
|
|
BVariant::operator=(const BVariant& other)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
Unset();
|
|
|
|
_SetTo(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-27 08:08:45 +04:00
|
|
|
bool
|
|
|
|
BVariant::operator!=(const BVariant& other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-22 15:55:26 +04:00
|
|
|
void
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::SetTo(const BVariant& other)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
Unset();
|
|
|
|
_SetTo(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-19 03:01:48 +04:00
|
|
|
void
|
|
|
|
BVariant::SetTo(bool value)
|
|
|
|
{
|
|
|
|
Unset();
|
|
|
|
_SetTo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-22 15:55:26 +04:00
|
|
|
void
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::SetTo(int8 value)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
Unset();
|
|
|
|
_SetTo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::SetTo(uint8 value)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
Unset();
|
|
|
|
_SetTo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::SetTo(int16 value)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
Unset();
|
|
|
|
_SetTo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::SetTo(uint16 value)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
Unset();
|
|
|
|
_SetTo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::SetTo(int32 value)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
Unset();
|
|
|
|
_SetTo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::SetTo(uint32 value)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
Unset();
|
|
|
|
_SetTo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::SetTo(int64 value)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
Unset();
|
|
|
|
_SetTo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::SetTo(uint64 value)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
Unset();
|
|
|
|
_SetTo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::SetTo(float value)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
Unset();
|
|
|
|
_SetTo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::SetTo(double value)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
Unset();
|
|
|
|
_SetTo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::SetTo(const void* value)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
Unset();
|
|
|
|
_SetTo(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2009-06-20 03:00:25 +04:00
|
|
|
BVariant::SetTo(const char* value, uint32 flags)
|
2009-04-22 15:55:26 +04:00
|
|
|
{
|
|
|
|
Unset();
|
|
|
|
_SetTo(value, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-25 04:20:53 +04:00
|
|
|
void
|
|
|
|
BVariant::SetTo(BReferenceable* value, type_code type)
|
|
|
|
{
|
|
|
|
Unset();
|
|
|
|
_SetTo(value, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-27 08:08:45 +04:00
|
|
|
bool
|
|
|
|
BVariant::IsNumber() const
|
|
|
|
{
|
|
|
|
return TypeIsNumber(fType);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
BVariant::IsInteger(bool* _isSigned) const
|
|
|
|
{
|
|
|
|
return TypeIsInteger(fType, _isSigned);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
BVariant::IsFloat() const
|
|
|
|
{
|
|
|
|
return TypeIsFloat(fType);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-20 03:00:25 +04:00
|
|
|
#endif // _VARIANT_H
|