From d8c41ef38f31a6d4e1f1548b5305099781447040 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Sat, 25 Jul 2009 00:20:53 +0000 Subject: [PATCH] Added support for containing BReferenceables of arbitrary actual type. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@31744 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/shared/Variant.h | 27 +++++++++++++++++++++++++-- src/kits/shared/Variant.cpp | 19 +++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/headers/private/shared/Variant.h b/headers/private/shared/Variant.h index 93904972a3..0a1fb80315 100644 --- a/headers/private/shared/Variant.h +++ b/headers/private/shared/Variant.h @@ -9,10 +9,13 @@ #include #include +#include + enum { - B_VARIANT_DONT_COPY_DATA = 0x01, - B_VARIANT_OWNS_DATA = 0x02 + B_VARIANT_DONT_COPY_DATA = 0x01, + B_VARIANT_OWNS_DATA = 0x02, + B_VARIANT_REFERENCEABLE_DATA = 0x04 }; @@ -33,6 +36,8 @@ public: inline BVariant(const void* value); inline BVariant(const char* value, uint32 flags = 0); + inline BVariant(BReferenceable* value, type_code type); + // type must be a custom type inline BVariant(const BVariant& other); ~BVariant(); @@ -51,6 +56,8 @@ public: inline void SetTo(const void* value); inline void SetTo(const char* value, uint32 flags = 0); + inline void SetTo(BReferenceable* value, type_code type); + // type must be a custom type status_t SetToTypedData(const void* data, type_code type); void Unset(); @@ -102,6 +109,7 @@ private: void _SetTo(const void* value); bool _SetTo(const char* value, uint32 flags); + void _SetTo(BReferenceable* value, type_code type); template inline NumberType _ToNumber() const; @@ -123,6 +131,7 @@ private: double fDouble; void* fPointer; char* fString; + BReferenceable* fReferenceable; uint8 fBytes[8]; }; }; @@ -214,6 +223,12 @@ BVariant::BVariant(const char* value, uint32 flags) } +BVariant::BVariant(BReferenceable* value, type_code type) +{ + _SetTo(value, type); +} + + BVariant::BVariant(const BVariant& other) { _SetTo(other); @@ -341,4 +356,12 @@ BVariant::SetTo(const char* value, uint32 flags) } +void +BVariant::SetTo(BReferenceable* value, type_code type) +{ + Unset(); + _SetTo(value, type); +} + + #endif // _VARIANT_H diff --git a/src/kits/shared/Variant.cpp b/src/kits/shared/Variant.cpp index 3855a208be..980de5b6d7 100644 --- a/src/kits/shared/Variant.cpp +++ b/src/kits/shared/Variant.cpp @@ -115,6 +115,9 @@ BVariant::Unset() default: break; } + } else if ((fFlags & B_VARIANT_REFERENCEABLE_DATA) != 0) { + if (fReferenceable != NULL) + fReferenceable->ReleaseReference(); } fType = 0; @@ -127,6 +130,8 @@ BVariant::Size() const { if (fType == B_STRING_TYPE) return fString != NULL ? strlen(fString) + 1 : 0; + if ((fFlags & B_VARIANT_REFERENCEABLE_DATA) != 0) + return sizeof(this->fReferenceable); return SizeOfType(fType); } @@ -329,6 +334,9 @@ BVariant::_SetTo(const BVariant& other) default: break; } + } else if ((other.fFlags & B_VARIANT_REFERENCEABLE_DATA) != 0) { + if (other.fReferenceable != NULL) + fReferenceable->AcquireReference(); } memcpy(this, &other, sizeof(BVariant)); @@ -509,3 +517,14 @@ BVariant::_SetTo(const char* value, uint32 flags) return true; } + +void +BVariant::_SetTo(BReferenceable* value, type_code type) +{ + fType = type; + fFlags = B_VARIANT_REFERENCEABLE_DATA; + fReferenceable = value; + + if (fReferenceable != NULL) + fReferenceable->AcquireReference(); +}