summaryrefslogtreecommitdiff
path: root/c++/tools/c_gen_iface.hpp
blob: 3209505fd4246404d4ae4deb9e066d367e83cded (plain)
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
#include <forstio/core/error.h>
#include <forstio/core/templates.h>
#include <forstio/codec/schema.h>

#include <string>
#include <sstream>
#include <map>

#include <iostream>

namespace saw {

namespace impl {

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";
};

template<typename Schema>
struct c_data_translater {
	static_assert(always_false<Schema>, "Not supported");
};

template<typename T, size_t N>
struct c_data_translater<schema::Primitive<T,N>> {
	using Schema = schema::Primitive<T,N>;
	static error_or<void> generate(std::map<std::string,std::string>& map_str, std::string& str, const std::string& prefix){
		str += c_primitive_string<Schema>::value;
		return void_t{};
	}
};

template<typename T, size_t Dim>
struct c_data_translater<schema::Array<T,Dim>> {
	static error_or<void> generate(std::map<std::string, std::string>& map_str, std::string& type_str, const std::string& prefix){
		type_str = prefix + "_";
		type_str += "array_";

		std::string inner_type_str;
		auto eov = impl::c_data_translater<T>::generate(map_str, inner_type_str, prefix);
		if(eov.is_error()){
			return eov;
		}

		type_str += inner_type_str;

		if(Dim > 1){
			type_str += "_";
			type_str += std::to_string(Dim);
			type_str += "d";
		}

		if(map_str.find(type_str) != map_str.end()){
			return void_t{};
		}

		std::string str = "struct ";
		str += type_str;

		str += " {\n";

		str += "\t" + inner_type_str + "* data;\n";
		str += "\tsize_t size;\n";
		if( Dim > 1 ){
			str += "\tsize_t dims["+std::to_string(Dim)+"];\n";
		}

		str += "};\n";

		map_str.insert(std::make_pair(std::move(type_str), std::move(str)));
		return void_t{};
	}
};

template<typename Interface>
struct c_rpc_translater {
	static_assert(always_false<Interface>,"Not supported");
};

template<class Request, class Response>
struct c_rpc_translater<schema::Function<Request, Response>> {
	using Map = std::map<std::string, std::string>;

	static error_or<void> generate(const std::string& prefix, Map& type_map, Map& func_map){
		
		return void_t{};
	}
};

template<class... Requests, class... Responses, string_literal... Names>
struct c_rpc_translater<schema::Interface<schema::Member<schema::Function<Requests, Responses>, Names>...>> {
	using Map = std::map<std::string, std::string>;

	template<size_t i>
	static error_or<void> generate_member(const std::string& prefix, Map& type_map, Map& func_map){
		using Req = typename parameter_pack_type<i,Requests...>::type;
		using Resp = typename parameter_pack_type<i,Responses...>::type;
		constexpr string_literal lit = parameter_key_pack_type<i, Names...>::literal;

		std::string func_name{lit.view()};
		if(func_map.find(func_name) != func_map.end()){
			return make_error<err::already_exists>();
		}

		{
			std::string type_str;
			auto eov = c_rpc_translater<schema::Function<Req, Resp>>::generate(prefix, type_map, func_map);
			if(eov.is_error()){
				return eov;
			}
		}

		if constexpr ((i+1) < sizeof...(Requests) ){
			return generate_member<i+1>(prefix, type_map, func_map);
		}
		return void_t{};
	}

	static error_or<void> generate(const std::string& prefix){
		Map type_map, func_map;

		if constexpr ( sizeof...(Requests) > 0 ){
			return generate_member<0>(prefix, type_map, func_map);
		}

		return void_t{};
	}
};
}

template<typename Interface>
error_or<std::string> generate_c_rpc_interface(){
	std::stringstream iss;
	auto eov = impl::c_rpc_translater<Interface>::generate(iss, "c_saw");

	if(eov.is_error()){
		return std::move(eov.get_error());
	}

	try {
		return iss.str();
	}catch(const std::exception& e){
		// Do nothing for now
	}
	return make_error<err::out_of_memory>();
}

error_or<void> generate_array_example(){
	using Schema = schema::Array<schema::Int32,2>;
	
	using Schema2 = schema::Array<schema::Float64,5>;
	
	using Schema3 = schema::Array<schema::UInt16,1>;

	std::string prefix = "c_saw";
	std::map<std::string,std::string> map_str;
	{
		std::string type_str;
		auto eov = impl::c_data_translater<Schema>::generate(map_str, type_str, prefix);
		if(eov.is_error()){
			return eov;
		}
	}
	{
		std::string type_str;
		auto eov = impl::c_data_translater<Schema2>::generate(map_str, type_str, prefix);
		if(eov.is_error()){
			return eov;
		}
	}
	{
		std::string type_str;
		auto eov = impl::c_data_translater<Schema3>::generate(map_str, type_str, prefix);
		if(eov.is_error()){
			return eov;
		}
	}

	std::cout<<"Prefix: "<<prefix<<std::endl;

	for(auto& iter : map_str){
		std::cout<<"\nType: \""<<iter.first<<"\""<<std::endl;
		std::cout<<"Definition:\n\"\"\"\n";
		std::cout<<iter.second;
		std::cout<<"\"\"\""<<std::endl;
	}

	return void_t{};
}
}