File: reference.h

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 (349 lines) | stat: -rw-r--r-- 9,661 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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
	Copyright (C) 2000 Nicolas Brodu, nicolas.brodu@free.fr
	                   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.
*/

/*
 * BC - Status (2002-03-08): Reference, SubClass, Object, DynamicCast.
 *
 * Part of the public API. Must be kept binary compatible by NOT TOUCHING
 * AT ALL. Interaction with generated and written code.
 */


#ifndef MCOP_REFERENCE_H
#define MCOP_REFERENCE_H

#include "common.h"
#include <string>
#include <vector>

#include "arts_export.h"

namespace Arts {

// Acts as a string or ObjectReference, but enables a different constructor
class ARTS_EXPORT Reference {
private:
	ObjectReference myref;
	std::string mys;
	bool strMode;
public:
	
	inline explicit Reference(const ObjectReference& ref) {
		myref=ref;
		strMode=false;
	}

	inline explicit Reference(const std::string& s) {
		mys=s;
		strMode=true;
	}
	
	inline explicit Reference(const char* c) {
		mys=c;
		strMode=true;
	}
	inline Reference& operator=(const std::string& s) {
		mys=s;
		strMode=true;
		return *this;
	}
	inline Reference& operator=(const char*c) {
		mys=c;
		strMode=true;
		return *this;
	}
	inline bool isString() const {return strMode;}
//	inline operator const std::string() const {return mys;}
	inline const std::string& string() const {return mys;}
//	inline operator const ObjectReference() const {return myref;}
	inline const ObjectReference& reference() const {return myref;}
};

// Acts as a string, but enables a different constructor
class ARTS_EXPORT SubClass {
private:
	std::string mys;
public:

	inline explicit SubClass(const std::string& s) : mys(s) {}
	inline explicit SubClass(const char* c) : mys(c) {}
	inline SubClass& operator=(const std::string& s) {mys=s; return *this;}
	inline SubClass& operator=(const char*c) {mys=c; return *this;}
//	inline operator const std::string() const {return mys;}
	inline const std::string& string() const {return mys;}
};


class ScheduleNode;
class FlowSystem;

// Object has _no_ virtual, and must not have. That way, all the
// wrappers can be passed as argument or return type, and there is also
// no virtual table bloat.
// Moreover, the virtual mechanism still works correctly thanks to the
// _pool->base redirection.
class ARTS_EXPORT Object {
protected:
	// Pool of common variables for a bunch a wrappers
	class Pool {
		friend class Object;
		Object_base* (*creator)();
		bool created;
		int count;
	public:
		Object_base* base;
		inline Pool(Object_base* b)
			: creator(0), created(true), count(1), base(b) {}
		inline Pool(Object_base* (*cor)())
			: creator(cor), created(false), count(1), base(0) {}
		inline void Inc() {count++;}
		inline void Dec() {
			if (--count==0) {
				if(base) base->_release();
				delete this;
			}
		}
		inline void checkcreate() {
			if (!created) {base = creator(); created=true;}
		}
	} *_pool;

	inline Object(Object_base* (*cor)()) {
		_pool = new Pool(cor);
	}
	inline Object(Pool* p) : _pool(p) {
		_pool->Inc();
	}
	inline Object(Pool& p) : _pool(&p) {
		_pool->Inc();
	}
	inline Object(Object_base* b) {
		_pool = new Pool(b);
	}
public:
	typedef Object_base _base_class;

	// Dynamic cast constructor of inherited classes needs to access the _pool
	// of a generic object if casting successful. But it could not without this
	inline Pool* _get_pool() const {return _pool;}
	
	inline ~Object() {
		_pool->Dec();
	}
	
	// Those constructors are public, since we don't need an actual creator.
	// They enable generic object creation (like from a subclass defined at
	// run-time!)
	inline Object(const SubClass& s) {
		_pool = new Pool(Object_base::_create(s.string()));
	}
	inline Object(const Reference &r) {
		_pool = new Pool(r.isString()?(Object_base::_fromString(r.string())):(Object_base::_fromReference(r.reference(),true)));
	}
	inline Object(const Object& target) : _pool(target._pool) {
		_pool->Inc();
	}
	inline Object() { // creates a null object
		_pool = new Pool((Object_base*)0);
	}
	inline Object& operator=(const Object& target) {
		if (_pool == target._pool) return *this;
		_pool->Dec();
		_pool = target._pool;
		_pool->Inc();
		return *this;
	}
	// No problem for the creator, this class has protected constructors.
	// So creator should give back an actual implementation
	inline Object_base* _base() const {
		_pool->checkcreate();
		return _pool->base;
	}

	// null, error?
	inline bool isNull() const {
		_pool->checkcreate();
		return !(_pool->base);
	}
	inline bool error() const {
		_pool->checkcreate();
    	return _pool->base && _pool->base->_error();
	}
	
