File: dynamicskeleton.cc

package info (click to toggle)
arts 1.5.9-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 8,436 kB
  • ctags: 9,848
  • sloc: ansic: 44,670; cpp: 33,776; sh: 10,486; perl: 3,470; makefile: 372; yacc: 347; lex: 160
file content (204 lines) | stat: -rw-r--r-- 5,447 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
    /*

    Copyright (C) 2001 Stefan Westerfeld
                       stefan@space.twc.de

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.
  
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.
   
    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.

    */

#include "dynamicskeleton.h"
#include "dispatcher.h"
#include "debug.h"
#include "stdio.h"

using namespace Arts;
using namespace std;

/* we could share the "class part" of those implicitely, as
   types never change */

namespace Arts {
class DynamicSkeletonData {
public:
	DynamicSkeletonData(DynamicSkeletonBase *base,
						Object_skel *skel,
						const string& interfaceName,
						const string& interfaceNameParent)
		: base(base), skel(skel), interfaceName(interfaceName),
		  interfaceNameParent(interfaceNameParent)
	{
	}

	enum InterfaceType { itNone, itParent, itDynamic };

	void buildInterfaces();
	void buildInterfaces(const string& s, InterfaceType t);
	void buildMethodTable();

	DynamicSkeletonBase *base;
	Object_skel *skel;
	string interfaceName, interfaceNameParent;
	map<string, InterfaceType> interfaceMap;
	map<string, void**> attrs;
};
}

DynamicSkeletonBase::DynamicSkeletonBase(Object_skel *skel,
	const std::string& interfaceName, const std::string& interfaceNameParent)
{
	arts_assert(skel != 0);
	_dsInit(skel,interfaceName,interfaceNameParent);
}

DynamicSkeletonBase::DynamicSkeletonBase()
{
}

void DynamicSkeletonBase::_dsInit(Object_skel *skel,
	const std::string& interfaceName, const std::string& interfaceNameParent)
{
	d = new DynamicSkeletonData(this,skel,interfaceName,interfaceNameParent);
	d->buildInterfaces();

	/* TODO: optimize me! */
	map<string, DynamicSkeletonData::InterfaceType>::iterator ii;
	for(ii = d->interfaceMap.begin(); ii != d->interfaceMap.end(); ii++)
	{
		if(ii->second == DynamicSkeletonData::itDynamic)
		{
			InterfaceDef id;
			id = Dispatcher::the()->interfaceRepo().queryInterface(ii->first);

			vector<AttributeDef>::iterator ai;
			for(ai = id.attributes.begin(); ai != id.attributes.end(); ai++)
			{
				AttributeDef& ad = *ai;
				if((ad.flags & attributeStream) == attributeStream)
				{
					void**& data = d->attrs[ad.name.c_str()];
					arts_assert(data == 0);
					data = new (void*);
					d->skel->_initStream(ad.name.c_str(),data,ad.flags);
				}
			}
		}
	}
}

DynamicSkeletonBase::~DynamicSkeletonBase()
{
	delete d;
}

std::string DynamicSkeletonBase::_dsInterfaceName()
{
	return d->interfaceName;
}

bool DynamicSkeletonBase::_dsIsCompatibleWith(const std::string& interfacename)
{
	d->buildInterfaces();
	return d->interfaceMap[interfacename] != 0;
}

void DynamicSkeletonBase::_dsBuildMethodTable()
{
	d->buildMethodTable();
}

static void _dispatch_dynamic(void *object, long methodID, Buffer *request, Buffer *result)
{
	((DynamicSkeletonBase *)object)->process(methodID, request, result);
}

void DynamicSkeletonData::buildMethodTable()
{
	buildInterfaces();

	map<string, DynamicSkeletonData::InterfaceType>::iterator ii;
	for(ii = interfaceMap.begin(); ii != interfaceMap.end(); ii++)
	{
		if(ii->second == itDynamic)
		{
			InterfaceDef id;
			id = Dispatcher::the()->interfaceRepo().queryInterface(ii->first);

			/* methods */
			vector<MethodDef>::iterator mi;
			for(mi = id.methods.begin(); mi != id.methods.end(); mi++)
				skel->_addMethod(_dispatch_dynamic, base, *mi);

			/* _get_ and _set_ methods for attributes */
			vector<AttributeDef>::iterator ai;
			for(ai = id.attributes.begin();ai != id.attributes.end();ai++)
			{
				AttributeDef& ad = *ai;

				if(ad.flags & attributeAttribute)
				{
					MethodDef md;
					if(ad.flags & streamOut)  /* readable from outside */
					{
						md.name = "_get_"+ad.name;
						md.type = ad.type;
						md.flags = methodTwoway;
						/* no parameters (don't set md.signature) */

						skel->_addMethod(_dispatch_dynamic, base, md);
					}
					if(ad.flags & streamIn)  /* writeable from outside */
					{
						md.name = "_set_"+ad.name;
						md.type = "void";
						md.flags = methodTwoway;

						ParamDef pd;
						pd.type = ad.type;
						pd.name = "newValue";
						md.signature.push_back(pd);

						skel->_addMethod(_dispatch_dynamic, base, md);
					}
				}
			}
		}
	}
}

void DynamicSkeletonData::buildInterfaces()
{
	if(interfaceMap.size() != 0) return;

	buildInterfaces(interfaceName, itDynamic);
	buildInterfaces(interfaceNameParent, itParent);
}

void DynamicSkeletonData::buildInterfaces(const string& name, DynamicSkeletonData::InterfaceType t)
{
	InterfaceDef id = Dispatcher::the()->interfaceRepo().queryInterface(name);  

	if(interfaceMap[name] == t) return;
	interfaceMap[name] = t;

    vector<string>::iterator ii;
    for(ii = id.inheritedInterfaces.begin();
        ii != id.inheritedInterfaces.end(); ii++)
	{
        buildInterfaces(*ii,t);
	}
	buildInterfaces("Arts::Object",t);
}