GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
proxy_functions_detail.hpp
Go to the documentation of this file.
1// This file is distributed under the BSD License.
2// See "license.txt" for details.
3// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
4// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
5// http://www.chaiscript.com
6
7// This is an open source non-commercial project. Dear PVS-Studio, please check it.
8// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
9
10
11#ifndef CHAISCRIPT_PROXY_FUNCTIONS_DETAIL_HPP_
12#define CHAISCRIPT_PROXY_FUNCTIONS_DETAIL_HPP_
13
14#include <functional>
15#include <stdexcept>
16#include <vector>
17#include <array>
18
20#include "boxed_cast.hpp"
21#include "boxed_value.hpp"
22#include "handle_return.hpp"
23#include "type_info.hpp"
24#include "callable_traits.hpp"
25
26namespace chaiscript {
28namespace exception {
29class bad_boxed_cast;
30} // namespace exception
31} // namespace chaiscript
32
33namespace chaiscript
34{
35 namespace exception
36 {
41 struct arity_error : std::range_error
42 {
43 arity_error(int t_got, int t_expected)
44 : std::range_error("Function dispatch arity mismatch"),
45 got(t_got), expected(t_expected)
46 {
47 }
48
49 arity_error(const arity_error &) = default;
50
51 ~arity_error() noexcept override = default;
52
53 int got;
55 };
56 }
57
58 namespace dispatch
59 {
60 namespace detail
61 {
66 template<typename Ret, typename ... Params>
67 std::vector<Type_Info> build_param_type_list(Ret (*)(Params...))
68 {
70 return { user_type<Ret>(), user_type<Params>()... };
71 }
72
73
79 template<typename Ret, typename ... Params>
80 bool compare_types_cast(Ret (*)(Params...),
81 const std::vector<Boxed_Value> &params, const Type_Conversions_State &t_conversions)
82 {
83 try {
84 std::vector<Boxed_Value>::size_type i = 0;
85 (void)i;
86 (void)params; (void)t_conversions;
87 // this is ok because the order of evaluation of initializer lists is well defined
88 (void)std::initializer_list<int>{(boxed_cast<Params>(params[i++], &t_conversions), 0)...};
89 return true;
90 } catch (const exception::bad_boxed_cast &) {
91 return false;
92 }
93 }
94
95
96 template<typename Callable, typename Ret, typename ... Params, size_t ... I>
98 std::index_sequence<I...>, const Callable &f,
99 const std::vector<Boxed_Value> &params, const Type_Conversions_State &t_conversions)
100 {
101 (void)params; (void)t_conversions;
102 return f(boxed_cast<Params>(params[I], &t_conversions)...);
103 }
104
105
110 template<typename Callable, typename Ret, typename ... Params>
112 const std::vector<Boxed_Value> &params, const Type_Conversions_State &t_conversions)
113 {
114 return Handle_Return<Ret>::handle(call_func(sig, std::index_sequence_for<Params...>{}, f, params, t_conversions));
115 }
116
117 template<typename Callable, typename ... Params>
118 Boxed_Value call_func(const chaiscript::dispatch::detail::Function_Signature<void (Params...)> &sig, const Callable &f,
119 const std::vector<Boxed_Value> &params, const Type_Conversions_State &t_conversions)
120 {
121 call_func(sig, std::index_sequence_for<Params...>{}, f, params, t_conversions);
122#ifdef CHAISCRIPT_MSVC
123#pragma warning(push)
124#pragma warning(disable : 4702)
125#endif
126 // MSVC is reporting that this is unreachable code - and it's wrong.
128#ifdef CHAISCRIPT_MSVC
129#pragma warning(pop)
130#endif
131 }
132
133 }
134 }
135
136}
137
138
139#endif
A wrapper for holding any valid C++ type. All types in ChaiScript are Boxed_Value objects.
Definition boxed_value.hpp:28
Definition type_conversions.hpp:512
Thrown in the event that a Boxed_Value cannot be cast to the desired type.
Definition bad_boxed_cast.hpp:34
Classes and functions reserved for internal use. Items in this namespace are not supported.
Ret call_func(const chaiscript::dispatch::detail::Function_Signature< Ret(Params...)> &, std::index_sequence< I... >, const Callable &f, const std::vector< Boxed_Value > &params, const Type_Conversions_State &t_conversions)
Definition proxy_functions_detail.hpp:97
std::vector< Type_Info > build_param_type_list(Ret(*)(Params...))
Definition proxy_functions_detail.hpp:67
bool compare_types_cast(Ret(*)(Params...), const std::vector< Boxed_Value > &params, const Type_Conversions_State &t_conversions)
Definition proxy_functions_detail.hpp:80
Classes and functions specific to the runtime dispatch side of ChaiScript. Some items may be of use t...
Definition callable_traits.hpp:13
Classes which may be thrown during error cases when ChaiScript is executing.
Definition bad_boxed_cast.hpp:27
Namespace chaiscript contains every API call that the average user will be concerned with.
constexpr Type_Info user_type(const T &)
Creates a Type_Info object representing the type passed in.
Definition type_info.hpp:221
decltype(auto) boxed_cast(const Boxed_Value &bv, const Type_Conversions_State *t_conversions=nullptr)
Function for extracting a value stored in a Boxed_Value object.
Definition boxed_cast.hpp:76
STL namespace.
double f(const std::string &, double, bool) noexcept
Definition profile_cpp_calls_2.cpp:4
Definition GameApi_bm.cc:4579
Definition callable_traits.hpp:78
static Boxed_Value handle(T r)
Definition handle_return.hpp:40
int expected
Definition proxy_functions_detail.hpp:54
~arity_error() noexcept override=default
int got
Definition proxy_functions_detail.hpp:53
arity_error(const arity_error &)=default
arity_error(int t_got, int t_expected)
Definition proxy_functions_detail.hpp:43