source: cpp/frams/param/paramobj.cpp @ 139

Last change on this file since 139 was 121, checked in by sz, 10 years ago

updated file headers and makefiles

  • Property svn:eol-style set to native
File size: 3.8 KB
Line 
1// This file is a part of the Framsticks GDK.
2// Copyright (C) 2002-2014  Maciej Komosinski and Szymon Ulatowski.  See LICENSE.txt for details.
3// Refer to http://www.framsticks.com/ for further information.
4
5#include <frams/param/paramobj.h>
6#include <frams/util/extvalue.h>
7#include <common/nonstd_stl.h>
8
9static const char* maybedup(bool dup,const char* src)
10{ return dup ? (src?strdup(src):0) : src; }
11static void maybefree(void* mem)
12{ if (mem) free(mem); }
13
14ParamEntry* ParamObject::makeParamTab(ParamInterface *pi,bool stripgroups,bool stripproc,
15                                      int firstprop, int maxprops, bool dupentries, int flagsexclude)
16{
17ParamEntry *tab,*t;
18int i,n,offs;
19static ExtValue ex;
20int count=0,gcount=1;
21if (!stripgroups) gcount=pi->getGroupCount();
22if (stripproc||flagsexclude)
23for (int i=firstprop;i < pi->getPropCount();i++)
24        {
25        const char*t=pi->type(i);
26        if ((!stripproc)||(strchr("dfsox",*t)))
27                if ((!flagsexclude)||(!(pi->flags(i)&flagsexclude)))
28                        if (++count >= maxprops) break;
29        }
30else count=pi->getPropCount()-firstprop;
31t=tab=(ParamEntry*)malloc(sizeof(ParamEntry)*(count+gcount+1));
32t->group=(short)gcount;
33t->flags=(short)count;
34t->name=maybedup(dupentries,pi->getName());
35t->type=maybedup(dupentries,pi->getDescription());
36for(i=0;i<gcount;i++)
37        {
38        t->id=maybedup(dupentries,pi->grname(i));
39        t->offset=0;
40        t++;
41        }
42n=1; offs=1;
43for (i=firstprop;i < pi->getPropCount();i++)
44        {
45        if ((!stripproc)||(strchr("dfsox",*pi->type(i))))
46                {
47                if ((!flagsexclude)||(!(pi->flags(i)&flagsexclude)))
48                        {
49                        t->offset=offs*sizeof(ExtValue);
50                        if (*pi->type(i)!='x') t->offset+=(((char*)&ex.data[0])-((char*)&ex));
51                        t->group=(short)(stripgroups?0:pi->group(i));
52                        t->flags=(short)pi->flags(i);
53                        t->fun1=0;
54                        t->fun2=0;
55                        t->id=maybedup(dupentries,pi->id(i));
56                        t->name=maybedup(dupentries,pi->name(i));
57                        t->type=maybedup(dupentries,pi->type(i));
58                        t->help=maybedup(dupentries,pi->help(i));
59                        t++;
60                        n++;
61                        if (n>count) break;
62                        }
63                offs++;
64                }
65        }
66t->id=0; t->group=0; t->flags=dupentries?MUTPARAM_ALLOCENTRY:0;
67return tab;
68}
69
70void ParamObject::freeParamTab(ParamEntry *pe)
71{
72if (pe[pe->flags+pe->group].flags & MUTPARAM_ALLOCENTRY)
73        {
74        int i;
75        ParamEntry *e;
76        maybefree((void*)pe->name);
77        maybefree((void*)pe->type);
78        for (i=0,e=pe;i<pe->group;i++,e++)
79                maybefree((void*)e->id);
80        for (i=pe->group,e=pe+i;i<pe->group+pe->flags;i++,e++)
81                {
82                maybefree((void*)e->id);
83                maybefree((void*)e->name);
84                maybefree((void*)e->type);
85                maybefree((void*)e->help);
86                }
87        }
88free(pe);
89}
90
91bool ParamObject::paramTabEqual(ParamEntry *pe1,ParamEntry *pe2)
92{
93if (pe1->flags != pe2->flags) return false;
94ParamEntry *e1=pe1+pe1->group, *e2=pe2+pe2->group;
95for(int i=0;i<pe1->flags;i++,e1++,e2++)
96        {
97        if (strcmp(e1->id,e2->id)) return false;
98        if (strcmp(e1->name,e2->name)) return false;
99        if (strcmp(e1->type,e2->type)) return false;
100        if (e1->offset!=e2->offset) return false;
101        }
102return true;
103}
104
105void* ParamObject::makeObject(ParamEntry *tab)
106{
107if (!tab) return 0;
108int n=tab->flags;
109if (!n) return 0;
110ExtValue *ret=new ExtValue[n+1];
111ret->setInt(n);
112ExtValue *v=ret+1;
113tab+=tab->group;
114for (;n>0;n--,v++,tab++)
115        switch(*tab->type)
116        {
117        case 'd': v->setInt(0); break;
118        case 'f': v->setDouble(0); break;
119        case 's': v->setString(SString::empty()); break;
120        case 'o': v->setObject(ExtObject()); break;
121        case 'x': break;
122        }
123return ret;
124}
125
126void ParamObject::copyObject(void* dst,void* src)
127{
128if ((!dst)||(!src)) return;
129ExtValue *s=(ExtValue *)src;
130ExtValue *d=(ExtValue *)dst;
131int n=min(s->getInt(),d->getInt());
132s++; d++;
133for (int i=0;i<n;i++,d++,s++)
134        *d=*s;
135}
136
137void* ParamObject::dupObject(void* src)
138{
139if (!src) return NULL;
140ExtValue *s=(ExtValue *)src;
141int n=s->getInt();
142
143ExtValue *ret=new ExtValue[n+1];
144ExtValue *d=ret;
145d->setInt(n);
146d++; s++;
147
148for (int i=0;i<n;i++,d++,s++)
149        *d=*s;
150
151return ret;
152}
153
154void ParamObject::freeObject(void* obj)
155{
156if (!obj) return;
157delete[] (ExtValue *)obj;
158}
159
160
161
162
163
164
165
166
167
Note: See TracBrowser for help on using the repository browser.