17 #ifdef V8TOOLKIT_PARAMETER_BUILDER_LOGGING 18 #define PB_PRINT(format_string, ...) \ 19 std::cerr << fmt::format(format_string, ##__VA_ARGS__) << std::endl; 21 #define PB_PRINT(format_string, ...) 34 template<
class T,
class =
void>
43 struct ParameterBuilder<T &, std::enable_if_t<!std::is_same_v<std::remove_const_t<T>,
char> && !is_wrapped_type_v<T>>> {
44 template<
int default_arg_position = -1,
class DefaultArgsTuple = std::tuple<>>
45 T &
operator()(
const v8::FunctionCallbackInfo<v8::Value> & info,
int & i,
46 std::vector<std::unique_ptr<StuffBase>> & stuff,
47 DefaultArgsTuple && default_args_tuple = DefaultArgsTuple()) {
48 PB_PRINT(
"ParameterBuilder handling lvalue reference: {}", demangle<T>());
55 struct ParameterBuilder<T &, std::enable_if_t<std::is_same_v<std::remove_const_t<T>,
char> && !is_wrapped_type_v<T>>> {
56 template<
int default_arg_position = -1,
class DefaultArgsTuple = std::tuple<>>
57 T &
operator()(
const v8::FunctionCallbackInfo<v8::Value> & info,
int & i,
58 std::vector<std::unique_ptr<StuffBase>> & stuff,
59 DefaultArgsTuple && default_args_tuple = DefaultArgsTuple()) {
60 PB_PRINT(
"ParameterBuilder handling lvalue reference for char: {}", demangle<T>());
61 auto value = info[i++];
62 auto isolate = info.GetIsolate();
64 return *
static_cast<Stuff<char> &
>(*stuff.back()).
get();
73 template<
int default_arg_position = -1,
class DefaultArgsTuple = std::tuple<>>
74 T &&
operator()(
const v8::FunctionCallbackInfo<v8::Value> & info,
int & i,
75 std::vector<std::unique_ptr<StuffBase>> & stuff,
76 DefaultArgsTuple && default_args_tuple = DefaultArgsTuple()) {
78 PB_PRINT(
"ParameterBuilder handling rvalue reference to unwrapped type: {}", demangle<T>());
92 using WrappedT = std::remove_const_t<std::remove_pointer_t<std::remove_reference_t<T>>>;
94 template<
int default_arg_position = -1,
class DefaultArgsTuple = std::tuple<>>
96 std::vector<std::unique_ptr<StuffBase>> & stuff,
97 DefaultArgsTuple && default_args_tuple = DefaultArgsTuple()) {
99 PB_PRINT(
"ParameterBuilder handling pointers to unwrapped types: {}", demangle<T>());
100 if (i >= info.Length()) {
101 return get_default_parameter<WrappedT, default_arg_position>(info, i, stuff, default_args_tuple);
119 using WrappedT = std::remove_pointer_t<std::remove_reference_t<T>>;
121 template<
int default_arg_position = -1,
class DefaultArgsTuple = std::tuple<>>
123 std::vector<std::unique_ptr<StuffBase>> & stuff,
124 DefaultArgsTuple && default_args_tuple = DefaultArgsTuple()) {
126 PB_PRINT(
"ParameterBuilder handling pointer to wrapped type: {}", demangle<T>());
129 if (i >= info.Length()) {
130 return *get_default_parameter<WrappedT *, default_arg_position>(info, i, stuff, default_args_tuple);
135 return wrapped_pointer;
141 throw CastException(
"Tried to send a pointer to a function but the JavaScript object wasn't a wrapped " 142 "C++ object and a new object couldn't be created from the JavaScript value provided");
151 struct ParameterBuilder<T,
std::enable_if_t<!std::is_pointer_v<T> && !std::is_reference_v<T> && !is_wrapped_type_v<T>>> {
152 using NoRefT = std::remove_reference_t<T>;
155 template<
int default_arg_position = -1,
class DefaultArgsTuple = std::tuple<>>
156 T
operator()(
const v8::FunctionCallbackInfo<v8::Value> & info,
int & i,
157 std::vector<std::unique_ptr<StuffBase>> & stuff,
158 DefaultArgsTuple && default_args_tuple = DefaultArgsTuple()) {
160 PB_PRINT(
"ParameterBuilder handling type: {}", demangle<T>());
161 if (i < info.Length()) {
164 }
else if constexpr(default_arg_position >= 0 && default_arg_position < std::tuple_size_v<DefaultArgsTuple>) {
166 std::cerr << fmt::format(
"getting default value from default args tuple: {} => {}",
168 std::get<(std::size_t) default_arg_position>(default_args_tuple)) << std::endl;
170 return std::get<(std::size_t) default_arg_position>(default_args_tuple);
172 throw CastException(
"Not enough parameters and no default value for {}", demangle<T>());
184 template<
int default_arg_position = -1,
class DefaultArgsTuple = std::tuple<>>
185 char *
operator()(
const v8::FunctionCallbackInfo<v8::Value> & info,
int & i,
186 std::vector<std::unique_ptr<StuffBase>> & stuff,
187 DefaultArgsTuple && default_args_tuple = DefaultArgsTuple()) {
189 PB_PRINT(
"ParameterBuilder handling char *");
194 if (i >= info.Length()) {
195 return *get_default_parameter<T, default_arg_position>(info, i, stuff, default_args_tuple);
199 stuff.emplace_back(std::make_unique<
Stuff<decltype(
string)>>(
std::move(
string)));
207 template<
template<
class,
class...>
class Container,
class... Rest>
208 struct ParameterBuilder<Container<char *, Rest...>, std::enable_if_t<!std::is_reference<std::result_of_t<
209 CastToNative<std::remove_const_t<std::remove_reference_t<Container<
char *, Rest...>>>>(v8::Isolate *,
219 template<
int default_arg_position,
class DefaultArgsTuple = std::tuple<>>
221 std::vector<std::unique_ptr<StuffBase>> & stuffs,
222 DefaultArgsTuple && default_args_tuple = DefaultArgsTuple()) {
224 PB_PRINT(
"ParameterBuilder handling container of char *");
228 if (i >= info.Length()) {
231 "Not enough javascript parameters for function call - requires {} but only {} were specified",
232 i + 1 +
sizeof(Rest)..., info.Length()));
235 auto data_holder = stuff.
get();
241 for (
auto & str : data_holder) {
242 result.push_back(str->c_str());
248 template<
template<
class,
class...>
class Container,
class... Rest>
250 std::enable_if_t<!std::is_reference<std::result_of_t<
260 template<
int default_arg_position,
class DefaultArgsTuple = std::tuple<>>
262 std::vector<std::unique_ptr<StuffBase>> & stuffs,
263 DefaultArgsTuple && default_args_tuple = DefaultArgsTuple()) {
265 PB_PRINT(
"ParameterBuilder handling container of char const *");
268 if (i >= info.Length()) {
269 return *get_default_parameter<ResultType, default_arg_position>(info, i, stuffs, default_args_tuple);
282 using T =
char const *;
284 template<
int default_arg_position,
class DefaultArgsTuple = std::tuple<>>
285 char const *
operator()(
const v8::FunctionCallbackInfo<v8::Value> & info,
287 std::vector<std::unique_ptr<StuffBase>> & stuff,
288 DefaultArgsTuple && default_args_tuple = DefaultArgsTuple()) {
290 PB_PRINT(
"ParameterBuilder handling char const *");
295 if (i >= info.Length()) {
296 return *get_default_parameter<T, default_arg_position>(info, i, stuff, default_args_tuple);
300 stuff.emplace_back(std::make_unique<
Stuff<decltype(
string)>>(
std::move(
string)));
310 template<
int default_arg_position,
class DefaultArgsTuple = std::tuple<>>
311 const v8::FunctionCallbackInfo<v8::Value> &
operator()(
const v8::FunctionCallbackInfo<v8::Value> & info,
313 std::vector<std::unique_ptr<StuffBase>> & stuff,
314 DefaultArgsTuple && default_args_tuple = DefaultArgsTuple()) {
315 PB_PRINT(
"ParameterBuilder handling v8::FunctionCallbackInfo<v8::Value> const &");
324 template<
int default_arg_position,
class DefaultArgsTuple = std::tuple<>>
325 v8::Isolate *
operator()(
const v8::FunctionCallbackInfo<v8::Value> & info,
int & i,
326 std::vector<std::unique_ptr<StuffBase>> & stuff,
327 DefaultArgsTuple && default_args_tuple = DefaultArgsTuple()) {
328 PB_PRINT(
"ParameterBuilder handling v8::Isolate *");
330 return info.GetIsolate();
337 template<
int default_arg_position,
class DefaultArgsTuple = std::tuple<>>
339 std::vector<std::unique_ptr<StuffBase>> & stuff,
340 DefaultArgsTuple
const & default_args_tuple = DefaultArgsTuple()) {
341 PB_PRINT(
"ParameterBuilder handling v8::Local<v8::Context>");
343 return info.GetIsolate()->GetCurrentContext();
356 !std::is_pointer_v<v8::Local<T>> &&
357 !std::is_reference_v<v8::Local<T>> &&
358 !is_wrapped_type_v<v8::Local<T>>
362 template<
int default_arg_position,
class DefaultArgsTuple = std::tuple<>>
364 std::vector<std::unique_ptr<StuffBase>> & stuff,
365 DefaultArgsTuple
const & default_args_tuple = DefaultArgsTuple()) {
366 PB_PRINT(
"ParameterBuilder handling v8::Local<{}>", demangle<T>());
368 return v8toolkit::get_value_as<T>(info.This());
v8::Local< v8::Context > operator()(const v8::FunctionCallbackInfo< v8::Value > &info, int &i, std::vector< std::unique_ptr< StuffBase >> &stuff, DefaultArgsTuple const &default_args_tuple=DefaultArgsTuple())
#define PB_PRINT(format_string,...)
const T & move(const T &t)