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
169
170
171
172
173
|
#pragma once
#include <forstio/core/common.h>
#include <forstio/core/templates.h>
#include <vector>
#include "schema.h"
namespace saw {
namespace encode {
struct Native {};
}
/*
* Helper for the basic message container, so the class doesn't have to be
* specialized 10 times.
*/
template <class T> struct native_data_type;
template <>
struct native_data_type<schema::Primitive<schema::SignedInteger, 1>> {
using type = int8_t;
};
template <>
struct native_data_type<schema::Primitive<schema::SignedInteger, 2>> {
using type = int16_t;
};
template <>
struct native_data_type<schema::Primitive<schema::SignedInteger, 4>> {
using type = int32_t;
};
template <>
struct native_data_type<schema::Primitive<schema::SignedInteger, 8>> {
using type = int64_t;
};
template <>
struct native_data_type<schema::Primitive<schema::UnsignedInteger, 1>> {
using type = uint8_t;
};
template <>
struct native_data_type<schema::Primitive<schema::UnsignedInteger, 2>> {
using type = uint16_t;
};
template <>
struct native_data_type<schema::Primitive<schema::UnsignedInteger, 4>> {
using type = uint32_t;
};
template <>
struct native_data_type<schema::Primitive<schema::UnsignedInteger, 8>> {
using type = uint64_t;
};
template <>
struct native_data_type<schema::Primitive<schema::FloatingPoint, 4>> {
using type = float;
};
template <>
struct native_data_type<schema::Primitive<schema::FloatingPoint, 8>> {
using type = double;
};
template<typename T, typename Encoding = encode::Native>
class data {
private:
static_assert(always_false<T>, "Type not supported");
};
template<typename... T, string_literal... literals>
class data<schema::Struct<schema::Member<T, literals>...>, encode::Native> {
private:
std::tuple<data<T,encode::Native>...> value_;
public:
template<string_literal literal>
data<
typename parameter_pack_type<
parameter_key_pack_index<
literal, literals...
>::value
, T...>::type
, encode::Native>& get(){
return std::get<parameter_key_pack_index<literal, literals...>::value>(value_);
}
constexpr size_t size() const {
return sizeof...(T);
}
};
template<typename... T>
class data<schema::Tuple<T...>, encode::Native> {
private:
std::tuple<data<T,encode::Native>> value_;
public:
template<size_t i>
data<typename parameter_pack_type<i>::type, encode::Native>& get(){
return std::get<i>();
}
constexpr size_t size() const {
return sizeof...(T);
}
};
template<typename T>
class data<schema::Array<T>, encode::Native> {
private:
std::vector<data<T, encode::Native>> value_;
public:
data(size_t size_){
value_.resize(size_);
}
data<T, encode::Native>& at(size_t i){
return value_.at(i);
}
size_t size() const { return value_;}
};
template<>
class data<schema::String, encode::Native> {
private:
std::string value_;
public:
SAW_FORBID_COPY(data);
data() = default;
data(std::string value__):value_{std::move(value__)}{}
std::size_t size() const {
return value_.size();
}
char get_at(size_t i) const{
return value_.at(i);
}
void set_at(size_t i, char val){
value_.at(i) = val;
}
bool operator==(const data<schema::String, encode::Native>& data){
return value_ == data.value_;
}
};
template<typename T, size_t N>
class data<schema::Primitive<T,N>, encode::Native> {
private:
typename native_data_type<schema::Primitive<T,N>>::type value_;
public:
SAW_FORBID_COPY(data);
data(typename native_data_type<schema::Primitive<T,N>>::type value__):
value_{std::move(value__)}{}
void set(typename native_data_type<schema::Primitive<T,N>>::type val){
value_ = val;
}
typename native_data_type<schema::Primitive<T,N>>::type get() const {return value_;}
};
}
|