Collage  1.0.1
Object-Oriented C++ Network Library
dataOStream.ipp
1 
2 /* Copyright (c) 2012, Daniel Nachbaur <danielnachbaur@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License version 2.1 as published
6  * by the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11  * details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17 
18 #include <co/object.h>
19 #include <co/objectVersion.h>
20 
21 namespace co
22 {
26  template<>
27  inline DataOStream& DataOStream::operator << ( const std::string& str )
28  {
29  const uint64_t nElems = str.length();
30  _write( &nElems, sizeof( nElems ));
31  if ( nElems > 0 )
32  _write( str.c_str(), nElems );
33 
34  return *this;
35  }
36 
38  template<> inline DataOStream&
39  DataOStream::operator << ( const Object* const& object )
40  {
41  LBASSERT( !object || object->isAttached( ));
42  (*this) << ObjectVersion( object );
43  return *this;
44  }
45 
47  template< class T > inline DataOStream&
48  DataOStream::operator << ( const lunchbox::Buffer< T >& buffer )
49  {
50  return (*this) << buffer.getSize()
51  << Array< const T >( buffer.getData(), buffer.getSize());
52  }
53 
54  template< class T > inline DataOStream&
55  DataOStream::operator << ( const std::vector< T >& value )
56  {
57  const uint64_t nElems = value.size();
58  *this << nElems;
59  for( uint64_t i = 0; i < nElems; ++i )
60  *this << value[i];
61  return *this;
62  }
63 
64  template< class K, class V > inline DataOStream&
65  DataOStream::operator << ( const std::map< K, V >& value )
66  {
67  const uint64_t nElems = value.size();
68  *this << nElems;
69  for( typename std::map< K, V >::const_iterator it = value.begin();
70  it != value.end(); ++it )
71  {
72  *this << it->first << it->second;
73  }
74  return *this;
75  }
76 
77  template< class T > inline DataOStream&
78  DataOStream::operator << ( const std::set< T >& value )
79  {
80  const uint64_t nElems = value.size();
81  *this << nElems;
82  for( typename std::set< T >::const_iterator it = value.begin();
83  it != value.end(); ++it )
84  {
85  *this << *it;
86  }
87  return *this;
88  }
89 
90  template< class K, class V > inline DataOStream&
91  DataOStream::operator << ( const stde::hash_map< K, V >& value )
92  {
93  const uint64_t nElems = value.size();
94  *this << nElems;
95  for( typename stde::hash_map< K, V >::const_iterator it = value.begin();
96  it != value.end(); ++it )
97  {
98  *this << it->first << it->second;
99  }
100  return *this;
101  }
102 
103  template< class T > inline DataOStream&
104  DataOStream::operator << ( const stde::hash_set< T >& value )
105  {
106  const uint64_t nElems = value.size();
107  *this << nElems;
108  for( typename stde::hash_set< T >::const_iterator it = value.begin();
109  it != value.end(); ++it )
110  {
111  *this << *it;
112  }
113  return *this;
114  }
115 
116  template< typename C > inline void
117  DataOStream::serializeChildren( const std::vector<C*>& children )
118  {
119  const uint64_t nElems = children.size();
120  (*this) << nElems;
121 
122  for( typename std::vector< C* >::const_iterator i = children.begin();
123  i != children.end(); ++i )
124  {
125  C* child = *i;
126  (*this) << ObjectVersion( child );
127  LBASSERTINFO( !child || child->isAttached(),
128  "Found unmapped object during serialization" );
129  }
130  }
134  template<> inline DataOStream&
135  DataOStream::operator << ( const std::vector< uint8_t >& value )
136  { return _writeFlatVector( value ); }
137 
139  template<> inline DataOStream&
140  DataOStream::operator << ( const std::vector< uint16_t >& value )
141  { return _writeFlatVector( value ); }
142 
144  template<> inline DataOStream&
145  DataOStream::operator << ( const std::vector< int16_t >& value )
146  { return _writeFlatVector( value ); }
147 
149  template<> inline DataOStream&
150  DataOStream::operator << ( const std::vector< uint32_t >& value )
151  { return _writeFlatVector( value ); }
152 
154  template<> inline DataOStream&
155  DataOStream::operator << ( const std::vector< int32_t >& value )
156  { return _writeFlatVector( value ); }
157 
159  template<> inline DataOStream&
160  DataOStream::operator << ( const std::vector< uint64_t >& value )
161  { return _writeFlatVector( value ); }
162 
164  template<> inline DataOStream&
165  DataOStream::operator << ( const std::vector< int64_t >& value )
166  { return _writeFlatVector( value ); }
167 
169  template<> inline DataOStream&
170  DataOStream::operator << ( const std::vector< float >& value )
171  { return _writeFlatVector( value ); }
172 
174  template<> inline DataOStream&
175  DataOStream::operator << ( const std::vector< double >& value )
176  { return _writeFlatVector( value ); }
177 
179  template<> inline DataOStream&
180  DataOStream::operator << ( const std::vector< ObjectVersion >& value )
181  { return _writeFlatVector( value ); }
183 }
A wrapper to (de)serialize arrays.
Definition: array.h:28
CO_API bool isAttached() const
A helper struct bundling an object identifier and version.
Definition: objectVersion.h:45
A distributed object.
Definition: object.h:45
A std::ostream-like interface for object serialization.
Definition: dataOStream.h:46
DataOStream & operator<<(const T &value)
Write a plain data item by copying it to the stream.
Definition: dataOStream.h:80