Collage  1.4.0
High-performance C++ library for developing object-oriented distributed applications.
dataOStream.ipp
1 
2 /* Copyright (c) 2012-2016, Daniel Nachbaur <danielnachbaur@gmail.com>
3  * 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 #include <servus/serializable.h> // used inline
22 
23 namespace co
24 {
28 template<>
29 inline DataOStream& DataOStream::operator << ( const std::string& str )
30 {
31  const uint64_t nElems = str.length();
32  _write( &nElems, sizeof( nElems ));
33  if ( nElems > 0 )
34  _write( str.c_str(), nElems );
35 
36  return *this;
37 }
38 
40 template<> inline
42 {
43  LBASSERT( !object || object->isAttached( ));
44  (*this) << ObjectVersion( object );
45  return *this;
46 }
47 
49 template< class T >
50 void DataOStream::_writeSerializable( const T& object, const boost::true_type& )
51 {
52  const auto& data = object.toBinary();
53  (*this) << data.size << Array< const void >( data.ptr.get(), data.size );
54 }
55 
56 template<> inline void DataOStream::_writeArray( const Array< void > array,
57  const boost::false_type& )
58 {
59  _write( array.data, array.getNumBytes( ));
60 }
61 
62 template<> inline void DataOStream::_writeArray( const Array< const void > array,
63  const boost::false_type& )
64 {
65  _write( array.data, array.getNumBytes( ));
66 }
67 
69 template< class T > inline
70 DataOStream& DataOStream::operator << ( const lunchbox::RefPtr< T >& ptr )
71 {
72  return *this << ptr.get();
73 }
74 
75 template< class T > inline
76 DataOStream& DataOStream::operator << ( const lunchbox::Buffer< T >& buffer )
77 {
78  return (*this) << buffer.getSize()
79  << Array< const T >( buffer.getData(), buffer.getSize());
80 }
81 
82 template< class T > inline
83 DataOStream& DataOStream::operator << ( const std::vector< T >& value )
84 {
85  const uint64_t nElems = value.size();
86  *this << nElems;
87  for( uint64_t i = 0; i < nElems; ++i )
88  *this << value[i];
89  return *this;
90 }
91 
92 template< class K, class V > inline
93 DataOStream& DataOStream::operator << ( const std::map< K, V >& value )
94 {
95  const uint64_t nElems = value.size();
96  *this << nElems;
97  for( typename std::map< K, V >::const_iterator it = value.begin();
98  it != value.end(); ++it )
99  {
100  *this << it->first << it->second;
101  }
102  return *this;
103 }
104 
105 template< class T > inline
106 DataOStream& DataOStream::operator << ( const std::set< T >& value )
107 {
108  const uint64_t nElems = value.size();
109  *this << nElems;
110  for( typename std::set< T >::const_iterator it = value.begin();
111  it != value.end(); ++it )
112  {
113  *this << *it;
114  }
115  return *this;
116 }
117 
118 template< class K, class V > inline
119 DataOStream& DataOStream::operator << ( const stde::hash_map< K, V >& value )
120 {
121  const uint64_t nElems = value.size();
122  *this << nElems;
123  for( typename stde::hash_map< K, V >::const_iterator it = value.begin();
124  it != value.end(); ++it )
125  {
126  *this << it->first << it->second;
127  }
128  return *this;
129 }
130 
131 template< class T > inline
132 DataOStream& DataOStream::operator << ( const stde::hash_set< T >& value )
133 {
134  const uint64_t nElems = value.size();
135  *this << nElems;
136  for( typename stde::hash_set< T >::const_iterator it = value.begin();
137  it != value.end(); ++it )
138  {
139  *this << *it;
140  }
141  return *this;
142 }
143 
144 template< typename C > inline
145 void DataOStream::serializeChildren( const std::vector<C*>& children )
146 {
147  const uint64_t nElems = children.size();
148  (*this) << nElems;
149 
150  for( typename std::vector< C* >::const_iterator i = children.begin();
151  i != children.end(); ++i )
152  {
153  C* child = *i;
154  (*this) << ObjectVersion( child );
155  LBASSERTINFO( !child || child->isAttached(),
156  "Found unmapped object during serialization" );
157  }
158 }
162 template<> inline
163 DataOStream& DataOStream::operator << ( const std::vector< uint8_t >& value )
164 { return _writeFlatVector( value ); }
165 
167 template<> inline
168 DataOStream& DataOStream::operator << ( const std::vector< uint16_t >& value )
169 { return _writeFlatVector( value ); }
170 
172 template<> inline
173 DataOStream& DataOStream::operator << ( const std::vector< int16_t >& value )
174 { return _writeFlatVector( value ); }
175 
177 template<> inline
178 DataOStream& DataOStream::operator << ( const std::vector< uint32_t >& value )
179 { return _writeFlatVector( value ); }
180 
182 template<> inline
183 DataOStream& DataOStream::operator << ( const std::vector< int32_t >& value )
184 { return _writeFlatVector( value ); }
185 
187 template<> inline
188 DataOStream& DataOStream::operator << ( const std::vector< uint64_t >& value )
189 { return _writeFlatVector( value ); }
190 
192 template<> inline
193 DataOStream& DataOStream::operator << ( const std::vector< int64_t >& value )
194 { return _writeFlatVector( value ); }
195 
197 template<> inline
198 DataOStream& DataOStream::operator << ( const std::vector< float >& value )
199 { return _writeFlatVector( value ); }
200 
202 template<> inline
203 DataOStream& DataOStream::operator << ( const std::vector< double >& value )
204 { return _writeFlatVector( value ); }
205 
207 template<> inline DataOStream&
208 DataOStream::operator << ( const std::vector< ObjectVersion >& value )
209 { return _writeFlatVector( value ); }
211 }
DataOStream & operator<<(const T &value)
Write a plain data item by copying it to the stream.
Definition: dataOStream.h:82
A distributed object.
Definition: object.h:45
A helper struct bundling an object identifier and version.
Definition: objectVersion.h:47
CO_API bool isAttached() const
A std::ostream-like interface for object serialization.
Definition: dataOStream.h:48
Object-oriented network library.
Definition: barrier.h:27