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