1 | #ifndef __XIOS_TYPE_IMPL__ |
---|
2 | #define __XIOS_TYPE_IMPL__ |
---|
3 | |
---|
4 | #include "xios_spl.hpp" |
---|
5 | #include "exception.hpp" |
---|
6 | #include "buffer_in.hpp" |
---|
7 | #include "buffer_out.hpp" |
---|
8 | #include "message.hpp" |
---|
9 | |
---|
10 | |
---|
11 | |
---|
12 | |
---|
13 | namespace xios |
---|
14 | { |
---|
15 | |
---|
16 | using namespace std; |
---|
17 | |
---|
18 | template <typename T> |
---|
19 | CType<T>::CType(void) |
---|
20 | { |
---|
21 | empty=true ; |
---|
22 | } |
---|
23 | |
---|
24 | template <typename T> |
---|
25 | CType<T>::CType(const T& val) |
---|
26 | { |
---|
27 | empty=true ; |
---|
28 | set(val) ; |
---|
29 | } |
---|
30 | |
---|
31 | template <typename T> |
---|
32 | CType<T>::CType(const CType<T>& type) |
---|
33 | { |
---|
34 | empty=true ; |
---|
35 | set(type) ; |
---|
36 | } |
---|
37 | |
---|
38 | template <typename T> |
---|
39 | CType<T>::CType(const CType_ref<T>& type) |
---|
40 | { |
---|
41 | empty=true ; |
---|
42 | set(type) ; |
---|
43 | } |
---|
44 | |
---|
45 | template <typename T> |
---|
46 | void CType<T>::set(const T& val) |
---|
47 | { |
---|
48 | if (empty) |
---|
49 | { |
---|
50 | ptrValue = new T(val) ; |
---|
51 | empty=false ; |
---|
52 | } |
---|
53 | else *ptrValue = val ; |
---|
54 | } |
---|
55 | |
---|
56 | template <typename T> |
---|
57 | void CType<T>::set(const CType<T>& type) |
---|
58 | { |
---|
59 | if (type.isEmpty()) reset() ; |
---|
60 | else |
---|
61 | { |
---|
62 | if (empty) |
---|
63 | { |
---|
64 | ptrValue = new T(*type.ptrValue) ; |
---|
65 | empty=false ; |
---|
66 | } |
---|
67 | else *ptrValue = *type.ptrValue ; |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | template <typename T> |
---|
72 | void CType<T>::set(const CType_ref<T>& type) |
---|
73 | { |
---|
74 | if (type.isEmpty()) reset() ; |
---|
75 | else |
---|
76 | { |
---|
77 | if (empty) |
---|
78 | { |
---|
79 | ptrValue = new T(*type.ptrValue) ; |
---|
80 | empty=false ; |
---|
81 | } |
---|
82 | else *ptrValue = *type.ptrValue ; |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | template <typename T> |
---|
87 | T& CType<T>::get(void) |
---|
88 | { |
---|
89 | this->checkEmpty(); |
---|
90 | return *ptrValue ; |
---|
91 | } |
---|
92 | |
---|
93 | template <typename T> |
---|
94 | const T& CType<T>::get(void) const |
---|
95 | { |
---|
96 | this->checkEmpty(); |
---|
97 | return *ptrValue ; |
---|
98 | } |
---|
99 | |
---|
100 | template <typename T> |
---|
101 | CType<T>& CType<T>::operator = (const T& val) |
---|
102 | { |
---|
103 | set(val) ; |
---|
104 | return *this ; |
---|
105 | } |
---|
106 | |
---|
107 | template <typename T> |
---|
108 | CType<T>& CType<T>::operator = (const CType<T>& type) |
---|
109 | { |
---|
110 | set(type) ; |
---|
111 | return *this ; |
---|
112 | } |
---|
113 | |
---|
114 | template <typename T> |
---|
115 | CType<T>& CType<T>::operator = (const CType_ref<T>& type) |
---|
116 | { |
---|
117 | set(type) ; |
---|
118 | return *this ; |
---|
119 | } |
---|
120 | |
---|
121 | template <typename T> |
---|
122 | CType<T>::operator T&() |
---|
123 | { |
---|
124 | this->checkEmpty(); |
---|
125 | return *ptrValue ; |
---|
126 | } |
---|
127 | |
---|
128 | template <typename T> |
---|
129 | CType<T>::operator const T&() const |
---|
130 | { |
---|
131 | this->checkEmpty(); |
---|
132 | return *ptrValue ; |
---|
133 | } |
---|
134 | |
---|
135 | template <typename T> |
---|
136 | CType<T>* CType<T>::_clone(void) const |
---|
137 | { |
---|
138 | this->checkEmpty(); |
---|
139 | return new CType(*this) ; |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | template <typename T> |
---|
144 | void CType<T>::_fromString(const string& str) |
---|
145 | { |
---|
146 | istringstream iss(str); |
---|
147 | allocate() ; |
---|
148 | iss>>*ptrValue ; |
---|
149 | } |
---|
150 | |
---|
151 | template <typename T> |
---|
152 | size_t CType<T>::_size(void) const |
---|
153 | { |
---|
154 | return sizeof(T) ; |
---|
155 | } |
---|
156 | |
---|
157 | template <typename T> |
---|
158 | bool CType<T>::_isEmpty(void) const |
---|
159 | { |
---|
160 | return empty ; |
---|
161 | } |
---|
162 | |
---|
163 | template <typename T> |
---|
164 | string CType<T>::_toString(void) const |
---|
165 | { |
---|
166 | ostringstream oss; |
---|
167 | this->checkEmpty(); |
---|
168 | oss<<*ptrValue ; |
---|
169 | return oss.str() ; |
---|
170 | } |
---|
171 | |
---|
172 | template <typename T> |
---|
173 | bool CType<T>::_toBuffer(CBufferOut& buffer) const |
---|
174 | { |
---|
175 | this->checkEmpty(); |
---|
176 | return buffer.put(*ptrValue) ; |
---|
177 | } |
---|
178 | |
---|
179 | template <typename T> |
---|
180 | bool CType<T>::_fromBuffer(CBufferIn& buffer) |
---|
181 | { |
---|
182 | allocate() ; |
---|
183 | return buffer.get(*ptrValue) ; |
---|
184 | } |
---|
185 | |
---|
186 | |
---|
187 | template <typename T> |
---|
188 | void CType<T>::allocate(void) |
---|
189 | { |
---|
190 | if (empty) |
---|
191 | { |
---|
192 | ptrValue = new T ; |
---|
193 | empty=false ; |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | template <typename T> |
---|
198 | void CType<T>::_reset(void) |
---|
199 | { |
---|
200 | if (!empty) |
---|
201 | { |
---|
202 | delete ptrValue ; |
---|
203 | empty=true ; |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | template <typename T> |
---|
208 | void CType<T>::_checkEmpty(void) const |
---|
209 | { |
---|
210 | if (empty) ERROR("template <typename T> void CType<T>::checkEmpty(void) const", << "Data is not initialized") ; |
---|
211 | } |
---|
212 | |
---|
213 | template <typename T> |
---|
214 | bool operator==(const CType<T>& lhs, const T& rhs) |
---|
215 | { |
---|
216 | if (lhs.isEmpty()) return false; |
---|
217 | return (*lhs.ptrValue == rhs); |
---|
218 | } |
---|
219 | |
---|
220 | template <typename T> |
---|
221 | bool operator==(const T& lhs, const CType<T>& rhs) |
---|
222 | { |
---|
223 | return (rhs == lhs); |
---|
224 | } |
---|
225 | |
---|
226 | template <typename T> |
---|
227 | bool operator==(const CType<T>& lhs, const CType<T>& rhs) |
---|
228 | { |
---|
229 | if ((lhs.isEmpty() && !rhs.isEmpty()) || (!lhs.isEmpty() && rhs.isEmpty())) return false; |
---|
230 | if (lhs.isEmpty() && rhs.isEmpty()) return true; |
---|
231 | |
---|
232 | return (*lhs.ptrValue == *rhs.ptrValue); |
---|
233 | } |
---|
234 | |
---|
235 | template <typename T> |
---|
236 | CBufferOut& operator<<(CBufferOut& buffer, const CType<T>& type) |
---|
237 | { |
---|
238 | if (!type.toBuffer(buffer)) ERROR("CBuffer& operator<<(CBuffer& buffer, CType<T>& type)", |
---|
239 | << "Not enough free space in buffer to queue the data."); |
---|
240 | return buffer ; |
---|
241 | } |
---|
242 | |
---|
243 | |
---|
244 | template <typename T> |
---|
245 | CBufferOut& operator<<(CBufferOut& buffer, const T& type) |
---|
246 | { |
---|
247 | if (!CType<T>(type).toBuffer(buffer)) ERROR("operator<<(CBuffer& buffer, const T& type)", |
---|
248 | << "Not enough free space in buffer to queue the data."); |
---|
249 | return buffer ; |
---|
250 | } |
---|
251 | |
---|
252 | template <typename T> |
---|
253 | CBufferIn& operator>>(CBufferIn& buffer, CType<T>& type) |
---|
254 | { |
---|
255 | if (! type.fromBuffer(buffer)) ERROR("CBuffer& operator<<(CBuffer& buffer, CType<T>& type)", |
---|
256 | << "Not enough data in buffer to unqueue the data."); |
---|
257 | return buffer ; |
---|
258 | } |
---|
259 | |
---|
260 | template <typename T> |
---|
261 | CMessage& operator<<(CMessage& msg, const T& type) |
---|
262 | { |
---|
263 | msg.push(CType<T>(type)) ; |
---|
264 | return msg ; |
---|
265 | } |
---|
266 | |
---|
267 | } |
---|
268 | |
---|
269 | #endif |
---|