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