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

Last change on this file since 1279 was 1158, checked in by Maciej Komosinski, 3 years ago

Cosmetic/minor improvements

  • Property svn:eol-style set to native
File size: 10.3 KB
RevLine 
[286]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
[1130]2// Copyright (C) 1999-2021  Maciej Komosinski and Szymon Ulatowski.
[286]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/threads.h>
[1130]11#include <vector>
[109]12
13#define EXTVALUEUNION
[325]14template <int A, int B> struct CompileTimeMax { enum { val = A > B ? A : B }; };
[109]15#define EXTVALUEUNIONSIZE CompileTimeMax<sizeof(ExtObject),sizeof(SString)>::val
16
[482]17//#define DEBUG_EXTOBJECT(txt) printf("%p ExtObj::" txt "\n",this)
18#define DEBUG_EXTOBJECT(txt)
19
[109]20enum ExtPType
[325]21{
22        TUnknown = 0, TInt, TDouble, TString, TObj, TInvalid
23};
[109]24
25/**
26   destructable object
[325]27   */
[109]28class DestrBase
29{
30public:
[325]31        int refcount;
32        DestrBase() :refcount(0) {}
33        void incref() { refcount++; }
34        void decref() { refcount--; if (refcount == 0) delete this; }
35        virtual ~DestrBase() {}
[109]36};
37
[464]38enum SerializationFormat { NativeSerialization, JSONSerialization };
39
[109]40/**
41   object reference.
[325]42   */
[109]43class ExtObject
44{
[1158]45        int subtype;                    //< 0/1=Generic/DPC Object,  0/2=Standalone/Shared Param,  0/4=RW/Readonly
[325]46        void incref() const;
47        void decref() const;
48public:
49        union {
50                void* object;           //< generic object, will use param
51                DestrBase *dbobject;    //< object with refcounting, will be deleted if refcount goes to 0
52        };
53        union {
54                Param* param;           //< if object!=0
55                ParamInterface *paraminterface; //< if object==0
56        };
[109]57
[851]58        void copyFrom(const ExtObject& src) { subtype = src.subtype; object = src.object; param = src.param; }
[109]59
[851]60        void* operator new(size_t s, void* mem) { return mem; }
[109]61#ifdef _MSC_VER
[851]62        void operator delete(void* mem, void* t) {}
[109]63#endif
[851]64        void* operator new(size_t s) { return malloc(sizeof(ExtObject)); }
[325]65        void operator delete(void* mem) { free(mem); }
66        ///@param tmp_param can be used for temporary storage, the result ParamInterface* is only valid for as long as tmp_param is valid
[851]67        ParamInterface *getParamInterface(Param &tmp_param) const { if (subtype & 2) { tmp_param.setParamTab(param->getParamTab()); tmp_param.select(object); return &tmp_param; } return paraminterface; }
[325]68        const char* interfaceName() const { if (isEmpty()) return "Empty"; return (subtype & 2) ? param->getName() : paraminterface->getName(); }
69        bool matchesInterfaceName(ParamInterface* pi) const { return !strcmp(interfaceName(), pi->getName()); }
70        void* getTarget() const { return (subtype & 1) ? dbobject : object; }
71        void* getTarget(const char* classname, bool through_barrier = true, bool warn = true) const;
[522]72        bool callDelegate(const char* delegate, ExtValue *args, ExtValue *ret);
[325]73        void setEmpty() { decref(); subtype = 0; param = NULL; object = NULL; }
74        int isEmpty() const { return !param; }
[1158]75        bool isReadonly() const {return (subtype&4)!=0;}
76        void setReadonly(bool ro) {subtype=(subtype&~4)|(ro?4:0);}
[482]77        static const ExtObject& empty() { static const ExtObject e((ParamInterface*)NULL); return e; }
[851]78        ExtObject(const ExtObject& src) { DEBUG_EXTOBJECT("(const&)"); src.incref(); copyFrom(src); }
[325]79        void operator=(const ExtObject& src) { src.incref(); decref(); copyFrom(src); }
80        bool makeUnique();//< @return false if nothing has changed
[109]81
[325]82        bool operator==(const ExtObject& src) const;
[109]83
[325]84        SString toString() const;
[464]85        SString serialize_inner(SerializationFormat format) const;
86        SString serialize(SerializationFormat format) const;
[109]87
[522]88        ExtObject(Param *p, void *o) :subtype(2), object(o), param(p) { DEBUG_EXTOBJECT("(Param,void)"); }
[482]89        ExtObject(ParamInterface *p = 0) :subtype(0), object(0), paraminterface(p) { DEBUG_EXTOBJECT("(ParamInterface)"); }
[851]90        ExtObject(Param *p, DestrBase *o) :subtype(1 + 2), dbobject(o), param(p) { DEBUG_EXTOBJECT("(Param,DestrBase)"); incref(); }
91        ExtObject(ParamInterface *p, DestrBase *o) :subtype(1), dbobject(o), paraminterface(p) { DEBUG_EXTOBJECT("(ParamInterface,DestrBase)"); incref(); }
[109]92
[851]93        ~ExtObject() { DEBUG_EXTOBJECT("~"); decref(); }
[109]94
[325]95        class Serialization
96        {
97                std::vector<ExtObject> refs;
98                int level;
99        public:
100                Serialization() :level(0) {}
101                void begin();
102                void end();
103                int add(const ExtObject& o);
104                void replace(const ExtObject& o, const ExtObject& other);
105                void remove(const ExtObject& o);
106                const ExtObject* get(int ref);
107        };
[109]108
109};
110
[371]111extern THREAD_LOCAL_DECL(ExtObject::Serialization, ExtObject_serialization);
112
[109]113class ExtValue
114{
115public:
[325]116        ExtPType type;
[109]117#ifdef EXTVALUEUNION
[384]118        intptr_t data[(EXTVALUEUNIONSIZE + sizeof(intptr_t) - 1) / sizeof(intptr_t)];
[325]119        paInt& idata() const { return (paInt&)data[0]; };
120        double& ddata() const { return *(double*)data; };
121        ExtObject& odata() const { return *(ExtObject*)data; };
122        SString& sdata() const { return *(SString*)data; };
[109]123#else
[325]124        union {
125                paInt i;
126                double d;
127                SString *s;
128                ExtObject *o;
129        };
[793]130        paInt& idata() const { return (paInt&)i; };
131        double& ddata() const { return (double&)d; };
132        ExtObject& odata() const { return *o; };
133        SString& sdata() const { return *s; };
[109]134#endif
135
[851]136        void* operator new(size_t s, void* mem) { return mem; }
137        void* operator new(size_t s) { return ::operator new(s); }
[109]138
[851]139        ExtValue() :type(TUnknown) {}
[325]140        ~ExtValue() { setEmpty(); }
141        ExtValue(paInt v) { seti(v); }
142        ExtValue(double v) { setd(v); }
143        ExtValue(const SString &v) { sets(v); }
144        ExtValue(const ExtObject &srco) { seto(srco); }
145        static ExtValue invalid() { ExtValue v; v.setInvalid(); return v; }
146        static const ExtValue& empty() { static const ExtValue v; return v; }
[333]147        static const ExtValue& zero() { static const ExtValue v(0); return v; }
148
149        enum CompareResult
150        {
[337]151                ResultLower = -1, ResultEqual = 0, ResultHigher = 1,
152                ResultEqualUnordered,
153                ResultUnequal_RelaxedEqual,
154                ResultUnequal_RelaxedUnequal,
155                ResultMismatch_RelaxedUnequal,
156                ResultMismatch
[333]157        };
158        // performs all script value comparisons.
159        // relaxed comparison (internal use only, not available in scripts) works like regular == with additional null~=0, notnull!~0
160        // and is used for pseudo-boolean conversion allowing for expressions like "if (null) ..."
161        CompareResult compare(const ExtValue& src) const;
162
[337]163        enum CmpOperator { CmpFIRST, CmpEQ = CmpFIRST, CmpNE, CmpGE, CmpLE, CmpGT, CmpLT,/*relaxed (not)equal*/CmpREQ, CmpRNE };
[333]164        static const char* cmp_op_names[];
[337]165        struct CmpContext { const ExtValue *v1, *v2; };
[333]166        // interpret compare() result, optional context controls error messages
167        // @return 0=false, 1=true, -1=undefined (null in script)
[337]168        static int interpretCompare(CmpOperator op, CompareResult result, CmpContext *context = NULL);
[333]169
[337]170        void divInt(paInt a);
171        void divDouble(double a);
[636]172        void modInt(paInt a);
173        void modDouble(double a);
[337]174
[325]175        int operator==(const ExtValue& src) const;
176        void operator+=(const ExtValue& src);
177        void operator-=(const ExtValue& src);
178        void operator*=(const ExtValue& src);
179        void operator/=(const ExtValue& src);
180        void operator%=(const ExtValue& src);
181        void operator=(const ExtValue& src)
182        {
183                setr(src);
184        }
185        ExtValue(const ExtValue& src)
186                :type(TUnknown) {
187                set(src);
188        }
189        void setEmpty();
190        void setInvalid() { setEmpty(); type = TInvalid; }
[490]191        void setError(const SString& msg);
[1158]192        bool isReadonly() const {return (type==TObj) && odata().isReadonly();}
193        void setReadonly(bool ro) {if (type==TObj) odata().setReadonly(ro);}
[325]194        bool makeUnique() { return (type == TObj) && odata().makeUnique(); } //< @return false if nothing has changed
[333]195        ExtPType getType() const { return type; }
[325]196        void *getObjectTarget(const char* classname, bool warn = true) const;
197        void setInt(paInt v) { if (type != TInt) setri(v); else idata() = v; }
198        void setDouble(double v) { if (type != TDouble) setrd(v); else ddata() = v; }
199        void setString(const SString &v) { if (type != TString) setrs(v); else sdata() = v; }
200        void setObject(const ExtObject &src) { if (type != TObj) setro(src); else odata() = src; }
[326]201        static bool parseInt(const char* s, paInt &result, bool strict, bool error);
202        static bool parseDouble(const char* s, double &result, bool error);
[325]203        static paInt getInt(const char* s, bool strict = false);//< @param strict=true will fail on floating point
204        static double getDouble(const char* s);
205        paInt getInt() const;
206        double getDouble() const;
207        SString getString() const;
208        const SString* getStringPtr() const;//< @return pointer to the internal sstring object or NULL if the current type is not string
[464]209        SString serialize(SerializationFormat format) const;
[325]210        ExtObject getObject() const;
211        bool isNull() const { return (type == TUnknown) || ((type == TObj) && odata().isEmpty()); }
212        SString typeDescription() const;//< @return human readable type name (used in error messages)
[337]213        SString typeAndValue() const;//< @return type and value (used in error messages)
[325]214        const char* parseNumber(const char* in, ExtPType strict_type = TUnknown);
215        const char* deserialize(const char* in);//< @return first character after the succesfully parsed string or NULL if failed
216        const char* deserialize_inner(const char* in);
217        static ParamInterface *findDeserializableClass(const char* name);
218        static PtrListTempl<ParamInterface*> &getDeserializableClasses();
219        template<typename T> class AddDeserializable
220        {
221        public:
222                AddDeserializable() { ExtValue::getDeserializableClasses() += &T::getStaticParam(); }
223        };
[222]224
[639]225        static SString formatTime(char fmt, double value);
226        static SString format(const SString& fmt, const ExtValue **values, int count);
[109]227
[325]228        ExtValue getExtType();
[109]229
[325]230private: // setrx - release and set, setx - assume released
[851]231        void setr(const ExtValue& src) { setEmpty(); set(src); }
[325]232        void set(const ExtValue& src);
233        void setri(paInt v) { setEmpty(); seti(v); }
234        void setrd(double v) { setEmpty(); setd(v); }
235        void seti(paInt v) { type = TInt; idata() = v; }
236        void setd(double v) { type = TDouble; ddata() = v; }
[109]237#ifdef EXTVALUEUNION
[325]238        void setrs(const SString &v) { setEmpty(); sets(v); }
239        void setro(const ExtObject &src) { setEmpty(); seto(src); }
240        void sets(const SString &v) { type = TString; new(data)SString(v); }
241        void seto(const ExtObject &src) { type = TObj; new(data)ExtObject(src); }
[109]242#else
[793]243        void setrs(const SString &v) { setEmpty(); sets(v); }
244        void setro(const ExtObject &src) { setEmpty(); seto(src); }
245        void sets(const SString &v) { type = TString; s = new SString(v); }
246        void seto(const ExtObject &src) { type = TObj; o = new ExtObject(src); }
[109]247#endif
248
249};
250
[222]251#define REGISTER_DESERIALIZABLE(name) ExtValue::AddDeserializable<name> deserializable_autoinit_ ## name;
[109]252
[522]253class ErrorObject : public DestrBase
[490]254{
[522]255public:
256        SString message;
257        static Param& getParam();
258        static Param& getStaticParam() { return getParam(); }
259        static ExtObject makeDynamicObject(ErrorObject* e);
260        static const SString TO_STRING_PREFIX;
[490]261#define STATRICKCLASS ErrorObject
[522]262        PARAMGETDEF(toString);
263        PARAMPROCDEF(p_newfromstring);
[490]264#undef STATRICKCLASS
265};
266
[109]267#endif
Note: See TracBrowser for help on using the repository browser.