dataIStream.h

00001 
00002 /* Copyright (c) 2007-2009, Stefan Eilemann <eile@equalizergraphics.com> 
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License version 2.1 as published
00006  * by the Free Software Foundation.
00007  *  
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00011  * details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016  */
00017 
00018 #ifndef EQNET_DATAISTREAM_H
00019 #define EQNET_DATAISTREAM_H
00020 
00021 #include <eq/net/object.h>  // nested VERSION_NONE enum
00022 #include <eq/net/types.h>
00023 #include <eq/base/buffer.h> // member
00024 
00025 #include <iostream>
00026 #include <vector>
00027 
00028 namespace eq
00029 {
00030 namespace net
00031 {
00033     class DataIStream
00034     {
00035     public:
00038         DataIStream();
00039         virtual ~DataIStream();
00040 
00042         virtual size_t nRemainingBuffers() const = 0;
00043 
00044         virtual uint32_t getVersion() const { return Object::VERSION_NONE; }
00045 
00046         virtual void reset();
00048 
00052         template< typename T >
00053         DataIStream& operator >> ( T& value )
00054             { read( &value, sizeof( value )); return *this; }
00055 
00057         template< typename T >
00058         DataIStream& operator >> ( std::vector< T >& value )
00059         {
00060             uint64_t nElems = 0;
00061             read( &nElems, sizeof( nElems ));
00062             value.resize( nElems );
00063             for( uint64_t i = 0; i < nElems; i++ )
00064                 (*this) >> value[i];
00065 
00066             return *this; 
00067         }
00068 
00070         EQ_EXPORT void read( void* data, uint64_t size );
00071 
00083         EQ_EXPORT const void*    getRemainingBuffer();
00084 
00086         EQ_EXPORT uint64_t       getRemainingBufferSize();
00087 
00089         EQ_EXPORT void           advanceBuffer( const uint64_t offset ); 
00091  
00092     protected:
00093         virtual bool getNextBuffer( const uint8_t** buffer, uint64_t* size ) =0;
00094 
00095     private:
00097         const uint8_t* _input;
00099         uint64_t _inputSize;
00101         uint64_t  _position;
00102 
00107         bool _checkBuffer();
00108 
00110         template< typename T >
00111         DataIStream& _readFlatVector ( std::vector< T >& value )
00112         {
00113             uint64_t nElems = 0;
00114             read( &nElems, sizeof( nElems ));
00115             value.resize( nElems );
00116             if( nElems > 0 )
00117                 read( &value.front(), nElems * sizeof( T ));            
00118             return *this; 
00119         }
00120     };
00121 }
00122 }
00123 
00124 namespace eq
00125 {
00126 namespace net
00127 {
00131     template<>
00132     inline DataIStream& DataIStream::operator >> ( std::string& str )
00133     { 
00134         uint64_t nElems = 0;
00135         read( &nElems, sizeof( nElems ));
00136         EQASSERT( nElems <= getRemainingBufferSize( ));
00137         if( nElems == 0 )
00138             str.clear();
00139         else
00140         {
00141             str.assign( static_cast< const char* >( getRemainingBuffer( )), 
00142                         nElems );
00143             advanceBuffer( nElems );
00144         }
00145         return *this; 
00146     }
00147 
00149     template<>
00150     inline DataIStream& DataIStream::operator >> ( base::UUID& id )
00151     { 
00152         read( &id, sizeof( id ));
00153         id.convertToHost();
00154         return *this;
00155     }
00156 
00158     template<>
00159     inline DataIStream& DataIStream::operator >> (std::vector< uint8_t >& value)
00160     {
00161         return _readFlatVector( value );
00162     }
00163 
00165     template<>
00166     inline DataIStream& DataIStream::operator >> (std::vector< uint32_t>& value)
00167     {
00168         return _readFlatVector( value );
00169     }
00170 
00172     template<>
00173     inline DataIStream& DataIStream::operator >> (std::vector< int32_t >& value)
00174     {
00175         return _readFlatVector( value );
00176     }
00177 
00179     template<>
00180     inline DataIStream& DataIStream::operator >> (std::vector< uint64_t>& value)
00181     {
00182         return _readFlatVector( value );
00183     }
00184 
00186     template<>
00187     inline DataIStream& DataIStream::operator >> (std::vector< int64_t >& value)
00188     {
00189         return _readFlatVector( value );
00190     }
00191 
00193     template<>
00194     inline DataIStream& DataIStream::operator >> ( std::vector< float >& value )
00195     {
00196         return _readFlatVector( value );
00197     }
00198 
00200     template<>
00201     inline DataIStream& DataIStream::operator >> ( std::vector< double >& value)
00202     {
00203         return _readFlatVector( value );
00204     }
00206 }
00207 }
00208 
00209 #endif //EQNET_DATAISTREAM_H
Generated on Mon Aug 10 18:58:32 2009 for Equalizer 0.9 by  doxygen 1.5.8