1 | // This file is a part of the Framsticks GDK. |
---|
2 | // Copyright (C) 1999-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
3 | // Refer to http://www.framsticks.com/ for further information. |
---|
4 | |
---|
5 | #ifndef _EXTVALUE_H_ |
---|
6 | #define _EXTVALUE_H_ |
---|
7 | |
---|
8 | #include "sstring.h" |
---|
9 | #include <frams/param/param.h> |
---|
10 | #include <common/nonstd_stl.h> |
---|
11 | #include <common/threads.h> |
---|
12 | |
---|
13 | #define EXTVALUEUNION |
---|
14 | template <int A,int B> struct CompileTimeMax {enum {val=A>B?A:B}; }; |
---|
15 | #define EXTVALUEUNIONSIZE CompileTimeMax<sizeof(ExtObject),sizeof(SString)>::val |
---|
16 | |
---|
17 | enum ExtPType |
---|
18 | {TUnknown=0,TInt,TDouble,TString,TObj,TInvalid}; |
---|
19 | |
---|
20 | /** |
---|
21 | destructable object |
---|
22 | */ |
---|
23 | class DestrBase |
---|
24 | { |
---|
25 | public: |
---|
26 | int refcount; |
---|
27 | DestrBase():refcount(0) {} |
---|
28 | void incref() {refcount++;} |
---|
29 | void decref() {refcount--; if (refcount==0) delete this;} |
---|
30 | virtual ~DestrBase() {} |
---|
31 | }; |
---|
32 | |
---|
33 | /** |
---|
34 | object reference. |
---|
35 | */ |
---|
36 | class ExtObject |
---|
37 | { |
---|
38 | int subtype; //< 0/1=Generic/DPC Object, 0/2=Standalone/Shared Param |
---|
39 | void incref() const; |
---|
40 | void decref() const; |
---|
41 | public: |
---|
42 | union { void* object; //< generic object, will use param |
---|
43 | DestrBase *dbobject;}; //< object with refcounting, will be deleted if refcount goes to 0 |
---|
44 | union { Param* param; //< if object!=0 |
---|
45 | ParamInterface *paraminterface;}; //< if object==0 |
---|
46 | |
---|
47 | void copyFrom(const ExtObject& src) {subtype=src.subtype;object=src.object;param=src.param;} |
---|
48 | |
---|
49 | void* operator new(size_t s, void* mem) {return mem;} |
---|
50 | #ifdef _MSC_VER |
---|
51 | void operator delete(void* mem,void* t) {} |
---|
52 | #endif |
---|
53 | void* operator new(size_t s) {return malloc(sizeof(ExtObject));} |
---|
54 | void operator delete(void* mem) {free(mem);} |
---|
55 | ///@param tmp_param can be used for temporary storage, the result ParamInterface* is only valid for as long as tmp_param is valid |
---|
56 | ParamInterface *getParamInterface(Param &tmp_param) const {if(subtype&2){tmp_param.setParamTab(param->getParamTab());tmp_param.select(object);return &tmp_param;} return paraminterface;} |
---|
57 | const char* interfaceName() const {if (isEmpty()) return "Empty"; return (subtype&2)?param->getName():paraminterface->getName();} |
---|
58 | bool matchesInterfaceName(ParamInterface* pi) const {return !strcmp(interfaceName(),pi->getName());} |
---|
59 | void* getTarget() const {return (subtype&1)?dbobject:object;} |
---|
60 | void* getTarget(const char* classname, bool through_barrier=true, bool warn=true) const; |
---|
61 | void setEmpty() {decref();subtype=0;param=NULL;object=NULL;} |
---|
62 | int isEmpty() const {return !param;} |
---|
63 | ExtObject(const ExtObject& src) {src.incref();copyFrom(src);} |
---|
64 | void operator=(const ExtObject& src) {src.incref();decref();copyFrom(src);} |
---|
65 | bool makeUnique();//< @return false if nothing has changed |
---|
66 | |
---|
67 | bool operator==(const ExtObject& src) const; |
---|
68 | |
---|
69 | SString toString() const; |
---|
70 | SString serialize_inner() const; |
---|
71 | SString serialize() const; |
---|
72 | |
---|
73 | ExtObject(Param *p,void *o):subtype(2),object(o),param(p){} |
---|
74 | ExtObject(ParamInterface *p=0):subtype(0),object(0),paraminterface(p){} |
---|
75 | ExtObject(Param *p,DestrBase *o):subtype(1+2),dbobject(o),param(p){incref();} |
---|
76 | ExtObject(ParamInterface *p,DestrBase *o):subtype(1),dbobject(o),paraminterface(p){incref();} |
---|
77 | |
---|
78 | ~ExtObject(){decref();} |
---|
79 | |
---|
80 | class Serialization |
---|
81 | { |
---|
82 | std::vector<ExtObject> refs; |
---|
83 | int level; |
---|
84 | public: |
---|
85 | Serialization():level(0) {} |
---|
86 | void begin(); |
---|
87 | void end(); |
---|
88 | int add(const ExtObject& o); |
---|
89 | void replace(const ExtObject& o,const ExtObject& other); |
---|
90 | void remove(const ExtObject& o); |
---|
91 | const ExtObject* get(int ref); |
---|
92 | }; |
---|
93 | |
---|
94 | static THREAD_LOCAL_DECL(Serialization,serialization); |
---|
95 | }; |
---|
96 | |
---|
97 | class ExtValue |
---|
98 | { |
---|
99 | public: |
---|
100 | ExtPType type; |
---|
101 | #ifdef EXTVALUEUNION |
---|
102 | long data[(EXTVALUEUNIONSIZE+sizeof(long)-1)/sizeof(long)]; |
---|
103 | paInt& idata() const {return (paInt&)data[0];}; |
---|
104 | double& ddata() const {return *(double*)data;}; |
---|
105 | ExtObject& odata() const {return *(ExtObject*)data;}; |
---|
106 | SString& sdata() const {return *(SString*)data;}; |
---|
107 | #else |
---|
108 | union { |
---|
109 | paInt i; |
---|
110 | double d; |
---|
111 | SString *s; |
---|
112 | ExtObject *o; |
---|
113 | }; |
---|
114 | paInt& idata() const {return (paInt&)i;}; |
---|
115 | double& ddata() const {return (double&)d;}; |
---|
116 | ExtObject& odata() const {return *o;}; |
---|
117 | SString& sdata() const {return *s;}; |
---|
118 | #endif |
---|
119 | |
---|
120 | void* operator new(size_t s, void* mem) {return mem;} |
---|
121 | void* operator new(size_t s) {return ::operator new(s);} |
---|
122 | |
---|
123 | ExtValue():type(TUnknown){} |
---|
124 | ~ExtValue() {setEmpty();} |
---|
125 | ExtValue(paInt v) {seti(v);} |
---|
126 | ExtValue(double v) {setd(v);} |
---|
127 | ExtValue(const SString &v) {sets(v);} |
---|
128 | ExtValue(const ExtObject &srco) {seto(srco);} |
---|
129 | static ExtValue invalid() {ExtValue v; v.setInvalid(); return v;} |
---|
130 | int compare(const ExtValue& src) const; |
---|
131 | int operator==(const ExtValue& src) const; |
---|
132 | void operator+=(const ExtValue& src); |
---|
133 | void operator-=(const ExtValue& src); |
---|
134 | void operator*=(const ExtValue& src); |
---|
135 | void operator/=(const ExtValue& src); |
---|
136 | void operator%=(const ExtValue& src); |
---|
137 | void operator=(const ExtValue& src) |
---|
138 | {setr(src);} |
---|
139 | ExtValue(const ExtValue& src) |
---|
140 | :type(TUnknown) {set(src);} |
---|
141 | void setEmpty(); |
---|
142 | void setInvalid() {setEmpty();type=TInvalid;} |
---|
143 | bool makeUnique() {return (type==TObj) && odata().makeUnique();} //< @return false if nothing has changed |
---|
144 | ExtPType getType() {return type;} |
---|
145 | void *getObjectTarget(const char* classname,bool warn=true) const; |
---|
146 | void setInt(paInt v) {if (type!=TInt) setri(v); else idata()=v;} |
---|
147 | void setDouble(double v) {if (type!=TDouble) setrd(v); else ddata()=v;} |
---|
148 | void setString(const SString &v) {if (type!=TString) setrs(v); else sdata()=v;} |
---|
149 | void setObject(const ExtObject &src) {if (type!=TObj) setro(src); else odata()=src;} |
---|
150 | static paInt getInt(const char* s); |
---|
151 | static double getDouble(const char* s); |
---|
152 | paInt getInt() const; |
---|
153 | double getDouble() const; |
---|
154 | SString getString() const; |
---|
155 | const SString* getStringPtr() const;//< @return pointer to the internal sstring object or NULL if the current type is not string |
---|
156 | SString serialize() const; |
---|
157 | ExtObject getObject() const; |
---|
158 | bool isNull() const {return (type==TUnknown)||((type==TObj)&&odata().isEmpty());} |
---|
159 | SString typeDescription() const;//< @return human readable type name (used in error messages) |
---|
160 | const char* parseNumber(const char* in); |
---|
161 | const char* deserialize(const char* in);//< @return first character after the succesfully parsed string or NULL if failed |
---|
162 | const char* deserialize_inner(const char* in); |
---|
163 | static ParamInterface *findDeserializableClass(const char* name); |
---|
164 | static PtrListTempl<ParamInterface*> &getDeserializableClasses(); |
---|
165 | template<typename T> class AddDeserializable |
---|
166 | { |
---|
167 | public: |
---|
168 | AddDeserializable() {ExtValue::getDeserializableClasses()+=&T::getStaticParam();} |
---|
169 | }; |
---|
170 | |
---|
171 | static SString format(SString& fmt,const ExtValue **values,int count); |
---|
172 | |
---|
173 | ExtValue getExtType(); |
---|
174 | |
---|
175 | private: // setrx - release and set, setx - assume released |
---|
176 | void setr(const ExtValue& src){setEmpty();set(src);} |
---|
177 | void set(const ExtValue& src); |
---|
178 | void setri(paInt v) {setEmpty();seti(v);} |
---|
179 | void setrd(double v) {setEmpty();setd(v);} |
---|
180 | void seti(paInt v) {type=TInt;idata()=v;} |
---|
181 | void setd(double v) {type=TDouble;ddata()=v;} |
---|
182 | #ifdef EXTVALUEUNION |
---|
183 | void setrs(const SString &v) {setEmpty();sets(v);} |
---|
184 | void setro(const ExtObject &src) {setEmpty();seto(src);} |
---|
185 | void sets(const SString &v) {type=TString;new(data) SString(v);} |
---|
186 | void seto(const ExtObject &src) {type=TObj;new(data) ExtObject(src);} |
---|
187 | #else |
---|
188 | void setrs(const SString &v) {setEmpty();sets(v);} |
---|
189 | void setro(const ExtObject &src) {setEmpty();seto(src);} |
---|
190 | void sets(const SString &v) {type=TString;s=new SString(v);} |
---|
191 | void seto(const ExtObject &src) {type=TObj;o=new ExtObject(src);} |
---|
192 | #endif |
---|
193 | |
---|
194 | }; |
---|
195 | |
---|
196 | #define REGISTER_DESERIALIZABLE(name) ExtValue::AddDeserializable<name> deserializable_autoinit_ ## name; |
---|
197 | |
---|
198 | #endif |
---|