Collage  1.0.0
Object-Oriented C++ Network Library
dataOStream.h
1 
2 /* Copyright (c) 2007-2012, Stefan Eilemann <eile@equalizergraphics.com>
3  * 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_DATAOSTREAM_H
23 #define CO_DATAOSTREAM_H
24 
25 #include <co/api.h>
26 #include <co/array.h> // used inline
27 #include <co/types.h>
28 #include <lunchbox/nonCopyable.h> // base class
29 #include <lunchbox/stdExt.h>
30 
31 #include <map>
32 #include <set>
33 #include <vector>
34 
35 namespace co
36 {
37 namespace detail { class DataOStream; }
38 namespace DataStreamTest { class Sender; }
39 
46  class DataOStream : public lunchbox::NonCopyable
47  {
48  public:
52  CO_API void disable();
53 
55  void enableSave();
56 
58  void disableSave();
59 
61  CO_API bool hasSentData() const;
62 
64  CO_API const Connections& getConnections() const;
65 
67  DataOStream& streamDataHeader( DataOStream& os );
68 
70  void sendBody( ConnectionPtr connection, const uint64_t dataSize );
71 
73  uint64_t getCompressedDataSize() const;
75 
79  template< class T > DataOStream& operator << ( const T& value )
80  { _write( &value, sizeof( value )); return *this; }
81 
83  template< class T > DataOStream& operator << ( Array< T > array )
84  { _write( array.data, array.getNumBytes( )); return *this; }
85 
87  template< class T >
88  DataOStream& operator << ( const lunchbox::Buffer< T >& buffer );
89 
91  template< class T >
92  DataOStream& operator << ( const std::vector< T >& value );
93 
95  template< class K, class V >
96  DataOStream& operator << ( const std::map< K, V >& value );
97 
99  template< class T >
100  DataOStream& operator << ( const std::set< T >& value );
101 
103  template< class K, class V >
104  DataOStream& operator << ( const stde::hash_map< K, V >& value );
105 
107  template< class T >
108  DataOStream& operator << ( const stde::hash_set< T >& value );
109 
116  template< typename C >
117  void serializeChildren( const std::vector< C* >& children );
119 
120  protected:
121  CO_API DataOStream();
122  DataOStream( DataOStream& rhs );
123  virtual CO_API ~DataOStream();
124 
126  CO_API lunchbox::Bufferb& getBuffer();
127 
129  void _initCompressor( const uint32_t compressor );
130 
132  CO_API void _enable();
133 
135  void flush( const bool last );
136 
141  void _setupConnections( const Nodes& receivers );
142 
143  void _setupConnections( const Connections& connections );
144 
146  void _setupConnection( NodePtr node, const bool useMulticast );
147 
149  CO_API void _setupConnection( ConnectionPtr connection );
150  friend class DataStreamTest::Sender;
151 
153  void _resend();
154 
155  void _clearConnections();
156 
160  virtual void sendData( const void* buffer, const uint64_t size,
161  const bool last ) = 0;
163 
165  virtual CO_API void reset();
166 
167  private:
168  detail::DataOStream* const _impl;
169 
171  CO_API uint64_t _getCompressedData( void** chunks,
172  uint64_t* chunkSizes ) const;
173 
175  CO_API void _write( const void* data, uint64_t size );
176 
178  void _sendData( const void* data, const uint64_t size );
179 
181  void _resetBuffer();
182 
184  template< class T >
185  DataOStream& _writeFlatVector( const std::vector< T >& value )
186  {
187  const uint64_t nElems = value.size();
188  _write( &nElems, sizeof( nElems ));
189  if( nElems > 0 )
190  _write( &value.front(), nElems * sizeof( T ));
191  return *this;
192  }
194  void _sendFooter( const void* buffer, const uint64_t size );
195  };
196 
197  std::ostream& operator << ( std::ostream&, const DataOStream& );
198 }
199 
200 #include "dataOStream.ipp" // template implementation
201 
202 #endif //CO_DATAOSTREAM_H
std::vector< ConnectionPtr > Connections
A vector of ConnectionPtr's.
Definition: types.h:116
lunchbox::RefPtr< Node > NodePtr
A reference pointer for Node pointers.
Definition: types.h:80
A std::ostream-like interface for object serialization.
Definition: dataOStream.h:46
std::vector< NodePtr > Nodes
A vector of NodePtr's.
Definition: types.h:98
DataOStream & operator<<(const T &value)
Write a plain data item by copying it to the stream.
Definition: dataOStream.h:79
lunchbox::RefPtr< Connection > ConnectionPtr
A reference pointer for Connection pointers.
Definition: types.h:88