source: cpp/gdk/extvalue.h @ 57

Last change on this file since 57 was 5, checked in by sz, 16 years ago

added the GDK (Genotype Development Kit)

File size: 5.0 KB
Line 
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
14enum ExtPType
15{TUnknown=0,TInt,TDouble,TString,TObj};
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();
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();
116ExtPType getType() {return type;}
117void *getObjectTarget(const char* classname) {return (type==TObj)?getObject().getTarget(classname):0;}
118void setInt(long v) {if (type!=TInt) setri(v); else idata()=v;}
119void setDouble(double v) {if (type!=TDouble) setrd(v); else ddata()=v;}
120void setString(const SString &v) {if (type!=TString) setrs(v); else sdata()=v;}
121void setObject(const ExtObject &src) {if (type!=TObj) setro(src); else odata()=src;}
122long getInt() const;
123double getDouble() const;
124SString getString() const;
125ExtObject getObject() const;
126
127ExtValue getExtType();
128
129  private: // setrx - release and set, setx - assume released
130void setr(const ExtValue& src){setEmpty();set(src);}
131void set(const ExtValue& src);
132void setri(long v) {setEmpty();seti(v);}
133void setrd(double v) {setEmpty();setd(v);}
134void seti(long v) {type=TInt;idata()=v;}
135void setd(double v) {type=TDouble;ddata()=v;}
136#ifdef EXTVALUEUNION
137void setrs(const SString &v) {setEmpty();sets(v);}
138void setro(const ExtObject &src) {setEmpty();seto(src);}
139void sets(const SString &v) {type=TString;new(data) SString(v);}
140void seto(const ExtObject &src) {type=TObj;new(data) ExtObject(src);}
141#else
142void setrs(const SString &v) {setEmpty();sets(v);}
143void setro(const ExtObject &src) {setEmpty();seto(src);}
144void sets(const SString &v) {type=TString;s=new SString(v);}
145void seto(const ExtObject &src) {type=TObj;o=new ExtObject(src);}
146#endif
147
148};
149
150
151#endif
Note: See TracBrowser for help on using the repository browser.