1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org//
2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Copyright 2012 Francisco Jerez
3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org//
4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Permission is hereby granted, free of charge, to any person obtaining a
5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// copy of this software and associated documentation files (the "Software"),
6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// to deal in the Software without restriction, including without limitation
7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// and/or sell copies of the Software, and to permit persons to whom the
9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Software is furnished to do so, subject to the following conditions:
10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org//
11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The above copyright notice and this permission notice shall be included in
12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// all copies or substantial portions of the Software.
13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org//
14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// SOFTWARE.
21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org//
22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <type_traits>
24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <algorithm>
25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "core/module.hpp"
27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgusing namespace clover;
29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace {
31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   template<typename T, typename = void>
32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct __serializer;
33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /// Serialize the specified object.
35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   template<typename T>
36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   __proc(compat::ostream &os, const T &x) {
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      __serializer<T>::proc(os, x);
39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /// Deserialize the specified object.
42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   template<typename T>
43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void
44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   __proc(compat::istream &is, T &x) {
45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      __serializer<T>::proc(is, x);
46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   template<typename T>
49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   T
50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   __proc(compat::istream &is) {
51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      T x;
52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      __serializer<T>::proc(is, x);
53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return x;
54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /// (De)serialize a scalar value.
57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   template<typename T>
58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct __serializer<T, typename std::enable_if<
59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                             std::is_scalar<T>::value>::type> {
60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      static void
61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      proc(compat::ostream &os, const T &x) {
62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         os.write(reinterpret_cast<const char *>(&x), sizeof(x));
63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      static void
66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      proc(compat::istream &is, T &x) {
67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         is.read(reinterpret_cast<char *>(&x), sizeof(x));
68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   };
70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /// (De)serialize a vector.
72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   template<typename T>
73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct __serializer<compat::vector<T>> {
74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      static void
75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      proc(compat::ostream &os, const compat::vector<T> &v) {
76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         __proc<uint32_t>(os, v.size());
77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         for (size_t i = 0; i < v.size(); i++)
79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            __proc<T>(os, v[i]);
80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      static void
83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      proc(compat::istream &is, compat::vector<T> &v) {
84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         v.reserve(__proc<uint32_t>(is));
85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         for (size_t i = 0; i < v.size(); i++)
87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            new(&v[i]) T(__proc<T>(is));
88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   };
90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /// (De)serialize a module::section.
92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   template<>
93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct __serializer<module::section> {
94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      template<typename S, typename QT>
95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      static void
96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      proc(S &s, QT &x) {
97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         __proc(s, x.type);
98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         __proc(s, x.size);
99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         __proc(s, x.data);
100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   };
102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /// (De)serialize a module::argument.
104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   template<>
105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct __serializer<module::argument> {
106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      template<typename S, typename QT>
107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      static void
108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      proc(S &s, QT &x) {
109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         __proc(s, x.type);
110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         __proc(s, x.size);
111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   };
113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /// (De)serialize a module::symbol.
115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   template<>
116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct __serializer<module::symbol> {
117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      template<typename S, typename QT>
118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      static void
119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      proc(S &s, QT &x) {
120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         __proc(s, x.section);
121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         __proc(s, x.offset);
122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         __proc(s, x.args);
123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   };
125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /// (De)serialize a module.
127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   template<>
128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct __serializer<module> {
129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      template<typename S, typename QT>
130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      static void
131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      proc(S &s, QT &x) {
132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         __proc(s, x.syms);
133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         __proc(s, x.secs);
134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   };
136f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org};
137f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
138f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace clover {
139f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void
140f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   module::serialize(compat::ostream &os) const {
141f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      __proc(os, *this);
142f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
143f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
144f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   module
145f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   module::deserialize(compat::istream &is) {
146f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return __proc<module>(is);
147f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
148f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
149f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   const module::symbol &
150f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   module::sym(compat::string name) const {
151f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      auto it = std::find_if(syms.begin(), syms.end(), [&](const symbol &x) {
152f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            return compat::string(x.name) == name;
153f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         });
154f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
155f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (it == syms.end())
156f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         throw noent_error();
157f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
158f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return *it;
159f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
160f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
161f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   const module::section &
162f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   module::sec(typename section::type type) const {
163f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      auto it = std::find_if(secs.begin(), secs.end(), [&](const section &x) {
164f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            return x.type == type;
165f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         });
166f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
167f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (it == secs.end())
168f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         throw noent_error();
169f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
170f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return *it;
171f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
172f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
173