Line data Source code
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 : {
24 : /** @name Specialized output operators */
25 : //@{
26 : /** Write a std::string. */
27 : template<>
28 11090 : inline DataOStream& DataOStream::operator << ( const std::string& str )
29 : {
30 11090 : const uint64_t nElems = str.length();
31 11090 : _write( &nElems, sizeof( nElems ));
32 11090 : if ( nElems > 0 )
33 11090 : _write( str.c_str(), nElems );
34 :
35 11090 : return *this;
36 : }
37 :
38 : /** Write an object identifier and version. */
39 : template<> inline
40 : DataOStream& DataOStream::operator << ( const Object* const& object )
41 : {
42 : LBASSERT( !object || object->isAttached( ));
43 : (*this) << ObjectVersion( object );
44 : return *this;
45 : }
46 :
47 : /** @cond IGNORE */
48 : template< class T > inline
49 33 : DataOStream& DataOStream::operator << ( const lunchbox::RefPtr< T >& ptr )
50 : {
51 33 : 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 2 : DataOStream& DataOStream::operator << ( const std::vector< T >& value )
63 : {
64 2 : const uint64_t nElems = value.size();
65 2 : *this << nElems;
66 5 : for( uint64_t i = 0; i < nElems; ++i )
67 3 : *this << value[i];
68 2 : 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 : }
138 : /** @endcond */
139 :
140 : /** Optimized specialization to write a std::vector of uint8_t. */
141 : template<> inline
142 : DataOStream& DataOStream::operator << ( const std::vector< uint8_t >& value )
143 : { return _writeFlatVector( value ); }
144 :
145 : /** Optimized specialization to write a std::vector of uint16_t. */
146 : template<> inline
147 : DataOStream& DataOStream::operator << ( const std::vector< uint16_t >& value )
148 : { return _writeFlatVector( value ); }
149 :
150 : /** Optimized specialization to write a std::vector of int16_t. */
151 : template<> inline
152 : DataOStream& DataOStream::operator << ( const std::vector< int16_t >& value )
153 : { return _writeFlatVector( value ); }
154 :
155 : /** Optimized specialization to write a std::vector of uint32_t. */
156 : template<> inline
157 : DataOStream& DataOStream::operator << ( const std::vector< uint32_t >& value )
158 : { return _writeFlatVector( value ); }
159 :
160 : /** Optimized specialization to write a std::vector of int32_t. */
161 : template<> inline
162 : DataOStream& DataOStream::operator << ( const std::vector< int32_t >& value )
163 : { return _writeFlatVector( value ); }
164 :
165 : /** Optimized specialization to write a std::vector of uint64_t. */
166 : template<> inline
167 : DataOStream& DataOStream::operator << ( const std::vector< uint64_t >& value )
168 : { return _writeFlatVector( value ); }
169 :
170 : /** Optimized specialization to write a std::vector of int64_t. */
171 : template<> inline
172 : DataOStream& DataOStream::operator << ( const std::vector< int64_t >& value )
173 : { return _writeFlatVector( value ); }
174 :
175 : /** Optimized specialization to write a std::vector of float. */
176 : template<> inline
177 : DataOStream& DataOStream::operator << ( const std::vector< float >& value )
178 : { return _writeFlatVector( value ); }
179 :
180 : /** Optimized specialization to write a std::vector of double. */
181 : template<> inline
182 1 : DataOStream& DataOStream::operator << ( const std::vector< double >& value )
183 1 : { return _writeFlatVector( value ); }
184 :
185 : /** Optimized specialization to write a std::vector of ObjectVersion. */
186 : template<> inline DataOStream&
187 3 : DataOStream::operator << ( const std::vector< ObjectVersion >& value )
188 3 : { return _writeFlatVector( value ); }
189 : //@}
190 : }
|