Collage  1.0.1
Object-Oriented C++ Network Library
dataIStream.h
1 
2 /* Copyright (c) 2007-2012, Stefan Eilemann <eile@equalizergraphics.com>
3  * 2009-2010, Cedric Stalder <cedric.stalder@gmail.com>
4  * 2012, Daniel Nachbaur <danielnachbaur@gmail.com>
5  *
6  * This file is part of Collage <https://github.com/Eyescale/Collage>
7  *
8  * This library is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License version 2.1 as published
10  * by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #ifndef CO_DATAISTREAM_H
23 #define CO_DATAISTREAM_H
24 
25 #include <co/api.h>
26 #include <co/array.h> // used inline
27 #include <co/types.h>
28 
29 #include <lunchbox/stdExt.h>
30 
31 #include <map>
32 #include <set>
33 #include <vector>
34 
35 namespace co
36 {
37 namespace detail { class DataIStream; }
38 
41 {
42 public:
46  virtual size_t nRemainingBuffers() const = 0;
47 
48  virtual uint128_t getVersion() const = 0;
49  virtual void reset() { _reset(); }
50  void setSwapping( const bool onOff );
51  CO_API bool isSwapping() const;
52  DataIStream& operator = ( const DataIStream& rhs );
53 
54 
58  template< class T > DataIStream& operator >> ( T& value )
59  { _read( &value, sizeof( value )); _swap( value ); return *this; }
60 
62  template< class T > DataIStream& operator >> ( Array< T > array );
63 
65  template< class T > DataIStream& operator >> ( lunchbox::Buffer< T >& );
66 
68  template< class T > DataIStream& operator >> ( std::vector< T >& );
69 
71  template< class K, class V >
72  DataIStream& operator >> ( std::map< K, V >& );
73 
75  template< class T > DataIStream& operator >> ( std::set< T >& );
76 
78  template< class K, class V >
79  DataIStream& operator >> ( stde::hash_map< K, V >& );
80 
82  template< class T > DataIStream& operator >> ( stde::hash_set< T >& );
83 
89 # ifdef CO_IGNORE_BYTESWAP
90 
91  template< class T > static void swap( T& ) { /* nop */ }
92 # else
93 
94  template< class T > static void swap( T& v ) { lunchbox::byteswap(v); }
95 # endif
96 
108  template< typename O, typename C >
109  void deserializeChildren( O* object, const std::vector< C* >& old,
110  std::vector< C* >& result );
111 
131  CO_API const void* getRemainingBuffer( const uint64_t size );
132 
137  CO_API uint64_t getRemainingBufferSize();
138 
140  bool hasData() { return _checkBuffer(); }
141 
143  CO_API virtual NodePtr getMaster() = 0;
145 
146 protected:
149  CO_API DataIStream( const bool swap );
150  DataIStream( const DataIStream& );
151  CO_API virtual ~DataIStream();
152 
153  virtual bool getNextBuffer( uint32_t& compressor, uint32_t& nChunks,
154  const void** chunkData, uint64_t& size )=0;
156 
157 private:
158  detail::DataIStream* const _impl;
159 
161  CO_API void _read( void* data, uint64_t size );
162 
167  CO_API bool _checkBuffer();
168  CO_API void _reset();
169 
170  const uint8_t* _decompress( const void* data, const uint32_t name,
171  const uint32_t nChunks,
172  const uint64_t dataSize );
173 
175  template< class T >
176  DataIStream& _readFlatVector ( std::vector< T >& value )
177  {
178  uint64_t nElems = 0;
179  *this >> nElems;
180  LBASSERTINFO( nElems < LB_BIT48,
181  "Out-of-sync co::DataIStream: " << nElems << " elements?" );
182  value.resize( size_t( nElems ));
183  if( nElems > 0 )
184  *this >> Array< T >( &value.front(), nElems );
185  return *this;
186  }
187 
189  template< class T > void _swap( T& value ) const
190  { if( isSwapping( )) swap( value ); }
191 
193  template< class T > void _swap( Array< T > array ) const
194  {
195  if( !isSwapping( ))
196  return;
197 #pragma omp parallel for
198  for( ssize_t i = 0; i < ssize_t( array.num ); ++i )
199  swap( array.data[i] );
200  }
201 };
202 }
203 
204 #include "dataIStream.ipp" // template implementation
205 
206 #endif //CO_DATAISTREAM_H
A wrapper to (de)serialize arrays.
Definition: array.h:28
CO_API const void * getRemainingBuffer(const uint64_t size)
static void swap(T &v)
Byte-swap a plain data item.
Definition: dataIStream.h:94
lunchbox::RefPtr< Node > NodePtr
A reference pointer for Node pointers.
Definition: types.h:80
A std::istream-like input data stream for binary data.
Definition: dataIStream.h:40
virtual CO_API NodePtr getMaster()=0
CO_API uint64_t getRemainingBufferSize()
DataIStream & operator>>(T &value)
Read a plain data item.
Definition: dataIStream.h:58