Collage  1.6.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( CompressorInfo& info, 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 CompressorInfo& info,
189  uint32_t nChunks, uint64_t dataSize );
190 
192  template< class T >
193  DataIStream& _readFlatVector ( std::vector< T >& value )
194  {
195  uint64_t nElems = 0;
196  *this >> nElems;
197  LBASSERTINFO( nElems < LB_BIT48,
198  "Out-of-sync DataIStream: " << nElems << " elements?" );
199  value.resize( size_t( nElems ));
200  if( nElems > 0 )
201  *this >> Array< T >( &value.front(), nElems );
202  return *this;
203  }
204 
206  template< class T > void _swap( T& value ) const
207  { if( isSwapping( )) swap( value ); }
208 
210  template< class T > void _read( T& value, const boost::true_type& )
211  { _read( &value, sizeof( value )); _swap( value ); }
212 
214  template< class T > void _read( T& value, const boost::false_type& )
215  { _readSerializable( value, boost::is_base_of< servus::Serializable, T >( ));}
216 
218  template< class T > void _readSerializable(T& value, const boost::true_type&);
219 
221  template< class T > void _readArray( Array< T >, const boost::true_type& );
222 
224  template< class T > void _readArray( Array< T >, const boost::false_type& );
225 
227  template< class T > void _swap( Array< T > array ) const
228  {
229  if( !isSwapping( ))
230  return;
231 #pragma omp parallel for
232  for( ssize_t i = 0; i < ssize_t( array.num ); ++i )
233  swap( array.data[i] );
234  }
235 };
236 }
237 
238 #include "dataIStream.ipp" // template implementation
239 
240 #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
lunchbox::RefPtr< LocalNode > LocalNodePtr
A reference pointer for LocalNode pointers.
Definition: types.h:90
lunchbox::RefPtr< Node > NodePtr
A reference pointer for Node pointers.
Definition: types.h:86
Object-oriented network library.
Definition: barrier.h:27
A std::istream-like input data stream for binary data.
Definition: dataIStream.h:41