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 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19
// OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
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>
103
/// (De)serialize a module::argument.
105
struct __serializer<module::argument> {
106
template<typename S, typename QT>
114
/// (De)serialize a module::symbol.
116
struct __serializer<module::symbol> {
117
template<typename S, typename QT>
120
__proc(s, x.section);
126
/// (De)serialize a module.
128
struct __serializer<module> {
129
template<typename S, typename QT>
140
module::serialize(compat::ostream &os) const {
145
module::deserialize(compat::istream &is) {
146
return __proc<module>(is);
149
const module::symbol &
150
module::sym(compat::string name) const {
151
auto it = std::find_if(syms.begin(), syms.end(), [&](const symbol &x) {
152
return compat::string(x.name) == name;
155
if (it == syms.end())
161
const module::section &
162
module::sec(typename section::type type) const {
163
auto it = std::find_if(secs.begin(), secs.end(), [&](const section &x) {
164
return x.type == type;
167
if (it == secs.end())