.developing SvoValue

git-svn-id: svn://ultimatepp.org/upp/trunk@4647 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2012-03-03 10:17:50 +00:00
parent 66bbc05738
commit 50e6553d39
11 changed files with 149 additions and 40 deletions

View file

@ -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<Color, COLOR_V, Moveable<Color> > {
class Color : public AssignValueTypeNo<Color, COLOR_V, Moveable<Color> > {
protected:
dword color;

View file

@ -15,6 +15,10 @@ struct Complex : std::complex<double>
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<double>& x) { return String().Cat() << "(" << x.real() << "," << x.imag() << ")"; }
template<> inline String AsString(const Complex& x) { return AsString((const std::complex<double>&)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; }

View file

@ -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"

View file

@ -81,6 +81,7 @@ struct Size_ : Moveable< Size_<T> > {
#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_<T> > {
#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_<T> > {
/*explicit */Rect_(const Value& src) { *this = RichValue<Rect_>::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; }

View file

@ -151,9 +151,9 @@ bool Value::GetOtherBool() const
(bool)GetSmall<int64>();
}
VectorMap<dword, Value::Void *(*)(Stream& s)>& Value::Typemap()
VectorMap<dword, Value::Void *(*)()>& Value::Typemap()
{
static VectorMap<dword, Value::Void *(*)(Stream& s)> x;
static VectorMap<dword, Value::Void *(*)()> 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

View file

@ -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 T, dword type, class B = EmptyClass>
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 <class T, dword type, class B = EmptyClass> // Backward compatiblity
class AssignValueTypeNo : public ValueType<T, type, B> {};
template <class T>
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<dword, Void* (*)(Stream& s) >& Typemap();
static VectorMap<dword, Void* (*)()>& 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 <class T>
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;

View file

@ -90,6 +90,8 @@ class RichValueRep : public RawValueRep<T> {
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<const RawValueRep<T> *>(p));
return static_cast<const RawValueRep<T> *>(p)->Get() == this->v; }
@ -97,15 +99,17 @@ public:
virtual String AsString() const { return UPP::AsString(this->v); }
RichValueRep(const T& v) : RawValueRep<T>(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 <class T>
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<T>::IsNull, SvoFn<T>::Serialize, SvoFn<T>::GetHashValue, SvoFn<T>::IsEqual, \
SvoFn<T>::IsNull, SvoFn<T>::Serialize, SvoFn<T>::Xmlize, SvoFn<T>::Jsonize, \
SvoFn<T>::GetHashValue, SvoFn<T>::IsEqual, \
SvoFn<T>::IsPolyEqual, SvoFn<T>::AsString \
};

View file

@ -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

View file

@ -180,7 +180,7 @@ class ValueArray : AssignValueTypeNo<ValueArray, VALUEARRAY_V, Moveable<ValueArr
Vector<Value>& Clone();
void Init0();
friend Value::Void *ValueArrayDataCreate(Stream& s);
friend Value::Void *ValueArrayDataCreate();
friend class Value;
public:
@ -252,7 +252,7 @@ class ValueMap : AssignValueTypeNo<ValueMap, VALUEMAP_V, Moveable<ValueMap> >{
Data& Clone();
void Init0();
friend Value::Void *ValueMapDataCreate(Stream& s);
friend Value::Void *ValueMapDataCreate();
friend class Value;
public:

View file

@ -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)
{

View file

@ -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);