Collage  1.0.1
Object-Oriented C++ Network Library
dataIStream.ipp
1 
2 /* Copyright (c) 2012, Daniel Nachbaur <danielnachbaur@gmail.com>
3  * 2013, Stefan.Eilemann@epfl.ch
4  *
5  * This library is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Lesser General Public License version 2.1 as published
7  * by the Free Software Foundation.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #include <co/object.h>
20 #include <co/objectVersion.h>
21 
22 namespace co
23 {
27  template<>
28  inline DataIStream& DataIStream::operator >> ( std::string& str )
29  {
30  uint64_t nElems = 0;
31  *this >> nElems;
32  LBASSERTINFO( nElems <= getRemainingBufferSize(),
33  nElems << " > " << getRemainingBufferSize( ));
34  if( nElems == 0 )
35  str.clear();
36  else
37  str.assign( static_cast< const char* >( getRemainingBuffer(nElems)),
38  size_t( nElems ));
39  return *this;
40  }
41 
43  template<> inline DataIStream& DataIStream::operator >> ( Object*& object )
44  {
45  ObjectVersion data;
46  *this >> data;
47  LBASSERT( object->getID() == data.identifier );
48  object->sync( data.version );
49  return *this;
50  }
51 
53  template< class T > inline DataIStream&
55  {
56  _read( array.data, array.getNumBytes( ));
57  _swap( array );
58  return *this;
59  }
60 
61  template< class T > inline DataIStream&
62  DataIStream::operator >> ( lunchbox::Buffer< T >& buffer )
63  {
64  uint64_t nElems = 0;
65  *this >> nElems;
66  LBASSERTINFO( nElems < LB_BIT48,
67  "Out-of-sync co::DataIStream: " << nElems << " elements?" );
68  buffer.resize( nElems );
69  return *this >> Array< T >( buffer.getData(), nElems );
70  }
71 
72 
73  template< class T > inline DataIStream&
74  DataIStream::operator >> ( std::vector< T >& value )
75  {
76  uint64_t nElems = 0;
77  *this >> nElems;
78  value.resize( nElems );
79  for( uint64_t i = 0; i < nElems; ++i )
80  *this >> value[i];
81  return *this;
82  }
83 
84  template< class K, class V > inline DataIStream&
85  DataIStream::operator >> ( std::map< K, V >& map )
86  {
87  map.clear();
88  uint64_t nElems = 0;
89  *this >> nElems;
90  for( uint64_t i = 0; i < nElems; ++i )
91  {
92  typename std::map< K, V >::key_type key;
93  typename std::map< K, V >::mapped_type value;
94  *this >> key >> value;
95  map.insert( std::make_pair( key, value ));
96  }
97  return *this;
98  }
99 
100  template< class T > inline DataIStream&
101  DataIStream::operator >> ( std::set< T >& value )
102  {
103  value.clear();
104  uint64_t nElems = 0;
105  *this >> nElems;
106  for( uint64_t i = 0; i < nElems; ++i )
107  {
108  T item;
109  *this >> item;
110  value.insert( item );
111  }
112  return *this;
113  }
114 
115  template< class K, class V > inline DataIStream&
116  DataIStream::operator >> ( stde::hash_map< K, V >& map )
117  {
118  map.clear();
119  uint64_t nElems = 0;
120  *this >> nElems;
121  for( uint64_t i = 0; i < nElems; ++i )
122  {
123  typename stde::hash_map< K, V >::key_type key;
124  typename stde::hash_map< K, V >::mapped_type value;
125  *this >> key >> value;
126  map.insert( std::make_pair( key, value ));
127  }
128  return *this;
129  }
130 
131  template< class T > inline DataIStream&
132  DataIStream::operator >> ( stde::hash_set< T >& value )
133  {
134  value.clear();
135  uint64_t nElems = 0;
136  *this >> nElems;
137  for( uint64_t i = 0; i < nElems; ++i )
138  {
139  T item;
140  *this >> item;
141  value.insert( item );
142  }
143  return *this;
144  }
145 
146  namespace
147  {
148  class ObjectFinder
149  {
150  public:
151  ObjectFinder( const UUID& id ) : _id( id ) {}
152  bool operator()( co::Object* candidate )
153  { return candidate->getID() == _id; }
154 
155  private:
156  const UUID _id;
157  };
158  }
159 
160  template<> inline void DataIStream::_swap( Array< void > ) const { /*NOP*/ }
161 
162  template< typename O, typename C > inline void
163  DataIStream::deserializeChildren( O* object, const std::vector< C* >& old_,
164  std::vector< C* >& result )
165  {
166  ObjectVersions versions;
167  *this >> versions;
168  std::vector< C* > old = old_;
169 
170  // rebuild vector from serialized list
171  result.clear();
172  for( ObjectVersions::const_iterator i = versions.begin();
173  i != versions.end(); ++i )
174  {
175  const ObjectVersion& version = *i;
176 
177  if( version.identifier == UUID( ))
178  {
179  result.push_back( 0 );
180  continue;
181  }
182 
183  typename std::vector< C* >::iterator j =
184  lunchbox::find_if( old, ObjectFinder( version.identifier ));
185 
186  if( j == old.end( )) // previously unknown child
187  {
188  C* child = 0;
189  object->create( &child );
190  LocalNodePtr localNode = object->getLocalNode();
191  LBASSERT( child );
192  LBASSERT( !object->isMaster( ));
193 
194  LBCHECK( localNode->mapObject( child, version ));
195  result.push_back( child );
196  }
197  else
198  {
199  C* child = *j;
200  old.erase( j );
201  if( object->isMaster( ))
202  child->sync( VERSION_HEAD );
203  else
204  child->sync( version.version );
205 
206  result.push_back( child );
207  }
208  }
209 
210  while( !old.empty( )) // removed children
211  {
212  C* child = old.back();
213  old.pop_back();
214  if( !child )
215  continue;
216 
217  if( child->isAttached() && !child->isMaster( ))
218  {
219  LocalNodePtr localNode = object->getLocalNode();
220  localNode->unmapObject( child );
221  }
222  object->release( child );
223  }
224  }
228  template<> inline DataIStream&
229  DataIStream::operator >> ( std::vector< uint8_t >& value )
230  { return _readFlatVector( value );}
231 
233  template<> inline DataIStream&
234  DataIStream::operator >> ( std::vector< uint16_t >& value )
235  { return _readFlatVector( value ); }
236 
238  template<> inline DataIStream&
239  DataIStream::operator >> ( std::vector< int16_t >& value )
240  { return _readFlatVector( value ); }
241 
243  template<> inline DataIStream&
244  DataIStream::operator >> ( std::vector< uint32_t >& value )
245  { return _readFlatVector( value ); }
246 
248  template<> inline DataIStream&
249  DataIStream::operator >> ( std::vector< int32_t >& value )
250  { return _readFlatVector( value ); }
251 
253  template<> inline DataIStream&
254  DataIStream::operator >> ( std::vector< uint64_t>& value )
255  { return _readFlatVector( value ); }
256 
258  template<> inline DataIStream&
259  DataIStream::operator >> ( std::vector< int64_t >& value )
260  { return _readFlatVector( value ); }
261 
263  template<> inline DataIStream&
264  DataIStream::operator >> ( std::vector< float >& value )
265  { return _readFlatVector( value ); }
266 
268  template<> inline DataIStream&
269  DataIStream::operator >> ( std::vector< double >& value )
270  { return _readFlatVector( value ); }
271 
273  template<> inline DataIStream&
274  DataIStream::operator >> ( std::vector< ObjectVersion >& value )
275  { return _readFlatVector( value ); }
277 }
A wrapper to (de)serialize arrays.
Definition: array.h:28
CO_API const void * getRemainingBuffer(const uint64_t size)
uint128_t version
the object version
Definition: objectVersion.h:90
lunchbox::RefPtr< LocalNode > LocalNodePtr
A reference pointer for LocalNode pointers.
Definition: types.h:84
A helper struct bundling an object identifier and version.
Definition: objectVersion.h:45
size_t getNumBytes() const
Definition: array.h:36
uint128_t identifier
the object identifier
Definition: objectVersion.h:89
A std::istream-like input data stream for binary data.
Definition: dataIStream.h:40
A distributed object.
Definition: object.h:45
CO_API uint64_t getRemainingBufferSize()
DataIStream & operator>>(T &value)
Read a plain data item.
Definition: dataIStream.h:58
CO_API const UUID & getID() const
T *const data
The data.
Definition: array.h:38