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