Collage  1.5.0
High-performance C++ library for developing object-oriented distributed applications.
dataIStream.h
1 
2 /* Copyright (c) 2007-2016, Stefan Eilemann <eile@equalizergraphics.com>
3  * Cedric Stalder <cedric.stalder@gmail.com>
4  * 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/types.h>
27 #include <lunchbox/array.h> // used inline
28 #include <lunchbox/bitOperation.h>
29 #include <lunchbox/stdExt.h>
30 
31 #include <boost/type_traits.hpp>
32 #include <map>
33 #include <set>
34 #include <vector>
35 
36 namespace co
37 {
38 namespace detail { class DataIStream; }
39 
42 {
43 public:
47  virtual size_t nRemainingBuffers() const = 0;
48 
49  virtual uint128_t getVersion() const = 0;
50  virtual void reset() { _reset(); }
51  void setSwapping( const bool onOff );
52  CO_API bool isSwapping() const;
53  DataIStream& operator = ( const DataIStream& rhs );
54 
55 
59  template< typename T > T read()
60  { T value; *this >> value; return value; }
61 
63  template< class T > DataIStream& operator >> ( T& value )
64  { _read( value, boost::has_trivial_copy< T >( )); return *this; }
65 
67  template< class T > DataIStream& operator >> ( Array< T > array )
68  { _readArray( array, boost::has_trivial_copy< T >( )); return *this; }
69 
74  template< class T > DataIStream& operator >> ( lunchbox::RefPtr< T >& );
75 
77  template< class T > DataIStream& operator >> ( lunchbox::Buffer< T >& );
78 
80  template< class T > DataIStream& operator >> ( std::vector< T >& );
81 
83  template< class K, class V >
84  DataIStream& operator >> ( std::map< K, V >& );
85 
87  template< class T > DataIStream& operator >> ( std::set< T >& );
88 
90  template< class K, class V >
91  DataIStream& operator >> ( stde::hash_map< K, V >& );
92 
94  template< class T > DataIStream& operator >> ( stde::hash_set< T >& );
95 
101 # ifdef CO_IGNORE_BYTESWAP
102 
103  template< class T > static void swap( T& ) { /* nop */ }
104 # else
105 
106  template< class T > static void swap( T& v ) { lunchbox::byteswap(v); }
107 # endif
108 
120  template< typename O, typename C >
121  void deserializeChildren( O* object, const std::vector< C* >& old,
122  std::vector< C* >& result );
123 
143  CO_API const void* getRemainingBuffer( const uint64_t size );
144 
149  CO_API uint64_t getRemainingBufferSize();
150 
152  bool wasUsed() const;
153 
155  bool hasData() { return _checkBuffer(); }
156 
158  CO_API virtual NodePtr getRemoteNode() const = 0;
159 
161  CO_API virtual LocalNodePtr getLocalNode() const = 0;
163 
164 protected:
167  CO_API explicit DataIStream( const bool swap );
168  explicit DataIStream( const DataIStream& );
169  CO_API virtual ~DataIStream();
170 
171  virtual bool getNextBuffer( uint32_t& compressor, uint32_t& nChunks,
172  const void** chunkData, uint64_t& size )=0;
174 
175 private:
176  detail::DataIStream* const _impl;
177 
179  CO_API void _read( void* data, uint64_t size );
180 
185  CO_API bool _checkBuffer();
186  CO_API void _reset();
187 
188  const uint8_t* _decompress( const void* data, const uint32_t name,
189  const uint32_t nChunks,
190  const uint64_t dataSize );
191 
193  template< class T >
194  DataIStream& _readFlatVector ( std::vector< T >& value )
195  {
196  uint64_t nElems = 0;
197  *this >> nElems;
198  LBASSERTINFO( nElems < LB_BIT48,
199  "Out-of-sync DataIStream: " << nElems << " elements?" );
200  value.resize( size_t( nElems ));
201  if( nElems > 0 )
202  *this >> Array< T >( &value.front(), nElems );
203  return *this;
204  }
205 
207  template< class T > void _swap( T& value ) const
208  { if( isSwapping( )) swap( value ); }
209 
211  template< class T > void _read( T& value, const boost::true_type& )
212  { _read( &value, sizeof( value )); _swap( value ); }
213 
215  template< class T > void _read( T& value, const boost::false_type& )
216  { _readSerializable( value, boost::is_base_of< servus::Serializable, T >( ));}
217 
219  template< class T > void _readSerializable(T& value, const boost::true_type&);
220 
222  template< class T > void _readArray( Array< T >, const boost::true_type& );
223 
225  template< class T > void _readArray( Array< T >, const boost::false_type& );
226 
228  template< class T > void _swap( Array< T > array ) const
229  {
230  if( !isSwapping( ))
231  return;
232 #pragma omp parallel for
233  for( ssize_t i = 0; i < ssize_t( array.num ); ++i )
234  swap( array.data[i] );
235  }
236 };
237 }
238 
239 #include "dataIStream.ipp" // template implementation
240 
241 #endif //CO_DATAISTREAM_H
Defines export visibility macros for library Collage.
static void swap(T &v)
Byte-swap a plain data item.
Definition: dataIStream.h:106
virtual CO_API LocalNodePtr getLocalNode() const =0
lunchbox::RefPtr< LocalNode > LocalNodePtr
A reference pointer for LocalNode pointers.
Definition: types.h:89
lunchbox::RefPtr< Node > NodePtr
A reference pointer for Node pointers.
Definition: types.h:85
Object-oriented network library.
Definition: barrier.h:27
CO_API const void * getRemainingBuffer(const uint64_t size)
DataIStream & operator>>(T &value)
Read a plain data item.
Definition: dataIStream.h:63
CO_API uint64_t getRemainingBufferSize()
A std::istream-like input data stream for binary data.
Definition: dataIStream.h:41
virtual CO_API NodePtr getRemoteNode() const =0