Collage  1.7.0
High-performance C++ library for developing object-oriented distributed applications.
dataIStream.h
1 
2 /* Copyright (c) 2007-2017, 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 
30 #include <boost/type_traits.hpp>
31 #include <map>
32 #include <set>
33 #include <unordered_map>
34 #include <unordered_set>
35 #include <vector>
36 
37 namespace co
38 {
39 namespace detail
40 {
41 class DataIStream;
42 }
43 
46 {
47 public:
51  virtual size_t nRemainingBuffers() const = 0;
52 
53  virtual uint128_t getVersion() const = 0;
54  virtual void reset() { _reset(); }
55 
56 
60  template <typename T>
61  T read()
62  {
63  T value;
64  *this >> value;
65  return value;
66  }
67 
69  template <class T>
71  {
72  _read(value, boost::has_trivial_copy<T>());
73  return *this;
74  }
75 
77  template <class T>
78  DataIStream& operator>>(Array<T> array)
79  {
80  _readArray(array, boost::has_trivial_copy<T>());
81  return *this;
82  }
83 
88  template <class T>
89  DataIStream& operator>>(lunchbox::RefPtr<T>&);
90 
92  template <class T>
93  DataIStream& operator>>(lunchbox::Buffer<T>&);
94 
96  template <class T>
97  DataIStream& operator>>(std::vector<T>&);
98 
100  template <class K, class V>
101  DataIStream& operator>>(std::map<K, V>&);
102 
104  template <class T>
105  DataIStream& operator>>(std::set<T>&);
106 
108  template <class K, class V>
109  DataIStream& operator>>(std::unordered_map<K, V>&);
110 
112  template <class T>
113  DataIStream& operator>>(std::unordered_set<T>&);
114 
126  template <typename O, typename C>
127  void deserializeChildren(O* object, const std::vector<C*>& old,
128  std::vector<C*>& result);
129 
149  CO_API const void* getRemainingBuffer(const uint64_t size);
150 
155  CO_API uint64_t getRemainingBufferSize();
156 
158  bool wasUsed() const;
159 
161  bool hasData() { return _checkBuffer(); }
163  CO_API virtual NodePtr getRemoteNode() const = 0;
164 
166  CO_API virtual LocalNodePtr getLocalNode() const = 0;
168 
169 protected:
172  CO_API DataIStream();
173  CO_API virtual ~DataIStream();
174 
175  virtual bool getNextBuffer(CompressorInfo& info, uint32_t& nChunks,
176  const void*& chunkData, uint64_t& size) = 0;
178 
179 private:
180  detail::DataIStream* const _impl;
181 
183  CO_API void _read(void* data, uint64_t size);
184 
189  CO_API bool _checkBuffer();
190  CO_API void _reset();
191 
192  const uint8_t* _decompress(const void* data, const CompressorInfo& info,
193  uint32_t nChunks, uint64_t dataSize);
194 
196  template <class T>
197  DataIStream& _readFlatVector(std::vector<T>& value)
198  {
199  uint64_t nElems = 0;
200  *this >> nElems;
201  LBASSERTINFO(nElems < LB_BIT48,
202  "Out-of-sync DataIStream: " << nElems << " elements?");
203  value.resize(size_t(nElems));
204  if (nElems > 0)
205  *this >> Array<T>(&value.front(), nElems);
206  return *this;
207  }
208 
210  template <class T>
211  void _read(T& value, const boost::true_type&)
212  {
213  _read(&value, sizeof(value));
214  }
215 
217  template <class T>
218  void _read(T& value, const boost::false_type&)
219  {
220  _readSerializable(value, boost::is_base_of<servus::Serializable, T>());
221  }
222 
224  template <class T>
225  void _readSerializable(T& value, const boost::true_type&);
226 
228  template <class T>
229  void _readArray(Array<T>, const boost::true_type&);
230 
232  template <class T>
233  void _readArray(Array<T>, const boost::false_type&);
234 };
235 }
236 
237 #include "dataIStream.ipp" // template implementation
238 
239 #endif // CO_DATAISTREAM_H
Defines export visibility macros for library Collage.
lunchbox::RefPtr< LocalNode > LocalNodePtr
A reference pointer for LocalNode pointers.
Definition: types.h:89
Object-oriented network library.
Definition: barrier.h:27
DataIStream & operator>>(T &value)
Read a plain data item.
Definition: dataIStream.h:70
A std::istream-like input data stream for binary data.
Definition: dataIStream.h:45
DataIStream & operator>>(Array< T > array)
Read a C array.
Definition: dataIStream.h:78
lunchbox::RefPtr< Node > NodePtr
A reference pointer for Node pointers.
Definition: types.h:85