Collage  1.2.1
High-performance C++ library for developing object-oriented distributed applications.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
dataOStream.ipp
1 
2 /* Copyright (c) 2012-2014, 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 DataOStream& DataOStream::operator << ( const std::string& str )
29 {
30  const uint64_t nElems = str.length();
31  _write( &nElems, sizeof( nElems ));
32  if ( nElems > 0 )
33  _write( str.c_str(), nElems );
34 
35  return *this;
36 }
37 
39 template<> inline
41 {
42  LBASSERT( !object || object->isAttached( ));
43  (*this) << ObjectVersion( object );
44  return *this;
45 }
46 
48 template< class T > inline
49 DataOStream& DataOStream::operator << ( const lunchbox::RefPtr< T >& ptr )
50 {
51  return *this << ptr.get();
52 }
53 
54 template< class T > inline
55 DataOStream& DataOStream::operator << ( const lunchbox::Buffer< T >& buffer )
56 {
57  return (*this) << buffer.getSize()
58  << Array< const T >( buffer.getData(), buffer.getSize());
59 }
60 
61 template< class T > inline
62 DataOStream& DataOStream::operator << ( const std::vector< T >& value )
63 {
64  const uint64_t nElems = value.size();
65  *this << nElems;
66  for( uint64_t i = 0; i < nElems; ++i )
67  *this << value[i];
68  return *this;
69 }
70 
71 template< class K, class V > inline
72 DataOStream& DataOStream::operator << ( const std::map< K, V >& value )
73 {
74  const uint64_t nElems = value.size();
75  *this << nElems;
76  for( typename std::map< K, V >::const_iterator it = value.begin();
77  it != value.end(); ++it )
78  {
79  *this << it->first << it->second;
80  }
81  return *this;
82 }
83 
84 template< class T > inline
85 DataOStream& DataOStream::operator << ( const std::set< T >& value )
86 {
87  const uint64_t nElems = value.size();
88  *this << nElems;
89  for( typename std::set< T >::const_iterator it = value.begin();
90  it != value.end(); ++it )
91  {
92  *this << *it;
93  }
94  return *this;
95 }
96 
97 template< class K, class V > inline
98 DataOStream& DataOStream::operator << ( const stde::hash_map< K, V >& value )
99 {
100  const uint64_t nElems = value.size();
101  *this << nElems;
102  for( typename stde::hash_map< K, V >::const_iterator it = value.begin();
103  it != value.end(); ++it )
104  {
105  *this << it->first << it->second;
106  }
107  return *this;
108 }
109 
110 template< class T > inline
111 DataOStream& DataOStream::operator << ( const stde::hash_set< T >& value )
112 {
113  const uint64_t nElems = value.size();
114  *this << nElems;
115  for( typename stde::hash_set< T >::const_iterator it = value.begin();
116  it != value.end(); ++it )
117  {
118  *this << *it;
119  }
120  return *this;
121 }
122 
123 template< typename C > inline
124 void DataOStream::serializeChildren( const std::vector<C*>& children )
125 {
126  const uint64_t nElems = children.size();
127  (*this) << nElems;
128 
129  for( typename std::vector< C* >::const_iterator i = children.begin();
130  i != children.end(); ++i )
131  {
132  C* child = *i;
133  (*this) << ObjectVersion( child );
134  LBASSERTINFO( !child || child->isAttached(),
135  "Found unmapped object during serialization" );
136  }
137 }
141 template<> inline
142 DataOStream& DataOStream::operator << ( const std::vector< uint8_t >& value )
143 { return _writeFlatVector( value ); }
144 
146 template<> inline
147 DataOStream& DataOStream::operator << ( const std::vector< uint16_t >& value )
148 { return _writeFlatVector( value ); }
149 
151 template<> inline
152 DataOStream& DataOStream::operator << ( const std::vector< int16_t >& value )
153 { return _writeFlatVector( value ); }
154 
156 template<> inline
157 DataOStream& DataOStream::operator << ( const std::vector< uint32_t >& value )
158 { return _writeFlatVector( value ); }
159 
161 template<> inline
162 DataOStream& DataOStream::operator << ( const std::vector< int32_t >& value )
163 { return _writeFlatVector( value ); }
164 
166 template<> inline
167 DataOStream& DataOStream::operator << ( const std::vector< uint64_t >& value )
168 { return _writeFlatVector( value ); }
169 
171 template<> inline
172 DataOStream& DataOStream::operator << ( const std::vector< int64_t >& value )
173 { return _writeFlatVector( value ); }
174 
176 template<> inline
177 DataOStream& DataOStream::operator << ( const std::vector< float >& value )
178 { return _writeFlatVector( value ); }
179 
181 template<> inline
182 DataOStream& DataOStream::operator << ( const std::vector< double >& value )
183 { return _writeFlatVector( value ); }
184 
186 template<> inline DataOStream&
187 DataOStream::operator << ( const std::vector< ObjectVersion >& value )
188 { return _writeFlatVector( value ); }
190 }
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