File tree Expand file tree Collapse file tree 6 files changed +64
-4
lines changed
Expand file tree Collapse file tree 6 files changed +64
-4
lines changed Original file line number Diff line number Diff line change @@ -52,6 +52,43 @@ namespace chaiscript
5252 }
5353 }
5454
55+ template <typename T, typename = typename std::enable_if<std::is_array<T>::value>::type >
56+ ModulePtr array (const std::string &type, ModulePtr m = ModulePtr(new Module()))
57+ {
58+ typedef typename std::remove_extent<T>::type ReturnType;
59+ const auto extent = std::extent<T>::value;
60+ m->add (user_type<T>(), type);
61+ m->add (fun<ReturnType& (T &, size_t )>(
62+ [extent](T& t, size_t index)->ReturnType &{
63+ if (extent > 0 && index >= extent) {
64+ throw std::range_error (" Array index out of range. Received: " + std::to_string (index) + " expected < " + std::to_string (extent));
65+ } else {
66+ return t[index];
67+ }
68+ }
69+ ), " []"
70+ );
71+
72+ m->add (fun<const ReturnType& (const T &, size_t )>(
73+ [extent](const T &t, size_t index)->const ReturnType &{
74+ if (extent > 0 && index >= extent) {
75+ throw std::range_error (" Array index out of range. Received: " + std::to_string (index) + " expected < " + std::to_string (extent));
76+ } else {
77+ return t[index];
78+ }
79+ }
80+ ), " []"
81+ );
82+
83+ m->add (fun<size_t (const T &)>(
84+ [extent](const T &) {
85+ return extent;
86+ }), " size" );
87+
88+
89+ return m;
90+ }
91+
5592 // / \brief Adds a copy constructor for the given type to the given Model
5693 // / \param[in] type The name of the type. The copy constructor will be named "type".
5794 // / \param[in,out] m The Module to add the copy constructor to
Original file line number Diff line number Diff line change @@ -40,7 +40,8 @@ namespace chaiscript
4040 {
4141 if (ob.get_type_info ().bare_equal_type_info (typeid (Result)))
4242 {
43- return *(static_cast <const Result *>(throw_if_null (ob.get_const_ptr ())));
43+ auto p = throw_if_null (ob.get_const_ptr ());
44+ return std::cref (*static_cast <const Result *>(p));
4445 } else {
4546 throw chaiscript::detail::exception::bad_any_cast ();
4647 }
Original file line number Diff line number Diff line change @@ -712,7 +712,7 @@ namespace chaiscript
712712
713713 fpp.save_params (params);
714714
715- std::string fun_name = [&](){
715+ std::string fun_name = [&]()-> std ::string {
716716 if ((this ->children [i]->identifier == AST_Node_Type::Fun_Call) || (this ->children [i]->identifier == AST_Node_Type::Array_Call)) {
717717 return this ->children [i]->children [0 ]->text ;
718718 }
Original file line number Diff line number Diff line change @@ -617,7 +617,7 @@ namespace chaiscript
617617 }
618618
619619
620- const size_t size = [&](){
620+ const size_t size = [&]()-> size_t {
621621 if (longlong_)
622622 {
623623 return sizeof (int64_t ) * 8 ;
@@ -800,7 +800,7 @@ namespace chaiscript
800800 const auto prev_line = m_line;
801801 if (Id_ ()) {
802802 m_match_stack.push_back (std::make_shared<eval::Id_AST_Node>(
803- [&](){
803+ [&]()-> std ::string {
804804 if (*start == ' `' ) {
805805 // Id Literal
806806 return std::string (start+1 , m_input_pos-1 );
Original file line number Diff line number Diff line change 11
22#include < chaiscript/chaiscript.hpp>
3+ #include < chaiscript/dispatchkit/bootstrap.hpp>
34#include < string>
45
56
@@ -21,6 +22,8 @@ class TestBaseType
2122 int val;
2223 const int const_val;
2324
25+ int mdarray[2 ][3 ][5 ];
26+
2427 private:
2528 TestBaseType &operator =(const TestBaseType &);
2629};
@@ -159,6 +162,13 @@ CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_test_mo
159162 m->add (chaiscript::fun (&TestBaseType::const_val), " const_val" );
160163 m->add (chaiscript::fun (&TestBaseType::base_only_func), " base_only_func" );
161164
165+ // Array types
166+ m->add (chaiscript::fun (&TestBaseType::mdarray), " mdarray" );
167+ m->add (chaiscript::bootstrap::array<int [2 ][3 ][5 ]>(" IntArray_2_3_5" ));
168+ m->add (chaiscript::bootstrap::array<int [3 ][5 ]>(" IntArray_3_5" ));
169+ m->add (chaiscript::bootstrap::array<int [5 ]>(" IntArray_5" ));
170+ // end array types
171+
162172 m->add (chaiscript::fun (&get_new_int), " get_new_int" );
163173
164174
Original file line number Diff line number Diff line change 1+ load_module("test_module")
2+
3+ auto t0 = TestBaseType()
4+
5+ assert_true(t0.mdarray.size() == 2)
6+ assert_true(t0.mdarray[0].size() == 3)
7+ assert_true(t0.mdarray[0][0].size() == 5)
8+
9+ t0.mdarray[1][2][4] = 15;
10+
11+ assert_true(t0.mdarray[1][2][4] == 15)
12+
You can’t perform that action at this time.
0 commit comments