Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 template<typename T>
51 T& decllval();
52 
53 template <bool B> struct bool_type;
54 template <> struct bool_type<true> { char c[1]; };
55 template <> struct bool_type<false> { char c[2]; };
56 
59 
60 template <typename Stream, typename T>
62 {
63 private:
64  template<class U> static true_type test(char(*)[sizeof(decllval<Stream>() >> decllval<U>(), void(), 0)]);
65  template<class U> static false_type test(...);
66 
67 public:
68  enum { bool_value = sizeof(true_type) == sizeof(test<T>(0)) };
70  static type value;
71 };
72 
73 template <typename Stream, typename T>
75 
76 
77 template <typename Stream, typename T>
79 {
80 private:
81  template<class U> static true_type test(char(*)[sizeof(decllval<Stream>() << decllval<U>(), void(), 0)]);
82  template<class U> static false_type test(...);
83 
84 public:
85  enum { bool_value = sizeof(true_type) == sizeof(test<T>(0)) };
87  static type value;
88 };
89 
90 template <typename Stream, typename T>
92 
93 
94 template <typename ValueT>
95 std::ostream& serialize_helper(std::ostream& _ostr, ValueT& _rhs, true_type)
96 {
97  _ostr << _rhs;
98  return _ostr;
99 }
100 
101 template <typename ValueT>
102 std::ostream& serialize_helper(std::ostream& _ostr, ValueT&, false_type)
103 {
104  std::cout << "Warning: trying to serialize a type that does not have a serialize function" << std::endl;
105  return _ostr;
106 }
107 
108 template <typename ValueT>
109 std::ostream& serialize(std::ostream& _ostr, const ValueT& _rhs)
110 {
111  return serialize_helper(_ostr, _rhs, has_output_operator<std::ostream, ValueT>::value);
112 }
113 
114 
115 template <typename ValueT>
116 std::istream& deserialize_helper(std::istream& _istr, ValueT& _rhs, true_type)
117 {
118  _istr >> _rhs;
119  return _istr;
120 }
121 
122 template <typename ValueT>
123 std::istream& deserialize_helper(std::istream& _istr, ValueT&, false_type)
124 {
125  std::cout << "Warning: trying to deserialize a type that does not have a deserialize function" << std::endl;
126  return _istr;
127 }
128 
129 template <typename ValueT>
130 std::istream& deserialize(std::istream& _istr, ValueT& _rhs)
131 {
132  return deserialize_helper(_istr, _rhs, has_input_operator<std::istream, ValueT>::value);
133 }
134 
135 template <typename KeyT, typename ValueT>
136 std::ostream& serialize(std::ostream& os, const std::map< KeyT, ValueT >& rhs)
137 {
138  os << rhs.size() << std::endl;
139  for (typename std::map< KeyT, ValueT >::const_iterator it = rhs.begin();
140  it != rhs.end();
141  ++it)
142  {
143  serialize(os,it->first) << std::endl;
144  serialize(os, it->second) << std::endl;
145  }
146 
147  return os;
148 }
149 
150 template <typename KeyT, typename ValueT>
151 std::istream& deserialize(std::istream& is, std::map< KeyT, ValueT >& rhs)
152 {
153 
154  size_t size;
155  is >> size;
156  rhs.clear();
157  for (size_t i=0; i<size; i++)
158  {
159  KeyT key;
160  ValueT value;
161  deserialize(is, key);
162  deserialize(is, value);
163  rhs[key] = value;
164  }
165 
166  return is;
167 }
168 
169 template <typename ValueT>
170 std::ostream& serialize(std::ostream& _ostr, const std::vector< ValueT >& _rhs)
171 {
172  _ostr << _rhs.size() << std::endl;
173  for (size_t i = 0; i < _rhs.size(); ++i)
174  serialize(_ostr, _rhs[i]) << std::endl;
175  return _ostr;
176 }
177 
178 template <typename ValueT>
179 std::istream& deserialize(std::istream& _istr, std::vector< ValueT >& _rhs)
180 {
181  size_t size;
182  _istr >> size;
183  _rhs.resize(size);
184  for (size_t i=0; i<size; i++)
185  deserialize(_istr,_rhs[i]);
186 
187  return _istr;
188 }
189 
190 
191 }