ChaiScript / ChaiScript
1

2
#include <chaiscript/chaiscript_basic.hpp>
3
#include <chaiscript/dispatchkit/bootstrap.hpp>
4
#include <string>
5

6

7

8
class TestBaseType
9
{
10
  public:
11 1
    TestBaseType() : val(10), const_val(15), mdarray{} { }
12
    TestBaseType(int) : val(10), const_val(15), mdarray{} { }
13 1
    TestBaseType(int *) : val(10), const_val(15), mdarray{} { }
14

15 1
    TestBaseType(const TestBaseType &other)
16 1
      : val(other.val), const_val(other.const_val), const_val_ptr(&const_val),
17 1
        func_member(other.func_member)
18
    {
19
    }
20

21 1
    TestBaseType(TestBaseType &&other)
22 1
      : val(other.val), const_val(other.const_val), const_val_ptr(&const_val),
23 1
        func_member(std::move(other.func_member))
24
    {
25
    }
26

27

28
    TestBaseType &operator=(TestBaseType &&) = delete;
29
    TestBaseType &operator=(const TestBaseType &) = delete;
30

31 1
    virtual ~TestBaseType() = default;
32 1
    virtual int func() { return 0; }
33

34 1
    int base_only_func() { return -9; }
35

36 1
    const TestBaseType &constMe() const { return *this; }
37

38
    int val;
39
    const int const_val;
40
    const int *const_val_ptr = &const_val;
41

42 1
    const int *get_const_val_ptr() {
43 1
      return const_val_ptr;
44
    }
45

46
    int mdarray[2][3][5];
47
    std::function<int (int)> func_member;
48

49 1
    void set_string_val(std::string &t_str)
50
    {
51 1
      t_str = "42";
52
    }
53

54
};
55

56
class Type2
57
{
58
  public:
59 1
    Type2(TestBaseType t_bt)
60 1
      : m_bt(std::move(t_bt)),
61 1
        m_str("Hello World")
62
    {
63
    }
64

65 0
    int get_val() const
66
    {
67 0
      return m_bt.val;
68
    }
69

70

71 1
    const char *get_str() const
72
    {
73 1
      return m_str.c_str();
74
    }
75

76
  private:
77
    TestBaseType m_bt;
78
    std::string m_str;
79
};
80

81
enum TestEnum
82
{
83
  TestValue1 = 1
84
};
85

86 1
int to_int(TestEnum t)
87
{
88 1
  return t;
89
}
90

91
class TestDerivedType : public TestBaseType
92
{
93
  public:
94 1
    virtual int func() override { return 1; }
95 1
    int derived_only_func() { return 19; }
96

97
};
98

99
class TestMoreDerivedType : public TestDerivedType
100
{
101
  public:
102
};
103

104 1
std::shared_ptr<TestBaseType> derived_type_factory()
105
{
106 1
  return std::make_shared<TestDerivedType>();
107
}
108

109 1
std::shared_ptr<TestBaseType> more_derived_type_factory()
110
{
111 1
  return std::make_shared<TestMoreDerivedType>();
112
}
113

114 1
std::shared_ptr<TestBaseType> null_factory()
115
{
116 1
  return std::shared_ptr<TestBaseType>();
117
}
118

119 1
void update_shared_ptr(std::shared_ptr<TestBaseType> &ptr)
120
{
121 1
  ptr = std::make_shared<TestDerivedType>();
122
}
123

124 1
void nullify_shared_ptr(std::shared_ptr<TestBaseType> &ptr)
125
{
126 1
  ptr = nullptr;
127
}
128

129 1
std::string hello_world()
130
{
131 1
  return "Hello World";
132
}
133

134
static int global_i = 1;
135

136 1
int *get_new_int()
137
{
138 1
  return &global_i;
139
}
140

141
// MSVC doesn't like that we are using C++ return types from our C declared module
142
// but this is the best way to do it for cross platform compatibility
143
#ifdef CHAISCRIPT_MSVC
144
#pragma warning(push)
145
#pragma warning(disable : 4190)
146
#endif
147

148
#ifdef __llvm__
149
#pragma clang diagnostic push
150
#pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
151
#endif
152

