[5] | 1 | // This file is a part of Framsticks GDK library. |
---|
| 2 | // Copyright (C) 2002-2006 Szymon Ulatowski. See LICENSE.txt for details. |
---|
| 3 | // Refer to http://www.frams.alife.pl/ for further information. |
---|
| 4 | |
---|
| 5 | #ifndef _EXTVALUE_H_ |
---|
| 6 | #define _EXTVALUE_H_ |
---|
| 7 | |
---|
| 8 | #include "sstring.h" |
---|
| 9 | #include "param.h" |
---|
| 10 | |
---|
| 11 | #define EXTVALUEUNION |
---|
| 12 | #define EXTVALUEUNIONSIZE sizeof(SString) |
---|
| 13 | |
---|
| 14 | enum ExtPType |
---|
| 15 | {TUnknown=0,TInt,TDouble,TString,TObj}; |
---|
| 16 | |
---|
| 17 | /** |
---|
| 18 | destructable object |
---|
| 19 | */ |
---|
| 20 | class DestrBase |
---|
| 21 | { |
---|
| 22 | public: |
---|
| 23 | int refcount; |
---|
| 24 | DestrBase():refcount(0) {} |
---|
| 25 | virtual ~DestrBase() {} |
---|
| 26 | }; |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | object reference. |
---|
| 30 | */ |
---|
| 31 | class ExtObject |
---|
| 32 | { |
---|
| 33 | int subtype; //< 0/1=Generic/DPC Object, 0/2=Standalone/Shared Param |
---|
| 34 | void incref() const {if (subtype&1) dbobject->refcount++;} |
---|
| 35 | void decref() const {if (subtype&1) if (!--dbobject->refcount) delete dbobject;} |
---|
| 36 | public: |
---|
| 37 | union { void* object; //< generic object, will use param |
---|
| 38 | DestrBase *dbobject;}; //< object with refcounting, will be deleted if refcount goes to 0 |
---|
| 39 | union { Param* param; //< if object!=0 |
---|
| 40 | ParamInterface *paraminterface;}; //< if object==0 |
---|
| 41 | |
---|
| 42 | void copyFrom(const ExtObject& src) {subtype=src.subtype;object=src.object;param=src.param;} |
---|
| 43 | |
---|
| 44 | void* operator new(size_t s, void* mem) {return mem;} |
---|
| 45 | #ifdef _MSC_VER |
---|
| 46 | void operator delete(void* mem,void* t) {} |
---|
| 47 | #endif |
---|
| 48 | void* operator new(size_t s) {return malloc(sizeof(ExtObject));} |
---|
| 49 | void operator delete(void* mem) {free(mem);} |
---|
| 50 | |
---|
| 51 | ParamInterface *getParamInterface() const {if(subtype&2){param->select(object);return param;} return paraminterface;} |
---|
| 52 | const char* interfaceName() const {if (isEmpty()) return "Empty"; return getParamInterface()->getName();} |
---|
| 53 | void* getTarget() const {return (subtype&1)?dbobject:object;} |
---|
| 54 | void* getTarget(const char* classname) const {return strcmp(interfaceName(),classname)?0:getTarget();} |
---|
| 55 | int isEmpty() const {return !param;} |
---|
| 56 | ExtObject(const ExtObject& src) {src.incref();copyFrom(src);} |
---|
| 57 | void operator=(const ExtObject& src) {src.incref();decref();copyFrom(src);} |
---|
| 58 | |
---|
| 59 | int operator==(const ExtObject& src) {if (object!=src.object) return 0; return (object==0)?(param==src.param):1;} |
---|
| 60 | |
---|
| 61 | SString toString(); |
---|
| 62 | |
---|
| 63 | ExtObject(Param *p,void *o):subtype(2),object(o),param(p){} |
---|
| 64 | ExtObject(ParamInterface *p=0):subtype(0),object(0),paraminterface(p){} |
---|
| 65 | ExtObject(Param *p,DestrBase *o):subtype(1+2),dbobject(o),param(p){incref();} |
---|
| 66 | ExtObject(ParamInterface *p,DestrBase *o):subtype(1),dbobject(o),paraminterface(p){incref();} |
---|
| 67 | |
---|
| 68 | ~ExtObject(){decref();} |
---|
| 69 | }; |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | class ExtValue |
---|
| 73 | { |
---|
| 74 | public: |
---|
| 75 | ExtPType type; |
---|
| 76 | #ifdef EXTVALUEUNION |
---|
| 77 | long data[(EXTVALUEUNIONSIZE+sizeof(long)-1)/sizeof(long)]; |
---|
| 78 | long& idata() const {return (long&)data[0];}; |
---|
| 79 | double& ddata() const {return *(double*)data;}; |
---|
| 80 | ExtObject& odata() const {return *(ExtObject*)data;}; |
---|
| 81 | SString& sdata() const {return *(SString*)data;}; |
---|
| 82 | #else |
---|
| 83 | union { |
---|
| 84 | long i; |
---|
| 85 | double d; |
---|
| 86 | SString *s; |
---|
| 87 | ExtObject *o; |
---|
| 88 | }; |
---|
| 89 | long& idata() const {return (long&)i;}; |
---|
| 90 | double& ddata() const {return (double&)d;}; |
---|
| 91 | ExtObject& odata() const {return *o;}; |
---|
| 92 | SString& sdata() const {return *s;}; |
---|
| 93 | #endif |
---|
| 94 | |
---|
| 95 | void* operator new(size_t s, void* mem) {return mem;} |
---|
| 96 | void* operator new(size_t s) {return ::operator new(s);} |
---|
| 97 | |
---|
| 98 | ExtValue():type(TUnknown){} |
---|
| 99 | ~ExtValue() {setEmpty();} |
---|
| 100 | ExtValue(long v) {seti(v);} |
---|
| 101 | ExtValue(double v) {setd(v);} |
---|
| 102 | ExtValue(const SString &v) {sets(v);} |
---|
| 103 | ExtValue(const ExtObject &srco) {seto(srco);} |
---|
| 104 | long compare(const ExtValue& src) const; |
---|
| 105 | int operator==(const ExtValue& src) const; |
---|
| 106 | void operator+=(const ExtValue& src); |
---|
| 107 | void operator-=(const ExtValue& src); |
---|
| 108 | void operator*=(const ExtValue& src); |
---|
| 109 | void operator/=(const ExtValue& src); |
---|
| 110 | void operator%=(const ExtValue& src); |
---|
| 111 | void operator=(const ExtValue& src) |
---|
| 112 | {setr(src);} |
---|
| 113 | ExtValue(const ExtValue& src) |
---|
| 114 | :type(TUnknown) {set(src);} |
---|
| 115 | void setEmpty(); |
---|
| 116 | ExtPType getType() {return type;} |
---|
| 117 | void *getObjectTarget(const char* classname) {return (type==TObj)?getObject().getTarget(classname):0;} |
---|
| 118 | void setInt(long v) {if (type!=TInt) setri(v); else idata()=v;} |
---|
| 119 | void setDouble(double v) {if (type!=TDouble) setrd(v); else ddata()=v;} |
---|
| 120 | void setString(const SString &v) {if (type!=TString) setrs(v); else sdata()=v;} |
---|
| 121 | void setObject(const ExtObject &src) {if (type!=TObj) setro(src); else odata()=src;} |
---|
| 122 | long getInt() const; |
---|
| 123 | double getDouble() const; |
---|
| 124 | SString getString() const; |
---|
| 125 | ExtObject getObject() const; |
---|
| 126 | |
---|
| 127 | ExtValue getExtType(); |
---|
| 128 | |
---|
| 129 | private: // setrx - release and set, setx - assume released |
---|
| 130 | void setr(const ExtValue& src){setEmpty();set(src);} |
---|
| 131 | void set(const ExtValue& src); |
---|
| 132 | void setri(long v) {setEmpty();seti(v);} |
---|
| 133 | void setrd(double v) {setEmpty();setd(v);} |
---|
| 134 | void seti(long v) {type=TInt;idata()=v;} |
---|
| 135 | void setd(double v) {type=TDouble;ddata()=v;} |
---|
| 136 | #ifdef EXTVALUEUNION |
---|
| 137 | void setrs(const SString &v) {setEmpty();sets(v);} |
---|
| 138 | void setro(const ExtObject &src) {setEmpty();seto(src);} |
---|
| 139 | void sets(const SString &v) {type=TString;new(data) SString(v);} |
---|
| 140 | void seto(const ExtObject &src) {type=TObj;new(data) ExtObject(src);} |
---|
| 141 | #else |
---|
| 142 | void setrs(const SString &v) {setEmpty();sets(v);} |
---|
| 143 | void setro(const ExtObject &src) {setEmpty();seto(src);} |
---|
| 144 | void sets(const SString &v) {type=TString;s=new SString(v);} |
---|
| 145 | void seto(const ExtObject &src) {type=TObj;o=new ExtObject(src);} |
---|
| 146 | #endif |
---|
| 147 | |
---|
| 148 | }; |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | #endif |
---|