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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
#include <forstio/error.hpp>
#include <forstio/codec/data.hpp>
#include <forstio/codec/schema.hpp>
#include <forstio/codec/schema_stringify.hpp>
#include <forstio/templates.hpp>
#include <string>
#include <sstream>
#include <map>
#include <iostream>
namespace saw {
namespace schema {
using CVar = Struct<
Member<schema::String, "schema">,
Member<schema::String, "type">,
Member<schema::String, "name">
>;
using CStruct = Struct<
Member<schema::String, "schema">,
Member<schema::String, "name">,
Member<Array<CVar>, "members">
>;
using CFunction = Struct<
Member<schema::String, "schema">,
Member<schema::String, "name">,
Member<Array<CVar>, "params">,
Member<CVar, "return">
>;
using CIface = Struct<
Member<schema::String, "schema">,
Member<Array<CStruct>,"structs">,
Member<Array<CFunction>,"functions">
>;
}
/**
* Type meant to provide future help if I decide to introduce more maps
*/
/**
namespace schema {
using namespace saw::schema;
Pseudo flattened
using Foo = Struct<
Member<Array<Int32>, "a">
Member<Array<Float32>, "b">
>;
Needs to be flattened to
template<typename InnerSchema>
using FlattenedSchemaElement = Struct<
Member<Array<String>, "path">,
Member<InnerSchema, "inner_schema">
>;
// Illegal, but doable with more lines of code
// Just use typename... T and
// "T..." for
// "Member<FlattenedSchemaElement<Ele>,Names>...>"
// and specialize somewhere else
template<typename... Ele, string_literal... Names>
using FlattenedSchema = Struct<
Member<String, "top_schema">,
Member<FlattenedSchemaElement<Ele>, Names>...
>;
}
*/
template<typename T, typename Res>
struct schema_flattener {
static_assert(always_false<T>, "Not supported");
};
template<typename T0, string_literal Name0, typename... T, string_literal... Names, typename Res>
struct schema_flattener<schema::Struct<schema::Member<T0,Name0>,schema::Member<T,Names>...>, Res> {
};
/**
* Helper to determine if we are dealing with primitive types
*/
template<typename Schema>
struct c_is_primitive {
static constexpr bool value = false;
};
template<typename T, size_t N>
struct c_is_primitive<schema::Primitive<T,N>> {
static constexpr bool value = true;
};
template<typename Schema>
struct c_primitive_string {
static_assert(always_false<Schema>, "Not supported");
};
template<>
struct c_primitive_string<schema::Int8> {
static constexpr std::string_view value = "int8_t";
};
template<>
struct c_primitive_string<schema::Int16> {
static constexpr std::string_view value = "int16_t";
};
template<>
struct c_primitive_string<schema::Int32> {
static constexpr std::string_view value = "int32_t";
};
template<>
struct c_primitive_string<schema::Int64> {
static constexpr std::string_view value = "int64_t";
};
template<>
struct c_primitive_string<schema::UInt8> {
static constexpr std::string_view value = "uint8_t";
};
template<>
struct c_primitive_string<schema::UInt16> {
static constexpr std::string_view value = "uint16_t";
};
template<>
struct c_primitive_string<schema::UInt32> {
static constexpr std::string_view value = "uint32_t";
};
template<>
struct c_primitive_string<schema::UInt64> {
static constexpr std::string_view value = "uint64_t";
};
template<>
struct c_primitive_string<schema::Float32> {
static constexpr std::string_view value = "float";
};
template<>
struct c_primitive_string<schema::Float64> {
static constexpr std::string_view value = "double";
};
bool c_interface_function_exists(data<schema::CIface>& state, const std::string_view& name){
auto& funcs = state.template get<"functions">();
for(uint64_t i = 0; i < funcs.size(); ++i){
if(funcs.at(i).get<"name">() == name){
return true;
}
}
return false;
}
template<typename Schema>
struct c_data_translater {
static_assert(always_false<Schema>, "Not supported");
};
template<typename T, uint64_t N>
struct c_data_translater<schema::Primitive<T,N>> {
using Schema = schema::Primitive<T,N>;
static error_or<void> generate(data<schema::CIface>& state, data<schema::CVar>& prim){
/// @TODO Check if exists in CVars already
try{
std::stringstream iss;
schema_stringify<Schema>::apply(iss);
prim.template get<"schema">().set(iss.str());
}catch(const std::exception&){
return make_error<err::out_of_memory>();
}
return void_t{};
}
};
template<typename Req, typename Ret>
struct c_data_translater<schema::Function<Req,Ret>> {
using Schema = schema::Function<Req,Ret>;
static error_or<void> generate(data<schema::CIface>& state, const std::string_view& func_name){
if(c_interface_function_exists(state, func_name)){
return make_error<err::invalid_state>("Function already exists");
}
auto& funcs = state.template get<"functions">();
data<schema::CFunction> function;
function.template get<"name">().set(std::string{func_name});
try{
std::stringstream iss;
schema_stringify<Schema>::apply(iss);
function.template get<"schema">().set(iss.str());
}catch(const std::exception&){
return make_error<err::out_of_memory>();
}
// Return value
{
auto& c_var = function.template get<"return">();
c_var.template get<"name">().set("ret_val");
auto eov = c_data_translater<Ret>::generate(state, c_var);
if(eov.is_error()){
return eov;
}
}
// Request values
{
}
{
auto eov = funcs.add(std::move(function));
if(eov.is_error()){
return eov;
}
}
return void_t{};
}
};
template<typename... Funcs, string_literal... Names>
struct c_data_translater<schema::Interface<schema::Member<Funcs,Names>...>> {
using Schema = schema::Interface<schema::Member<Funcs, Names>...>;
template<std::size_t i>
static error_or<void> generate_ele(data<schema::CIface>& state){
using InnerSchema = typename parameter_pack_type<i, Funcs...>::type;
constexpr string_literal Literal = parameter_key_pack_type<i, Names...>::literal;
{
auto eov = c_data_translater<InnerSchema>::generate(state, Literal.view());
if(eov.is_error()){
return eov;
}
}
/**
* Continue if elements remain or finish
*/
if constexpr ( (i+1) < sizeof...(Funcs) ){
return generate_ele<i+1>(state);
}
return void_t{};
}
static error_or<void> generate(data<schema::CIface>& state){
try{
std::stringstream iss;
schema_stringify<Schema>::apply(iss);
state.template get<"schema">().set(iss.str());
}catch(const std::exception&){
return make_error<err::out_of_memory>();
}
if constexpr (sizeof...(Funcs) > 0){
return generate_ele<0>(state);
}
return void_t{};
}
};
template<typename Interface>
error_or<void> generate_c_interface(data<schema::CIface>& state){
auto eov = c_data_translater<Interface>::generate(state);
return eov;
}
}
|