ChaiScript / ChaiScript
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-2018, 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

19
#include "../language/chaiscript_common.hpp"
20
#include "../dispatchkit/register_function.hpp"
21
#include "../dispatchkit/operators.hpp"
22

23

24
namespace chaiscript 
25
{
26
  namespace utility
27
  {
28

29
    /// Single step command for registering a class with ChaiScript
30
    /// 
31
    /// \param[in,out] t_module Model to add class to
32
    /// \param[in] t_class_name Name of the class being registered
33
    /// \param[in] t_constructors Vector of constructors to add
34
    /// \param[in] t_funcs Vector of methods to add
35
    ///
36
    /// \example Adding a basic class to ChaiScript in one step
37
    /// 
38
    /// \code
39
    /// chaiscript::utility::add_class<test>(*m,
40
    ///      "test",
41
    ///      { constructor<test ()>(),
42
    ///        constructor<test (const test &)>() },
43
    ///      { {fun(&test::function), "function"},
44
    ///        {fun(&test::function2), "function2"},
45
    ///        {fun(&test::function3), "function3"},
46
    ///        {fun(static_cast<std::string(test::*)(double)>(&test::function_overload)), "function_overload" },
47
    ///        {fun(static_cast<std::string(test::*)(int)>(&test::function_overload)), "function_overload" },
48
    ///        {fun(static_cast<test & (test::*)(const test &)>(&test::operator=)), "=" }
49
    ///        }
50
    ///      );
51
    /// 
52
    template<typename Class, typename ModuleType>
53 1
      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 1
        t_module.add(chaiscript::user_type<Class>(), t_class_name); 
59

60 1
        for(const chaiscript::Proxy_Function &ctor: t_constructors)
61
        {
62 1
          t_module.add(ctor, t_class_name);
63
        }
64

65 1
        for(const auto &fun: t_funcs)
66
        {
67 1
          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 1
      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 1
        t_module.add(chaiscript::user_type<Enum>(), t_class_name);
79

80 1
        t_module.add(chaiscript::constructor<Enum ()>(), t_class_name);
81 1
        t_module.add(chaiscript::constructor<Enum (const Enum &)>(), t_class_name);
82

83
        using namespace chaiscript::bootstrap::operators;
84 1
        equal<Enum>(t_module);
85 1
        not_equal<Enum>(t_module);
86 1
        assign<Enum>(t_module);
87

88 1
        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 1
        for (const auto &constant : t_constants)
92
        {
93 1
          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

Read our documentation on viewing source code .

Loading