[308] | 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 | |
---|
[369] | 12 | |
---|
[335] | 13 | namespace xios |
---|
[308] | 14 | { |
---|
[369] | 15 | |
---|
[308] | 16 | using namespace std; |
---|
| 17 | |
---|
| 18 | template <typename T> |
---|
[369] | 19 | CType<T>::CType(void) |
---|
| 20 | { |
---|
| 21 | empty=true ; |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | template <typename T> |
---|
[308] | 25 | CType<T>::CType(const T& val) |
---|
| 26 | { |
---|
[369] | 27 | empty=true ; |
---|
| 28 | set(val) ; |
---|
[308] | 29 | } |
---|
| 30 | |
---|
| 31 | template <typename T> |
---|
[369] | 32 | CType<T>::CType(const CType<T>& type) |
---|
[308] | 33 | { |
---|
[369] | 34 | empty=true ; |
---|
| 35 | set(type) ; |
---|
[308] | 36 | } |
---|
| 37 | |
---|
| 38 | template <typename T> |
---|
[369] | 39 | CType<T>::CType(const CType_ref<T>& type) |
---|
[308] | 40 | { |
---|
[369] | 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 |
---|
[308] | 61 | { |
---|
[369] | 62 | if (empty) |
---|
| 63 | { |
---|
| 64 | ptrValue = new T(*type.ptrValue) ; |
---|
| 65 | empty=false ; |
---|
| 66 | } |
---|
| 67 | else *ptrValue = *type.ptrValue ; |
---|
[308] | 68 | } |
---|
[369] | 69 | } |
---|
| 70 | |
---|
| 71 | template <typename T> |
---|
| 72 | void CType<T>::set(const CType_ref<T>& type) |
---|
| 73 | { |
---|
| 74 | if (type.isEmpty()) reset() ; |
---|
| 75 | else |
---|
[308] | 76 | { |
---|
[369] | 77 | if (empty) |
---|
| 78 | { |
---|
| 79 | ptrValue = new T(*type.ptrValue) ; |
---|
| 80 | empty=false ; |
---|
| 81 | } |
---|
| 82 | else *ptrValue = *type.ptrValue ; |
---|
[308] | 83 | } |
---|
| 84 | } |
---|
[369] | 85 | |
---|
| 86 | template <typename T> |
---|
| 87 | T& CType<T>::get(void) |
---|
| 88 | { |
---|
| 89 | checkEmpty(); |
---|
| 90 | return *ptrValue ; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | template <typename T> |
---|
| 94 | const T& CType<T>::get(void) const |
---|
| 95 | { |
---|
| 96 | checkEmpty(); |
---|
| 97 | return *ptrValue ; |
---|
| 98 | } |
---|
[308] | 99 | |
---|
[369] | 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 | checkEmpty(); |
---|
| 125 | return *ptrValue ; |
---|
| 126 | } |
---|
[308] | 127 | |
---|
[369] | 128 | template <typename T> |
---|
| 129 | CType<T>* CType<T>::_clone(void) const |
---|
| 130 | { |
---|
| 131 | checkEmpty(); |
---|
| 132 | return new CType(*this) ; |
---|
| 133 | } |
---|
| 134 | |
---|
[308] | 135 | |
---|
| 136 | template <typename T> |
---|
[369] | 137 | void CType<T>::_fromString(const string& str) |
---|
[308] | 138 | { |
---|
| 139 | istringstream iss(str); |
---|
[369] | 140 | allocate() ; |
---|
[308] | 141 | iss>>*ptrValue ; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | template <typename T> |
---|
[369] | 145 | size_t CType<T>::_size(void) const |
---|
[308] | 146 | { |
---|
| 147 | return sizeof(T) ; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | template <typename T> |
---|
[369] | 151 | bool CType<T>::_isEmpty(void) const |
---|
[308] | 152 | { |
---|
[369] | 153 | return empty ; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | template <typename T> |
---|
| 157 | string CType<T>::_toString(void) const |
---|
| 158 | { |
---|
[308] | 159 | ostringstream oss; |
---|
[369] | 160 | checkEmpty(); |
---|
[308] | 161 | oss<<*ptrValue ; |
---|
| 162 | return oss.str() ; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | template <typename T> |
---|
[369] | 166 | bool CType<T>::_toBuffer(CBufferOut& buffer) const |
---|
[308] | 167 | { |
---|
[369] | 168 | checkEmpty(); |
---|
[308] | 169 | return buffer.put(*ptrValue) ; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | template <typename T> |
---|
[369] | 173 | bool CType<T>::_fromBuffer(CBufferIn& buffer) |
---|
[308] | 174 | { |
---|
[369] | 175 | allocate() ; |
---|
[308] | 176 | return buffer.get(*ptrValue) ; |
---|
| 177 | } |
---|
| 178 | |
---|
[369] | 179 | |
---|
[308] | 180 | template <typename T> |
---|
[369] | 181 | void CType<T>::allocate(void) |
---|
[308] | 182 | { |
---|
[369] | 183 | if (empty) |
---|
| 184 | { |
---|
| 185 | ptrValue = new T ; |
---|
| 186 | empty=false ; |
---|
| 187 | } |
---|
[308] | 188 | } |
---|
[369] | 189 | |
---|
[308] | 190 | template <typename T> |
---|
[369] | 191 | void CType<T>::_reset(void) |
---|
[308] | 192 | { |
---|
[369] | 193 | if (!empty) |
---|
| 194 | { |
---|
| 195 | delete ptrValue ; |
---|
| 196 | empty=true ; |
---|
| 197 | } |
---|
[308] | 198 | } |
---|
| 199 | |
---|
| 200 | template <typename T> |
---|
[369] | 201 | void CType<T>::checkEmpty(void) const |
---|
[308] | 202 | { |
---|
[369] | 203 | if (empty) ERROR("template <typename T> void CTypef<T>::checkEmpty(void) const", <<"Type is not initialized") ; |
---|
| 204 | } |
---|
[308] | 205 | |
---|
| 206 | |
---|
| 207 | template <typename T> |
---|
| 208 | CBufferOut& operator<<(CBufferOut& buffer, const CType<T>& type) |
---|
| 209 | { |
---|
| 210 | if (!type.toBuffer(buffer)) ERROR("CBuffer& operator<<(CBuffer& buffer, CType<T>& type)", |
---|
| 211 | <<"Buffer remain size is to low for size type") ; |
---|
| 212 | return buffer ; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | |
---|
| 216 | template <typename T> |
---|
| 217 | CBufferOut& operator<<(CBufferOut& buffer, const T& type) |
---|
| 218 | { |
---|
| 219 | if (!CType<T>(type).toBuffer(buffer)) ERROR("operator<<(CBuffer& buffer, const T& type)", |
---|
| 220 | <<"Buffer remain size is to low for size type") ; |
---|
| 221 | return buffer ; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | template <typename T> |
---|
[369] | 225 | CBufferIn& operator>>(CBufferIn& buffer, CType<T>& type) |
---|
[308] | 226 | { |
---|
[369] | 227 | if (! type.fromBuffer(buffer)) ERROR("CBuffer& operator<<(CBuffer& buffer, CType<T>& type)", |
---|
[308] | 228 | <<"Buffer remain size is to low for size type") ; |
---|
| 229 | return buffer ; |
---|
| 230 | } |
---|
| 231 | |
---|
[401] | 232 | /* |
---|
[308] | 233 | template <typename T> |
---|
| 234 | CMessage& operator<<(CMessage& msg, const CType<T>& type) |
---|
| 235 | { |
---|
[369] | 236 | msg.push(*type.clone()) ; |
---|
[308] | 237 | return msg ; |
---|
| 238 | } |
---|
[401] | 239 | */ |
---|
[308] | 240 | template <typename T> |
---|
| 241 | CMessage& operator<<(CMessage& msg, const T& type) |
---|
| 242 | { |
---|
[401] | 243 | msg.push(CType<T>(type)) ; |
---|
[308] | 244 | return msg ; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | #endif |
---|