Collage  1.4.0
High-performance C++ library for developing object-oriented distributed applications.
dataOStream.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_DATAOSTREAM_H
23 #define CO_DATAOSTREAM_H
24 
25 #include <co/api.h>
26 #include <co/types.h>
27 
28 #include <lunchbox/array.h> // used inline
29 #include <lunchbox/stdExt.h>
30 
31 #include <boost/noncopyable.hpp>
32 #include <boost/type_traits.hpp>
33 #include <map>
34 #include <set>
35 #include <vector>
36 
37 namespace co
38 {
39 namespace detail { class DataOStream; }
40 namespace DataStreamTest { class Sender; }
41 
48 class DataOStream : public boost::noncopyable
49 {
50 public:
54  CO_API void disable();
55 
57  void enableSave();
58 
60  void disableSave();
61 
63  CO_API bool hasSentData() const;
64 
66  CO_API const Connections& getConnections() const;
67 
69  DataOStream& streamDataHeader( DataOStream& os );
70 
72  void sendBody( ConnectionPtr connection, const void* data,
73  const uint64_t dataSize );
74 
76  uint64_t getCompressedDataSize() const;
78 
82  template< class T > DataOStream& operator << ( const T& value )
83  { _write( value, boost::has_trivial_copy< T >( )); return *this; }
84 
86  template< class T > DataOStream& operator << ( const Array< T > array )
87  { _writeArray( array, boost::has_trivial_copy<T>( )); return *this; }
88 
93  template< class T >
94  DataOStream& operator << ( const lunchbox::RefPtr< T >& ptr );
95 
97  template< class T >
98  DataOStream& operator << ( const lunchbox::Buffer< T >& buffer );
99 
101  template< class T >
102  DataOStream& operator << ( const lunchbox::Request<T>& request )
103  { return (*this) << request.getID(); }
104 
106  template< class T >
107  DataOStream& operator << ( const std::vector< T >& value );
108 
110  template< class K, class V >
111  DataOStream& operator << ( const std::map< K, V >& value );
112 
114  template< class T >
115  DataOStream& operator << ( const std::set< T >& value );
116 
118  template< class K, class V >
119  DataOStream& operator << ( const stde::hash_map< K, V >& value );
120 
122  template< class T >
123  DataOStream& operator << ( const stde::hash_set< T >& value );
124 
131  template< typename C >
132  void serializeChildren( const std::vector< C* >& children );
134 
135 protected:
136  CO_API DataOStream();
137  explicit DataOStream( DataOStream& rhs );
138  virtual CO_API ~DataOStream();
139 
141  CO_API lunchbox::Bufferb& getBuffer();
142 
144  void _initCompressor( const uint32_t compressor );
145 
147  CO_API void _enable();
148 
150  void flush( const bool last );
151 
155  void _setupConnections( const Nodes& receivers );
156 
157  void _setupConnections( const Connections& connections );
158 
160  void _setupConnection( NodePtr node, const bool useMulticast );
161 
163  CO_API void _setupConnection( ConnectionPtr connection );
164  friend class DataStreamTest::Sender;
165 
167  void _resend();
168 
169  void _clearConnections();
170 
174  virtual void sendData( const void* buffer, const uint64_t size,
175  const bool last ) = 0;
177 
179  virtual CO_API void reset();
180 
181 private:
182  detail::DataOStream* const _impl;
183 
185  CO_API uint64_t _getCompressedData( void** chunks,
186  uint64_t* chunkSizes ) const;
187 
189  CO_API void _write( const void* data, uint64_t size );
190 
192  void _sendData( const void* data, const uint64_t size );
193 
195  void _resetBuffer();
196 
198  template< class T >
199  DataOStream& _writeFlatVector( const std::vector< T >& value )
200  {
201  const uint64_t nElems = value.size();
202  _write( &nElems, sizeof( nElems ));
203  if( nElems > 0 )
204  _write( &value.front(), nElems * sizeof( T ));
205  return *this;
206  }
207 
209  template< class T > void _write( const T& value, const boost::true_type& )
210  { _write( &value, sizeof( value )); }
211 
213  template< class T > void _write( const T& value, const boost::false_type& )
214  { _writeSerializable( value, boost::is_base_of< servus::Serializable, T >());}
215 
217  template< class T>
218  void _writeSerializable( const T& value, const boost::true_type& );
219 
221  template< class T >
222  void _writeArray( const Array< T > array, const boost::true_type& )
223  { _write( array.data, array.getNumBytes( )); }
224 
226  template< class T >
227  void _writeArray( const Array< T > array, const boost::false_type& )
228  {
229  for( size_t i = 0; i < array.num; ++i )
230  *this << array.data[ i ];
231  }
232 
234  void _sendFooter( const void* buffer, const uint64_t size );
235 };
236 
237 std::ostream& operator << ( std::ostream&, const DataOStream& );
238 }
239 
240 #include "dataOStream.ipp" // template implementation
241 
242 #endif //CO_DATAOSTREAM_H
Defines export visibility macros for library Collage.
std::vector< ConnectionPtr > Connections
A vector of ConnectionPtr's.
Definition: types.h:124
DataOStream & operator<<(const T &value)
Write a plain data item by copying it to the stream.
Definition: dataOStream.h:82
lunchbox::RefPtr< Node > NodePtr
A reference pointer for Node pointers.
Definition: types.h:85
A std::ostream-like interface for object serialization.
Definition: dataOStream.h:48
Object-oriented network library.
Definition: barrier.h:27
std::vector< NodePtr > Nodes
A vector of NodePtr's.
Definition: types.h:103
lunchbox::RefPtr< Connection > ConnectionPtr
A reference pointer for Connection pointers.
Definition: types.h:93