153 1
CHAISCRIPT_MODULE_EXPORT  chaiscript::ModulePtr create_chaiscript_module_test_module()
154
{
155 1
  chaiscript::ModulePtr m(new chaiscript::Module());
156

157 1
  m->add(chaiscript::fun(hello_world), "hello_world");
158

159 1
  m->add(chaiscript::user_type<TestBaseType>(), "TestBaseType");
160 1
  m->add(chaiscript::user_type<TestDerivedType>(), "TestDerivedType");
161 1
  m->add(chaiscript::user_type<TestMoreDerivedType>(), "TestMoreDerivedType");
162 1
  m->add(chaiscript::user_type<Type2>(), "Type2");
163

164 1
  m->add(chaiscript::constructor<TestBaseType ()>(), "TestBaseType");
165
//  m->add(chaiscript::constructor<TestBaseType (int)>(), "TestBaseType");
166 1
  m->add(chaiscript::constructor<TestBaseType (const TestBaseType &)>(), "TestBaseType");
167 1
  m->add(chaiscript::constructor<TestBaseType (int *)>(), "TestBaseType");
168

169 1
  m->add(chaiscript::constructor<TestDerivedType ()>(), "TestDerivedType");
170 1
  m->add(chaiscript::constructor<TestDerivedType (const TestDerivedType &)>(), "TestDerivedType");
171

172 1
  m->add(chaiscript::constructor<TestMoreDerivedType ()>(), "TestMoreDerivedType");
173 1
  m->add(chaiscript::constructor<TestMoreDerivedType (const TestMoreDerivedType &)>(), "TestMoreDerivedType");
174

175
  /// \todo automatic chaining of base classes?
176 1
  m->add(chaiscript::base_class<TestBaseType, TestDerivedType>());
177 1
  m->add(chaiscript::base_class<TestBaseType, TestMoreDerivedType>());
178 1
  m->add(chaiscript::base_class<TestDerivedType, TestMoreDerivedType>());
179

180 1
  m->add(chaiscript::fun(&TestDerivedType::derived_only_func), "derived_only_func");
181

182 1
  m->add(chaiscript::fun(&derived_type_factory), "derived_type_factory");
183 1
  m->add(chaiscript::fun(&more_derived_type_factory), "more_derived_type_factory");
184 1
  m->add(chaiscript::fun(&null_factory), "null_factory");
185

186 1
  m->add(chaiscript::fun(&TestDerivedType::func), "func");
187

188 1
  m->add(chaiscript::fun(&TestBaseType::func), "func");
189 1
  m->add(chaiscript::fun(&TestBaseType::val), "val");
190 1
  m->add(chaiscript::fun(&TestBaseType::const_val), "const_val");
191 1
  m->add(chaiscript::fun(&TestBaseType::const_val_ptr), "const_val_ptr");
192 1
  m->add(chaiscript::fun(&TestBaseType::get_const_val_ptr), "get_const_val_ptr");
193 1
  m->add(chaiscript::fun(&TestBaseType::base_only_func), "base_only_func");
194 1
  m->add(chaiscript::fun(&TestBaseType::set_string_val), "set_string_val");
195

196 1
  m->add(chaiscript::fun(&TestBaseType::mdarray), "mdarray");
197 1
  chaiscript::bootstrap::array<int[2][3][5]>("IntArray_2_3_5", *m);
198 1
  chaiscript::bootstrap::array<int[3][5]>("IntArray_3_5", *m);
199 1
  chaiscript::bootstrap::array<int[5]>("IntArray_5", *m);
200

201
  // member that is a function
202 1
  m->add(chaiscript::fun(&TestBaseType::func_member), "func_member");
203 1
  m->add(chaiscript::fun(&get_new_int), "get_new_int");
204

205

206 1
  m->add_global_const(chaiscript::const_var(TestValue1), "TestValue1");
207

208 1
  m->add(chaiscript::user_type<TestEnum>(), "TestEnum");
209

210 1
  m->add(chaiscript::fun(&to_int), "to_int");
211 1
  m->add(chaiscript::fun(&TestBaseType::constMe), "constMe");
212

213 1
  m->add(chaiscript::type_conversion<TestBaseType, Type2>([](const TestBaseType &t_bt) { return Type2(t_bt); }));
214

215 1
  m->add(chaiscript::fun(&Type2::get_val), "get_val");
216 1
  m->add(chaiscript::fun(&Type2::get_str), "get_str");
217 1
  m->add(chaiscript::type_conversion<const char *, std::string>());
218 1
  m->add(chaiscript::constructor<Type2 (const TestBaseType &)>(), "Type2");
219

220 1
  m->add(chaiscript::fun(&update_shared_ptr), "update_shared_ptr");
221 1
  m->add(chaiscript::fun(&nullify_shared_ptr), "nullify_shared_ptr");
222

223

224 1
  return m;
225
}
226

227

228
#ifdef __llvm__
229
#pragma clang diagnostic pop
230
#endif
231

232
#ifdef CHAISCRIPT_MSVC
233
#pragma warning(pop)
234
#endif

Read our documentation on viewing source code .

Loading