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>
25 #include "core/module.hpp"
27 using namespace clover;
30 template<typename T, typename = void>
33 /// Serialize the specified object.
36 _proc(compat::ostream &os, const T &x) {
37 _serializer<T>::proc(os, x);
40 /// Deserialize the specified object.
43 _proc(compat::istream &is, T &x) {
44 _serializer<T>::proc(is, x);
49 _proc(compat::istream &is) {
51 _serializer<T>::proc(is, x);
55 /// (De)serialize a scalar value.
57 struct _serializer<T, typename std::enable_if<
58 std::is_scalar<T>::value>::type> {
60 proc(compat::ostream &os, const T &x) {
61 os.write(reinterpret_cast<const char *>(&x), sizeof(x));
65 proc(compat::istream &is, T &x) {
66 is.read(reinterpret_cast<char *>(&x), sizeof(x));
70 /// (De)serialize a vector.
72 struct _serializer<compat::vector<T>> {
74 proc(compat::ostream &os, const compat::vector<T> &v) {
75 _proc<uint32_t>(os, v.size());
77 for (size_t i = 0; i < v.size(); i++)
82 proc(compat::istream &is, compat::vector<T> &v) {
83 v.reserve(_proc<uint32_t>(is));
85 for (size_t i = 0; i < v.size(); i++)
86 new(&v[i]) T(_proc<T>(is));
90 /// (De)serialize a module::section.
92 struct _serializer<module::section> {
93 template<typename S, typename QT>
103 /// (De)serialize a module::argument.
105 struct _serializer<module::argument> {
106 template<typename S, typename QT>
111 _proc(s, x.target_size);
112 _proc(s, x.target_align);
113 _proc(s, x.ext_type);
117 /// (De)serialize a module::symbol.
119 struct _serializer<module::symbol> {
120 template<typename S, typename QT>
130 /// (De)serialize a module.
132 struct _serializer<module> {
133 template<typename S, typename QT>
144 module::serialize(compat::ostream &os) const {
149 module::deserialize(compat::istream &is) {
150 return _proc<module>(is);