Extend BVariant to support storing BRects as well.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@43080 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Rene Gollent
2011-11-01 20:57:31 +00:00
parent f908ff9bb6
commit 15dbca93da
2 changed files with 78 additions and 0 deletions
+44
View File
@@ -1,11 +1,13 @@
/* /*
* Copyright 2009, Ingo Weinhold, [email protected]. * Copyright 2009, Ingo Weinhold, [email protected].
* Copyright 2011, Rene Gollent, [email protected].
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#ifndef _VARIANT_H #ifndef _VARIANT_H
#define _VARIANT_H #define _VARIANT_H
#include <Rect.h>
#include <SupportDefs.h> #include <SupportDefs.h>
#include <TypeConstants.h> #include <TypeConstants.h>
@@ -36,6 +38,9 @@ public:
inline BVariant(uint64 value); inline BVariant(uint64 value);
inline BVariant(float value); inline BVariant(float value);
inline BVariant(double value); inline BVariant(double value);
inline BVariant(const BRect &value);
inline BVariant(float left, float top, float right,
float bottom);
inline BVariant(const void* value); inline BVariant(const void* value);
inline BVariant(const char* value, inline BVariant(const char* value,
uint32 flags = 0); uint32 flags = 0);
@@ -56,6 +61,9 @@ public:
inline void SetTo(uint64 value); inline void SetTo(uint64 value);
inline void SetTo(float value); inline void SetTo(float value);
inline void SetTo(double value); inline void SetTo(double value);
inline void SetTo(const BRect& value);
inline void SetTo(float left, float top, float right,
float bottom);
inline void SetTo(const void* value); inline void SetTo(const void* value);
inline void SetTo(const char* value, inline void SetTo(const char* value,
uint32 flags = 0); uint32 flags = 0);
@@ -92,6 +100,7 @@ public:
double ToDouble() const; double ToDouble() const;
void* ToPointer() const; void* ToPointer() const;
const char* ToString() const; const char* ToString() const;
BRect ToRect() const;
BReferenceable* ToReferenceable() const; BReferenceable* ToReferenceable() const;
void SwapEndianess(); void SwapEndianess();
@@ -123,6 +132,8 @@ private:
void _SetTo(float value); void _SetTo(float value);
void _SetTo(double value); void _SetTo(double value);
void _SetTo(const void* value); void _SetTo(const void* value);
void _SetTo(float left, float top, float right,
float bottom);
bool _SetTo(const char* value, bool _SetTo(const char* value,
uint32 flags); uint32 flags);
void _SetTo(BReferenceable* value, type_code type); void _SetTo(BReferenceable* value, type_code type);
@@ -150,6 +161,11 @@ private:
BReferenceable* fReferenceable; BReferenceable* fReferenceable;
uint8 fBytes[8]; uint8 fBytes[8];
}; };
float fLeft;
float fTop;
float fRight;
float fBottom;
}; };
@@ -227,6 +243,18 @@ BVariant::BVariant(double value)
} }
BVariant::BVariant(const BRect& value)
{
_SetTo(value);
}
BVariant::BVariant(float left, float top, float right, float bottom)
{
_SetTo(left, top, right, bottom);
}
BVariant::BVariant(const void* value) BVariant::BVariant(const void* value)
{ {
_SetTo(value); _SetTo(value);
@@ -363,6 +391,22 @@ BVariant::SetTo(double value)
} }
void
BVariant::SetTo(const BRect& value)
{
Unset();
_SetTo(value.left, value.top, value.right, value.bottom);
}
void
BVariant::SetTo(float left, float top, float right, float bottom)
{
Unset();
_SetTo(left, top, right, bottom);
}
void void
BVariant::SetTo(const void* value) BVariant::SetTo(const void* value)
{ {
+34
View File
@@ -1,5 +1,6 @@
/* /*
* Copyright 2009, Ingo Weinhold, [email protected]. * Copyright 2009, Ingo Weinhold, [email protected].
* Copyright 2011, Rene Gollent, [email protected].
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
@@ -96,6 +97,12 @@ BVariant::SetToTypedData(const void* data, type_code type)
break; break;
case B_STRING_TYPE: case B_STRING_TYPE:
return _SetTo((const char*)data, 0) ? B_OK : B_NO_MEMORY; return _SetTo((const char*)data, 0) ? B_OK : B_NO_MEMORY;
case B_RECT_TYPE:
{
BRect *rect = (BRect *)data;
_SetTo(rect->left, rect->top, rect->right, rect->bottom);
break;
}
default: default:
return B_BAD_TYPE; return B_BAD_TYPE;
} }
@@ -169,6 +176,9 @@ BVariant::operator==(const BVariant& other) const
if (fString == NULL || other.fString == NULL) if (fString == NULL || other.fString == NULL)
return fString == other.fString; return fString == other.fString;
return strcmp(fString, other.fString) == 0; return strcmp(fString, other.fString) == 0;
case B_RECT_TYPE:
return BRect(fLeft, fTop, fRight, fBottom) == BRect(
other.fLeft, other.fTop, other.fRight, other.fBottom);
default: default:
return false; return false;
} }
@@ -303,6 +313,13 @@ BVariant::ToDouble() const
} }
BRect
BVariant::ToRect() const
{
return BRect(fLeft, fTop, fRight, fBottom);
}
void* void*
BVariant::ToPointer() const BVariant::ToPointer() const
{ {
@@ -387,6 +404,9 @@ BVariant::AddToMessage(BMessage& message, const char* fieldName) const
return message.AddPointer(fieldName, fPointer); return message.AddPointer(fieldName, fPointer);
case B_STRING_TYPE: case B_STRING_TYPE:
return message.AddString(fieldName, fString); return message.AddString(fieldName, fString);
case B_RECT_TYPE:
return message.AddRect(fieldName, BRect(fLeft, fTop, fRight,
fBottom));
default: default:
return B_UNSUPPORTED; return B_UNSUPPORTED;
} }
@@ -443,6 +463,8 @@ BVariant::SizeOfType(type_code type)
return sizeof(double); return sizeof(double);
case B_POINTER_TYPE: case B_POINTER_TYPE:
return sizeof(void*); return sizeof(void*);
case B_RECT_TYPE:
return sizeof(BRect);
default: default:
return 0; return 0;
} }
@@ -606,6 +628,18 @@ BVariant::_SetTo(double value)
} }
void
BVariant::_SetTo(float left, float top, float right, float bottom)
{
fType = B_RECT_TYPE;
fFlags = 0;
fLeft = left;
fTop = top;
fRight = right;
fBottom = bottom;
}
void void
BVariant::_SetTo(const void* value) BVariant::_SetTo(const void* value)
{ {