/*! \file xstream/xdr.h * * \brief C++ iostream like interface to read and write xdr streams * * \arg endian safe * \arg needs floats and doubles stores in IEEE format in hardware * (most architecture work this way, \c SPARC is the only exception I know) * */ #ifndef __XSTREAM_XDR #define __XSTREAM_XDR #include #include #include //for pair definition #include #include #include #include #include namespace{ #if HAVE_INTTYPES_H # include #else # if HAVE_STDINT_H # include # endif # error "I need inttypes.h or stdint.h to exist" #endif } /*! * \brief Library top namespace */ namespace xstream{ /*! * \brief xdr related objects */ namespace xdr{ using std::string; using std::streambuf; using std::pair; using std::vector; /*! * \brief Output xdr stream class * * Just a thin wrapper for streambuf to serialize all the datatypes * */ class ostream { private: streambuf *_sb; public: /*! * \brief construct using a streambuf * */ ostream(streambuf* sb):_sb(sb){}; /*! * \brief construct using an ostream * */ ostream(const std::ostream &os):_sb(os.rdbuf()){}; ostream& operator<<(const int32_t v); //ostream& operator<<(const int v); ostream& operator<<(const uint32_t v); //ostream& operator<<(const unsigned int v); ostream& operator<<(const int64_t v); //ostream& operator<<(const long int v); ostream& operator<<(const uint64_t v); //ostream& operator<<(const unsigned long v); ostream& operator<<(const float v); ostream& operator<<(const double v); ostream& operator<<(const string &v); /*! * \brief Serializes STL pair containers to xdr * */ template ostream& operator<<(const pair &p){ (*this)<<(p.first); (*this)<<(p.second); return *this; } /*! * \brief Serializes STL vector containers to xdr * */ template ostream& operator<<( const vector &t){ uint32_t sz = static_cast( t.size() ); (*this)<::const_iterator cit = t.begin(); const typename vector::const_iterator cend = t.end(); while(cit!=cend){ (*this)<<*(cit++); } return (*this); } }; /*! * \brief Input xdr stream class * * Just a thin wrapper for streambuf to deserialize all the datatypes * */ class istream { private: streambuf *_sb; public: /*! * \brief construct using a streambuf * */ istream(streambuf* sb):_sb(sb){}; /*! * \brief construct using an istream * */ istream(const std::istream &os):_sb(os.rdbuf()){}; istream& operator>>(int32_t &v); //istream& operator>>(int &v); istream& operator>>(uint32_t &v); //istream& operator>>(unsigned int &v); istream& operator>>(int64_t &v); //istream& operator>>(long int &v); istream& operator>>(uint64_t &v); //istream& operator>>(unsigned long int &v); istream& operator>>(float &v); istream& operator>>(double &v); istream& operator>>(string &v); /*! * \brief Deserializes STL pair containers from xdr * */ template istream& operator>>(pair &p){ (*this)>>(p.first); (*this)>>(p.second); return *this; } /*! * \brief Deserializes STL vector containers from xdr * */ template istream& operator>>( vector &t){ uint32_t sz; T val; //maybe I could allocate all the necessary entries here (*this)>>sz; std::clog<<"VECTOR SIZE: "<>val; std::clog<<"read: "<