summaryrefslogtreecommitdiffstats
path: root/mcop/anyref.h
blob: 60e1836ccbddc1a275fe2d122dd9132170cbb492 (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
    /*

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

    */

#ifndef MCOP_ANYREF_H
#define MCOP_ANYREF_H

#include "buffer.h"
#include <string>
#include "arts_export.h"
/*
 * BC - Status (2002-03-08): AnyRefBase, AnyRef, AnyConstRef
 *
 * These classes will be kept binary compatibile. To change it, adding a new
 * representation is necessary. No private d pointer for this reason.
 */

namespace Arts {

class Any;
class ARTS_EXPORT AnyRefBase {
protected:
	void *data;

	/*
	 * This is used for specifying which is the content of a sequence or
	 * an enum or similar. For normal types, it remains unset, as their
	 * name can be generated from rep.
	 */
	std::string _type;

	/*
	 * What representation data is pointing to?
	 *
	 * repInt (int *), repDouble (double *) and repConstChar (const char *)
	 * are no native MCOP types, but alternative ways or representing the
	 * native "int", "float" and "const char *" types
	 */

	// BC: adding new representations and types is possible, however, existing
	//     numbers may not be changed
	enum Representation {
		repVoid = 0,
		repByte = 10,
		repLong = 20, repInt = 21 /* treated as long */,
		repFloat = 30, repDouble = 31 /* treated as float */,
		repString = 40, repCString = 41 /* string */,
		repBool = 50,
		repByteSeq = 510,
		repLongSeq = 520,
		repFloatSeq = 530,
		repStringSeq = 540,
		repBoolSeq = 550,
		repAny = 1000			  /* may hold any type */
	} rep;

	void _write(Buffer *b) const;
	void _read(Buffer *b) const;

	AnyRefBase(const void *data, Representation rep)
		: data(const_cast<void *>(data)), rep(rep) { };
	AnyRefBase(const void *data, Representation rep, const char *type)
		: data(const_cast<void *>(data)), _type(type), rep(rep) { };
	AnyRefBase(const AnyRefBase &copy)
		: data(copy.data), _type(copy._type), rep(copy.rep) { }
public:
	std::string type() const;
};

class ARTS_EXPORT AnyConstRef : public AnyRefBase {
public:
	AnyConstRef()					  	: AnyRefBase(0,repVoid) { };
	AnyConstRef(const mcopbyte& value)	: AnyRefBase(&value,repByte) { };
	AnyConstRef(const int& 	value)		: AnyRefBase(&value,repInt) { };
	AnyConstRef(const long& 	value)	: AnyRefBase(&value,repLong) { };
	AnyConstRef(const float& 	value)	: AnyRefBase(&value,repFloat) { };
	AnyConstRef(const double& 	value)	: AnyRefBase(&value,repDouble) { };
	AnyConstRef(const std::string& value) : AnyRefBase(&value,repString) { };
	AnyConstRef(const char *value)		: AnyRefBase(value,repCString) { };
	AnyConstRef(const bool& 	value)	: AnyRefBase(&value,repBool) { };

	AnyConstRef(const std::vector<mcopbyte>& v)
										: AnyRefBase(&v,repByteSeq) { };
	AnyConstRef(const std::vector<long>& v)	
										: AnyRefBase(&v,repLongSeq) { };
	AnyConstRef(const std::vector<float>& v)
										: AnyRefBase(&v,repFloatSeq) { };
	AnyConstRef(const std::vector<std::string>& v)
										: AnyRefBase(&v,repStringSeq) { };
	AnyConstRef(const std::vector<bool>& v)
										: AnyRefBase(&v,repBoolSeq) { };

	AnyConstRef(const Any& value)		: AnyRefBase(&value,repAny) { };

	AnyConstRef(const AnyConstRef& ref) : AnyRefBase(ref) { }
	void write(Buffer *b) const			{ _write(b); }
};

class ARTS_EXPORT AnyRef : public AnyRefBase {
public:
	AnyRef()					  		: AnyRefBase(0,repVoid) { };

	// primitive types
	AnyRef(mcopbyte& value)				: AnyRefBase(&value,repByte) { };
	AnyRef(int& 	value)				: AnyRefBase(&value,repInt) { };
	AnyRef(long& 	value)				: AnyRefBase(&value,repLong) { };
	AnyRef(float& 	value)				: AnyRefBase(&value,repFloat) { };
	AnyRef(double& 	value)				: AnyRefBase(&value,repDouble) { };
	AnyRef(std::string&	value)			: AnyRefBase(&value,repString) { };
	AnyRef(bool& 	value)				: AnyRefBase(&value,repBool) { };

	// sequence of primitive types
	AnyRef(std::vector<mcopbyte>& value)	: AnyRefBase(&value,repByteSeq) { };
	AnyRef(std::vector<long>& value)		: AnyRefBase(&value,repLongSeq) { };
	AnyRef(std::vector<float>& value)		: AnyRefBase(&value,repFloatSeq) { };
	AnyRef(std::vector<std::string>& value)	: AnyRefBase(&value,repStringSeq){};
	AnyRef(std::vector<bool>& value)	: AnyRefBase(&value,repBoolSeq){};

	AnyRef(Any& value)						: AnyRefBase(&value,repAny) { };

	AnyRef(const AnyRef& ref) : AnyRefBase(ref) { }

	void read(Buffer *b) const			{ _read(b);  }
	void write(Buffer *b) const			{ _write(b); }
};

}
#endif /* MCOP_ANYREF_H */