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/binary.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(binary::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(binary::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(binary::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(binary::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(binary::size_t &sz, const std::string &s) {
154
sz += sizeof(uint32_t) + sizeof(std::string::value_type) * s.size();
158
/// (De)serialize a printf format
160
struct _serializer<binary::printf_info> {
161
template<typename S, typename QT>
164
_proc(s, x.arg_sizes);
169
/// (De)serialize a binary::section.
171
struct _serializer<binary::section> {
172
template<typename S, typename QT>
182
/// (De)serialize a binary::argument.
184
struct _serializer<binary::argument> {
185
template<typename S, typename QT>
190
_proc(s, x.target_size);
191
_proc(s, x.target_align);
192
_proc(s, x.ext_type);
193
_proc(s, x.semantic);
197
/// (De)serialize a binary::symbol.
199
struct _serializer<binary::symbol> {
200
template<typename S, typename QT>
204
_proc(s, x.attributes);
205
_proc(s, x.reqd_work_group_size);
212
/// (De)serialize a binary.
214
struct _serializer<binary> {
215
template<typename S, typename QT>
220
_proc(s, x.printf_infos);
221
_proc(s, x.printf_strings_in_buffer);
228
binary::serialize(std::ostream &os) const {
233
binary::deserialize(std::istream &is) {
234
return _proc<binary>(is);
238
binary::size() const {