#ifndef __XIOS_TYPE_IMPL__ #define __XIOS_TYPE_IMPL__ #include "xios_spl.hpp" #include "exception.hpp" #include "buffer_in.hpp" #include "buffer_out.hpp" #include "message.hpp" namespace xios { using namespace std; template CType::CType(void) { empty=true ; } template CType::CType(const T& val) { empty=true ; set(val) ; } template CType::CType(const CType& type) { empty=true ; set(type) ; } template CType::CType(const CType_ref& type) { empty=true ; set(type) ; } template void CType::set(const T& val) { if (empty) { ptrValue = new T(val) ; empty=false ; } else *ptrValue = val ; } template void CType::set(const CType& type) { if (type.isEmpty()) reset() ; else { if (empty) { ptrValue = new T(*type.ptrValue) ; empty=false ; } else *ptrValue = *type.ptrValue ; } } template void CType::set(const CType_ref& type) { if (type.isEmpty()) reset() ; else { if (empty) { ptrValue = new T(*type.ptrValue) ; empty=false ; } else *ptrValue = *type.ptrValue ; } } template T& CType::get(void) { checkEmpty(); return *ptrValue ; } template const T& CType::get(void) const { checkEmpty(); return *ptrValue ; } template CType& CType::operator = (const T& val) { set(val) ; return *this ; } template CType& CType::operator = (const CType& type) { set(type) ; return *this ; } template CType& CType::operator = (const CType_ref& type) { set(type) ; return *this ; } template CType::operator T&() { try { checkEmpty(); } catch (xios::CException & exc) { StdString msg("Data is not initialized\n"); ERROR("template void CType::checkEmpty(void) const", << msg); } return *ptrValue ; } template CType::operator const T&() const { checkEmpty(); return *ptrValue ; } template CType* CType::_clone(void) const { checkEmpty(); return new CType(*this) ; } template void CType::_fromString(const string& str) { istringstream iss(str); allocate() ; iss>>*ptrValue ; } template size_t CType::_size(void) const { return sizeof(T) ; } template bool CType::_isEmpty(void) const { return empty ; } template string CType::_toString(void) const { ostringstream oss; checkEmpty(); oss<<*ptrValue ; return oss.str() ; } template bool CType::_toBuffer(CBufferOut& buffer) const { checkEmpty(); return buffer.put(*ptrValue) ; } template bool CType::_fromBuffer(CBufferIn& buffer) { allocate() ; return buffer.get(*ptrValue) ; } template void CType::allocate(void) { if (empty) { ptrValue = new T ; empty=false ; } } template void CType::_reset(void) { if (!empty) { delete ptrValue ; empty=true ; } } template void CType::checkEmpty(void) const { // if (empty) ERROR("template void CType::checkEmpty(void) const", << "Data is not initialized") ; if (empty) throw CException(); } template bool operator==(const CType& lhs, const T& rhs) { if (lhs.isEmpty()) return false; return (*lhs.ptrValue == rhs); } template bool operator==(const T& lhs, const CType& rhs) { return (rhs == lhs); } template bool operator==(const CType& lhs, const CType& rhs) { if ((lhs.isEmpty() && !rhs.isEmpty()) || (!lhs.isEmpty() && rhs.isEmpty())) return false; if (lhs.isEmpty() && rhs.isEmpty()) return true; return (*lhs.ptrValue == *rhs.ptrValue); } template CBufferOut& operator<<(CBufferOut& buffer, const CType& type) { if (!type.toBuffer(buffer)) ERROR("CBuffer& operator<<(CBuffer& buffer, CType& type)", << "Not enough free space in buffer to queue the data."); return buffer ; } template CBufferOut& operator<<(CBufferOut& buffer, const T& type) { if (!CType(type).toBuffer(buffer)) ERROR("operator<<(CBuffer& buffer, const T& type)", << "Not enough free space in buffer to queue the data."); return buffer ; } template CBufferIn& operator>>(CBufferIn& buffer, CType& type) { if (! type.fromBuffer(buffer)) ERROR("CBuffer& operator<<(CBuffer& buffer, CType& type)", << "Not enough data in buffer to unqueue the data."); return buffer ; } template CMessage& operator<<(CMessage& msg, const T& type) { msg.push(CType(type)) ; return msg ; } } #endif