From a9a6d541f8e0a4cfa54500aacb5ec8576ff3febd Mon Sep 17 00:00:00 2001 From: cxl Date: Tue, 6 Mar 2012 14:19:15 +0000 Subject: [PATCH] Core: Ref support in ValueType git-svn-id: svn://ultimatepp.org/upp/trunk@4663 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/Value.h | 16 ++++++++------- uppsrc/Core/ValueUtil.h | 43 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/uppsrc/Core/Value.h b/uppsrc/Core/Value.h index 491e0818f..3cd2abdf2 100644 --- a/uppsrc/Core/Value.h +++ b/uppsrc/Core/Value.h @@ -1,11 +1,13 @@ #ifdef SVO_VALUE -class Id; -class ValueArray; -class ValueMap; -class XmlIO; -class JsonIO; -class Ref; +class Id; +class ValueArray; +class ValueMap; +class XmlIO; +class JsonIO; + +class Ref; +struct ValueTypeRef; template void Jsonize(JsonIO& io, T& var); @@ -61,7 +63,7 @@ public: bool operator==(const T&) const { NEVER(); return false; } String ToString() const { return typeid(T).name(); } - operator Ref(); + operator ValueTypeRef(); }; template // Backward compatiblity diff --git a/uppsrc/Core/ValueUtil.h b/uppsrc/Core/ValueUtil.h index f82017db0..c8d8c9d63 100644 --- a/uppsrc/Core/ValueUtil.h +++ b/uppsrc/Core/ValueUtil.h @@ -87,6 +87,7 @@ struct RefManager { virtual bool IsNull(const void *) { return false; } virtual void SetValue(void *, const Value& v) { NEVER(); } virtual void SetNull(void *) { NEVER(); } + virtual void Assign(void *t, const void *s) { NEVER(); } virtual ~RefManager() {} }; @@ -97,6 +98,7 @@ struct StdRef : public RefManager { virtual int GetType() { return GetValueTypeNo(); } virtual bool IsNull(const void *p) { return UPP::IsNull(*(T *) p); } virtual void SetNull(void *p) { UPP::SetNull(*(T *)p); } + void Assign(void *t, const void *s) { *(T*)t = *(T*)s; } virtual ~StdRef() {} }; @@ -113,7 +115,7 @@ public: void *GetVoidPtr() const { return ptr; } template - bool Is() const { return GetType() == GetValueTypeNo(); } + bool Is() const { return GetType() == GetValueTypeNo(); } // VALUE_V!!! template T& Get() const { ASSERT(GetValueTypeNo() == GetType()); return *(T *)GetVoidPtr(); } @@ -124,6 +126,8 @@ public: operator Value() const { return GetValue(); } Value operator~() const { return GetValue(); } Ref& operator=(const Value& v) { SetValue(v); return *this; } +// Ref& operator=(const Ref& r); +// Ref& operator=(ValueTypeRef& r); Ref(String& s); Ref(WString& s); @@ -135,6 +139,7 @@ public: Ref(Time& t); Ref(Value& v); Ref(void *_ptr, RefManager *_m) { ptr = _ptr, m = _m; } + Ref(const ValueTypeRef& r); Ref() { ptr = m = NULL; } }; @@ -160,10 +165,40 @@ Ref AsRef(T& x) { return Ref(&x, &Single< StdRef >()); } -template -ValueType::operator Ref() +struct ValueTypeRef { + RefManager *m; + void *ptr; +}; + +inline +Ref::Ref(const ValueTypeRef& r) { - return AsRef(*static_cast(this)); + ptr = r.ptr; + m = r.m; +} +/* +Ref& Ref::operator=(ValueTypeRef& r) +{ + ASSERT(r.m->GetType() == GetType()); + m->Assign(ptr, r.ptr); + return *this; +} + +Ref& Ref::operator=(const Ref& r) +{ + ASSERT(r.m->GetType() == GetType()); + m->Assign(ptr, r.ptr); + return *this; +} +*/ + +template +ValueType::operator ValueTypeRef() +{ + ValueTypeRef h; + h.ptr = this; + h.m = &Single< StdRef >(); + return h; } #define E__Value(I) Value p##I