GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
function_call.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_FUNCTION_CALL_HPP_
12#define CHAISCRIPT_FUNCTION_CALL_HPP_
13
14#include <functional>
15#include <string>
16#include <vector>
17
18#include "boxed_cast.hpp"
20#include "proxy_functions.hpp"
21#include "callable_traits.hpp"
22
23namespace chaiscript {
24class Boxed_Value;
26namespace detail {
27template <typename T> struct Cast_Helper;
28} // namespace detail
29} // namespace chaiscript
30
31namespace chaiscript
32{
33 namespace dispatch
34 {
41 template<typename FunctionType>
42 std::function<FunctionType> functor(const std::vector<Const_Proxy_Function> &funcs, const Type_Conversions_State *t_conversions)
43 {
44 const bool has_arity_match = std::any_of(funcs.begin(), funcs.end(),
45 [](const Const_Proxy_Function &f) {
46 return f->get_arity() == -1 || size_t(f->get_arity()) == chaiscript::dispatch::detail::Arity<FunctionType>::arity;
47 });
48
49 if (!has_arity_match) {
50 throw exception::bad_boxed_cast(user_type<Const_Proxy_Function>(), typeid(std::function<FunctionType>));
51 }
52
53 FunctionType *p=nullptr;
54 return detail::build_function_caller_helper(p, funcs, t_conversions);
55 }
56
68 template<typename FunctionType>
69 std::function<FunctionType> functor(Const_Proxy_Function func, const Type_Conversions_State *t_conversions)
70 {
71 return functor<FunctionType>(std::vector<Const_Proxy_Function>({std::move(func)}), t_conversions);
72 }
73
76 template<typename FunctionType>
77 std::function<FunctionType> functor(const Boxed_Value &bv, const Type_Conversions_State *t_conversions)
78 {
79 return functor<FunctionType>(boxed_cast<Const_Proxy_Function >(bv, t_conversions), t_conversions);
80 }
81 }
82
83 namespace detail{
85 template<typename Signature>
86 struct Cast_Helper<const std::function<Signature> &>
87 {
88 static std::function<Signature> cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions)
89 {
91 {
92 return dispatch::functor<Signature>(ob, t_conversions);
93 } else {
95 }
96 }
97 };
98
100 template<typename Signature>
101 struct Cast_Helper<std::function<Signature> >
102 {
103 static std::function<Signature> cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions)
104 {
106 {
107 return dispatch::functor<Signature>(ob, t_conversions);
108 } else {
109 return Cast_Helper_Inner<std::function<Signature> >::cast(ob, t_conversions);
110 }
111 }
112 };
113
115 template<typename Signature>
116 struct Cast_Helper<const std::function<Signature> >
117 {
118 static std::function<Signature> cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions)
119 {
121 {
122 return dispatch::functor<Signature>(ob, t_conversions);
123 } else {
125 }
126 }
127 };
128 }
129}
130
131#endif
132
P func(int c, EveryApi &ev)
Definition test.cpp:59
A wrapper for holding any valid C++ type. All types in ChaiScript are Boxed_Value objects.
Definition boxed_value.hpp:28
const Type_Info & get_type_info() const noexcept
Definition boxed_value.hpp:224
Definition type_conversions.hpp:512
constexpr bool bare_equal(const Type_Info &ti) const noexcept
Definition type_info.hpp:75
Thrown in the event that a Boxed_Value cannot be cast to the desired type.
Definition bad_boxed_cast.hpp:34
void function(void)
Definition memory_leak_test.cpp:26
Classes and functions reserved for internal use. Items in this namespace are not supported.
std::function< Ret(Params...)> build_function_caller_helper(Ret(Params...), const std::vector< Const_Proxy_Function > &funcs, const Type_Conversions_State *t_conversions)
Definition function_call_detail.hpp:146
Classes and functions specific to the runtime dispatch side of ChaiScript. Some items may be of use t...
Definition callable_traits.hpp:13
std::function< FunctionType > functor(const std::vector< Const_Proxy_Function > &funcs, const Type_Conversions_State *t_conversions)
Definition function_call.hpp:42
Namespace chaiscript contains every API call that the average user will be concerned with.
std::shared_ptr< const dispatch::Proxy_Function_Base > Const_Proxy_Function
Const version of Proxy_Function. Points to a const Proxy_Function. This is how most registered functi...
Definition proxy_functions.hpp:321
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
static std::function< Signature > cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions)
Definition function_call.hpp:118
static std::function< Signature > cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions)
Definition function_call.hpp:88
static std::function< Signature > cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions)
Definition function_call.hpp:103
Generic Cast_Helper_Inner, for casting to any type.
Definition boxed_cast_helper.hpp:76
The exposed Cast_Helper object that by default just calls the Cast_Helper_Inner.
Definition boxed_cast_helper.hpp:313
static decltype(auto) cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions)
Definition boxed_cast_helper.hpp:314