#ifndef __XIOS_TYPE_REF_IMPL__ #define __XIOS_TYPE_REF_IMPL__ #include "xios_spl.hpp" #include "exception.hpp" #include "buffer_in.hpp" #include "buffer_out.hpp" #include "message.hpp" #include "type.hpp" namespace xios { using namespace std; template CType_ref::CType_ref(void) { empty=true ; } template CType_ref::CType_ref(T& val) { empty=true ; set_ref(val) ; } template CType_ref::CType_ref(CType& type) { empty=true ; set_ref(type) ; } template CType_ref::CType_ref(const CType_ref& type) { empty=true ; set_ref(type) ; } template void CType_ref::set_ref(T& val) { ptrValue=&val ; empty=false ; } template void CType_ref::set_ref(CType& type) { ptrValue=&type.get() ; empty=false ; } template void CType_ref::set_ref(const CType_ref& type) { ptrValue=type.ptrValue ; empty=type.empty ; } template void CType_ref::set(const T& val) const { checkEmpty() ; *ptrValue=val ; } template void CType_ref::set(const CType& type) const { checkEmpty() ; *ptrValue=type.get() ; } template void CType_ref::set(const CType_ref& type) const { checkEmpty() ; *ptrValue=type.get() ; } template T& CType_ref::get(void) const { checkEmpty() ; return *ptrValue ; } template const CType_ref& CType_ref::operator = (T& val) const { set(val) ; return *this ; } template const CType_ref& CType_ref::operator = (CType& type) const { set(type) ; return *this ; } template const CType_ref& CType_ref::operator = (const CType_ref& type) const { set(type) ; return *this ; } template CType_ref::operator T&() const { checkEmpty() ; return *ptrValue ; } template CType_ref* CType_ref::_clone(void) const { checkEmpty() ; return new CType_ref(*this) ; } template void CType_ref::_fromString(const string& str) const { istringstream iss(str); checkEmpty() ; iss>>*ptrValue ; } template void CType_ref::_fromString(const string& str) { istringstream iss(str); checkEmpty() ; iss>>*ptrValue ; } template string CType_ref::_toString(void) const { ostringstream oss; checkEmpty() ; oss<<*ptrValue ; return oss.str() ; } template bool CType_ref::_toBuffer(CBufferOut& buffer) const { checkEmpty() ; return buffer.put(*ptrValue) ; } template bool CType_ref::_fromBuffer(CBufferIn& buffer) { checkEmpty() ; return buffer.get(*ptrValue) ; } template bool CType_ref::_fromBuffer(CBufferIn& buffer) const { checkEmpty() ; return buffer.get(*ptrValue) ; } template size_t CType_ref::_size(void) const { return sizeof(T) ; } template bool CType_ref::_isEmpty(void) const { return empty ; } template void CType_ref::_reset(void) { empty=true ; } template void CType_ref::checkEmpty(void) const { if (empty) ERROR("template void CType_ref::checkEmpty(void)", <<"Data reference is not initialized.") ; } template bool operator==(const CType_ref& lhs, const T& rhs) { if (lhs.isEmpty()) return false; return (*lhs.ptrValue == rhs); } template bool operator==(const T& lhs, const CType_ref& rhs) { return (rhs == lhs); } template bool operator==(const CType_ref& lhs, const CType_ref& 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_ref& 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, T& type) { if (!CType_ref(type).toBuffer(buffer)) ERROR("CBufferOut& operator<<(CBufferOut& buffer, T& type)", << "Not enough free space in buffer to queue the data."); return buffer ; } template CBufferIn& operator>>(CBufferIn& buffer, T& type) { if (! CType_ref(type).fromBuffer(buffer)) ERROR(" template CBufferIn& operator>>(CBufferIn& buffer, T& type)", << "Not enough data in buffer to unqueue the data."); return buffer ; } template CBufferIn& operator>>(CBufferIn& buffer, const CType_ref& 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, T& type) { msg.push(CType_ref(type)) ; return msg ; } } #endif