summaryrefslogtreecommitdiffstats
path: root/mcop/anyref.cc
blob: c5cf2cf49a4195118a74e9df807e9e1792c90458 (plain)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
    /*

    Copyright (C) 2000 Stefan Westerfeld
                       [email protected]

    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., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

    */

#include "anyref.h"
#include "common.h"
#include "startupmanager.h"
#include "debug.h"
#include <assert.h>

using namespace Arts;
using namespace std;

/*
 * private utility class to deal with any types
 */
namespace Arts {
	static class AnyRefHelper {
	private:
		Arts::InterfaceRepoV2 interfaceRepo;

	public:
		AnyRefHelper() {
			interfaceRepo = DynamicCast(Dispatcher::the()->interfaceRepo());
		}
		void skipType(Buffer& buffer, const string& type);
	} *anyRefHelper = 0;

	static class AnyRefHelperStartup : public StartupClass {
		void startup()	{
			assert(anyRefHelper == 0);
			anyRefHelper = new AnyRefHelper();
		}
		void shutdown()	{
			delete anyRefHelper;
			anyRefHelper = 0;
		}
	}	The_AnyRefHelperStartup;
}

string AnyRefBase::type() const
{
/*
   keep common types here, so that the string constructor doesn't need
   to get called each and every time
 */
	static const string tVoid("void");
	static const string tByte("byte");
	static const string tsByte("*byte");
	static const string tLong("long");
	static const string tsLong("*long");
	static const string tFloat("float");
	static const string tsFloat("*float");
	static const string tString("string");
	static const string tsString("*string");
	static const string tBool("bool");
	static const string tsBool("*bool");

	switch(rep)
	{
		case repVoid:		return tVoid;

		// primitive types
		case repByte:		return tByte;
		case repInt:
		case repLong:		return tLong;
		case repFloat:
		case repDouble:		return tFloat;
		case repString:
		case repCString:	return tString;
		case repBool:		return tBool;

		// sequences of primitive types
		case repByteSeq:	return tsByte;
		case repLongSeq:	return tsLong;
		case repFloatSeq:	return tsFloat;
		case repStringSeq:	return tsString;
		case repBoolSeq:	return tsBool;

		case repAny:		return ((Any *)data)->type;
	}
	assert(false);
	return tVoid; // silence compiler
}

void AnyRefBase::_write(Buffer *b) const
{
	switch(rep)
	{
		case repVoid:	
			break;

		// primitive types
		case repByte:		b->writeByte(*(mcopbyte *)data);
			break;

		case repInt:		b->writeLong(*(int *)data);
			break;

		case repLong:		b->writeLong(*(long *)data);
			break;

		case repFloat:		b->writeFloat(*(float *)data);
			break;

		case repDouble:		b->writeFloat(*(double *)data);
			break;

		case repString:		b->writeString(*(string *)data);
			break;

		case repCString:	b->writeString((const char *)data);
			break;

		case repBool:		b->writeBool(*(bool *)data);
			break;

		// sequences of primitive types
		case repByteSeq:	b->writeByteSeq(*(vector<mcopbyte> *)data);
			break;

		case repLongSeq:	b->writeLongSeq(*(vector<long> *)data);
			break;

		case repFloatSeq:	b->writeFloatSeq(*(vector<float> *)data);
			break;

		case repStringSeq:	b->writeStringSeq(*(vector<string> *)data);
			break;

		case repBoolSeq:	b->writeBoolSeq(*(vector<bool> *)data);
			break;

		case repAny:		b->write(((Any *)data)->value);
			break;

		default:			assert(false);
	}
}

void AnyRefBase::_read(Buffer *b) const
{
	switch(rep)
	{
		case repVoid:
			break;

		// primitive types
		case repByte:		*(mcopbyte *)data = b->readByte();
			break;

		case repInt:		*(int *)data  = b->readLong();
			break;

		case repLong:		*(long *)data = b->readLong();
			break;

		case repFloat:		*(float *)data = b->readFloat();
			break;

		case repDouble:		*(double *)data = b->readFloat();
			break;

		case repString:		b->readString(*(string *)data);
			break;

		case repBool:		*(bool *)data = b->readBool();
			break;

		// sequences of primitive types
		case repByteSeq:	b->readByteSeq(*(vector<mcopbyte> *)data);
			break;

		case repLongSeq:	b->readLongSeq(*(vector<long> *)data);
			break;

		case repFloatSeq:	b->readFloatSeq(*(vector<float> *)data);
			break;

		case repStringSeq:	b->readStringSeq(*(vector<string> *)data);
			break;

		case repBoolSeq:	b->readBoolSeq(*(vector<bool> *)data);
			break;

		case repAny:	
			{
				// find out the size by skipping over it for the first time
				long startPos = b->size() - b->remaining();
				anyRefHelper->skipType(*b, ((Any *)data)->type);

				// if everything went well, read the raw value in one step
				long size = (b->size() - b->remaining()) - startPos;
				if(!b->readError())
				{
					b->rewind();
					b->skip(startPos);
					b->read(((Any *)data)->value, size);
				}
			}
			break;

		default:			assert(false);
	}
}

/**
 * correct skipping of an arbitary type not known at compile-time (this is
 * a problem, since the size of the type will vary, due to the sequence<...>s
 * contained)
 */
void AnyRefHelper::skipType(Buffer& buffer, const string& type)
{
	/* sequences */
	if(type[0] == '*')
	{
		long seqlen = buffer.readLong();
		while(seqlen > 0 && !buffer.readError())
		{
			skipType(buffer, type.c_str()+1);
			seqlen--;
		}
	}
	else
	{
		TypeIdentification ti = interfaceRepo.identifyType(type);
		switch(ti)
		{
			case tiString:
				{
					string s;
					buffer.readString(s);
				}
				break;

			case tiLong:
				buffer.readLong();
				break;

			case tiFloat:
				buffer.readFloat();
				break;

			case tiByte:
				buffer.readByte();
				break;

			case tiBoolean:
				buffer.readBool();
				break;

			case tiVoid:
				/* nothing to do */
				break;

			case tiType:
				{
					Arts::TypeDef td = interfaceRepo.queryType(type);

					if(td.name == type)
					{
						vector<TypeComponent>::iterator tci;
						for(tci = td.contents.begin(); tci != td.contents.end();
																		tci++)
						{
							skipType(buffer,tci->type);
						}
					}
					else
					{
						arts_warning("unknown type %s",type.c_str());
					}
				}
				break;

			case tiEnum:
				buffer.readLong();
				break;

			case tiInterface:
				{
					ObjectReference oref;
					oref.readType(buffer);
				}
				break;

			default:
				arts_warning("AnyRefHelper: can't read %s",type.c_str());
				break;
		}
	}
}