1 | #ifndef __XIOS_TYPE_IMPL__ |
---|
2 | #define __XIOS_TYPE_IMPL__ |
---|
3 | |
---|
4 | #include "xmlioserver_spl.hpp" |
---|
5 | #include "exception.hpp" |
---|
6 | #include "buffer_in.hpp" |
---|
7 | #include "buffer_out.hpp" |
---|
8 | #include "message.hpp" |
---|
9 | |
---|
10 | |
---|
11 | |
---|
12 | namespace xios |
---|
13 | { |
---|
14 | using namespace std; |
---|
15 | |
---|
16 | template <typename T> |
---|
17 | CType<T>::CType(const T& val) |
---|
18 | { |
---|
19 | value=val ; |
---|
20 | ptrValue=&value ; |
---|
21 | clone=false ; |
---|
22 | } |
---|
23 | |
---|
24 | template <typename T> |
---|
25 | CType<T>::CType(T& val) |
---|
26 | { |
---|
27 | ptrValue=&val ; |
---|
28 | clone=false ; |
---|
29 | } |
---|
30 | |
---|
31 | template <typename T> |
---|
32 | CType<T>::CType(const CType<T>& type) |
---|
33 | { |
---|
34 | if (type.ptrValue==&type.value) |
---|
35 | { |
---|
36 | value=type.value ; |
---|
37 | ptrValue=&value ; |
---|
38 | } |
---|
39 | else |
---|
40 | { |
---|
41 | ptrValue=type.ptrValue ; |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | |
---|
46 | |
---|
47 | template <typename T> |
---|
48 | void CType<T>::fromString(const string& str) |
---|
49 | { |
---|
50 | istringstream iss(str); |
---|
51 | checkAccess() ; |
---|
52 | iss>>*ptrValue ; |
---|
53 | |
---|
54 | } |
---|
55 | |
---|
56 | template <typename T> |
---|
57 | size_t CType<T>::size(void) const |
---|
58 | { |
---|
59 | return sizeof(T) ; |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | template <typename T> |
---|
64 | string CType<T>::toString(void) const |
---|
65 | { |
---|
66 | ostringstream oss; |
---|
67 | checkAccess() ; |
---|
68 | oss<<*ptrValue ; |
---|
69 | return oss.str() ; |
---|
70 | } |
---|
71 | |
---|
72 | template <typename T> |
---|
73 | bool CType<T>::toBuffer(CBufferOut& buffer) const |
---|
74 | { |
---|
75 | checkAccess() ; |
---|
76 | return buffer.put(*ptrValue) ; |
---|
77 | } |
---|
78 | |
---|
79 | template <typename T> |
---|
80 | bool CType<T>::fromBuffer(CBufferIn& buffer) |
---|
81 | { |
---|
82 | checkAccess() ; |
---|
83 | return buffer.get(*ptrValue) ; |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | template <typename T> |
---|
88 | void CType<T>::checkAccess(void) const |
---|
89 | { |
---|
90 | // if (!isAssign) ERROR("void CType<T>::checkAccess",<<"CType<type> has not been assign to any buffer") ; |
---|
91 | } |
---|
92 | |
---|
93 | template <typename T> |
---|
94 | CBaseType* CType<T>::duplicate(void) const |
---|
95 | { |
---|
96 | |
---|
97 | CType<T>* ret= new CType<T>(*this) ; |
---|
98 | ret->clone=true ; |
---|
99 | return ret ; |
---|
100 | } |
---|
101 | |
---|
102 | template <typename T> |
---|
103 | void CType<T>::destroy(void) |
---|
104 | { |
---|
105 | if (clone) delete this ; |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | template <typename T> |
---|
111 | CBufferOut& operator<<(CBufferOut& buffer, const CType<T>& type) |
---|
112 | { |
---|
113 | if (!type.toBuffer(buffer)) ERROR("CBuffer& operator<<(CBuffer& buffer, CType<T>& type)", |
---|
114 | <<"Buffer remain size is to low for size type") ; |
---|
115 | return buffer ; |
---|
116 | } |
---|
117 | |
---|
118 | template <typename T> |
---|
119 | CBufferOut& operator<<(CBufferOut& buffer, T& type) |
---|
120 | { |
---|
121 | if (!CType<T>(type).toBuffer(buffer)) ERROR("operator<<(CBuffer& buffer, T& type)", |
---|
122 | <<"Buffer remain size is to low for size type") ; |
---|
123 | return buffer ; |
---|
124 | } |
---|
125 | |
---|
126 | template <typename T> |
---|
127 | CBufferOut& operator<<(CBufferOut& buffer, const T& type) |
---|
128 | { |
---|
129 | if (!CType<T>(type).toBuffer(buffer)) ERROR("operator<<(CBuffer& buffer, const T& type)", |
---|
130 | <<"Buffer remain size is to low for size type") ; |
---|
131 | return buffer ; |
---|
132 | } |
---|
133 | |
---|
134 | template <typename T> |
---|
135 | CBufferIn& operator>>(CBufferIn& buffer, const CType<T>& type) |
---|
136 | { |
---|
137 | if (! const_cast<CType<T> & >(type).fromBuffer(buffer)) ERROR("CBuffer& operator<<(CBuffer& buffer, CType<T>& type)", |
---|
138 | <<"Buffer remain size is to low for size type") ; |
---|
139 | return buffer ; |
---|
140 | } |
---|
141 | |
---|
142 | template <typename T> |
---|
143 | CBufferIn& operator>>(CBufferIn& buffer, T& type) |
---|
144 | { |
---|
145 | if (!CType<T>(type).fromBuffer(buffer)) ERROR(" CBuffer& operator>>(CBuffer& buffer, T& type)", |
---|
146 | <<"Buffer remain size is to low for size type") ; |
---|
147 | return buffer ; |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | |
---|
152 | template <typename T> |
---|
153 | CMessage& operator<<(CMessage& msg, const CType<T>& type) |
---|
154 | { |
---|
155 | msg.push(*type.duplicate()) ; |
---|
156 | return msg ; |
---|
157 | } |
---|
158 | |
---|
159 | template <typename T> |
---|
160 | CMessage& operator<<(CMessage& msg,CType<T>& type) |
---|
161 | { |
---|
162 | msg.push(type) ; |
---|
163 | return msg ; |
---|
164 | } |
---|
165 | |
---|
166 | template <typename T> |
---|
167 | CMessage& operator<<(CMessage& msg, const T& type) |
---|
168 | { |
---|
169 | msg.push(*CType<T>(type).duplicate()) ; |
---|
170 | return msg ; |
---|
171 | } |
---|
172 | |
---|
173 | template <typename T> |
---|
174 | CMessage& operator<<(CMessage& msg, T& type) |
---|
175 | { |
---|
176 | msg.push(*CType<T>(type).duplicate()) ; |
---|
177 | return msg ; |
---|
178 | } |
---|
179 | |
---|
180 | } |
---|
181 | |
---|
182 | #endif |
---|