source: cpp/gdk/extvalue.h @ 97

Last change on this file since 97 was 92, checked in by Maciej Komosinski, 11 years ago

SString::valueOf(value) can handle more argument types

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