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