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(compat::ostream
&os
, const T
&x
) {
38 __serializer
<T
>::proc(os
, x
);
41 /// Deserialize the specified object.
44 __proc(compat::istream
&is
, T
&x
) {
45 __serializer
<T
>::proc(is
, x
);
50 __proc(compat::istream
&is
) {
52 __serializer
<T
>::proc(is
, x
);
56 /// (De)serialize a scalar value.
58 struct __serializer
<T
, typename
std::enable_if
<
59 std::is_scalar
<T
>::value
>::type
> {
61 proc(compat::ostream
&os
, const T
&x
) {
62 os
.write(reinterpret_cast<const char *>(&x
), sizeof(x
));
66 proc(compat::istream
&is
, T
&x
) {
67 is
.read(reinterpret_cast<char *>(&x
), sizeof(x
));
71 /// (De)serialize a vector.
73 struct __serializer
<compat::vector
<T
>> {
75 proc(compat::ostream
&os
, const compat::vector
<T
> &v
) {
76 __proc
<uint32_t>(os
, v
.size());
78 for (size_t i
= 0; i
< v
.size(); i
++)
83 proc(compat::istream
&is
, compat::vector
<T
> &v
) {
84 v
.reserve(__proc
<uint32_t>(is
));
86 for (size_t i
= 0; i
< v
.size(); i
++)
87 new(&v
[i
]) T(__proc
<T
>(is
));
91 /// (De)serialize a module::section.
93 struct __serializer
<module::section
> {
94 template<typename S
, typename QT
>
104 /// (De)serialize a module::argument.
106 struct __serializer
<module::argument
> {
107 template<typename S
, typename QT
>
115 /// (De)serialize a module::symbol.
117 struct __serializer
<module::symbol
> {
118 template<typename S
, typename QT
>
122 __proc(s
, x
.section
);
128 /// (De)serialize a module.
130 struct __serializer
<module
> {
131 template<typename S
, typename QT
>
142 module::serialize(compat::ostream
&os
) const {
147 module::deserialize(compat::istream
&is
) {
148 return __proc
<module
>(is
);
151 const module::symbol
&
152 module::sym(compat::string name
) const {
153 auto it
= std::find_if(syms
.begin(), syms
.end(), [&](const symbol
&x
) {
154 return compat::string(x
.name
) == name
;
157 if (it
== syms
.end())
163 const module::section
&
164 module::sec(typename
section::type type
) const {
165 auto it
= std::find_if(secs
.begin(), secs
.end(), [&](const section
&x
) {
166 return x
.type
== type
;
169 if (it
== secs
.end())