Collage  1.0.1
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 void* data,
71  const uint64_t size );
72 
74  uint64_t getCompressedDataSize() const;
76 
80  template< class T > DataOStream& operator << ( const T& value )
81  { _write( &value, sizeof( value )); return *this; }
82 
84  template< class T > DataOStream& operator << ( Array< T > array )
85  { _write( array.data, array.getNumBytes( )); return *this; }
86 
88  template< class T >
89  DataOStream& operator << ( const lunchbox::Buffer< T >& buffer );
90 
92  template< class T >
93  DataOStream& operator << ( const std::vector< T >& value );
94 
96  template< class K, class V >
97  DataOStream& operator << ( const std::map< K, V >& value );
98 
100  template< class T >
101  DataOStream& operator << ( const std::set< T >& value );
102 
104  template< class K, class V >
105  DataOStream& operator << ( const stde::hash_map< K, V >& value );
106 
108  template< class T >
109  DataOStream& operator << ( const stde::hash_set< T >& value );
110 
117  template< typename C >
118  void serializeChildren( const std::vector< C* >& children );
120 
121  protected:
122  CO_API DataOStream();
123  DataOStream( DataOStream& rhs );
124  virtual CO_API ~DataOStream();
125 
127  CO_API lunchbox::Bufferb& getBuffer();
128 
130  void _initCompressor( const uint32_t compressor );
131 
133  CO_API void _enable();
134 
136  void flush( const bool last );
137 
142  void _setupConnections( const Nodes& receivers );
143 
144  void _setupConnections( const Connections& connections );
145 
147  void _setupConnection( NodePtr node, const bool useMulticast );
148 
150  CO_API void _setupConnection( ConnectionPtr connection );
151  friend class DataStreamTest::Sender;
152 
154  void _resend();
155 
156  void _clearConnections();
157 
161  virtual void sendData( const void* buffer, const uint64_t size,
162  const bool last ) = 0;
164 
166  virtual CO_API void reset();
167 
168  private:
169  detail::DataOStream* const _impl;
170 
172  CO_API uint64_t _getCompressedData( void** chunks,
173  uint64_t* chunkSizes ) const;
174 
176  CO_API void _write( const void* data, uint64_t size );
177 
179  void _sendData( const void* data, const uint64_t size );
180 
182  void _resetBuffer();
183 
185  template< class T >
186  DataOStream& _writeFlatVector( const std::vector< T >& value )
187  {
188  const uint64_t nElems = value.size();
189  _write( &nElems, sizeof( nElems ));
190  if( nElems > 0 )
191  _write( &value.front(), nElems * sizeof( T ));
192  return *this;
193  }
195  void _sendFooter( const void* buffer, const uint64_t size );
196  };
197 
198  std::ostream& operator << ( std::ostream&, const DataOStream& );
199 }
200 
201 #include "dataOStream.ipp" // template implementation
202 
203 #endif //CO_DATAOSTREAM_H
std::vector< ConnectionPtr > Connections
A vector of ConnectionPtr&#39;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&#39;s.
Definition: types.h:98
DataOStream & operator<<(const T &value)
Write a plain data item by copying it to the stream.
Definition: dataOStream.h:80
lunchbox::RefPtr< Connection > ConnectionPtr
A reference pointer for Connection pointers.
Definition: types.h:88