From 50e6553d3909186277b7f541e7a17d247be16324 Mon Sep 17 00:00:00 2001 From: cxl Date: Sat, 3 Mar 2012 10:17:50 +0000 Subject: [PATCH] .developing SvoValue git-svn-id: svn://ultimatepp.org/upp/trunk@4647 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/Color.h | 2 +- uppsrc/Core/Complex.h | 13 ++---- uppsrc/Core/Core.h | 4 +- uppsrc/Core/Gtypes.h | 3 ++ uppsrc/Core/Value.cpp | 94 ++++++++++++++++++++++++++++++++------- uppsrc/Core/Value.h | 23 ++++++++-- uppsrc/Core/Value.hpp | 11 +++-- uppsrc/Core/ValueUtil.cpp | 31 +++++++++++-- uppsrc/Core/ValueUtil.h | 4 +- uppsrc/Core/Xmlize.cpp | 2 + uppsrc/Core/Xmlize.h | 2 + 11 files changed, 149 insertions(+), 40 deletions(-) diff --git a/uppsrc/Core/Color.h b/uppsrc/Core/Color.h index 8a6e7dddb..de4e83f35 100644 --- a/uppsrc/Core/Color.h +++ b/uppsrc/Core/Color.h @@ -25,7 +25,7 @@ inline dword RGB(byte r, byte g, byte b) { return r | (g << 8) | (b << 1 const int COLOR_V = 39; -class Color : AssignValueTypeNo > { +class Color : public AssignValueTypeNo > { protected: dword color; diff --git a/uppsrc/Core/Complex.h b/uppsrc/Core/Complex.h index 0654e60aa..e60a76b85 100644 --- a/uppsrc/Core/Complex.h +++ b/uppsrc/Core/Complex.h @@ -15,6 +15,10 @@ struct Complex : std::complex bool operator==(const Complex& c) const { return (const C&)(*this) == (const C&)c; } bool operator!=(const Complex& c) const { return (const C&)(*this) != (const C&)c; } + + void Serialize(Stream& s); + void Xmlize(XmlIO& xio); + void Jsonize(JsonIO& jio); }; template<> inline bool IsNull(const Complex& r) { return r.real() < DOUBLE_NULL_LIM || r.imag() < DOUBLE_NULL_LIM; } @@ -22,15 +26,6 @@ template<> inline unsigned GetHashValue(const Complex& x) { return CombineHash(x template<> inline String AsString(const std::complex& x) { return String().Cat() << "(" << x.real() << "," << x.imag() << ")"; } template<> inline String AsString(const Complex& x) { return AsString((const std::complex&)x); } -template<> inline Stream& operator%(Stream& s, Complex& c) -{ - double r,i; - if(s.IsStoring()) { r = c.real(); i = c.imag(); } - s % r % i; - if(s.IsLoading()) { c = Complex(r,i); } - return s; -} - template<> inline dword ValueTypeNo(const Complex*) { return COMPLEX_V; } inline const Complex& Nvl(const Complex& a, const Complex& b) { return IsNull(a) ? b : a; } diff --git a/uppsrc/Core/Core.h b/uppsrc/Core/Core.h index cda6a4504..24f1fa6ed 100644 --- a/uppsrc/Core/Core.h +++ b/uppsrc/Core/Core.h @@ -250,7 +250,6 @@ NAMESPACE_UPP #include "Value.h" #include "ValueUtil.h" -#include "Gtypes.h" #include "Color.h" #include "Complex.h" @@ -267,8 +266,9 @@ NAMESPACE_UPP #include "Hash.h" #include "Parser.h" -#include "XML.h" #include "JSON.h" +#include "Gtypes.h" +#include "XML.h" #include "Lang.h" #include "i18n.h" #include "Topic.h" diff --git a/uppsrc/Core/Gtypes.h b/uppsrc/Core/Gtypes.h index 4733498d7..8956a0b5c 100644 --- a/uppsrc/Core/Gtypes.h +++ b/uppsrc/Core/Gtypes.h @@ -81,6 +81,7 @@ struct Size_ : Moveable< Size_ > { #endif void Serialize(Stream& s) { s % cx % cy; } + void Jsonize(JsonIO& jio) { jio("cx", cx)("cy", cy); } #ifdef PLATFORM_WIN32 operator SIZE*() { ASSERT(sizeof(*this) == sizeof(SIZE)); return (SIZE*)this; } @@ -188,6 +189,7 @@ struct Point_ : Moveable< Point_ > { #endif void Serialize(Stream& s) { s % x % y; } + void Jsonize(JsonIO& jio) { jio("x", x)("y", y); } #ifdef PLATFORM_WIN32 operator POINT*() { ASSERT(sizeof(*this) == sizeof(POINT)); return (POINT*)this; } @@ -367,6 +369,7 @@ struct Rect_ : Moveable< Rect_ > { /*explicit */Rect_(const Value& src) { *this = RichValue::Extract(src); } void Serialize(Stream& s) { s % left % top % right % bottom; } + void Jsonize(JsonIO& jio) { jio("left", left)("top", top)("right", right)("bottom", bottom); } #ifdef PLATFORM_WIN32 operator const RECT*() const { ASSERT(sizeof(*this) == sizeof(RECT)); return (RECT*)this; } diff --git a/uppsrc/Core/Value.cpp b/uppsrc/Core/Value.cpp index 59f1d1263..c8f549b7d 100644 --- a/uppsrc/Core/Value.cpp +++ b/uppsrc/Core/Value.cpp @@ -151,9 +151,9 @@ bool Value::GetOtherBool() const (bool)GetSmall(); } -VectorMap& Value::Typemap() +VectorMap& Value::Typemap() { - static VectorMap x; + static VectorMap x; return x; } @@ -168,14 +168,17 @@ SVO_FN(s_time, Time); struct SvoVoidFn { static bool IsNull(const void *p) { return true; } static void Serialize(void *p, Stream& s) {} + static void Xmlize(void *p, XmlIO& xio) {} + static void Jsonize(void *p, JsonIO& jio) {} static unsigned GetHashValue(const void *p) { return 0; } static bool IsEqual(const void *p1, const void *p2) { return true; } static bool IsPolyEqual(const void *p, const Value& v) { return false; } static String AsString(const void *p) { return String(); } }; -static Value::Sval s_void = { \ - SvoVoidFn::IsNull, SvoVoidFn::Serialize, SvoVoidFn::GetHashValue, SvoVoidFn::IsEqual, +static Value::Sval s_void = { + SvoVoidFn::IsNull, SvoVoidFn::Serialize,SvoVoidFn::Xmlize, SvoVoidFn::Jsonize, + SvoVoidFn::GetHashValue, SvoVoidFn::IsEqual, SvoVoidFn::IsPolyEqual, SvoVoidFn::AsString }; @@ -202,18 +205,14 @@ Value::Sval *Value::svo[256] = { NULL, //VALUEMAP_V = 12; }; -Value::Void *ValueArrayDataCreate(Stream& s) +Value::Void *ValueArrayDataCreate() { - ValueArray::Data *a = new ValueArray::Data; - a->Serialize(s); - return a; + return new ValueArray::Data; } -Value::Void *ValueMapDataCreate(Stream& s) +Value::Void *ValueMapDataCreate() { - ValueMap::Data *a = new ValueMap::Data; - a->Serialize(s); - return a; + return new ValueMap::Data; } static void sRegisterStd() @@ -230,10 +229,11 @@ INITBLOCK { sRegisterStd(); } -void Value::Serialize(Stream& s) { +void Value::Ize(int type, void *io) { sRegisterStd(); dword type; if(s.IsLoading()) { + if(t s / type; Free(); int st = type == VOID_V ? VOIDV : type == STRING_V ? STRING : type; @@ -245,10 +245,13 @@ void Value::Serialize(Stream& s) { svo[st]->Serialize(&data, s); } else { - typedef Void* (*vp)(Stream& s); + typedef Void* (*vp)(); vp *cr = Typemap().FindPtr(type); - if(cr) - InitRef((**cr)(s)); + if(cr) { + Void *p = (**cr)(); + p->Serialize(s); + InitRef(p); + } else { Free(); data.SetSpecial(3); @@ -276,7 +279,64 @@ void Value::Serialize(Stream& s) { } } -void Value::Register(dword w, Void* (*c)(Stream& s)) init_ { +void Value::Serialize(Stream& s) { + sRegisterStd(); + dword type; + if(s.IsLoading()) { + s / type; + Free(); + int st = type == VOID_V ? VOIDV : type == STRING_V ? STRING : type; + if(st == STRING) + s % data; + else + if(st < 255 && svo[st]) { + data.SetSpecial((byte)type); + svo[st]->Serialize(&data, s); + } + else { + typedef Void* (*vp)(); + vp *cr = Typemap().FindPtr(type); + if(cr) { + Void *p = (**cr)(); + p->Serialize(s); + InitRef(p); + } + else { + Free(); + data.SetSpecial(3); + if(type != VOID_V && type != ERROR_V) + s.LoadError(); + } + } + } + else { + type = GetType(); + s / type; + int st = data.GetSpecial(); + ASSERT_(!type || type == ERROR_V || type == UNKNOWN_V || st == STRING || + (IsRef() ? Typemap().Find(type) >= 0 : st < 255 && svo[st]), + AsString(type) + " is not registred for serialization"); + if(st == VOIDV) + return; + if(st == STRING) + s % data; + else + if(IsRef()) + ptr()->Serialize(s); + else + svo[st]->Serialize(&data, s); + } +} + +void Value::Xmlize(XmlIO& xio) +{ +} + +void Value::Jsonize(JsonIO& jio) +{ +} + +void Value::Register(dword w, Void* (*c)()) init_ { #ifdef flagCHECKINIT RLOG("Register valuetype " << w); #endif diff --git a/uppsrc/Core/Value.h b/uppsrc/Core/Value.h index 23ec998c1..3bfd3ef2b 100644 --- a/uppsrc/Core/Value.h +++ b/uppsrc/Core/Value.h @@ -3,6 +3,8 @@ class Id; class ValueArray; class ValueMap; +class XmlIO; +class JsonIO; const dword VOID_V = 0; @@ -40,11 +42,22 @@ template<> inline dword ValueTypeNo(const Date*) { return DATE_V; } template<> inline dword ValueTypeNo(const Time*) { return TIME_V; } template -class AssignValueTypeNo : public B { +class ValueType : public B { public: friend dword ValueTypeNo(const T*) { return type; } + + bool IsNullInstance() const { return false; } + void Serialize(Stream& s) { NEVER(); } + void Xmlize(XmlIO& xio) { NEVER(); } + void Jsonize(JsonIO& jio) { NEVER(); } + unsigned GetHashValue() const { return 0; } + bool operator==(const T&) const { NEVER(); return false; } + String ToString() const { return typeid(T).name(); } }; +template // Backward compatiblity +class AssignValueTypeNo : public ValueType {}; + template dword GetValueTypeNo() { return ValueTypeNo((T*)NULL); } @@ -76,6 +89,8 @@ public: struct Sval { bool (*IsNull)(const void *p); void (*Serialize)(void *p, Stream& s); + void (*Xmlize)(void *p, XmlIO& xio); + void (*Jsonize)(void *p, JsonIO& jio); unsigned (*GetHashValue)(const void *p); bool (*IsEqual)(const void *p1, const void *p2); bool (*IsPolyEqual)(const void *p, const Value& v); @@ -85,7 +100,7 @@ public: protected: enum { STRING = 0, REF = 255, VOIDV = 3 }; - static VectorMap& Typemap(); + static VectorMap& Typemap(); static Sval *svo[256]; String data; @@ -127,7 +142,7 @@ protected: String GetName() const; public: - static void Register(dword w, Void* (*c)(Stream& s)) init_; + static void Register(dword w, Void* (*c)()) init_; // Direct use deprecated template static void Register(); @@ -172,6 +187,8 @@ public: String ToString() const; void Serialize(Stream& s); + void Xmlize(XmlIO& xio); + void Jsonize(JsonIO& jio); unsigned GetHashValue() const; diff --git a/uppsrc/Core/Value.hpp b/uppsrc/Core/Value.hpp index 6148236fc..ec23a46e4 100644 --- a/uppsrc/Core/Value.hpp +++ b/uppsrc/Core/Value.hpp @@ -90,6 +90,8 @@ class RichValueRep : public RawValueRep { public: virtual bool IsNull() const { return UPP::IsNull(this->v); } virtual void Serialize(Stream& s) { s % this->v; } + virtual void Xmlize(XmlIO& xio) { Upp::Xmlize(xio, this->v); } + virtual void Jsonize(JsonIO& jio) { Upp::Jsonize(jio, this->v); } virtual unsigned GetHashValue() const { return UPP::ValueGetHashValue(this->v); } virtual bool IsEqual(const Value::Void *p) { ASSERT(dynamic_cast *>(p)); return static_cast *>(p)->Get() == this->v; } @@ -97,15 +99,17 @@ public: virtual String AsString() const { return UPP::AsString(this->v); } RichValueRep(const T& v) : RawValueRep(v) {} - RichValueRep(Stream& s) { Serialize(s); } + RichValueRep() {} - static Value::Void *Create(Stream& s) { return new RichValueRep(s); } + static Value::Void *Create() { return new RichValueRep; } }; template struct SvoFn { static bool IsNull(const void *p) { return UPP::IsNull(*(T *)p); } static void Serialize(void *p, Stream& s) { s % *(T*)p; } + static void Xmlize(void *p, XmlIO& xio) { Upp::Xmlize(xio, *(T*)p); } + static void Jsonize(void *p, JsonIO& jio) { Upp::Jsonize(jio, *(T*)p); } static unsigned GetHashValue(const void *p) { return UPP::ValueGetHashValue(*(T*)p); } static bool IsEqual(const void *p1, const void *p2) { return *(T*)p1 == *(T*)p2; } static bool IsPolyEqual(const void *p, const Value& v) { return UPP::IsPolyEqual(*(T*)p, v); } @@ -114,7 +118,8 @@ struct SvoFn { #define SVO_FN(id, T) \ static Value::Sval id = { \ - SvoFn::IsNull, SvoFn::Serialize, SvoFn::GetHashValue, SvoFn::IsEqual, \ + SvoFn::IsNull, SvoFn::Serialize, SvoFn::Xmlize, SvoFn::Jsonize, \ + SvoFn::GetHashValue, SvoFn::IsEqual, \ SvoFn::IsPolyEqual, SvoFn::AsString \ }; diff --git a/uppsrc/Core/ValueUtil.cpp b/uppsrc/Core/ValueUtil.cpp index 497291d55..bb65e6f52 100644 --- a/uppsrc/Core/ValueUtil.cpp +++ b/uppsrc/Core/ValueUtil.cpp @@ -1,9 +1,9 @@ #include "Core.h" -#ifdef SVO_VALUE - NAMESPACE_UPP +#ifdef SVO_VALUE + #define LTIMING(x) // RTIMING(x) struct Ref::ValueRef : public RefManager { @@ -470,6 +470,31 @@ bool FnValuePairOrder::operator()(const Value& keya, const Value& valuea, const return (*fn)(keya, valuea, keyb, valueb) < 0; } +#endif + +void Complex::Xmlize(XmlIO& xio) +{ + double r, i; + r = real(); i = imag(); + xio.Attr("real", r).Attr("image", i); + *this = C(r, i); +} + +void Complex::Jsonize(JsonIO& jio) +{ + double r, i; + r = real(); i = imag(); + jio("real", r)("image", i); + *this = C(r, i); +} + +void Complex::Serialize(Stream& s) +{ + double r, i; + r = real(); i = imag(); + s % r % i; + *this = C(r, i); +} + END_UPP_NAMESPACE -#endif diff --git a/uppsrc/Core/ValueUtil.h b/uppsrc/Core/ValueUtil.h index 670a6c547..4863a0f23 100644 --- a/uppsrc/Core/ValueUtil.h +++ b/uppsrc/Core/ValueUtil.h @@ -180,7 +180,7 @@ class ValueArray : AssignValueTypeNo& Clone(); void Init0(); - friend Value::Void *ValueArrayDataCreate(Stream& s); + friend Value::Void *ValueArrayDataCreate(); friend class Value; public: @@ -252,7 +252,7 @@ class ValueMap : AssignValueTypeNo >{ Data& Clone(); void Init0(); - friend Value::Void *ValueMapDataCreate(Stream& s); + friend Value::Void *ValueMapDataCreate(); friend class Value; public: diff --git a/uppsrc/Core/Xmlize.cpp b/uppsrc/Core/Xmlize.cpp index 2f52e3bd7..594108fab 100644 --- a/uppsrc/Core/Xmlize.cpp +++ b/uppsrc/Core/Xmlize.cpp @@ -238,6 +238,7 @@ void RegisterValueXmlize(dword type, void (*xmlize)(XmlIO& xml, Value& v), const ValueXmlizeName().Add(name); } +#ifndef SVO_VALUE REGISTER_VALUE_XMLIZE(String); REGISTER_VALUE_XMLIZE(WString); REGISTER_VALUE_XMLIZE(int); @@ -295,6 +296,7 @@ template<> void Xmlize(XmlIO& xml, Value& v) } } } +#endif template<> void Xmlize(XmlIO& xml, ValueArray& v) { diff --git a/uppsrc/Core/Xmlize.h b/uppsrc/Core/Xmlize.h index 9af75aaa8..e36cfea9e 100644 --- a/uppsrc/Core/Xmlize.h +++ b/uppsrc/Core/Xmlize.h @@ -142,7 +142,9 @@ template<> void Xmlize(XmlIO& xml, Rectf& r); template<> void Xmlize(XmlIO& xml, Color& c); +#ifndef SVO_VALUE template<> void Xmlize(XmlIO& xml, Value& v); +#endif template<> void Xmlize(XmlIO& xml, ValueArray& v); template<> void Xmlize(XmlIO& xml, ValueMap& v);