00001 #ifndef _BOOST_RPC_DATASTREAM_HPP_
00002 #define _BOOST_RPC_DATASTREAM_HPP_
00003 #include <boost/type_traits/is_fundamental.hpp>
00004 #include <boost/static_assert.hpp>
00005 #include <stdint.h>
00006 namespace boost { namespace rpc {
00007
00008 struct pack_size{};
00009
00017 template<typename T>
00018 struct datastream
00019 {
00020 datastream( T start, uint32_t s )
00021 :m_start(start),m_pos(start),m_end(start+s){};
00022
00023 template<typename DATA>
00024 inline datastream& operator<<(const DATA& d)
00025 {
00026 BOOST_STATIC_ASSERT( boost::is_fundamental<DATA>::value );
00027 write( (const char*)&d, sizeof(d) );
00028 return *this;
00029 }
00030 template<typename DATA>
00031 inline datastream& operator>>(DATA& d)
00032 {
00033 BOOST_STATIC_ASSERT( boost::is_fundamental<DATA>::value );
00034 read((char*)&d, sizeof(d) );
00035 return *this;
00036 }
00037
00038 inline void skip( uint32_t s ){ m_pos += s; }
00039 inline bool read( char* d, uint32_t s ) {
00040 if( m_end - m_pos >= (int32_t)s ) {
00041 memcpy( d, m_pos, s );
00042 m_pos += s;
00043 return true;
00044 }
00045 return false;
00046 }
00047 inline bool write( const char* d, uint32_t s ) {
00048 if( m_end - m_pos >= (int32_t)s ) {
00049 memcpy( m_pos, d, s );
00050 m_pos += s;
00051 return true;
00052 }
00053 return false;
00054 }
00055 inline bool putc(char c) {
00056 if( m_pos < m_end ) {
00057 *m_pos = c;
00058 ++m_pos;
00059 return true;
00060 }
00061 return false;
00062 }
00063 inline bool getc( unsigned char& c ) { return getc( *(char*)&c ); }
00064 inline bool getc( char& c ) {
00065 if( m_pos < m_end ) {
00066 c = *m_pos;
00067 ++m_pos;
00068 return true;
00069 }
00070 ++m_pos;
00071 return false;
00072 }
00073 T pos()const { return m_pos; }
00074 inline bool valid()const { return m_pos <= m_end && m_pos >= m_start; }
00075 inline bool seekp(uint32_t p) { m_pos = m_start + p; return m_pos <= m_end; }
00076 inline uint32_t tellp()const { return m_pos - m_start; }
00077 inline uint32_t remaining()const { return m_end - m_pos; }
00078 private:
00079 T m_start;
00080 T m_pos;
00081 T m_end;
00082 };
00083
00084 template<>
00085 struct datastream<size_t>
00086 {
00087 datastream( size_t init_size = 0):m_size(init_size){};
00088 template<typename DATA>
00089 inline datastream& operator<<(const DATA& d) {
00090 BOOST_STATIC_ASSERT( boost::is_fundamental<DATA>::value );
00091 m_size += sizeof(d);
00092 return *this;
00093 }
00094 inline bool skip( uint32_t s ) {
00095 m_size += s;
00096 return true;
00097 }
00098 inline bool write( const char* d, uint32_t s ) {
00099 m_size += s;
00100 return true;
00101 }
00102 inline bool putc(char c) {
00103 ++m_size;
00104 return true;
00105 }
00106 inline bool valid()const { return true; }
00107 inline bool seekp(uint32_t p) { m_size = p; }
00108 inline uint32_t tellp()const { return m_size; }
00109 inline uint32_t remaining()const { return 0; }
00110 private:
00111 uint32_t m_size;
00112 };
00113
00114
00115
00116
00117 } }
00118
00119 #endif