Developer Documentation
SerializersT_impl.hh
1#pragma once
2/*===========================================================================*\
3 * *
4 * OpenVolumeMesh *
5 * Copyright (C) 2011 by Computer Graphics Group, RWTH Aachen *
6 * www.openvolumemesh.org *
7 * *
8 *---------------------------------------------------------------------------*
9 * This file is part of OpenVolumeMesh. *
10 * *
11 * OpenVolumeMesh is free software: you can redistribute it and/or modify *
12 * it under the terms of the GNU Lesser General Public License as *
13 * published by the Free Software Foundation, either version 3 of *
14 * the License, or (at your option) any later version with the *
15 * following exceptions: *
16 * *
17 * If other files instantiate templates or use macros *
18 * or inline functions from this file, or you compile this file and *
19 * link it with other files to produce an executable, this file does *
20 * not by itself cause the resulting executable to be covered by the *
21 * GNU Lesser General Public License. This exception does not however *
22 * invalidate any other reasons why the executable file might be *
23 * covered by the GNU Lesser General Public License. *
24 * *
25 * OpenVolumeMesh is distributed in the hope that it will be useful, *
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
28 * GNU Lesser General Public License for more details. *
29 * *
30 * You should have received a copy of the GNU LesserGeneral Public *
31 * License along with OpenVolumeMesh. If not, *
32 * see <http://www.gnu.org/licenses/>. *
33 * *
34\*===========================================================================*/
35
36
37
38#include <OpenVolumeMesh/FileManager/Serializers.hh>
39
40namespace OpenVolumeMesh
41{
42
43template<typename T>
44T& decllval();
45
46template <bool B> struct bool_type;
47template <> struct bool_type<true> { char c[1]; };
48template <> struct bool_type<false> { char c[2]; };
49
52
53template <typename Stream, typename T>
55{
56private:
57 template<class U> static true_type test(char(*)[sizeof(decllval<Stream>() >> decllval<U>(), void(), 0)]);
58 template<class U> static false_type test(...);
59
60public:
61 enum { bool_value = sizeof(true_type) == sizeof(test<T>(nullptr)) };
63 static type value;
64};
65
66template <typename Stream, typename T>
68
69
70template <typename Stream, typename T>
72{
73private:
74 template<class U> static true_type test(char(*)[sizeof(decllval<Stream>() << decllval<U>(), void(), 0)]);
75 template<class U> static false_type test(...);
76
77public:
78 enum { bool_value = sizeof(true_type) == sizeof(test<T>(nullptr)) };
80 static type value;
81};
82
83template <typename Stream, typename T>
85
86
87template <typename ValueT>
88std::ostream& serialize_helper(std::ostream& _ostr, ValueT& _rhs, true_type)
89{
90 _ostr << _rhs;
91 return _ostr;
92}
93
94template <typename ValueT>
95std::ostream& serialize_helper(std::ostream& _ostr, ValueT&, false_type)
96{
97 std::cout << "Warning: trying to serialize a type that does not have a serialize function" << std::endl;
98 return _ostr;
99}
100
101template <typename ValueT>
102std::ostream& serialize(std::ostream& _ostr, const ValueT& _rhs)
103{
104 return serialize_helper(_ostr, _rhs, has_output_operator<std::ostream, ValueT>::value);
105}
106
107
108template <typename ValueT>
109std::istream& deserialize_helper(std::istream& _istr, ValueT& _rhs, true_type)
110{
111 _istr >> _rhs;
112 return _istr;
113}
114
115template <typename ValueT>
116std::istream& deserialize_helper(std::istream& _istr, ValueT&, false_type)
117{
118 std::cout << "Warning: trying to deserialize a type that does not have a deserialize function" << std::endl;
119 return _istr;
120}
121
122template <typename ValueT>
123std::istream& deserialize(std::istream& _istr, ValueT& _rhs)
124{
125 return deserialize_helper(_istr, _rhs, has_input_operator<std::istream, ValueT>::value);
126}
127
128template <typename KeyT, typename ValueT>
129std::ostream& serialize(std::ostream& os, const std::map< KeyT, ValueT >& rhs)
130{
131 os << rhs.size() << std::endl;
132 for (typename std::map< KeyT, ValueT >::const_iterator it = rhs.begin();
133 it != rhs.end();
134 ++it)
135 {
136 serialize(os,it->first) << std::endl;
137 serialize(os, it->second) << std::endl;
138 }
139
140 return os;
141}
142
143template <typename KeyT, typename ValueT>
144std::istream& deserialize(std::istream& is, std::map< KeyT, ValueT >& rhs)
145{
146
147 size_t size;
148 is >> size;
149 rhs.clear();
150 for (size_t i=0; i<size; i++)
151 {
152 KeyT key;
153 ValueT value;
154 deserialize(is, key);
155 deserialize(is, value);
156 rhs[key] = value;
157 }
158
159 return is;
160}
161
162template <typename ValueT>
163std::ostream& serialize(std::ostream& _ostr, const std::vector< ValueT >& _rhs)
164{
165 _ostr << _rhs.size() << std::endl;
166 for (size_t i = 0; i < _rhs.size(); ++i)
167 serialize(_ostr, _rhs[i]) << std::endl;
168 return _ostr;
169}
170
171template <typename ValueT>
172std::istream& deserialize(std::istream& _istr, std::vector< ValueT >& _rhs)
173{
174 size_t size;
175 _istr >> size;
176 _rhs.resize(size);
177 for (size_t i=0; i<size; i++)
178 deserialize(_istr,_rhs[i]);
179
180 return _istr;
181}
182
183
184}