rice 2.1.1 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/CHANGELOG.md +121 -0
- data/CONTRIBUTORS.md +19 -0
- data/COPYING +2 -2
- data/Gemfile +3 -0
- data/README.md +69 -0
- data/Rakefile +95 -12
- data/include/rice/rice.hpp +7766 -0
- data/lib/mkmf-rice.rb +127 -0
- data/lib/version.rb +3 -0
- data/rice/Address_Registration_Guard.ipp +75 -32
- data/rice/Address_Registration_Guard_defn.hpp +60 -56
- data/rice/Arg.hpp +80 -4
- data/rice/Arg.ipp +51 -0
- data/rice/Constructor.hpp +30 -376
- data/rice/Data_Object.ipp +234 -107
- data/rice/Data_Object_defn.hpp +77 -117
- data/rice/Data_Type.hpp +1 -2
- data/rice/Data_Type.ipp +251 -295
- data/rice/Data_Type_defn.hpp +175 -243
- data/rice/Director.hpp +14 -9
- data/rice/Enum.hpp +54 -104
- data/rice/Enum.ipp +104 -230
- data/rice/Exception.hpp +2 -8
- data/rice/Exception.ipp +65 -0
- data/rice/Exception_defn.hpp +46 -47
- data/rice/Identifier.hpp +28 -28
- data/rice/Identifier.ipp +23 -27
- data/rice/Return.hpp +39 -0
- data/rice/Return.ipp +33 -0
- data/rice/detail/Exception_Handler.ipp +22 -62
- data/rice/detail/Exception_Handler_defn.hpp +76 -91
- data/rice/detail/Iterator.hpp +18 -88
- data/rice/detail/Iterator.ipp +47 -0
- data/rice/detail/Jump_Tag.hpp +21 -0
- data/rice/detail/MethodInfo.hpp +44 -0
- data/rice/detail/MethodInfo.ipp +78 -0
- data/rice/detail/NativeAttribute.hpp +53 -0
- data/rice/detail/NativeAttribute.ipp +83 -0
- data/rice/detail/NativeFunction.hpp +69 -0
- data/rice/detail/NativeFunction.ipp +248 -0
- data/rice/detail/RubyFunction.hpp +39 -0
- data/rice/detail/RubyFunction.ipp +92 -0
- data/rice/detail/Type.hpp +29 -0
- data/rice/detail/Type.ipp +138 -0
- data/rice/detail/TypeRegistry.hpp +50 -0
- data/rice/detail/TypeRegistry.ipp +106 -0
- data/rice/detail/Wrapper.hpp +51 -0
- data/rice/detail/Wrapper.ipp +151 -0
- data/rice/detail/default_allocation_func.hpp +8 -19
- data/rice/detail/default_allocation_func.ipp +9 -8
- data/rice/detail/from_ruby.hpp +2 -37
- data/rice/detail/from_ruby.ipp +1020 -46
- data/rice/detail/from_ruby_defn.hpp +38 -0
- data/rice/detail/function_traits.hpp +124 -0
- data/rice/detail/method_data.hpp +23 -15
- data/rice/detail/method_data.ipp +53 -0
- data/rice/detail/rice_traits.hpp +116 -0
- data/rice/detail/ruby.hpp +9 -49
- data/rice/detail/to_ruby.hpp +3 -17
- data/rice/detail/to_ruby.ipp +409 -31
- data/rice/detail/to_ruby_defn.hpp +48 -0
- data/rice/forward_declares.ipp +82 -0
- data/rice/global_function.hpp +16 -20
- data/rice/global_function.ipp +8 -17
- data/rice/rice.hpp +59 -0
- data/rice/ruby_mark.hpp +5 -3
- data/rice/ruby_try_catch.hpp +4 -4
- data/rice/stl.hpp +11 -0
- data/sample/callbacks/extconf.rb +6 -0
- data/sample/callbacks/sample_callbacks.cpp +35 -0
- data/sample/callbacks/test.rb +28 -0
- data/sample/enum/extconf.rb +3 -0
- data/sample/enum/sample_enum.cpp +3 -17
- data/sample/enum/test.rb +2 -2
- data/sample/inheritance/animals.cpp +8 -24
- data/sample/inheritance/extconf.rb +3 -0
- data/sample/inheritance/test.rb +1 -1
- data/sample/map/extconf.rb +3 -0
- data/sample/map/map.cpp +10 -18
- data/sample/map/test.rb +1 -1
- data/test/embed_ruby.cpp +34 -0
- data/test/embed_ruby.hpp +4 -0
- data/test/ext/t1/extconf.rb +3 -0
- data/test/ext/t1/t1.cpp +1 -3
- data/test/ext/t2/extconf.rb +3 -0
- data/test/ext/t2/t2.cpp +1 -1
- data/test/extconf.rb +23 -0
- data/test/ruby/test_callbacks_sample.rb +28 -0
- data/test/ruby/test_multiple_extensions.rb +18 -0
- data/test/ruby/test_multiple_extensions_same_class.rb +14 -0
- data/test/ruby/test_multiple_extensions_with_inheritance.rb +20 -0
- data/test/test_Address_Registration_Guard.cpp +25 -11
- data/test/test_Array.cpp +131 -74
- data/test/test_Attribute.cpp +147 -0
- data/test/test_Builtin_Object.cpp +36 -15
- data/test/test_Class.cpp +151 -274
- data/test/test_Constructor.cpp +10 -9
- data/test/test_Data_Object.cpp +135 -193
- data/test/test_Data_Type.cpp +323 -252
- data/test/test_Director.cpp +56 -42
- data/test/test_Enum.cpp +230 -104
- data/test/test_Exception.cpp +7 -7
- data/test/test_Hash.cpp +33 -31
- data/test/test_Identifier.cpp +6 -6
- data/test/test_Inheritance.cpp +221 -0
- data/test/test_Iterator.cpp +161 -0
- data/test/test_Jump_Tag.cpp +1 -1
- data/test/test_Keep_Alive.cpp +161 -0
- data/test/test_Memory_Management.cpp +4 -5
- data/test/test_Module.cpp +169 -111
- data/test/test_Object.cpp +51 -19
- data/test/test_Ownership.cpp +275 -0
- data/test/test_Self.cpp +205 -0
- data/test/test_Stl_Optional.cpp +90 -0
- data/test/test_Stl_Pair.cpp +144 -0
- data/test/test_Stl_SmartPointer.cpp +200 -0
- data/test/test_Stl_String.cpp +74 -0
- data/test/test_Stl_Vector.cpp +652 -0
- data/test/test_String.cpp +3 -3
- data/test/test_Struct.cpp +31 -40
- data/test/test_Symbol.cpp +3 -3
- data/test/test_To_From_Ruby.cpp +283 -218
- data/test/test_global_functions.cpp +41 -20
- data/test/unittest.cpp +34 -8
- data/test/unittest.hpp +0 -4
- metadata +117 -137
- data/Doxyfile +0 -2280
- data/Makefile.am +0 -26
- data/Makefile.in +0 -920
- data/README +0 -1055
- data/README.mingw +0 -8
- data/aclocal.m4 +0 -1088
- data/bootstrap +0 -8
- data/check_stdcxx_11.ac +0 -142
- data/config.guess +0 -1421
- data/config.sub +0 -1807
- data/configure +0 -7481
- data/configure.ac +0 -55
- data/depcomp +0 -791
- data/doxygen.ac +0 -314
- data/doxygen.am +0 -186
- data/extconf.rb +0 -69
- data/install-sh +0 -501
- data/missing +0 -215
- data/post-autoconf.rb +0 -22
- data/post-automake.rb +0 -28
- data/rice/Address_Registration_Guard.cpp +0 -22
- data/rice/Arg_impl.hpp +0 -129
- data/rice/Arg_operators.cpp +0 -21
- data/rice/Arg_operators.hpp +0 -19
- data/rice/Array.hpp +0 -214
- data/rice/Array.ipp +0 -256
- data/rice/Builtin_Object.hpp +0 -8
- data/rice/Builtin_Object.ipp +0 -50
- data/rice/Builtin_Object_defn.hpp +0 -50
- data/rice/Class.cpp +0 -57
- data/rice/Class.hpp +0 -8
- data/rice/Class.ipp +0 -6
- data/rice/Class_defn.hpp +0 -83
- data/rice/Data_Type.cpp +0 -54
- data/rice/Data_Type_fwd.hpp +0 -12
- data/rice/Director.cpp +0 -13
- data/rice/Exception.cpp +0 -59
- data/rice/Exception_Base.hpp +0 -8
- data/rice/Exception_Base.ipp +0 -13
- data/rice/Exception_Base_defn.hpp +0 -27
- data/rice/Hash.hpp +0 -227
- data/rice/Hash.ipp +0 -329
- data/rice/Identifier.cpp +0 -8
- data/rice/Jump_Tag.hpp +0 -24
- data/rice/Makefile.am +0 -124
- data/rice/Makefile.in +0 -839
- data/rice/Module.cpp +0 -84
- data/rice/Module.hpp +0 -8
- data/rice/Module.ipp +0 -6
- data/rice/Module_defn.hpp +0 -88
- data/rice/Module_impl.hpp +0 -281
- data/rice/Module_impl.ipp +0 -345
- data/rice/Object.cpp +0 -169
- data/rice/Object.hpp +0 -8
- data/rice/Object.ipp +0 -19
- data/rice/Object_defn.hpp +0 -191
- data/rice/Require_Guard.hpp +0 -21
- data/rice/String.cpp +0 -94
- data/rice/String.hpp +0 -91
- data/rice/Struct.cpp +0 -117
- data/rice/Struct.hpp +0 -162
- data/rice/Struct.ipp +0 -26
- data/rice/Symbol.cpp +0 -25
- data/rice/Symbol.hpp +0 -66
- data/rice/Symbol.ipp +0 -44
- data/rice/config.hpp +0 -47
- data/rice/config.hpp.in +0 -46
- data/rice/detail/Arguments.hpp +0 -118
- data/rice/detail/Auto_Function_Wrapper.hpp +0 -898
- data/rice/detail/Auto_Function_Wrapper.ipp +0 -3694
- data/rice/detail/Auto_Member_Function_Wrapper.hpp +0 -897
- data/rice/detail/Auto_Member_Function_Wrapper.ipp +0 -2774
- data/rice/detail/Caster.hpp +0 -103
- data/rice/detail/Not_Copyable.hpp +0 -25
- data/rice/detail/Wrapped_Function.hpp +0 -33
- data/rice/detail/cfp.hpp +0 -24
- data/rice/detail/cfp.ipp +0 -51
- data/rice/detail/check_ruby_type.cpp +0 -27
- data/rice/detail/check_ruby_type.hpp +0 -23
- data/rice/detail/creation_funcs.hpp +0 -37
- data/rice/detail/creation_funcs.ipp +0 -36
- data/rice/detail/define_method_and_auto_wrap.hpp +0 -31
- data/rice/detail/define_method_and_auto_wrap.ipp +0 -30
- data/rice/detail/demangle.cpp +0 -56
- data/rice/detail/demangle.hpp +0 -19
- data/rice/detail/env.hpp +0 -11
- data/rice/detail/method_data.cpp +0 -86
- data/rice/detail/node.hpp +0 -13
- data/rice/detail/object_call.hpp +0 -69
- data/rice/detail/object_call.ipp +0 -131
- data/rice/detail/protect.cpp +0 -29
- data/rice/detail/protect.hpp +0 -34
- data/rice/detail/ruby_version_code.hpp +0 -6
- data/rice/detail/ruby_version_code.hpp.in +0 -6
- data/rice/detail/st.hpp +0 -22
- data/rice/detail/traits.hpp +0 -43
- data/rice/detail/win32.hpp +0 -16
- data/rice/detail/wrap_function.hpp +0 -341
- data/rice/detail/wrap_function.ipp +0 -514
- data/rice/protect.hpp +0 -92
- data/rice/protect.ipp +0 -1134
- data/rice/rubypp.rb +0 -97
- data/rice/to_from_ruby.hpp +0 -8
- data/rice/to_from_ruby.ipp +0 -294
- data/rice/to_from_ruby_defn.hpp +0 -70
- data/ruby.ac +0 -135
- data/ruby/Makefile.am +0 -1
- data/ruby/Makefile.in +0 -625
- data/ruby/lib/Makefile.am +0 -3
- data/ruby/lib/Makefile.in +0 -503
- data/ruby/lib/mkmf-rice.rb.in +0 -217
- data/ruby/lib/version.rb +0 -3
- data/sample/Makefile.am +0 -47
- data/sample/Makefile.in +0 -486
- data/test/Makefile.am +0 -72
- data/test/Makefile.in +0 -1150
- data/test/ext/Makefile.am +0 -41
- data/test/ext/Makefile.in +0 -480
- data/test/test_rice.rb +0 -41
data/test/test_Data_Type.cpp
CHANGED
@@ -1,13 +1,19 @@
|
|
1
|
+
#include <assert.h>
|
2
|
+
|
1
3
|
#include "unittest.hpp"
|
2
|
-
#include "
|
3
|
-
#include
|
4
|
-
#include
|
5
|
-
#include "rice/global_function.hpp"
|
4
|
+
#include "embed_ruby.hpp"
|
5
|
+
#include <rice/rice.hpp>
|
6
|
+
#include <rice/stl.hpp>
|
6
7
|
|
7
8
|
using namespace Rice;
|
8
9
|
|
9
10
|
TESTSUITE(Data_Type);
|
10
11
|
|
12
|
+
SETUP(Data_Type)
|
13
|
+
{
|
14
|
+
embed_ruby();
|
15
|
+
}
|
16
|
+
|
11
17
|
/**
|
12
18
|
* The tests here are for the feature of taking an instance
|
13
19
|
* of a Ruby-subclass of a Rice wrapped class and passing
|
@@ -16,333 +22,398 @@ TESTSUITE(Data_Type);
|
|
16
22
|
* to see what we're talking about.
|
17
23
|
*/
|
18
24
|
|
19
|
-
namespace
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
virtual ~Listener() { }
|
29
|
-
|
30
|
-
virtual int getValue() { return 4; }
|
31
|
-
};
|
25
|
+
namespace
|
26
|
+
{
|
27
|
+
class MyClass
|
28
|
+
{
|
29
|
+
public:
|
30
|
+
static inline bool no_return_no_arg_called = false;
|
31
|
+
static inline bool no_arg_called = false;
|
32
|
+
static inline bool int_arg_called = false;
|
33
|
+
static inline bool multiple_args_called = false;
|
32
34
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
35
|
+
static void reset()
|
36
|
+
{
|
37
|
+
no_return_no_arg_called = false;
|
38
|
+
no_arg_called = false;
|
39
|
+
int_arg_called = false;
|
40
|
+
multiple_args_called = false;
|
41
|
+
}
|
38
42
|
|
39
|
-
|
43
|
+
static Object singleton_method_object_int(Object object, int anInt)
|
44
|
+
{
|
45
|
+
return object;
|
46
|
+
}
|
40
47
|
|
41
|
-
|
48
|
+
static int singleton_function_int(int anInt)
|
49
|
+
{
|
50
|
+
return anInt;
|
51
|
+
}
|
42
52
|
|
43
|
-
|
44
|
-
|
45
|
-
|
53
|
+
public:
|
54
|
+
MyClass() = default;
|
55
|
+
MyClass(const MyClass& other) = delete;
|
56
|
+
MyClass(MyClass&& other) = delete;
|
46
57
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
accum += (*i)->getValue();
|
52
|
-
}
|
58
|
+
void no_return_no_arg()
|
59
|
+
{
|
60
|
+
no_return_no_arg_called = true;
|
61
|
+
}
|
53
62
|
|
54
|
-
|
55
|
-
|
63
|
+
bool no_arg()
|
64
|
+
{
|
65
|
+
no_arg_called = true;
|
66
|
+
return true;
|
67
|
+
}
|
56
68
|
|
57
|
-
|
69
|
+
int int_arg(int i)
|
70
|
+
{
|
71
|
+
int_arg_called = true;
|
72
|
+
return i;
|
73
|
+
}
|
58
74
|
|
59
|
-
|
60
|
-
|
75
|
+
std::string multiple_args(int i, bool b, float f, std::string s, char* c)
|
76
|
+
{
|
77
|
+
multiple_args_called = true;
|
78
|
+
return "multiple_args(" + std::to_string(i) + ", " + std::to_string(b) + ", " +
|
79
|
+
std::to_string(f) + ", " + s + ", " + std::string(c) + ")";
|
80
|
+
}
|
61
81
|
};
|
62
|
-
}
|
82
|
+
} // namespace
|
63
83
|
|
64
|
-
|
84
|
+
TESTCASE(methods_with_member_pointers)
|
65
85
|
{
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
.
|
70
|
-
.define_method("
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
86
|
+
Class c = define_class<MyClass>("MyClass")
|
87
|
+
.define_constructor(Constructor<MyClass>())
|
88
|
+
.define_method("no_return_no_arg", &MyClass::no_return_no_arg)
|
89
|
+
.define_method("no_arg", &MyClass::no_arg)
|
90
|
+
.define_method("int_arg", &MyClass::int_arg)
|
91
|
+
.define_method("multiple_args", &MyClass::multiple_args);
|
92
|
+
|
93
|
+
MyClass::reset();
|
94
|
+
Object o = c.call("new");
|
95
|
+
|
96
|
+
Object result = o.call("no_return_no_arg");
|
97
|
+
ASSERT(MyClass::no_return_no_arg_called);
|
98
|
+
ASSERT_EQUAL(Qnil, result.value());
|
99
|
+
|
100
|
+
result = o.call("no_arg");
|
101
|
+
ASSERT(MyClass::no_arg_called);
|
102
|
+
ASSERT_EQUAL(Qtrue, result.value());
|
103
|
+
|
104
|
+
result = o.call("int_arg", 42);
|
105
|
+
ASSERT(MyClass::int_arg_called);
|
106
|
+
ASSERT_EQUAL(42, detail::From_Ruby<int>().convert(result.value()));
|
107
|
+
|
108
|
+
result = o.call("multiple_args", 81, true, 7.0, "a string", "a char");
|
109
|
+
ASSERT(MyClass::multiple_args_called);
|
110
|
+
ASSERT_EQUAL("multiple_args(81, 1, 7.000000, a string, a char)", detail::From_Ruby<std::string>().convert(result.value()));
|
78
111
|
}
|
79
112
|
|
80
|
-
TESTCASE(
|
113
|
+
TESTCASE(incorrect_number_of_args)
|
81
114
|
{
|
82
|
-
|
83
|
-
|
115
|
+
Class c =
|
116
|
+
define_class<MyClass>("MyClass")
|
117
|
+
.define_constructor(Constructor<MyClass>())
|
118
|
+
.define_method("int_arg", &MyClass::int_arg);
|
119
|
+
|
120
|
+
Object o = c.call("new");
|
121
|
+
|
122
|
+
ASSERT_EXCEPTION_CHECK(
|
123
|
+
Exception,
|
124
|
+
o.call("int_arg", 1, 2),
|
125
|
+
ASSERT_EQUAL(rb_eArgError, ex.class_of())
|
126
|
+
);
|
127
|
+
}
|
84
128
|
|
85
|
-
|
129
|
+
TESTCASE(incorrect_no_args)
|
130
|
+
{
|
131
|
+
Class c =
|
132
|
+
define_class<MyClass>("MyClass")
|
133
|
+
.define_constructor(Constructor<MyClass>())
|
134
|
+
.define_method("int_arg", &MyClass::int_arg);
|
135
|
+
|
136
|
+
Object o = c.call("new");
|
137
|
+
|
138
|
+
ASSERT_EXCEPTION_CHECK(
|
139
|
+
Exception,
|
140
|
+
o.call("int_arg"),
|
141
|
+
ASSERT_EQUAL(rb_eArgError, ex.class_of())
|
142
|
+
);
|
143
|
+
}
|
86
144
|
|
87
|
-
|
88
|
-
|
145
|
+
TESTCASE(methods_with_lambdas)
|
146
|
+
{
|
147
|
+
Class c = define_class<MyClass>("MyClass")
|
148
|
+
.define_constructor(Constructor<MyClass>())
|
149
|
+
.define_method("no_return_no_arg",
|
150
|
+
[](MyClass& instance)
|
151
|
+
{
|
152
|
+
instance.no_return_no_arg();
|
153
|
+
})
|
154
|
+
.define_method("no_arg",
|
155
|
+
[](MyClass& instance)
|
156
|
+
{
|
157
|
+
return instance.no_arg();
|
158
|
+
})
|
159
|
+
.define_method("int_arg",
|
160
|
+
[](MyClass& instance, int anInt)
|
161
|
+
{
|
162
|
+
return instance.int_arg(anInt);
|
163
|
+
})
|
164
|
+
.define_method("multiple_args",
|
165
|
+
[](MyClass& instance, int anInt, bool aBool, float aFloat, std::string aString, char* aChar)
|
166
|
+
{
|
167
|
+
return instance.multiple_args(anInt, aBool, aFloat, aString, aChar);
|
168
|
+
});
|
169
|
+
|
170
|
+
MyClass::reset();
|
171
|
+
Object o = c.call("new");
|
172
|
+
|
173
|
+
Object result = o.call("no_return_no_arg");
|
174
|
+
ASSERT(MyClass::no_return_no_arg_called);
|
175
|
+
ASSERT_EQUAL(Qnil, result.value());
|
176
|
+
|
177
|
+
result = o.call("no_arg");
|
178
|
+
ASSERT(MyClass::no_arg_called);
|
179
|
+
ASSERT_EQUAL(Qtrue, result.value());
|
180
|
+
|
181
|
+
result = o.call("int_arg", 42);
|
182
|
+
ASSERT(MyClass::int_arg_called);
|
183
|
+
ASSERT_EQUAL(42, detail::From_Ruby<int>().convert(result.value()));
|
184
|
+
|
185
|
+
result = o.call("multiple_args", 81, true, 7.0, "a string", "a char");
|
186
|
+
ASSERT(MyClass::multiple_args_called);
|
187
|
+
ASSERT_EQUAL("multiple_args(81, 1, 7.000000, a string, a char)", detail::From_Ruby<std::string>().convert(result.value()));
|
188
|
+
}
|
89
189
|
|
90
|
-
|
91
|
-
|
190
|
+
TESTCASE(static_singleton_method)
|
191
|
+
{
|
192
|
+
Class c = define_class<MyClass>("MyClass")
|
193
|
+
.define_constructor(Constructor<MyClass>())
|
194
|
+
.define_singleton_method("singleton_method_object_int", &MyClass::singleton_method_object_int);
|
92
195
|
|
93
|
-
|
196
|
+
MyClass::reset();
|
94
197
|
|
95
|
-
|
96
|
-
ASSERT_EQUAL(
|
198
|
+
Object result = c.call("singleton_method_object_int", 42);
|
199
|
+
ASSERT_EQUAL(c, result);
|
97
200
|
}
|
98
201
|
|
99
|
-
|
100
|
-
* The following test SEGFAULTs right now
|
101
|
-
*/
|
102
|
-
/*
|
103
|
-
TESTCASE(no_super_in_constructor_still_works)
|
202
|
+
TESTCASE(static_singleton_function)
|
104
203
|
{
|
105
|
-
|
106
|
-
|
204
|
+
Class c = define_class<MyClass>("MyClass")
|
205
|
+
.define_constructor(Constructor<MyClass>())
|
206
|
+
.define_singleton_function("singleton_function_int", &MyClass::singleton_function_int);
|
107
207
|
|
108
|
-
|
208
|
+
MyClass::reset();
|
109
209
|
|
110
|
-
|
111
|
-
|
112
|
-
|
210
|
+
Object result = c.call("singleton_function_int", 42);
|
211
|
+
ASSERT_EQUAL(42, detail::From_Ruby<int>().convert(result));
|
212
|
+
}
|
113
213
|
|
114
|
-
|
115
|
-
|
214
|
+
TESTCASE(static_singleton_method_lambda)
|
215
|
+
{
|
216
|
+
Class c = define_class<MyClass>("MyClass")
|
217
|
+
.define_constructor(Constructor<MyClass>())
|
218
|
+
.define_singleton_method("singleton_method_object_int", [](Object object, int anInt)
|
219
|
+
{
|
220
|
+
return MyClass::singleton_method_object_int(object, anInt);
|
221
|
+
});
|
116
222
|
|
117
|
-
|
223
|
+
MyClass::reset();
|
118
224
|
|
119
|
-
|
120
|
-
ASSERT_EQUAL(
|
225
|
+
Object result = c.call("singleton_method_object_int", 42);
|
226
|
+
ASSERT_EQUAL(c, result);
|
121
227
|
}
|
122
|
-
*/
|
123
|
-
|
124
|
-
/**
|
125
|
-
* Implicit Casting across unrelated types
|
126
|
-
*
|
127
|
-
* Two ways of defining if types are implicitly castable
|
128
|
-
*
|
129
|
-
* 1) operator
|
130
|
-
* 2) constructor
|
131
|
-
*/
|
132
228
|
|
133
|
-
|
134
|
-
* Examples here taken from Ogre's Math library.
|
135
|
-
* This uses the constructor method of casting types.
|
136
|
-
*/
|
137
|
-
namespace
|
229
|
+
TESTCASE(static_singleton_function_lambda)
|
138
230
|
{
|
139
|
-
|
140
|
-
|
231
|
+
Class c = define_class<MyClass>("MyClass")
|
232
|
+
.define_constructor(Constructor<MyClass>())
|
233
|
+
.define_singleton_function("singleton_function_int", [](int anInt)
|
234
|
+
{
|
235
|
+
return MyClass::singleton_function_int(anInt);
|
236
|
+
});
|
141
237
|
|
142
|
-
|
238
|
+
MyClass::reset();
|
143
239
|
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
explicit Degree(float d) : val_(d) {}
|
148
|
-
Degree(const Radian& r);
|
149
|
-
|
150
|
-
float valueDegrees() const { return val_; }
|
151
|
-
float valueRadians() const { return val_ * degree2Radians; }
|
152
|
-
|
153
|
-
private:
|
154
|
-
float val_;
|
155
|
-
};
|
240
|
+
Object result = c.call("singleton_function_int", 42);
|
241
|
+
ASSERT_EQUAL(42, detail::From_Ruby<int>().convert(result));
|
242
|
+
}
|
156
243
|
|
157
|
-
|
244
|
+
namespace {
|
245
|
+
class BaseClass
|
158
246
|
{
|
159
|
-
|
160
|
-
|
161
|
-
Radian(const Degree& d) : val_(d.valueRadians()) {}
|
162
|
-
|
163
|
-
float valueRadians() const { return val_; }
|
164
|
-
float valueDegrees() const { return val_ * radian2Degrees; }
|
165
|
-
|
166
|
-
private:
|
167
|
-
float val_;
|
247
|
+
public:
|
248
|
+
BaseClass() {}
|
168
249
|
};
|
250
|
+
}
|
169
251
|
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
252
|
+
TESTCASE(subclassing)
|
253
|
+
{
|
254
|
+
Module m = define_module("Testing");
|
255
|
+
define_class_under<BaseClass>(m, "BaseClass").
|
256
|
+
define_constructor(Constructor<BaseClass>());
|
257
|
+
|
258
|
+
// Not sure how to make this a true failure case. If the subclassing
|
259
|
+
// doesn't work, Ruby will throw an error:
|
260
|
+
//
|
261
|
+
// in `new': wrong instance allocation
|
262
|
+
//
|
263
|
+
m.instance_eval("class NewClass < Testing::BaseClass; end;");
|
264
|
+
m.instance_eval("n = NewClass.new");
|
265
|
+
}
|
176
266
|
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
bool isAcute(Degree degree) {
|
181
|
-
return degree.valueDegrees() < 90;
|
182
|
-
}
|
267
|
+
namespace {
|
268
|
+
float with_reference_defaults_x;
|
269
|
+
std::string with_reference_defaults_str;
|
183
270
|
|
184
|
-
|
185
|
-
|
186
|
-
|
271
|
+
class DefaultArgsRefs
|
272
|
+
{
|
273
|
+
public:
|
274
|
+
void with_reference_defaults(float x, std::string const& str = std::string("testing"))
|
275
|
+
{
|
276
|
+
with_reference_defaults_x = x;
|
277
|
+
with_reference_defaults_str = str;
|
278
|
+
}
|
279
|
+
};
|
187
280
|
|
188
|
-
bool isRight(Degree* degree) {
|
189
|
-
return degree->valueDegrees() == 90;
|
190
|
-
}
|
191
281
|
}
|
192
282
|
|
193
|
-
TESTCASE(
|
283
|
+
TESTCASE(define_method_works_with_reference_const_default_values)
|
194
284
|
{
|
195
|
-
define_class<
|
196
|
-
.define_constructor(Constructor<
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
define_implicit_cast<Degree, Radian>();
|
202
|
-
define_implicit_cast<Radian, Degree>();
|
285
|
+
Class c = define_class<DefaultArgsRefs>("DefaultArgsRefs")
|
286
|
+
.define_constructor(Constructor<DefaultArgsRefs>())
|
287
|
+
.define_method("bar",
|
288
|
+
&DefaultArgsRefs::with_reference_defaults,
|
289
|
+
Arg("x"), Arg("str") = std::string("testing"));
|
203
290
|
|
204
|
-
|
205
|
-
|
206
|
-
define_global_function("is_right", &isRight);
|
291
|
+
Object o = c.call("new");
|
292
|
+
o.call("bar", 3);
|
207
293
|
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
// ACUTE
|
212
|
-
result = m.instance_eval("is_acute(Degree.new(75))");
|
213
|
-
ASSERT(from_ruby<bool>(result.value()));
|
294
|
+
ASSERT_EQUAL(3, with_reference_defaults_x);
|
295
|
+
ASSERT_EQUAL("testing", with_reference_defaults_str);
|
296
|
+
}
|
214
297
|
|
215
|
-
|
216
|
-
|
298
|
+
namespace
|
299
|
+
{
|
300
|
+
class RefTest
|
301
|
+
{
|
302
|
+
public:
|
303
|
+
RefTest() {}
|
217
304
|
|
218
|
-
|
219
|
-
|
220
|
-
|
305
|
+
static std::string& getReference()
|
306
|
+
{
|
307
|
+
static std::string foo = "foo";
|
308
|
+
return foo;
|
309
|
+
}
|
310
|
+
};
|
311
|
+
}
|
221
312
|
|
222
|
-
|
223
|
-
|
313
|
+
TESTCASE(define_singleton_method_returning_reference)
|
314
|
+
{
|
315
|
+
Class c = define_class<RefTest>("RefTest")
|
316
|
+
.define_constructor(Constructor<RefTest>())
|
317
|
+
.define_singleton_function("get_reference", &RefTest::getReference);
|
224
318
|
|
225
|
-
|
226
|
-
result = m.instance_eval("is_right(Degree.new(90))");
|
227
|
-
ASSERT(from_ruby<bool>(result.value()));
|
319
|
+
Module m(anonymous_module());
|
228
320
|
|
229
|
-
result = m.instance_eval("
|
230
|
-
|
321
|
+
Object result = m.instance_eval("RefTest.get_reference");
|
322
|
+
ASSERT_EQUAL(result, String("foo"));
|
231
323
|
}
|
232
324
|
|
233
|
-
namespace
|
234
|
-
|
325
|
+
namespace
|
326
|
+
{
|
327
|
+
struct MyStruct
|
235
328
|
{
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
Explicit(const Degree &d) {
|
242
|
-
value = d.valueDegrees();
|
243
|
-
}
|
244
|
-
|
245
|
-
float getValue() { return value; }
|
329
|
+
MyStruct* set(MyStruct* ptr)
|
330
|
+
{
|
331
|
+
assert(ptr == nullptr);
|
332
|
+
return ptr;
|
333
|
+
}
|
246
334
|
|
247
|
-
|
248
|
-
|
335
|
+
MyStruct* get()
|
336
|
+
{
|
337
|
+
return nullptr;
|
338
|
+
}
|
249
339
|
};
|
250
|
-
|
251
|
-
float getExplicitValue(Explicit* v) {
|
252
|
-
return v->getValue();
|
253
|
-
}
|
254
340
|
}
|
255
341
|
|
256
|
-
TESTCASE(
|
342
|
+
TESTCASE(null_ptrs)
|
257
343
|
{
|
258
|
-
define_class<
|
259
|
-
.define_constructor(Constructor<
|
260
|
-
|
261
|
-
|
262
|
-
.define_constructor(Constructor<Radian, float>());
|
263
|
-
|
264
|
-
define_class<Explicit>("Explicit")
|
265
|
-
.define_constructor(Constructor<Explicit, float>());
|
344
|
+
Class c = define_class<MyStruct>("MyStruct")
|
345
|
+
.define_constructor(Constructor<MyStruct>())
|
346
|
+
.define_method("get", &MyStruct::get)
|
347
|
+
.define_method("set", &MyStruct::set);
|
266
348
|
|
267
|
-
|
268
|
-
define_global_function("explicit_value", &getExplicitValue);
|
349
|
+
Object o = c.call("new");
|
269
350
|
|
270
|
-
|
271
|
-
|
272
|
-
define_implicit_cast<Degree, Explicit>();
|
351
|
+
Object result = o.call("get");
|
352
|
+
ASSERT_EQUAL(Qnil, result.value());
|
273
353
|
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
result = m.instance_eval("is_obtuse(Degree.new(75))");
|
278
|
-
ASSERT(!from_ruby<bool>(result.value()));
|
279
|
-
|
280
|
-
result = m.instance_eval("explicit_value(Degree.new(75))");
|
281
|
-
ASSERT_EQUAL(75.0, from_ruby<float>(result.value()));
|
354
|
+
result = o.call("set", nullptr);
|
355
|
+
ASSERT_EQUAL(Qnil, result.value());
|
282
356
|
}
|
283
357
|
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
*
|
288
|
-
* This is the operator version of casting and shows that this works for
|
289
|
-
* base types as well as defined types
|
290
|
-
*/
|
291
|
-
/*
|
292
|
-
namespace {
|
293
|
-
struct Real
|
358
|
+
namespace
|
359
|
+
{
|
360
|
+
class SomeClass
|
294
361
|
{
|
295
|
-
Real(int x)
|
296
|
-
: v(x)
|
297
|
-
{}
|
298
|
-
|
299
|
-
operator int() const
|
300
|
-
{
|
301
|
-
return v;
|
302
|
-
}
|
303
|
-
|
304
|
-
int v;
|
305
362
|
};
|
306
363
|
|
307
|
-
|
364
|
+
void undefinedArg(SomeClass& someClass)
|
308
365
|
{
|
309
|
-
return x.v;
|
310
366
|
}
|
311
367
|
|
312
|
-
|
368
|
+
SomeClass undefinedReturn()
|
313
369
|
{
|
314
|
-
return
|
370
|
+
return SomeClass();
|
315
371
|
}
|
316
372
|
}
|
317
373
|
|
318
|
-
TESTCASE(
|
374
|
+
TESTCASE(not_defined)
|
319
375
|
{
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
376
|
+
#ifdef _MSC_VER
|
377
|
+
const char* message = "Type is not defined with Rice: class `anonymous namespace'::SomeClass";
|
378
|
+
#else
|
379
|
+
const char* message = "Type is not defined with Rice: (anonymous namespace)::SomeClass";
|
380
|
+
#endif
|
381
|
+
|
382
|
+
ASSERT_EXCEPTION_CHECK(
|
383
|
+
std::invalid_argument,
|
384
|
+
define_global_function("undefined_arg", &undefinedArg),
|
385
|
+
ASSERT_EQUAL(message, ex.what())
|
386
|
+
);
|
387
|
+
|
388
|
+
ASSERT_EXCEPTION_CHECK(
|
389
|
+
std::invalid_argument,
|
390
|
+
define_global_function("undefined_return", &undefinedReturn),
|
391
|
+
ASSERT_EQUAL(message, ex.what())
|
392
|
+
);
|
393
|
+
}
|
326
394
|
|
327
|
-
define_global_function("real_value", &realValue);
|
328
|
-
define_global_function("make_real", &makeReal);
|
329
395
|
|
396
|
+
/**
|
397
|
+
* The following test SEGFAULTs right now
|
398
|
+
*/
|
399
|
+
/*
|
400
|
+
TESTCASE(no_super_in_constructor_still_works)
|
401
|
+
{
|
330
402
|
Module m = define_module("TestingModule");
|
403
|
+
Object handler = m.instance_eval("@handler = ListenerHandler.new");
|
404
|
+
|
405
|
+
ASSERT_EQUAL(INT2NUM(0), handler.call("listener_count").value());
|
331
406
|
|
332
|
-
//
|
333
|
-
|
334
|
-
|
407
|
+
// Because of this, there's a constructor but no super call
|
408
|
+
m.instance_eval("class MyListener < Listener; def initialize; @val = 10; end; end;");
|
409
|
+
m.instance_eval("@handler.add_listener(MyListener.new)");
|
335
410
|
|
336
|
-
|
337
|
-
|
338
|
-
ASSERT_EQUAL(4, from_ruby<int>(result.value()));
|
411
|
+
ASSERT_EQUAL(INT2NUM(1), handler.call("listener_count").value());
|
412
|
+
ASSERT_EQUAL(INT2NUM(4), handler.call("process").value());
|
339
413
|
|
340
|
-
|
341
|
-
result = m.instance_eval("r = make_real( Real.new(6) ); real_value(r)");
|
342
|
-
ASSERT_EQUAL(6, from_ruby<int>(result.value()));
|
414
|
+
m.instance_eval("@handler.add_listener(MyListener.new)");
|
343
415
|
|
344
|
-
|
345
|
-
|
346
|
-
ASSERT_EQUAL(6, from_ruby<int>(result.value()));
|
416
|
+
ASSERT_EQUAL(INT2NUM(2), handler.call("listener_count").value());
|
417
|
+
ASSERT_EQUAL(INT2NUM(8), handler.call("process").value());
|
347
418
|
}
|
348
419
|
*/
|