Line data Source code
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 : {
25 : /** @name Specialized output operators */
26 : //@{
27 : /** Write a std::string. */
28 : template<>
29 11262 : inline DataOStream& DataOStream::operator << ( const std::string& str )
30 : {
31 11262 : const uint64_t nElems = str.length();
32 11262 : _write( &nElems, sizeof( nElems ));
33 11262 : if ( nElems > 0 )
34 11100 : _write( str.c_str(), nElems );
35 :
36 11262 : return *this;
37 : }
38 :
39 : /** Write an object identifier and version. */
40 : template<> inline
41 : DataOStream& DataOStream::operator << ( const Object* const& object )
42 : {
43 : LBASSERT( !object || object->isAttached( ));
44 : (*this) << ObjectVersion( object );
45 : return *this;
46 : }
47 :
48 : /** Serialize an inline serializable object. */
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 1 : template<> inline void DataOStream::_writeArray( const Array< void > array,
57 : const boost::false_type& )
58 : {
59 1 : _write( array.data, array.getNumBytes( ));
60 1 : }
61 :
62 16 : template<> inline void DataOStream::_writeArray( const Array< const void > array,
63 : const boost::false_type& )
64 : {
65 16 : _write( array.data, array.getNumBytes( ));
66 16 : }
67 :
68 : /** @cond IGNORE */
69 : template< class T > inline
70 34 : DataOStream& DataOStream::operator << ( const lunchbox::RefPtr< T >& ptr )
71 : {
72 34 : 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 2 : DataOStream& DataOStream::operator << ( const std::vector< T >& value )
84 : {
85 2 : const uint64_t nElems = value.size();
86 2 : *this << nElems;
87 5 : for( uint64_t i = 0; i < nElems; ++i )
88 3 : *this << value[i];
89 2 : 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 : }
159 : /** @endcond */
160 :
161 : /** Optimized specialization to write a std::vector of uint8_t. */
162 : template<> inline
163 : DataOStream& DataOStream::operator << ( const std::vector< uint8_t >& value )
164 : { return _writeFlatVector( value ); }
165 :
166 : /** Optimized specialization to write a std::vector of uint16_t. */
167 : template<> inline
168 : DataOStream& DataOStream::operator << ( const std::vector< uint16_t >& value )
169 : { return _writeFlatVector( value ); }
170 :
171 : /** Optimized specialization to write a std::vector of int16_t. */
172 : template<> inline
173 : DataOStream& DataOStream::operator << ( const std::vector< int16_t >& value )
174 : { return _writeFlatVector( value ); }
175 :
176 : /** Optimized specialization to write a std::vector of uint32_t. */
177 : template<> inline
178 : DataOStream& DataOStream::operator << ( const std::vector< uint32_t >& value )
179 : { return _writeFlatVector( value ); }
180 :
181 : /** Optimized specialization to write a std::vector of int32_t. */
182 : template<> inline
183 : DataOStream& DataOStream::operator << ( const std::vector< int32_t >& value )
184 : { return _writeFlatVector( value ); }
185 :
186 : /** Optimized specialization to write a std::vector of uint64_t. */
187 : template<> inline
188 : DataOStream& DataOStream::operator << ( const std::vector< uint64_t >& value )
189 : { return _writeFlatVector( value ); }
190 :
191 : /** Optimized specialization to write a std::vector of int64_t. */
192 : template<> inline
193 : DataOStream& DataOStream::operator << ( const std::vector< int64_t >& value )
194 : { return _writeFlatVector( value ); }
195 :
196 : /** Optimized specialization to write a std::vector of float. */
197 : template<> inline
198 : DataOStream& DataOStream::operator << ( const std::vector< float >& value )
199 : { return _writeFlatVector( value ); }
200 :
201 : /** Optimized specialization to write a std::vector of double. */
202 : template<> inline
203 1 : DataOStream& DataOStream::operator << ( const std::vector< double >& value )
204 1 : { return _writeFlatVector( value ); }
205 :
206 : /** Optimized specialization to write a std::vector of ObjectVersion. */
207 : template<> inline DataOStream&
208 3 : DataOStream::operator << ( const std::vector< ObjectVersion >& value )
209 3 : { return _writeFlatVector( value ); }
210 : //@}
211 : }
|