-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathjumppad.h
More file actions
204 lines (174 loc) · 5.06 KB
/
jumppad.h
File metadata and controls
204 lines (174 loc) · 5.06 KB
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
// Copyright (C) 2016-2018 Egor Pugin <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#pragma once
#include <primitives/exceptions.h>
#include <primitives/filesystem.h>
#include <gsl/span>
#include <functional>
#ifdef SW_PACKAGE_API
#define SW_JUMPPAD_API SW_PACKAGE_API
#else
#define SW_JUMPPAD_API SW_BUILDER_API
#endif
#define SW_JUMPPAD_PREFIX _sw_fn_jumppad_
#define SW_JUMPPAD_DEFAULT_FUNCTION_VERSION 0
// strict macro
// M(visible name, function name in code, ...)
#ifdef _MSC_VER
#define SW_DEFINE_VISIBLE_FUNCTION_JUMPPAD(n, f, ...) \
extern "C" SW_JUMPPAD_API int CONCATENATE(SW_JUMPPAD_PREFIX, n)(const Strings &s) \
{ \
::sw::VisibleFunctionJumppad j(&f, #n, __VA_ARGS__); \
return j.call(s); \
}
#else
#define SW_DEFINE_VISIBLE_FUNCTION_JUMPPAD(n, f, ...) \
extern "C" SW_JUMPPAD_API int CONCATENATE(SW_JUMPPAD_PREFIX, n)(const Strings &s) \
{ \
::sw::VisibleFunctionJumppad j(&f, #n, ##__VA_ARGS__); \
return j.call(s); \
}
#endif
namespace sw
{
namespace detail
{
template <class T>
inline T from_string(gsl::span<const String> &s)
{
auto v = s[0];
s = s.subspan(1);
return v;
}
template <>
inline int from_string(gsl::span<const String> &s)
{
auto v = s[0];
s = s.subspan(1);
return std::stoi(v);
}
template <>
inline int64_t from_string(gsl::span<const String> &s)
{
auto v = s[0];
s = s.subspan(1);
return std::stoll(v);
}
template <>
inline Strings from_string(gsl::span<const String> &s)
{
auto b = s.begin();
Strings f;
auto n = std::stoi(*b++);
while (n--)
f.push_back(*b++);
s = s.subspan(b - s.begin());
return f;
}
template <>
inline Files from_string(gsl::span<const String> &s)
{
auto b = s.begin();
Files f;
auto n = std::stoi(*b++);
while (n--)
f.insert(*b++);
s = s.subspan(b - s.begin());
return f;
}
template <typename Tuple, std::size_t... I>
inline auto strings2tuple(gsl::span<const String> &s, std::index_sequence<I...>)
{
return std::tuple{ from_string<std::tuple_element_t<I, Tuple>>(s)... };
}
template <class T>
inline size_t get_n_arg(gsl::span<const String> &s)
{
s = s.subspan(1);
return 1;
}
template <>
inline size_t get_n_arg<Strings>(gsl::span<const String> &s)
{
try
{
auto n = std::stoi(*s.begin());
s = s.subspan(n + 1);
}
catch (...)
{
// on invalid number, we consider this as zero
return 0;
}
return 1;
}
template <>
inline size_t get_n_arg<Files>(gsl::span<const String> &s)
{
try
{
auto n = std::stoi(*s.begin());
s = s.subspan(n + 1);
}
catch (...)
{
// on invalid number, we consider this as zero
return 0;
}
return 1;
}
template <class T, class ... ArgTypes>
inline size_t get_n_args2(gsl::span<const String> &s)
{
if (s.empty())
return 0;
auto n = get_n_arg<T>(s);
if constexpr (sizeof...(ArgTypes) > 0)
n += get_n_args2<ArgTypes...>(s);
return n;
}
template <class ... ArgTypes>
inline size_t get_n_args(gsl::span<const String> &s)
{
if constexpr (sizeof...(ArgTypes) == 0)
return 0;
else
return get_n_args2<ArgTypes...>(s);
}
}
template <class>
struct VisibleFunctionJumppad;
template <class R, class ... ArgTypes>
struct VisibleFunctionJumppad<R(ArgTypes...)>
{
std::function<R(ArgTypes...)> f;
String name;
int version;
VisibleFunctionJumppad(std::function<R(ArgTypes...)> f, const String &n, int v = SW_JUMPPAD_DEFAULT_FUNCTION_VERSION)
: f(f), name(n), version(v)
{}
R call(const Strings &s = {})
{
auto sp = gsl::make_span(s);
auto sp2 = sp; // need a copy!
auto nargs = detail::get_n_args<ArgTypes...>(sp2);
if (sizeof...(ArgTypes) != nargs)
{
throw SW_RUNTIME_ERROR("pf call: " + name + ", version: " + std::to_string(version) + ": incorrect number of arguments " +
std::to_string(nargs) + ", expected " + std::to_string(sizeof...(ArgTypes)));
}
return std::apply(f,
detail::strings2tuple<std::tuple<ArgTypes...>>(sp,
std::make_index_sequence<sizeof...(ArgTypes)>{}));
}
};
template <class R, class ... ArgTypes>
VisibleFunctionJumppad(R(*)(ArgTypes...), const String &, int = SW_JUMPPAD_DEFAULT_FUNCTION_VERSION)->VisibleFunctionJumppad<R(ArgTypes...)>;
SW_BUILDER_API
int jumppad_call(const path &module, const String &name, int version, const Strings &s = {});
SW_BUILDER_API
int jumppad_call(const Strings &s);
}