forked from henson-insitu/henson
/
data.hpp
168 lines (137 loc) · 5.25 KB
/
data.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#ifndef HENSON_DATA_HPP
#define HENSON_DATA_HPP
#include <string>
#include <map>
#include <queue>
#include <vector>
#include <memory>
#include "variant.hpp"
#include "serialization.hpp"
namespace henson
{
using mpark::variant;
using mpark::variant_alternative_t;
using mpark::get;
using mpark::visit;
struct Array
{
using Address = variant<void*, float*, double*, int*, long*>;
Array(Address address_ = Address(), size_t type_ = 0, size_t count_ = 0, size_t stride_ = 0):
address(address_), type(type_), count(count_), stride(stride_) {}
struct get_void_ptr
{
void* operator()(void* x) const { return x; }
void* operator()(float* x) const { return x; }
void* operator()(double* x) const { return x; }
void* operator()(int* x) const { return x; }
void* operator()(long* x) const { return x; }
void* operator()(char* x) const { return x; }
};
void* void_ptr() const { return visit(get_void_ptr {}, address); }
// fill the storage with array contents
void copy()
{
storage = std::make_shared<std::vector<char>>(count*type);
char* char_ptr = static_cast<char*>(void_ptr());
for (size_t i = 0; i < count; ++i)
std::copy(char_ptr + i*stride, char_ptr + i*stride + type, storage->data() + i*type);
visit(replace_ptr {storage->data()}, address);
stride = type;
}
struct replace_ptr
{
void operator()(void*& x) const { x = static_cast<void*>(ptr); }
void operator()(float*& x) const { x = static_cast<float*>(ptr); }
void operator()(double*& x) const { x = static_cast<double*>(ptr); }
void operator()(int*& x) const { x = static_cast<int*>(ptr); }
void operator()(long*& x) const { x = static_cast<long*>(ptr); }
void operator()(char*& x) const { x = static_cast<char*>(ptr); }
void* ptr;
};
Address address;
size_t type;
size_t count;
size_t stride;
std::shared_ptr<std::vector<char>> storage {}; // necessary for de-serialization
};
template<>
struct Serialization< Array >
{
static void save(BinaryBuffer& bb, const Array& x)
{
henson::save(bb, x.address.index());
henson::save(bb, x.type);
henson::save(bb, x.count);
for (size_t i = 0; i < x.count; ++i)
henson::save(bb, static_cast<char*>(x.void_ptr()) + i*x.stride, x.type);
}
static void load(BinaryBuffer& bb, Array& x)
{
size_t index;
henson::load(bb, index);
henson::load(bb, x.type);
henson::load(bb, x.count);
x.storage = std::make_shared<std::vector<char>>(x.count*x.type);
henson::load(bb, x.storage->data(), x.storage->size());
void* data = x.storage->data();
if (index == 0)
x.address = static_cast<variant_alternative_t<0, Array::Address>>(data);
else if (index == 1)
x.address = static_cast<variant_alternative_t<1, Array::Address>>(data);
else if (index == 2)
x.address = static_cast<variant_alternative_t<2, Array::Address>>(data);
else if (index == 3)
x.address = static_cast<variant_alternative_t<3, Array::Address>>(data);
else if (index == 4)
x.address = static_cast<variant_alternative_t<4, Array::Address>>(data);
x.stride = x.type;
}
};
using Value = variant<int, size_t, void*, float, double, Array, std::string>;
class NameMap
{
public:
typedef std::map<std::string, Value> DataMap;
typedef std::queue<Value> ValueQueue;
typedef std::map<std::string, ValueQueue> QueuesMap;
public:
void add(const std::string& name, Value value)
{
auto it = queues_.find(name);
if (it != queues_.end())
it->second.push(value);
else
values_[name] = value;
}
Value get(const std::string& name)
{
auto it = queues_.find(name);
if (it != queues_.end())
{
auto& q = it->second;
Value v = q.front();
q.pop();
return v;
}
else return
values_.find(name)->second;
}
void create_queue(const std::string& name) { queues_.emplace(name, ValueQueue()); }
bool queue_empty(const std::string& name) const { return queues_.find(name)->second.empty(); }
bool exists(const std::string& name) const { return values_.find(name) != values_.end() || queues_.find(name) != queues_.end(); }
void clear() { values_.clear(); queues_.clear(); }
private:
DataMap values_;
QueuesMap queues_;
};
bool exists(const std::string& name);
void save(const std::string& name, Value x);
template<class T>
void save(const std::string& name, const std::vector<T>& v)
{
save(name, Array(const_cast<T*>(v.data()), sizeof(T), v.size(), sizeof(T)));
}
NameMap* get_namemap();
Value load(const std::string& name);
}
#endif