ChaiScript / ChaiScript
1

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

6
class TestBaseType {
7
public:
8 1
  TestBaseType()
9 1
      : val(10)
10
      , const_val(15)
11 1
      , mdarray{} {
12
  }
13
  TestBaseType(int)
14
      : val(10)
15
      , const_val(15)
16
      , mdarray{} {
17
  }
18 1
  TestBaseType(int *)
19 1
      : val(10)
20
      , const_val(15)
21 1
      , mdarray{} {
22
  }
23

24 1
  TestBaseType(const TestBaseType &other)
25 1
      : val(other.val)
26 1
      , const_val(other.const_val)
27 1
      , const_val_ptr(&const_val)
28 1
      , func_member(other.func_member) {
29
  }
30

31 1
  TestBaseType(TestBaseType &&other)
32 1
      : val(other.val)
33 1
      , const_val(other.const_val)
34 1
      , const_val_ptr(&const_val)
35 1
      , func_member(std::move(other.func_member)) {
36
  }
37

38
  TestBaseType &operator=(TestBaseType &&) = delete;
39
  TestBaseType &operator=(const TestBaseType &) = delete;
40

41 1
  virtual ~TestBaseType() = default;
42 1
  virtual int func() { return 0; }
43

44 1
  int base_only_func() { return -9; }
45

46 1
  const TestBaseType &constMe() const { return *this; }
47

48
  int val;
49
  const int const_val;
50
  const int *const_val_ptr = &const_val;
51

52 1
  const int *get_const_val_ptr() { return const_val_ptr; }
53

54
  int mdarray[2][3][5];
55
  std::function<int(int)> func_member;
56

57 1
  void set_string_val(std::string &t_str) { t_str = "42"; }
58
};
59

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

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

69 1
  const char *get_str() const { return m_str.c_str(); }
70

71
private:
72
  TestBaseType m_bt;
73
  std::string m_str;
74
};
75

76
enum TestEnum {
77
  TestValue1 = 1
78
};
79

80 1
int to_int(TestEnum t) {
81 1
  return t;
82
}
83

84
class TestDerivedType : public TestBaseType {
85
public:
86 1
  int func() override { return 1; }
87 1
  int derived_only_func() { return 19; }
88
};
89

90
class TestMoreDerivedType : public TestDerivedType {
91
public:
92
};
93

94 1
std::shared_ptr<TestBaseType> derived_type_factory() {
95 1
  return std::make_shared<TestDerivedType>();
96
}
97

98 1
std::shared_ptr<TestBaseType> more_derived_type_factory() {
99 1
  return std::make_shared<TestMoreDerivedType>();
100
}
101

102 1
std::shared_ptr<TestBaseType> null_factory() {
103 1
  return std::shared_ptr<TestBaseType>();
104
}
105

106 1
void update_shared_ptr(std::shared_ptr<TestBaseType> &ptr) {
107 1
  ptr = std::make_shared<TestDerivedType>();
108
}
109

110 1
void nullify_shared_ptr(std::shared_ptr<TestBaseType> &ptr) {
111 1
  ptr = nullptr;
112
}
113

114 1
std::string hello_world() {
115 1
  return "Hello World";
116
}
117

118
static int global_i = 1;
119

120 1
int *get_new_int() {
121 1
  return &global_i;
122
}
123

124
// MSVC doesn't like that we are using C++ return types from our C declared module
125
// but this is the best way to do it for cross platform compatibility
126
#ifdef CHAISCRIPT_MSVC
127
#pragma warning(push)
128
#pragma warning(disable : 4190)
129
#endif
130

131
#ifdef __llvm__
132
#pragma clang diagnostic push
133
#pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
134
#endif
135

