GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
dynamic_object_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#ifndef CHAISCRIPT_DYNAMIC_OBJECT_DETAIL_HPP_
8#define CHAISCRIPT_DYNAMIC_OBJECT_DETAIL_HPP_
9
10#include <cassert>
11#include <map>
12#include <memory>
13#include <string>
14#include <typeinfo>
15#include <utility>
16#include <vector>
17
19#include "boxed_cast.hpp"
20#include "boxed_cast_helper.hpp"
21#include "boxed_value.hpp"
22#include "proxy_functions.hpp"
23#include "type_info.hpp"
24#include "dynamic_object.hpp"
25
26namespace chaiscript {
28namespace dispatch {
30} // namespace dispatch
31} // namespace chaiscript
32
33namespace chaiscript
34{
35 namespace dispatch
36 {
37 namespace detail
38 {
43 {
44 public:
46 std::string t_type_name,
47 const Proxy_Function &t_func,
48 bool t_is_attribute = false)
49 : Proxy_Function_Base(t_func->get_param_types(), t_func->get_arity()),
50 m_type_name(std::move(t_type_name)), m_func(t_func), m_doti(user_type<Dynamic_Object>()),
51 m_is_attribute(t_is_attribute)
52 {
53 assert( (t_func->get_arity() > 0 || t_func->get_arity() < 0)
54 && "Programming error, Dynamic_Object_Function must have at least one parameter (this)");
55 }
56
58 std::string t_type_name,
59 const Proxy_Function &t_func,
60 const Type_Info &t_ti,
61 bool t_is_attribute = false)
62 : Proxy_Function_Base(build_param_types(t_func->get_param_types(), t_ti), t_func->get_arity()),
63 m_type_name(std::move(t_type_name)), m_func(t_func), m_ti(t_ti.is_undef()?nullptr:new Type_Info(t_ti)), m_doti(user_type<Dynamic_Object>()),
64 m_is_attribute(t_is_attribute)
65 {
66 assert( (t_func->get_arity() > 0 || t_func->get_arity() < 0)
67 && "Programming error, Dynamic_Object_Function must have at least one parameter (this)");
68 }
69
70
73
74 bool operator==(const Proxy_Function_Base &f) const override
75 {
76 if (const auto *df = dynamic_cast<const Dynamic_Object_Function *>(&f))
77 {
78 return df->m_type_name == m_type_name && (*df->m_func) == (*m_func);
79 } else {
80 return false;
81 }
82 }
83
84 bool is_attribute_function() const override { return m_is_attribute; }
85
86 bool call_match(const std::vector<Boxed_Value> &vals, const Type_Conversions_State &t_conversions) const override
87 {
88 if (dynamic_object_typename_match(vals, m_type_name, m_ti, t_conversions))
89 {
90 return m_func->call_match(vals, t_conversions);
91 } else {
92 return false;
93 }
94 }
95
96 std::vector<Const_Proxy_Function> get_contained_functions() const override
97 {
98 return {m_func};
99 }
100
101 protected:
102 Boxed_Value do_call(const std::vector<Boxed_Value> &params, const Type_Conversions_State &t_conversions) const override
103 {
104 if (dynamic_object_typename_match(params, m_type_name, m_ti, t_conversions))
105 {
106 return (*m_func)(params, t_conversions);
107 } else {
109 }
110 }
111
112 bool compare_first_type(const Boxed_Value &bv, const Type_Conversions_State &t_conversions) const override
113 {
114 return dynamic_object_typename_match(bv, m_type_name, m_ti, t_conversions);
115 }
116
117 private:
118 static std::vector<Type_Info> build_param_types(
119 const std::vector<Type_Info> &t_inner_types, const Type_Info& t_objectti)
120 {
121 std::vector<Type_Info> types(t_inner_types);
122
123 assert(types.size() > 1);
124 //assert(types[1].bare_equal(user_type<Boxed_Value>()));
125 types[1] = t_objectti;
126 return types;
127 }
128
129 bool dynamic_object_typename_match(const Boxed_Value &bv, const std::string &name,
130 const std::unique_ptr<Type_Info> &ti, const Type_Conversions_State &t_conversions) const
131 {
132 if (bv.get_type_info().bare_equal(m_doti))
133 {
134 try {
135 const Dynamic_Object &d = boxed_cast<const Dynamic_Object &>(bv, &t_conversions);
136 return name == "Dynamic_Object" || d.get_type_name() == name;
137 } catch (const std::bad_cast &) {
138 return false;
139 }
140 } else {
141 if (ti)
142 {
143 return bv.get_type_info().bare_equal(*ti);
144 } else {
145 return false;
146 }
147 }
148
149 }
150
151 bool dynamic_object_typename_match(const std::vector<Boxed_Value> &bvs, const std::string &name,
152 const std::unique_ptr<Type_Info> &ti, const Type_Conversions_State &t_conversions) const
153 {
154 if (!bvs.empty())
155 {
156 return dynamic_object_typename_match(bvs[0], name, ti, t_conversions);
157 } else {
158 return false;
159 }
160 }
161
162 std::string m_type_name;
163 Proxy_Function m_func;
164 std::unique_ptr<Type_Info> m_ti;
165 const Type_Info m_doti;
166 const bool m_is_attribute;
167 };
168
169
177 {
178 public:
180 std::string t_type_name,
181 const Proxy_Function &t_func)
183 m_type_name(std::move(t_type_name)), m_func(t_func)
184 {
185 assert( (t_func->get_arity() > 0 || t_func->get_arity() < 0)
186 && "Programming error, Dynamic_Object_Function must have at least one parameter (this)");
187 }
188
189 static std::vector<Type_Info> build_type_list(const std::vector<Type_Info> &tl)
190 {
191 auto begin = tl.begin();
192 auto end = tl.end();
193
194 if (begin != end)
195 {
196 ++begin;
197 }
198
199 return std::vector<Type_Info>(begin, end);
200 }
201
202 bool operator==(const Proxy_Function_Base &f) const override
203 {
204 const Dynamic_Object_Constructor *dc = dynamic_cast<const Dynamic_Object_Constructor*>(&f);
205 return (dc != nullptr) && dc->m_type_name == m_type_name && (*dc->m_func) == (*m_func);
206 }
207
208 bool call_match(const std::vector<Boxed_Value> &vals, const Type_Conversions_State &t_conversions) const override
209 {
210 std::vector<Boxed_Value> new_vals{Boxed_Value(Dynamic_Object(m_type_name))};
211 new_vals.insert(new_vals.end(), vals.begin(), vals.end());
212
213 return m_func->call_match(new_vals, t_conversions);
214 }
215
216 protected:
217 Boxed_Value do_call(const std::vector<Boxed_Value> &params, const Type_Conversions_State &t_conversions) const override
218 {
219 auto bv = Boxed_Value(Dynamic_Object(m_type_name), true);
220 std::vector<Boxed_Value> new_params{bv};
221 new_params.insert(new_params.end(), params.begin(), params.end());
222
223 (*m_func)(new_params, t_conversions);
224
225 return bv;
226 }
227
228 private:
229 const std::string m_type_name;
230 const Proxy_Function m_func;
231
232 };
233 }
234 }
235}
236#endif
237
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
Definition type_conversions.hpp:321
Compile time deduced information about a type.
Definition type_info.hpp:34
constexpr bool bare_equal(const Type_Info &ti) const noexcept
Definition type_info.hpp:75
Definition dynamic_object.hpp:44
Definition proxy_functions.hpp:196
Proxy_Function_Base(std::vector< Type_Info > t_types, int t_arity)
Definition proxy_functions.hpp:279
int get_arity() const
Definition proxy_functions.hpp:247
const std::vector< Type_Info > & get_param_types() const
Definition proxy_functions.hpp:213
Boxed_Value do_call(const std::vector< Boxed_Value > &params, const Type_Conversions_State &t_conversions) const override
Definition dynamic_object_detail.hpp:217
static std::vector< Type_Info > build_type_list(const std::vector< Type_Info > &tl)
Definition dynamic_object_detail.hpp:189
Dynamic_Object_Constructor(std::string t_type_name, const Proxy_Function &t_func)
Definition dynamic_object_detail.hpp:179
bool operator==(const Proxy_Function_Base &f) const override
Definition dynamic_object_detail.hpp:202
bool call_match(const std::vector< Boxed_Value > &vals, const Type_Conversions_State &t_conversions) const override
Definition dynamic_object_detail.hpp:208
Dynamic_Object_Function(std::string t_type_name, const Proxy_Function &t_func, bool t_is_attribute=false)
Definition dynamic_object_detail.hpp:45
bool is_attribute_function() const override
Definition dynamic_object_detail.hpp:84
bool operator==(const Proxy_Function_Base &f) const override
Definition dynamic_object_detail.hpp:74
Boxed_Value do_call(const std::vector< Boxed_Value > &params, const Type_Conversions_State &t_conversions) const override
Definition dynamic_object_detail.hpp:102
Dynamic_Object_Function & operator=(const Dynamic_Object_Function)=delete
Dynamic_Object_Function(std::string t_type_name, const Proxy_Function &t_func, const Type_Info &t_ti, bool t_is_attribute=false)
Definition dynamic_object_detail.hpp:57
bool compare_first_type(const Boxed_Value &bv, const Type_Conversions_State &t_conversions) const override
Definition dynamic_object_detail.hpp:112
bool call_match(const std::vector< Boxed_Value > &vals, const Type_Conversions_State &t_conversions) const override
Definition dynamic_object_detail.hpp:86
std::vector< Const_Proxy_Function > get_contained_functions() const override
Definition dynamic_object_detail.hpp:96
Dynamic_Object_Function(Dynamic_Object_Function &)=delete
Exception thrown if a function's guard fails.
Definition proxy_functions.hpp:327
d
Definition meshpage.php:3895
iVBORw0KGgoAAAANSUhEUgAAANcAAAB5CAMAAACdtUQZAAAA51BMVEX</div ></div >< div style="padding: 0px 0px; width: 1324px;">< div style="height: 400px; width: 1324px;"></div >< div itemscope itemtype="https: </div><div itemscope itemtype="https:</div >< div itemscope itemtype="https: </div></div><p><div style="padding:0px 0px;width:1324px;"><div></div><div itemscope itemtype="https:< ul >< li >< b > Application name
8AkrcAgqgAs9wqQ0ey2tkAo8gAuuQfeI4hPUH09fXHy8z4+fng4uNKXF/Lz9BzfoAcgpkQMTUAKjAbOT4Qr9AXhJ8Rl7ktMCwTosA...
Definition meshpage.php:1610
Definition callable_traits.hpp:14
Classes and functions specific to the runtime dispatch side of ChaiScript. Some items may be of use t...
Definition callable_traits.hpp:13
Namespace chaiscript contains every API call that the average user will be concerned with.
std::shared_ptr< dispatch::Proxy_Function_Base > Proxy_Function
Common typedef used for passing of any registered function in ChaiScript.
Definition proxy_functions.hpp:317
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