source: cpp/gdk/extvalue.h @ 105

Last change on this file since 105 was 104, checked in by sz, 11 years ago

introducing object de/serialization - see serialtest.cpp
the core GDK classes can be now used in multiple threads (ifdef MULTITHREADED)

  • Property svn:eol-style set to native
File size: 6.2 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#include "nonstd_stl.h"
11#include "threads.h"
12
13#define EXTVALUEUNION
14#define EXTVALUEUNIONSIZE sizeof(SString)
15
16enum ExtPType
17{TUnknown=0,TInt,TDouble,TString,TObj,TInvalid};
18
19/**
20   destructable object
21 */
22class DestrBase
23{
24public:
25int refcount;
26DestrBase():refcount(0) {}
27virtual ~DestrBase() {}
28};
29
30/**
31   object reference.
32 */
33class ExtObject
34{
35int subtype;                    //< 0/1=Generic/DPC Object,  0/2=Standalone/Shared Param
36void incref() const {if (subtype&1) dbobject->refcount++;}
37void decref() const {if (subtype&1) if (!--dbobject->refcount) delete dbobject;}
38  public:
39union { void* object;           //< generic object, will use param
40DestrBase *dbobject;};  //< object with refcounting, will be deleted if refcount goes to 0
41union { Param* param;           //< if object!=0
42        ParamInterface *paraminterface;}; //< if object==0
43
44void copyFrom(const ExtObject& src)  {subtype=src.subtype;object=src.object;param=src.param;}
45
46void* operator new(size_t s, void* mem) {return mem;}
47#ifdef _MSC_VER
48void operator delete(void* mem,void* t) {}
49#endif
50void* operator new(size_t s) {return malloc(sizeof(ExtObject));}
51void operator delete(void* mem) {free(mem);}
52///@param tmp_param can be used for temporary storage, the result ParamInterface* is only valid for as long as tmp_param is valid
53ParamInterface *getParamInterface(Param &tmp_param) const  {if(subtype&2){tmp_param.setParamTab(param->getParamTab());tmp_param.select(object);return &tmp_param;} return paraminterface;}
54const char* interfaceName() const {if (isEmpty()) return "Empty"; return (subtype&2)?param->getName():paraminterface->getName();}
55bool matchesInterfaceName(ParamInterface* pi) const {return !strcmp(interfaceName(),pi->getName());}
56void* getTarget() const {return (subtype&1)?dbobject:object;}
57void* getTarget(const char* classname) const {return strcmp(interfaceName(),classname)?0:getTarget();}
58void setEmpty() {decref();subtype=0;param=NULL;object=NULL;}
59int isEmpty() const {return !param;}
60ExtObject(const ExtObject& src)      {src.incref();copyFrom(src);}
61void operator=(const ExtObject& src) {src.incref();decref();copyFrom(src);}
62
63int operator==(const ExtObject& src) const {if (object!=src.object) return 0; return (object==0)?(!strcmp(param->getName(),src.param->getName())):1;}
64
65SString toString() const;
66SString serialize_inner() const;
67SString serialize() const;
68
69ExtObject(Param *p,void *o):subtype(2),object(o),param(p){}
70ExtObject(ParamInterface *p=0):subtype(0),object(0),paraminterface(p){}
71ExtObject(Param *p,DestrBase *o):subtype(1+2),dbobject(o),param(p){incref();}
72ExtObject(ParamInterface *p,DestrBase *o):subtype(1),dbobject(o),paraminterface(p){incref();}
73
74~ExtObject(){decref();}
75
76class Serialization
77{
78std::vector<ExtObject> refs;
79int level;
80  public:
81void begin();
82void end();
83int add(const ExtObject& o);
84void replace(const ExtObject& o,const ExtObject& other);
85void remove(const ExtObject& o);
86const ExtObject* get(int ref);
87};
88
89static ThreadSingleton<Serialization> serialization;
90};
91
92class ExtValue
93{
94public:
95ExtPType type;
96#ifdef EXTVALUEUNION
97long data[(EXTVALUEUNIONSIZE+sizeof(long)-1)/sizeof(long)];
98long& idata() const {return (long&)data[0];};
99double& ddata() const {return *(double*)data;};
100ExtObject& odata() const {return *(ExtObject*)data;};
101SString& sdata() const {return *(SString*)data;};
102#else
103union {
104long i;
105double d;
106SString *s;
107ExtObject *o;
108};
109long& idata() const {return (long&)i;};
110double& ddata() const {return (double&)d;};
111ExtObject& odata() const {return *o;};
112SString& sdata() const {return *s;};
113#endif
114
115void* operator new(size_t s, void* mem) {return mem;}
116void* operator new(size_t s) {return ::operator new(s);}
117
118ExtValue():type(TUnknown){}
119~ExtValue() {setEmpty();}
120ExtValue(long v) {seti(v);}
121ExtValue(double v) {setd(v);}
122ExtValue(const SString &v) {sets(v);}
123ExtValue(const ExtObject &srco) {seto(srco);}
124long compare(const ExtValue& src) const;
125int operator==(const ExtValue& src) const;
126void operator+=(const ExtValue& src);
127void operator-=(const ExtValue& src);
128void operator*=(const ExtValue& src);
129void operator/=(const ExtValue& src);
130void operator%=(const ExtValue& src);
131void operator=(const ExtValue& src)
132        {setr(src);}
133ExtValue(const ExtValue& src)
134        :type(TUnknown) {set(src);}
135void setEmpty();
136void setInvalid() {setEmpty();type=TInvalid;}
137ExtPType getType() {return type;}
138void *getObjectTarget(const char* classname) {return (type==TObj)?getObject().getTarget(classname):0;}
139void setInt(long v) {if (type!=TInt) setri(v); else idata()=v;}
140void setDouble(double v) {if (type!=TDouble) setrd(v); else ddata()=v;}
141void setString(const SString &v) {if (type!=TString) setrs(v); else sdata()=v;}
142void setObject(const ExtObject &src) {if (type!=TObj) setro(src); else odata()=src;}
143long getInt() const;
144double getDouble() const;
145SString getString() const;
146SString serialize() const;
147ExtObject getObject() const;
148const char* parseNumber(const char* in);
149const char* deserialize(const char* in);//< @return first character after the succesfully parsed string or NULL if failed
150const char* deserialize_inner(const char* in);
151static PtrListTempl<ParamInterface*> deserializable_classes;
152static ParamInterface *findDeserializableClass(const char* name);
153static void initDeserializableClasses();
154
155ExtValue getExtType();
156
157  private: // setrx - release and set, setx - assume released
158void setr(const ExtValue& src){setEmpty();set(src);}
159void set(const ExtValue& src);
160void setri(long v) {setEmpty();seti(v);}
161void setrd(double v) {setEmpty();setd(v);}
162void seti(long v) {type=TInt;idata()=v;}
163void setd(double v) {type=TDouble;ddata()=v;}
164#ifdef EXTVALUEUNION
165void setrs(const SString &v) {setEmpty();sets(v);}
166void setro(const ExtObject &src) {setEmpty();seto(src);}
167void sets(const SString &v) {type=TString;new(data) SString(v);}
168void seto(const ExtObject &src) {type=TObj;new(data) ExtObject(src);}
169#else
170void setrs(const SString &v) {setEmpty();sets(v);}
171void setro(const ExtObject &src) {setEmpty();seto(src);}
172void sets(const SString &v) {type=TString;s=new SString(v);}
173void seto(const ExtObject &src) {type=TObj;o=new ExtObject(src);}
174#endif
175
176};
177
178
179#endif
Note: See TracBrowser for help on using the repository browser.