source: cpp/frams/util/extvalue.h @ 375

Last change on this file since 375 was 371, checked in by Maciej Komosinski, 9 years ago

THREAD_LOCAL uses ThreadLocal_ prefix for variables. The static object inside the single-threaded implementation is no longer associated with a class.

  • Property svn:eol-style set to native
File size: 9.0 KB
Line 
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.
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
14template <int A, int B> struct CompileTimeMax { enum { val = A > B ? A : B }; };
15#define EXTVALUEUNIONSIZE CompileTimeMax<sizeof(ExtObject),sizeof(SString)>::val
16
17enum ExtPType
18{
19        TUnknown = 0, TInt, TDouble, TString, TObj, TInvalid
20};
21
22/**
23   destructable object
24   */
25class DestrBase
26{
27public:
28        int refcount;
29        DestrBase() :refcount(0) {}
30        void incref() { refcount++; }
31        void decref() { refcount--; if (refcount == 0) delete this; }
32        virtual ~DestrBase() {}
33};
34
35/**
36   object reference.
37   */
38class ExtObject
39{
40        int subtype;                    //< 0/1=Generic/DPC Object,  0/2=Standalone/Shared Param
41        void incref() const;
42        void decref() const;
43public:
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        };
52
53        void copyFrom(const ExtObject& src)  { subtype = src.subtype; object = src.object; param = src.param; }
54
55        void* operator new(size_t s, void* mem){ return mem; }
56#ifdef _MSC_VER
57                void operator delete(void* mem, void* t) {}
58#endif
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
73
74        bool operator==(const ExtObject& src) const;
75
76        SString toString() const;
77        SString serialize_inner() const;
78        SString serialize() const;
79
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(); }
84
85        ~ExtObject(){ decref(); }
86
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        };
100
101};
102
103extern THREAD_LOCAL_DECL(ExtObject::Serialization, ExtObject_serialization);
104
105class ExtValue
106{
107public:
108        ExtPType type;
109#ifdef EXTVALUEUNION
110        long data[(EXTVALUEUNIONSIZE + sizeof(long) - 1) / sizeof(long)];
111        paInt& idata() const { return (paInt&)data[0]; };
112        double& ddata() const { return *(double*)data; };
113        ExtObject& odata() const { return *(ExtObject*)data; };
114        SString& sdata() const { return *(SString*)data; };
115#else
116        union {
117                paInt i;
118                double d;
119                SString *s;
120                ExtObject *o;
121        };
122        paInt& idata() const {return (paInt&)i;};
123        double& ddata() const {return (double&)d;};
124        ExtObject& odata() const {return *o;};
125        SString& sdata() const {return *s;};
126#endif
127
128        void* operator new(size_t s, void* mem){ return mem; }
129        void* operator new(size_t s){ return ::operator new(s); }
130
131                ExtValue() :type(TUnknown){}
132        ~ExtValue() { setEmpty(); }
133        ExtValue(paInt v) { seti(v); }
134        ExtValue(double v) { setd(v); }
135        ExtValue(const SString &v) { sets(v); }
136        ExtValue(const ExtObject &srco) { seto(srco); }
137        static ExtValue invalid() { ExtValue v; v.setInvalid(); return v; }
138        static const ExtValue& empty() { static const ExtValue v; return v; }
139        static const ExtValue& zero() { static const ExtValue v(0); return v; }
140
141        enum CompareResult
142        {
143                ResultLower = -1, ResultEqual = 0, ResultHigher = 1,
144                ResultEqualUnordered,
145                ResultUnequal_RelaxedEqual,
146                ResultUnequal_RelaxedUnequal,
147                ResultMismatch_RelaxedUnequal,
148                ResultMismatch
149        };
150        // performs all script value comparisons.
151        // relaxed comparison (internal use only, not available in scripts) works like regular == with additional null~=0, notnull!~0
152        // and is used for pseudo-boolean conversion allowing for expressions like "if (null) ..."
153        CompareResult compare(const ExtValue& src) const;
154
155        enum CmpOperator { CmpFIRST, CmpEQ = CmpFIRST, CmpNE, CmpGE, CmpLE, CmpGT, CmpLT,/*relaxed (not)equal*/CmpREQ, CmpRNE };
156        static const char* cmp_op_names[];
157        struct CmpContext { const ExtValue *v1, *v2; };
158        // interpret compare() result, optional context controls error messages
159        // @return 0=false, 1=true, -1=undefined (null in script)
160        static int interpretCompare(CmpOperator op, CompareResult result, CmpContext *context = NULL);
161
162        void divInt(paInt a);
163        void divDouble(double a);
164
165        int operator==(const ExtValue& src) const;
166        void operator+=(const ExtValue& src);
167        void operator-=(const ExtValue& src);
168        void operator*=(const ExtValue& src);
169        void operator/=(const ExtValue& src);
170        void operator%=(const ExtValue& src);
171        void operator=(const ExtValue& src)
172        {
173                setr(src);
174        }
175        ExtValue(const ExtValue& src)
176                :type(TUnknown) {
177                set(src);
178        }
179        void setEmpty();
180        void setInvalid() { setEmpty(); type = TInvalid; }
181        bool makeUnique() { return (type == TObj) && odata().makeUnique(); } //< @return false if nothing has changed
182        ExtPType getType() const { return type; }
183        void *getObjectTarget(const char* classname, bool warn = true) const;
184        void setInt(paInt v) { if (type != TInt) setri(v); else idata() = v; }
185        void setDouble(double v) { if (type != TDouble) setrd(v); else ddata() = v; }
186        void setString(const SString &v) { if (type != TString) setrs(v); else sdata() = v; }
187        void setObject(const ExtObject &src) { if (type != TObj) setro(src); else odata() = src; }
188        static bool parseInt(const char* s, paInt &result, bool strict, bool error);
189        static bool parseDouble(const char* s, double &result, bool error);
190        static paInt getInt(const char* s, bool strict = false);//< @param strict=true will fail on floating point
191        static double getDouble(const char* s);
192        paInt getInt() const;
193        double getDouble() const;
194        SString getString() const;
195        const SString* getStringPtr() const;//< @return pointer to the internal sstring object or NULL if the current type is not string
196        SString serialize() const;
197        ExtObject getObject() const;
198        bool isNull() const { return (type == TUnknown) || ((type == TObj) && odata().isEmpty()); }
199        SString typeDescription() const;//< @return human readable type name (used in error messages)
200        SString typeAndValue() const;//< @return type and value (used in error messages)
201        const char* parseNumber(const char* in, ExtPType strict_type = TUnknown);
202        const char* deserialize(const char* in);//< @return first character after the succesfully parsed string or NULL if failed
203        const char* deserialize_inner(const char* in);
204        static ParamInterface *findDeserializableClass(const char* name);
205        static PtrListTempl<ParamInterface*> &getDeserializableClasses();
206        template<typename T> class AddDeserializable
207        {
208        public:
209                AddDeserializable() { ExtValue::getDeserializableClasses() += &T::getStaticParam(); }
210        };
211
212        static SString format(SString& fmt, const ExtValue **values, int count);
213
214        ExtValue getExtType();
215
216private: // setrx - release and set, setx - assume released
217        void setr(const ExtValue& src){ setEmpty(); set(src); }
218        void set(const ExtValue& src);
219        void setri(paInt v) { setEmpty(); seti(v); }
220        void setrd(double v) { setEmpty(); setd(v); }
221        void seti(paInt v) { type = TInt; idata() = v; }
222        void setd(double v) { type = TDouble; ddata() = v; }
223#ifdef EXTVALUEUNION
224        void setrs(const SString &v) { setEmpty(); sets(v); }
225        void setro(const ExtObject &src) { setEmpty(); seto(src); }
226        void sets(const SString &v) { type = TString; new(data)SString(v); }
227        void seto(const ExtObject &src) { type = TObj; new(data)ExtObject(src); }
228#else
229        void setrs(const SString &v) {setEmpty();sets(v);}
230        void setro(const ExtObject &src) {setEmpty();seto(src);}
231        void sets(const SString &v) {type=TString;s=new SString(v);}
232        void seto(const ExtObject &src) {type=TObj;o=new ExtObject(src);}
233#endif
234
235};
236
237#define REGISTER_DESERIALIZABLE(name) ExtValue::AddDeserializable<name> deserializable_autoinit_ ## name;
238
239#endif
Note: See TracBrowser for help on using the repository browser.