GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
utility.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_UTILITY_UTILITY_HPP_
12#define CHAISCRIPT_UTILITY_UTILITY_HPP_
13
14#include <string>
15#include <type_traits>
16#include <utility>
17#include <vector>
18
22
23
24namespace chaiscript
25{
26 namespace utility
27 {
28
52 template<typename Class, typename ModuleType>
53 void add_class(ModuleType &t_module,
54 const std::string &t_class_name,
55 const std::vector<chaiscript::Proxy_Function> &t_constructors,
56 const std::vector<std::pair<chaiscript::Proxy_Function, std::string>> &t_funcs)
57 {
58 t_module.add(chaiscript::user_type<Class>(), t_class_name);
59
60 for(const chaiscript::Proxy_Function &ctor: t_constructors)
61 {
62 t_module.add(ctor, t_class_name);
63 }
64
65 for(const auto &fun: t_funcs)
66 {
67 t_module.add(fun.first, fun.second);
68 }
69 }
70
71 template<typename Enum, typename ModuleType>
72 typename std::enable_if<std::is_enum<Enum>::value, void>::type
73 add_class(ModuleType &t_module,
74 const std::string &t_class_name,
75 const std::vector<std::pair<typename std::underlying_type<Enum>::type, std::string>> &t_constants
76 )
77 {
78 t_module.add(chaiscript::user_type<Enum>(), t_class_name);
79
80 t_module.add(chaiscript::constructor<Enum ()>(), t_class_name);
81 t_module.add(chaiscript::constructor<Enum (const Enum &)>(), t_class_name);
82
83 using namespace chaiscript::bootstrap::operators;
84 equal<Enum>(t_module);
85 not_equal<Enum>(t_module);
86 assign<Enum>(t_module);
87
88 t_module.add(chaiscript::fun([](const Enum &e, const int &i) { return e == i; }), "==");
89 t_module.add(chaiscript::fun([](const int &i, const Enum &e) { return i == e; }), "==");
90
91 for (const auto &constant : t_constants)
92 {
93 t_module.add_global_const(chaiscript::const_var(Enum(constant.first)), constant.second);
94 }
95 }
96
97 template<typename EnumClass, typename ModuleType>
98 typename std::enable_if<std::is_enum<EnumClass>::value, void>::type
99 add_class(ModuleType &t_module,
100 const std::string &t_class_name,
101 const std::vector<std::pair<EnumClass, std::string>> &t_constants
102 )
103 {
104 t_module.add(chaiscript::user_type<EnumClass>(), t_class_name);
105
106 t_module.add(chaiscript::constructor<EnumClass()>(), t_class_name);
107 t_module.add(chaiscript::constructor<EnumClass(const EnumClass &)>(), t_class_name);
108
109 using namespace chaiscript::bootstrap::operators;
110 equal<EnumClass>(t_module);
111 not_equal<EnumClass>(t_module);
112 assign<EnumClass>(t_module);
113
114 for (const auto &constant : t_constants)
115 {
116 t_module.add_global_const(chaiscript::const_var(EnumClass(constant.first)), constant.second);
117 }
118 }
119 }
120}
121
122#endif
123
Env e
Definition Common.cc:2
Definition fnv1a.hpp:20
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
Proxy_Function constructor()
Generates a constructor function for use with ChaiScript.
Definition proxy_constructors.hpp:47
Proxy_Function fun(const T &t)
Creates a new Proxy_Function object from a free function, member function or data member.
Definition register_function.hpp:43
constexpr Type_Info user_type()
Creates a Type_Info object representing the templated type.
Definition type_info.hpp:236
Boxed_Value const_var(const T &t)
Takes an object and returns an immutable Boxed_Value. If the object is a std::reference or pointer ty...
Definition boxed_value.hpp:461