SerializersT_impl.hh 6.86 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*===========================================================================*\
 *                                                                           *
 *                            OpenVolumeMesh                                 *
 *        Copyright (C) 2011 by Computer Graphics Group, RWTH Aachen         *
 *                        www.openvolumemesh.org                             *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenVolumeMesh.                                     *
 *                                                                           *
 *  OpenVolumeMesh is free software: you can redistribute it and/or modify   *
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenVolumeMesh is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenVolumeMesh.  If not,                              *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $Revision: 236 $                                                         *
 *   $Date: 2013-02-19 12:32:33 +0100 (Tue, 19 Feb 2013) $                    *
 *   $LastChangedBy: kremer $                                                *
 *                                                                           *
\*===========================================================================*/

#define SERIALIZERST_CC

#include "Serializers.hh"

namespace OpenVolumeMesh
{

Max Lyon's avatar
Max Lyon committed
50
51
52
template<typename T>
T& decllval();

Max Lyon's avatar
Max Lyon committed
53
54
55
template <bool B> struct bool_type;
template <>       struct bool_type<true>  { char c[1]; };
template <>       struct bool_type<false> { char c[2]; };
Max Lyon's avatar
Max Lyon committed
56
57
58
59
60
61
62
63

typedef bool_type<true>  true_type;
typedef bool_type<false> false_type;

template <typename Stream, typename T>
class has_input_operator
{
private:
Max Lyon's avatar
Max Lyon committed
64
  template<class U> static true_type  test(char(*)[sizeof(decllval<Stream>() >> decllval<U>(), void(), 0)]);
Max Lyon's avatar
Max Lyon committed
65
66
67
  template<class U> static false_type test(...);

public:
68
  enum { bool_value = sizeof(true_type) == sizeof(test<T>(nullptr)) };
Max Lyon's avatar
Max Lyon committed
69
70
71
72
  typedef bool_type<bool_value> type;
  static type value;
};

Max Lyon's avatar
Max Lyon committed
73
74
75
76
template <typename Stream, typename T>
typename has_input_operator<Stream, T>::type has_input_operator<Stream, T>::value = typename has_input_operator<Stream, T>::type();


Max Lyon's avatar
Max Lyon committed
77
78
79
80
template <typename Stream, typename T>
class has_output_operator
{
private:
Max Lyon's avatar
Max Lyon committed
81
  template<class U> static true_type  test(char(*)[sizeof(decllval<Stream>() << decllval<U>(), void(), 0)]);
Max Lyon's avatar
Max Lyon committed
82
83
84
  template<class U> static false_type test(...);

public:
85
  enum { bool_value = sizeof(true_type) == sizeof(test<T>(nullptr)) };
Max Lyon's avatar
Max Lyon committed
86
87
88
89
  typedef bool_type<bool_value> type;
  static type value;
};

Max Lyon's avatar
Max Lyon committed
90
91
92
template <typename Stream, typename T>
typename has_output_operator<Stream, T>::type has_output_operator<Stream, T>::value = typename has_output_operator<Stream, T>::type();

Max Lyon's avatar
Max Lyon committed
93
94
95
96
97
98
99
100
101

template <typename ValueT>
std::ostream& serialize_helper(std::ostream& _ostr, ValueT& _rhs, true_type)
{
  _ostr << _rhs;
  return _ostr;
}

template <typename ValueT>
Max Lyon's avatar
Max Lyon committed
102
std::ostream& serialize_helper(std::ostream& _ostr, ValueT&, false_type)
Max Lyon's avatar
Max Lyon committed
103
104
105
106
107
{
  std::cout << "Warning: trying to serialize a type that does not have a serialize function" << std::endl;
  return _ostr;
}

108
109
110
template <typename ValueT>
std::ostream& serialize(std::ostream& _ostr, const ValueT& _rhs)
{
Max Lyon's avatar
Max Lyon committed
111
    return serialize_helper(_ostr, _rhs, has_output_operator<std::ostream, ValueT>::value);
112
113
114
115
}


template <typename ValueT>
Max Lyon's avatar
Max Lyon committed
116
std::istream& deserialize_helper(std::istream& _istr, ValueT& _rhs, true_type)
117
{
Max Lyon's avatar
Max Lyon committed
118
119
120
121
122
  _istr >> _rhs;
  return _istr;
}

template <typename ValueT>
Max Lyon's avatar
Max Lyon committed
123
std::istream& deserialize_helper(std::istream& _istr, ValueT&, false_type)
Max Lyon's avatar
Max Lyon committed
124
125
126
{
  std::cout << "Warning: trying to deserialize a type that does not have a deserialize function" << std::endl;
  return _istr;
127
128
}

Max Lyon's avatar
Max Lyon committed
129
130
131
132
133
template <typename ValueT>
std::istream& deserialize(std::istream& _istr, ValueT& _rhs)
{
  return deserialize_helper(_istr, _rhs, has_input_operator<std::istream, ValueT>::value);
}
134
135

template <typename KeyT, typename ValueT>
Max Lyon's avatar
Max Lyon committed
136
std::ostream& serialize(std::ostream& os, const std::map< KeyT, ValueT >& rhs)
137
138
139
140
141
142
143
144
145
146
147
148
149
150
{
    os << rhs.size() << std::endl;
    for (typename std::map< KeyT, ValueT >::const_iterator it = rhs.begin();
         it != rhs.end();
         ++it)
    {
        serialize(os,it->first) << std::endl;
        serialize(os, it->second) << std::endl;
    }

    return os;
}

template <typename KeyT, typename ValueT>
Max Lyon's avatar
Max Lyon committed
151
std::istream& deserialize(std::istream& is, std::map< KeyT, ValueT >& rhs)
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
{

    size_t size;
    is >> size;
    rhs.clear();
    for (size_t i=0; i<size; i++)
    {
        KeyT key;
        ValueT value;
        deserialize(is, key);
        deserialize(is, value);
        rhs[key] = value;
    }

    return is;
}

template <typename ValueT>
Max Lyon's avatar
Max Lyon committed
170
std::ostream& serialize(std::ostream& _ostr, const std::vector< ValueT >& _rhs)
171
172
173
174
175
176
177
178
{
    _ostr << _rhs.size() << std::endl;
    for (size_t i = 0; i < _rhs.size(); ++i)
        serialize(_ostr, _rhs[i]) << std::endl;
    return _ostr;
}

template <typename ValueT>
Max Lyon's avatar
Max Lyon committed
179
std::istream& deserialize(std::istream& _istr, std::vector< ValueT >& _rhs)
180
181
182
183
184
185
186
187
188
189
190
191
{
    size_t size;
    _istr >> size;
    _rhs.resize(size);
    for (size_t i=0; i<size; i++)
        deserialize(_istr,_rhs[i]);

    return _istr;
}


}