Collage  1.1.2
High-performance C++ library for developing object-oriented distributed applications.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
dataOStream.h
1 
2 /* Copyright (c) 2007-2014, Stefan Eilemann <eile@equalizergraphics.com>
3  * 2010, Cedric Stalder <cedric.stalder@gmail.com>
4  * 2012-2014, 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/nonCopyable.h> // base class
30 #include <lunchbox/stdExt.h>
31 
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, sizeof( value )); return *this; }
84 
86  template< class T > DataOStream& operator << ( const Array< T > array )
87  { _writeArray( array, boost::is_pod<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  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 >
210  void _writeArray( const Array< T > array, const boost::true_type& )
211  { _write( array.data, array.getNumBytes( )); }
212 
214  template< class T >
215  void _writeArray( const Array< T > array, const boost::false_type& )
216  {
217  for( size_t i = 0; i < array.num; ++i )
218  *this << array.data[ i ];
219  }
220 
222  void _sendFooter( const void* buffer, const uint64_t size );
223 };
224 
225 std::ostream& operator << ( std::ostream&, const DataOStream& );
226 }
227 
228 #include "dataOStream.ipp" // template implementation
229 
230 #endif //CO_DATAOSTREAM_H
Defines export visibility macros for Collage.
std::vector< ConnectionPtr > Connections
A vector of ConnectionPtr's.
Definition: types.h:125
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:86
A std::ostream-like interface for object serialization.
Definition: dataOStream.h:48
std::vector< NodePtr > Nodes
A vector of NodePtr's.
Definition: types.h:104
lunchbox::RefPtr< Connection > ConnectionPtr
A reference pointer for Connection pointers.
Definition: types.h:94