	// Comparision
	inline bool _isEqual(const Object& other) const {
		if(isNull() != other.isNull()) return false;

		// we can assume that things are created here, as we've
		// called isNull of both wrappers once
		if(!isNull())
			return _pool->base->_isEqual(other._pool->base);

		// both null references
		return true;
	}


// Object_base wrappers

	// Custom messaging - see Object_base for comments
	inline Buffer *_allocCustomMessage(long handlerID) const {
		_pool->checkcreate();
		assert(_pool->base);
		return _pool->base->_allocCustomMessage(handlerID);
	}

	inline void _sendCustomMessage(Buffer *data) const {
		_pool->checkcreate();
		assert(_pool->base);
		_pool->base->_sendCustomMessage(data);
	}
	
	// generic capabilities, which allow find out what you can do with an
	// object even if you don't know it's interface
	inline long _lookupMethod(const MethodDef& methodDef) const {
		_pool->checkcreate();
		assert(_pool->base);
		return _pool->base->_lookupMethod(methodDef);
	}
	inline std::string _interfaceName() const {
		_pool->checkcreate();
		assert(_pool->base);
		return _pool->base->_interfaceName();
	}
	inline InterfaceDef _queryInterface(const std::string& name) const {
		_pool->checkcreate();
		assert(_pool->base);
		return _pool->base->_queryInterface(name);
	}
	inline TypeDef _queryType(const std::string& name) const {
		_pool->checkcreate();
		assert(_pool->base);
		return _pool->base->_queryType(name);
	}
	inline EnumDef _queryEnum(const std::string& name) const {
		_pool->checkcreate();
		assert(_pool->base);
		return _pool->base->_queryEnum(name);
	}
	// Stringification
	inline std::string _toString() const {
		_pool->checkcreate();
		assert(_pool->base);
		return _pool->base->_toString();
	}
	inline std::string toString() const {return _toString();}

	// stuff for streaming (put in a seperate interface?)
	inline void calculateBlock(unsigned long cycles) const {
		_pool->checkcreate();
		assert(_pool->base);
		_pool->base->calculateBlock(cycles);
	}
	// Node info
	inline ScheduleNode *_node() const {
		_pool->checkcreate();
		assert(_pool->base);
		return _pool->base->_node();
	}
	
	// Ah! Flowsystem is not defined yet, so cannot be returned inline.
	FlowSystem _flowSystem() const;
	
	inline void _copyRemote() const {
		_pool->checkcreate();
		assert(_pool->base);
		_pool->base->_copyRemote();
	}
	inline void _useRemote() const {
		_pool->checkcreate();
		assert(_pool->base);
		_pool->base->_useRemote();
	}
	inline void _releaseRemote() const {
		_pool->checkcreate();
		assert(_pool->base);
		_pool->base->_releaseRemote();
	}
	
	// Default I/O info
	inline std::vector<std::string> _defaultPortsIn() const {
		_pool->checkcreate();
		assert(_pool->base);
		return _pool->base->_defaultPortsIn();
	}
	inline std::vector<std::string> _defaultPortsOut() const {
		_pool->checkcreate();
		assert(_pool->base);
		return _pool->base->_defaultPortsOut();
	}
	
	// aggregation
	inline std::string _addChild(Arts::Object child, const std::string& name) const {
		_pool->checkcreate();
		assert(_pool->base);
		return _pool->base->_addChild(child, name);
	}
	inline bool _removeChild(const std::string& name) const {
		_pool->checkcreate();
		assert(_pool->base);
		return _pool->base->_removeChild(name);
	}
	inline Arts::Object _getChild(const std::string& name) const {
		_pool->checkcreate();
		assert(_pool->base);
		return _pool->base->_getChild(name);
	}
	inline std::vector<std::string> * _queryChildren() const {
		_pool->checkcreate();
		assert(_pool->base);
		return _pool->base->_queryChildren();
	}
	// Do we really need those in the Wrapper?
	// And would it really be sensible to make _cast wrappers?
/*	inline void _release() const {
		_pool->checkcreate();
		assert(_pool->base);
		return _pool->base->_release();
	}
	inline Object_base *_copy() const {
		_poo->checkcreate();
		assert(_pool->base);
		return _pool->base->_copy();
	}
*/

	// Object::null() returns a null object (and not just a reference to one)
	inline static Object null() {return Object((Object_base*)0);}
	inline static Object _from_base(Object_base* b) {return Object(b);}
};

// Enables a different constructor, that should do the cast
class ARTS_EXPORT DynamicCast {
private:
	Object obj;
public:
	inline explicit DynamicCast(const Object& o) : obj(o) {}
	inline const Object& object() const {return obj;}
};

}

#endif