source: cpp/gdk/paramobj.cpp @ 5

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

added the GDK (Genotype Development Kit)

File size: 3.5 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#include "paramobj.h"
6#include "extvalue.h"
7
8static const char* maybedup(bool dup,const char* src)
9{ return dup ? (src?strdup(src):0) : src; }
10static void maybefree(void* mem)
11{ if (mem) free(mem); }
12
13ParamEntry* ParamObject::makeParamTab(ParamInterface *pi,bool stripgroups,bool stripproc,
14                                      int firstprop, int maxprops, bool dupentries, int flagsexclude)
15{
16ParamEntry *tab,*t;
17int i,n,offs;
18static ExtValue ex;
19int count=0,gcount=1;
20if (!stripgroups) gcount=pi->getGroupCount();
21if (stripproc||flagsexclude)
22for (int i=firstprop;i < pi->getPropCount();i++)
23        {
24        const char*t=pi->type(i);
25        if ((!stripproc)||(strchr("dfsox",*t)))
26                if ((!flagsexclude)||(!(pi->flags(i)&flagsexclude)))
27                        if (++count >= maxprops) break;
28        }
29else count=pi->getPropCount()-firstprop;
30t=tab=(ParamEntry*)malloc(sizeof(ParamEntry)*(count+gcount+1));
31t->group=gcount;
32t->flags=count;
33t->name=maybedup(dupentries,pi->getName());
34t->type=maybedup(dupentries,pi->getDescription());
35for(i=0;i<gcount;i++)
36        {
37        t->id=maybedup(dupentries,pi->grname(i));
38        t->offset=0;
39        t++;
40        }
41n=1; offs=1;
42for (i=firstprop;i < pi->getPropCount();i++)
43        {
44        if ((!stripproc)||(strchr("dfsox",*pi->type(i))))
45                {
46                if ((!flagsexclude)||(!(pi->flags(i)&flagsexclude)))
47                        {
48                        t->offset=offs*sizeof(ExtValue);
49                        if (*pi->type(i)!='x') t->offset+=(((char*)&ex.data[0])-((char*)&ex));
50                        t->group=stripgroups?0:pi->group(i);
51                        t->flags=pi->flags(i);
52                        t->fun1=0;
53                        t->fun2=0;
54                        t->id=maybedup(dupentries,pi->id(i));
55                        t->name=maybedup(dupentries,pi->name(i));
56                        t->type=maybedup(dupentries,pi->type(i));
57                        t->help=maybedup(dupentries,pi->help(i));
58                        t++;
59                        n++;
60                        if (n>count) break;
61                        }
62                offs++;
63                }
64        }
65t->id=0; t->group=0; t->flags=dupentries?MUTPARAM_ALLOCENTRY:0;
66return tab;
67}
68
69void ParamObject::freeParamTab(ParamEntry *pe)
70{
71if (pe[pe->flags+pe->group].flags & MUTPARAM_ALLOCENTRY)
72        {
73        int i;
74        ParamEntry *e;
75        maybefree((void*)pe->name);
76        maybefree((void*)pe->type);
77        for (i=0,e=pe;i<pe->group;i++,e++)
78                maybefree((void*)e->id);
79        for (i=pe->group,e=pe+i;i<pe->group+pe->flags;i++,e++)
80                {
81                maybefree((void*)e->id);
82                maybefree((void*)e->name);
83                maybefree((void*)e->type);
84                maybefree((void*)e->help);
85                }
86        }
87free(pe);
88}
89
90void* ParamObject::makeObject(ParamEntry *tab)
91{
92if (!tab) return 0;
93int n=tab->flags;
94if (!n) return 0;
95ExtValue *ret,*v;
96ret=v=(ExtValue*)malloc( sizeof(ExtValue)*(n+1) );
97for (int i=0;i<=n;i++,v++)
98        new(v) ExtValue;
99ret->setInt(n);
100v=ret+1;
101tab+=tab->group;
102for (;n>0;n--,v++,tab++)
103        switch(*tab->type)
104        {
105        case 'd': v->setInt(0); break;
106        case 'f': v->setDouble(0); break;
107        case 's': v->setString(SString::empty); break;
108        case 'o': v->setObject(ExtObject()); break;
109        case 'x': break;
110        }
111return ret;
112}
113
114void ParamObject::copyObject(void* dst,void* src)
115{
116if ((!dst)||(!src)) return;
117ExtValue *s=(ExtValue *)src;
118ExtValue *d=(ExtValue *)dst;
119int n=min(s->getInt(),d->getInt());
120s++; d++;
121for (int i=0;i<n;i++,d++,s++)
122        *d=*s;
123}
124
125void* ParamObject::dupObject(void* src)
126{
127if (!src) return 0;
128ExtValue *s=(ExtValue *)src;
129int n=s->getInt();
130
131ExtValue *ret=new ExtValue[n+1];
132ExtValue *d=ret;
133d->setInt(n);
134d++; s++;
135
136for (int i=0;i<n;i++,d++,s++)
137        *d=*s;
138
139return ret;
140}
141
142void ParamObject::freeObject(void* obj)
143{
144if (!obj) return;
145ExtValue *o=(ExtValue *)obj,*t=o+1;
146int n=o->getInt();
147for (int i=0;i<n;i++,t++)
148        t -> ~ExtValue();
149free(obj);
150}
151
152
153
154
155
156
157
158
159
Note: See TracBrowser for help on using the repository browser.