2 // Copyright 2012 Francisco Jerez
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
23 #include <type_traits>
26 #include "core/module.hpp"
28 using namespace clover
;
31 template<typename T
, typename
= void>
34 /// Serialize the specified object.
37 _proc(std::ostream
&os
, const T
&x
) {
38 _serializer
<T
>::proc(os
, x
);
41 /// Deserialize the specified object.
44 _proc(std::istream
&is
, T
&x
) {
45 _serializer
<T
>::proc(is
, x
);
50 _proc(std::istream
&is
) {
52 _serializer
<T
>::proc(is
, x
);
56 /// Calculate the size of the specified object.
59 _proc(module::size_t &sz
, const T
&x
) {
60 _serializer
<T
>::proc(sz
, x
);
63 /// (De)serialize a scalar value.
65 struct _serializer
<T
, typename
std::enable_if
<
66 std::is_scalar
<T
>::value
>::type
> {
68 proc(std::ostream
&os
, const T
&x
) {
69 os
.write(reinterpret_cast<const char *>(&x
), sizeof(x
));
73 proc(std::istream
&is
, T
&x
) {
74 is
.read(reinterpret_cast<char *>(&x
), sizeof(x
));
78 proc(module::size_t &sz
, const T
&x
) {
83 /// (De)serialize a vector.
85 struct _serializer
<std::vector
<T
>,
86 typename
std::enable_if
<
87 !std::is_scalar
<T
>::value
>::type
> {
89 proc(std::ostream
&os
, const std::vector
<T
> &v
) {
90 _proc
<uint32_t>(os
, v
.size());
92 for (size_t i
= 0; i
< v
.size(); i
++)
97 proc(std::istream
&is
, std::vector
<T
> &v
) {
98 v
.resize(_proc
<uint32_t>(is
));
100 for (size_t i
= 0; i
< v
.size(); i
++)
101 new(&v
[i
]) T(_proc
<T
>(is
));
105 proc(module::size_t &sz
, const std::vector
<T
> &v
) {
106 sz
+= sizeof(uint32_t);
108 for (size_t i
= 0; i
< v
.size(); i
++)
114 struct _serializer
<std::vector
<T
>,
115 typename
std::enable_if
<
116 std::is_scalar
<T
>::value
>::type
> {
118 proc(std::ostream
&os
, const std::vector
<T
> &v
) {
119 _proc
<uint32_t>(os
, v
.size());
120 os
.write(reinterpret_cast<const char *>(&v
[0]),
121 v
.size() * sizeof(T
));
125 proc(std::istream
&is
, std::vector
<T
> &v
) {
126 v
.resize(_proc
<uint32_t>(is
));
127 is
.read(reinterpret_cast<char *>(&v
[0]),
128 v
.size() * sizeof(T
));
132 proc(module::size_t &sz
, const std::vector
<T
> &v
) {
133 sz
+= sizeof(uint32_t) + sizeof(T
) * v
.size();
137 /// (De)serialize a string.
139 struct _serializer
<std::string
> {
141 proc(std::ostream
&os
, const std::string
&s
) {
142 _proc
<uint32_t>(os
, s
.size());
143 os
.write(&s
[0], s
.size() * sizeof(std::string::value_type
));
147 proc(std::istream
&is
, std::string
&s
) {
148 s
.resize(_proc
<uint32_t>(is
));
149 is
.read(&s
[0], s
.size() * sizeof(std::string::value_type
));
153 proc(module::size_t &sz
, const std::string
&s
) {
154 sz
+= sizeof(uint32_t) + sizeof(std::string::value_type
) * s
.size();
158 /// (De)serialize a module::section.
160 struct _serializer
<module::section
> {
161 template<typename S
, typename QT
>
171 /// (De)serialize a module::argument.
173 struct _serializer
<module::argument
> {
174 template<typename S
, typename QT
>
179 _proc(s
, x
.target_size
);
180 _proc(s
, x
.target_align
);
181 _proc(s
, x
.ext_type
);
182 _proc(s
, x
.semantic
);
186 /// (De)serialize a module::symbol.
188 struct _serializer
<module::symbol
> {
189 template<typename S
, typename QT
>
199 /// (De)serialize a module.
201 struct _serializer
<module
> {
202 template<typename S
, typename QT
>
213 module::serialize(std::ostream
&os
) const {
218 module::deserialize(std::istream
&is
) {
219 return _proc
<module
>(is
);
223 module::size() const {