136 1
CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_test_module() {
137 1
  chaiscript::ModulePtr m(new chaiscript::Module());
138

139 1
  m->add(chaiscript::fun(hello_world), "hello_world");
140

141 1
  m->add(chaiscript::user_type<TestBaseType>(), "TestBaseType");
142 1
  m->add(chaiscript::user_type<TestDerivedType>(), "TestDerivedType");
143 1
  m->add(chaiscript::user_type<TestMoreDerivedType>(), "TestMoreDerivedType");
144 1
  m->add(chaiscript::user_type<Type2>(), "Type2");
145

146 1
  m->add(chaiscript::constructor<TestBaseType()>(), "TestBaseType");
147
  //  m->add(chaiscript::constructor<TestBaseType (int)>(), "TestBaseType");
148 1
  m->add(chaiscript::constructor<TestBaseType(const TestBaseType &)>(), "TestBaseType");
149 1
  m->add(chaiscript::constructor<TestBaseType(int *)>(), "TestBaseType");
150

151 1
  m->add(chaiscript::constructor<TestDerivedType()>(), "TestDerivedType");
152 1
  m->add(chaiscript::constructor<TestDerivedType(const TestDerivedType &)>(), "TestDerivedType");
153

154 1
  m->add(chaiscript::constructor<TestMoreDerivedType()>(), "TestMoreDerivedType");
155 1
  m->add(chaiscript::constructor<TestMoreDerivedType(const TestMoreDerivedType &)>(), "TestMoreDerivedType");
156

157
  /// \todo automatic chaining of base classes?
158 1
  m->add(chaiscript::base_class<TestBaseType, TestDerivedType>());
159 1
  m->add(chaiscript::base_class<TestBaseType, TestMoreDerivedType>());
160 1
  m->add(chaiscript::base_class<TestDerivedType, TestMoreDerivedType>());
161

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

164 1
  m->add(chaiscript::fun(&derived_type_factory), "derived_type_factory");
165 1
  m->add(chaiscript::fun(&more_derived_type_factory), "more_derived_type_factory");
166 1
  m->add(chaiscript::fun(&null_factory), "null_factory");
167

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

170 1
  m->add(chaiscript::fun(&TestBaseType::func), "func");
171 1
  m->add(chaiscript::fun(&TestBaseType::val), "val");
172 1
  m->add(chaiscript::fun(&TestBaseType::const_val), "const_val");
173 1
  m->add(chaiscript::fun(&TestBaseType::const_val_ptr), "const_val_ptr");
174 1
  m->add(chaiscript::fun(&TestBaseType::get_const_val_ptr), "get_const_val_ptr");
175 1
  m->add(chaiscript::fun(&TestBaseType::base_only_func), "base_only_func");
176 1
  m->add(chaiscript::fun(&TestBaseType::set_string_val), "set_string_val");
177

178 1
  m->add(chaiscript::fun(&TestBaseType::mdarray), "mdarray");
179 1
  chaiscript::bootstrap::array<int[2][3][5]>("IntArray_2_3_5", *m);
180 1
  chaiscript::bootstrap::array<int[3][5]>("IntArray_3_5", *m);
181 1
  chaiscript::bootstrap::array<int[5]>("IntArray_5", *m);
182

183
  // member that is a function
184 1
  m->add(chaiscript::fun(&TestBaseType::func_member), "func_member");
185 1
  m->add(chaiscript::fun(&get_new_int), "get_new_int");
186

187 1
  m->add_global_const(chaiscript::const_var(TestValue1), "TestValue1");
188

189 1
  m->add(chaiscript::user_type<TestEnum>(), "TestEnum");
190

191 1
  m->add(chaiscript::fun(&to_int), "to_int");
192 1
  m->add(chaiscript::fun(&TestBaseType::constMe), "constMe");
193

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

196 1
  m->add(chaiscript::fun(&Type2::get_val), "get_val");
197 1
  m->add(chaiscript::fun(&Type2::get_str), "get_str");
198 1
  m->add(chaiscript::type_conversion<const char *, std::string>());
199 1
  m->add(chaiscript::constructor<Type2(const TestBaseType &)>(), "Type2");
200

201 1
  m->add(chaiscript::fun(&update_shared_ptr), "update_shared_ptr");
202 1
  m->add(chaiscript::fun(&nullify_shared_ptr), "nullify_shared_ptr");
203

204 1
  return m;
205
}
206

207
#ifdef __llvm__
208
#pragma clang diagnostic pop
209
#endif
210

211
#ifdef CHAISCRIPT_MSVC
212
#pragma warning(pop)
213
#endif

Read our documentation on viewing source code .

Loading