rice 4.3.3 → 4.6.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.
Files changed (237) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +86 -26
  3. data/CMakeLists.txt +31 -0
  4. data/CMakePresets.json +75 -0
  5. data/COPYING +3 -2
  6. data/FindRuby.cmake +437 -0
  7. data/README.md +7 -2
  8. data/Rakefile +12 -5
  9. data/include/rice/rice.hpp +9522 -4426
  10. data/include/rice/stl.hpp +2831 -1198
  11. data/lib/make_rice_headers.rb +79 -0
  12. data/lib/mkmf-rice.rb +40 -94
  13. data/lib/rice/version.rb +3 -0
  14. data/lib/rice.rb +1 -0
  15. data/lib/rubygems/builder.rb +11 -0
  16. data/lib/rubygems/cmake_builder.rb +113 -0
  17. data/lib/rubygems_plugin.rb +9 -0
  18. data/rice/Address_Registration_Guard.hpp +72 -3
  19. data/rice/Arg.hpp +26 -6
  20. data/rice/Arg.ipp +35 -2
  21. data/rice/Buffer.hpp +123 -0
  22. data/rice/Buffer.ipp +599 -0
  23. data/rice/Callback.hpp +21 -0
  24. data/rice/Callback.ipp +13 -0
  25. data/rice/Constructor.hpp +4 -27
  26. data/rice/Constructor.ipp +79 -0
  27. data/rice/Data_Object.hpp +73 -3
  28. data/rice/Data_Object.ipp +388 -96
  29. data/rice/Data_Type.hpp +214 -3
  30. data/rice/Data_Type.ipp +144 -67
  31. data/rice/Director.hpp +0 -2
  32. data/rice/Enum.hpp +4 -7
  33. data/rice/Enum.ipp +102 -55
  34. data/rice/Exception.hpp +62 -2
  35. data/rice/Exception.ipp +7 -12
  36. data/rice/Init.hpp +8 -0
  37. data/rice/Init.ipp +8 -0
  38. data/rice/JumpException.hpp +44 -0
  39. data/rice/JumpException.ipp +48 -0
  40. data/rice/MemoryView.hpp +11 -0
  41. data/rice/MemoryView.ipp +3 -0
  42. data/rice/Return.hpp +7 -27
  43. data/rice/Return.ipp +13 -13
  44. data/rice/cpp_api/Array.hpp +209 -0
  45. data/rice/cpp_api/Array.ipp +304 -0
  46. data/rice/cpp_api/Builtin_Object.hpp +31 -0
  47. data/rice/cpp_api/Builtin_Object.ipp +37 -0
  48. data/rice/cpp_api/Class.hpp +70 -0
  49. data/rice/cpp_api/Class.ipp +97 -0
  50. data/rice/cpp_api/Encoding.hpp +32 -0
  51. data/rice/cpp_api/Encoding.ipp +59 -0
  52. data/rice/cpp_api/Hash.hpp +194 -0
  53. data/rice/cpp_api/Hash.ipp +257 -0
  54. data/rice/{Identifier.hpp → cpp_api/Identifier.hpp} +2 -6
  55. data/rice/{Identifier.ipp → cpp_api/Identifier.ipp} +4 -2
  56. data/rice/cpp_api/Module.hpp +72 -0
  57. data/rice/cpp_api/Module.ipp +101 -0
  58. data/rice/cpp_api/Object.hpp +272 -0
  59. data/rice/cpp_api/Object.ipp +235 -0
  60. data/rice/cpp_api/String.hpp +74 -0
  61. data/rice/cpp_api/String.ipp +120 -0
  62. data/rice/cpp_api/Struct.hpp +113 -0
  63. data/rice/cpp_api/Struct.ipp +92 -0
  64. data/rice/cpp_api/Symbol.hpp +46 -0
  65. data/rice/cpp_api/Symbol.ipp +93 -0
  66. data/rice/cpp_api/shared_methods.hpp +134 -0
  67. data/rice/detail/DefaultHandler.hpp +12 -0
  68. data/rice/detail/DefaultHandler.ipp +8 -0
  69. data/rice/detail/HandlerRegistry.hpp +5 -35
  70. data/rice/detail/HandlerRegistry.ipp +7 -11
  71. data/rice/detail/InstanceRegistry.hpp +1 -4
  72. data/rice/detail/MethodInfo.hpp +12 -10
  73. data/rice/detail/MethodInfo.ipp +26 -21
  74. data/rice/detail/Native.hpp +33 -0
  75. data/rice/detail/Native.ipp +157 -0
  76. data/rice/detail/NativeAttributeGet.hpp +52 -0
  77. data/rice/detail/NativeAttributeGet.ipp +57 -0
  78. data/rice/detail/NativeAttributeSet.hpp +44 -0
  79. data/rice/detail/NativeAttributeSet.ipp +88 -0
  80. data/rice/detail/NativeCallbackFFI.hpp +55 -0
  81. data/rice/detail/NativeCallbackFFI.ipp +151 -0
  82. data/rice/detail/NativeCallbackSimple.hpp +30 -0
  83. data/rice/detail/NativeCallbackSimple.ipp +29 -0
  84. data/rice/detail/NativeFunction.hpp +33 -23
  85. data/rice/detail/NativeFunction.ipp +309 -70
  86. data/rice/detail/NativeIterator.hpp +9 -11
  87. data/rice/detail/NativeIterator.ipp +33 -31
  88. data/rice/detail/NativeRegistry.hpp +24 -15
  89. data/rice/detail/NativeRegistry.ipp +23 -48
  90. data/rice/detail/Proc.hpp +4 -0
  91. data/rice/detail/Proc.ipp +85 -0
  92. data/rice/detail/Registries.hpp +0 -7
  93. data/rice/detail/Registries.ipp +0 -18
  94. data/rice/detail/RubyFunction.hpp +0 -3
  95. data/rice/detail/RubyFunction.ipp +4 -8
  96. data/rice/detail/RubyType.hpp +16 -0
  97. data/rice/detail/RubyType.ipp +232 -0
  98. data/rice/detail/Type.hpp +7 -6
  99. data/rice/detail/Type.ipp +192 -45
  100. data/rice/detail/TypeRegistry.hpp +15 -7
  101. data/rice/detail/TypeRegistry.ipp +105 -12
  102. data/rice/detail/Wrapper.hpp +68 -32
  103. data/rice/detail/Wrapper.ipp +121 -109
  104. data/rice/detail/cpp_protect.hpp +5 -6
  105. data/rice/detail/default_allocation_func.ipp +0 -2
  106. data/rice/detail/from_ruby.hpp +38 -3
  107. data/rice/detail/from_ruby.ipp +1321 -492
  108. data/rice/detail/ruby.hpp +18 -0
  109. data/rice/detail/to_ruby.hpp +41 -3
  110. data/rice/detail/to_ruby.ipp +1424 -194
  111. data/rice/global_function.hpp +0 -4
  112. data/rice/global_function.ipp +0 -1
  113. data/rice/libc/file.hpp +11 -0
  114. data/rice/libc/file.ipp +32 -0
  115. data/rice/rice.hpp +116 -26
  116. data/rice/ruby_mark.hpp +4 -3
  117. data/rice/stl/complex.hpp +6 -0
  118. data/rice/stl/complex.ipp +93 -0
  119. data/rice/stl/exception.hpp +11 -0
  120. data/rice/stl/exception.ipp +29 -0
  121. data/rice/stl/exception_ptr.hpp +6 -0
  122. data/rice/stl/exception_ptr.ipp +27 -0
  123. data/rice/stl/map.hpp +12 -0
  124. data/rice/stl/map.ipp +469 -0
  125. data/rice/stl/monostate.hpp +6 -0
  126. data/rice/stl/monostate.ipp +80 -0
  127. data/rice/stl/multimap.hpp +14 -0
  128. data/rice/stl/multimap.ipp +448 -0
  129. data/rice/stl/optional.hpp +6 -0
  130. data/rice/stl/optional.ipp +118 -0
  131. data/rice/stl/pair.hpp +13 -0
  132. data/rice/stl/pair.ipp +155 -0
  133. data/rice/stl/reference_wrapper.hpp +6 -0
  134. data/rice/stl/reference_wrapper.ipp +41 -0
  135. data/rice/stl/set.hpp +12 -0
  136. data/rice/stl/set.ipp +495 -0
  137. data/rice/stl/shared_ptr.hpp +28 -0
  138. data/rice/stl/shared_ptr.ipp +224 -0
  139. data/rice/stl/string.hpp +6 -0
  140. data/rice/stl/string.ipp +158 -0
  141. data/rice/stl/string_view.hpp +6 -0
  142. data/rice/stl/string_view.ipp +65 -0
  143. data/rice/stl/tuple.hpp +6 -0
  144. data/rice/stl/tuple.ipp +128 -0
  145. data/rice/stl/type_index.hpp +6 -0
  146. data/rice/stl/type_index.ipp +30 -0
  147. data/rice/stl/type_info.hpp +6 -0
  148. data/rice/stl/type_info.ipp +29 -0
  149. data/rice/stl/unique_ptr.hpp +22 -0
  150. data/rice/stl/unique_ptr.ipp +139 -0
  151. data/rice/stl/unordered_map.hpp +12 -0
  152. data/rice/stl/unordered_map.ipp +469 -0
  153. data/rice/stl/variant.hpp +6 -0
  154. data/rice/stl/variant.ipp +242 -0
  155. data/rice/stl/vector.hpp +12 -0
  156. data/rice/stl/vector.ipp +590 -0
  157. data/rice/stl.hpp +11 -3
  158. data/rice/traits/attribute_traits.hpp +26 -0
  159. data/rice/traits/function_traits.hpp +95 -0
  160. data/rice/traits/method_traits.hpp +47 -0
  161. data/rice/traits/rice_traits.hpp +160 -0
  162. data/rice.gemspec +85 -0
  163. data/test/embed_ruby.cpp +7 -1
  164. data/test/extconf.rb +2 -0
  165. data/test/test_Address_Registration_Guard.cpp +5 -0
  166. data/test/test_Array.cpp +18 -4
  167. data/test/test_Attribute.cpp +136 -21
  168. data/test/test_Buffer.cpp +285 -0
  169. data/test/test_Builtin_Object.cpp +5 -0
  170. data/test/test_Callback.cpp +230 -0
  171. data/test/test_Class.cpp +5 -31
  172. data/test/test_Constructor.cpp +69 -6
  173. data/test/test_Data_Object.cpp +97 -38
  174. data/test/test_Data_Type.cpp +470 -65
  175. data/test/test_Director.cpp +17 -8
  176. data/test/test_Enum.cpp +155 -40
  177. data/test/test_Exception.cpp +235 -0
  178. data/test/test_File.cpp +70 -0
  179. data/test/test_From_Ruby.cpp +609 -0
  180. data/test/test_Hash.cpp +5 -0
  181. data/test/test_Identifier.cpp +5 -0
  182. data/test/test_Inheritance.cpp +6 -1
  183. data/test/test_Iterator.cpp +6 -1
  184. data/test/test_Jump_Exception.cpp +23 -0
  185. data/test/test_Keep_Alive.cpp +13 -19
  186. data/test/test_Keep_Alive_No_Wrapper.cpp +5 -1
  187. data/test/test_Memory_Management.cpp +5 -0
  188. data/test/test_Module.cpp +128 -67
  189. data/test/test_Native_Registry.cpp +2 -34
  190. data/test/test_Object.cpp +5 -0
  191. data/test/test_Overloads.cpp +806 -0
  192. data/test/test_Ownership.cpp +160 -54
  193. data/test/test_Proc.cpp +44 -0
  194. data/test/test_Self.cpp +9 -4
  195. data/test/test_Stl_Exception.cpp +109 -0
  196. data/test/test_Stl_Map.cpp +54 -42
  197. data/test/test_Stl_Multimap.cpp +693 -0
  198. data/test/test_Stl_Optional.cpp +5 -0
  199. data/test/test_Stl_Pair.cpp +14 -9
  200. data/test/test_Stl_Reference_Wrapper.cpp +9 -2
  201. data/test/test_Stl_Set.cpp +790 -0
  202. data/test/test_Stl_SharedPtr.cpp +458 -0
  203. data/test/test_Stl_String.cpp +5 -0
  204. data/test/test_Stl_String_View.cpp +5 -0
  205. data/test/test_Stl_Tuple.cpp +116 -0
  206. data/test/test_Stl_Type.cpp +147 -0
  207. data/test/test_Stl_UniquePtr.cpp +202 -0
  208. data/test/test_Stl_Unordered_Map.cpp +43 -38
  209. data/test/test_Stl_Variant.cpp +217 -84
  210. data/test/test_Stl_Vector.cpp +306 -58
  211. data/test/test_String.cpp +5 -0
  212. data/test/test_Struct.cpp +5 -0
  213. data/test/test_Symbol.cpp +5 -0
  214. data/test/test_Template.cpp +192 -0
  215. data/test/test_To_Ruby.cpp +524 -0
  216. data/test/test_Tracking.cpp +1 -0
  217. data/test/test_Type.cpp +171 -0
  218. data/test/test_global_functions.cpp +67 -7
  219. data/test/unittest.cpp +8 -0
  220. metadata +127 -26
  221. data/lib/version.rb +0 -3
  222. data/rice/Address_Registration_Guard_defn.hpp +0 -79
  223. data/rice/Data_Object_defn.hpp +0 -84
  224. data/rice/Data_Type_defn.hpp +0 -190
  225. data/rice/Exception_defn.hpp +0 -68
  226. data/rice/HandlerRegistration.hpp +0 -15
  227. data/rice/detail/ExceptionHandler.hpp +0 -8
  228. data/rice/detail/ExceptionHandler.ipp +0 -28
  229. data/rice/detail/ExceptionHandler_defn.hpp +0 -77
  230. data/rice/detail/Jump_Tag.hpp +0 -21
  231. data/rice/detail/NativeAttribute.hpp +0 -64
  232. data/rice/detail/NativeAttribute.ipp +0 -112
  233. data/rice/detail/from_ruby_defn.hpp +0 -38
  234. data/rice/detail/to_ruby_defn.hpp +0 -48
  235. data/test/test_Jump_Tag.cpp +0 -17
  236. data/test/test_Stl_SmartPointer.cpp +0 -283
  237. data/test/test_To_From_Ruby.cpp +0 -399
@@ -0,0 +1,458 @@
1
+ #include "unittest.hpp"
2
+ #include "embed_ruby.hpp"
3
+ #include <rice/rice.hpp>
4
+ #include <rice/stl.hpp>
5
+
6
+ #include <memory>
7
+
8
+ using namespace Rice;
9
+
10
+ TESTSUITE(SharedPtr);
11
+
12
+ namespace
13
+ {
14
+ class MyClass
15
+ {
16
+ public:
17
+ static inline int32_t constructorCalls = 0;
18
+ static inline int32_t copyConstructorCalls = 0;
19
+ static inline int32_t moveConstructorCalls = 0;
20
+ static inline int32_t destructorCalls = 0;
21
+
22
+ static void reset()
23
+ {
24
+ constructorCalls = 0;
25
+ copyConstructorCalls = 0;
26
+ moveConstructorCalls = 0;
27
+ destructorCalls = 0;
28
+ }
29
+
30
+ public:
31
+ int flag = 0;
32
+
33
+ public:
34
+ MyClass()
35
+ {
36
+ constructorCalls++;
37
+ }
38
+
39
+ ~MyClass()
40
+ {
41
+ destructorCalls++;
42
+ }
43
+
44
+ MyClass(const MyClass& other): flag(other.flag)
45
+ {
46
+ copyConstructorCalls++;
47
+ }
48
+
49
+ MyClass(MyClass&& other) : flag(other.flag)
50
+ {
51
+ moveConstructorCalls++;
52
+ }
53
+
54
+ void setFlag(int value)
55
+ {
56
+ this->flag = value;
57
+ }
58
+ };
59
+
60
+ class Factory
61
+ {
62
+ public:
63
+ static void reset()
64
+ {
65
+ Factory::instance_.reset();
66
+ }
67
+
68
+ ~Factory()
69
+ {
70
+ }
71
+
72
+ std::shared_ptr<MyClass> share()
73
+ {
74
+ if (!instance_)
75
+ {
76
+ instance_ = std::make_shared<MyClass>();
77
+ }
78
+ return instance_;
79
+ }
80
+
81
+ std::shared_ptr<MyClass>& share_ref()
82
+ {
83
+ if (!instance_)
84
+ {
85
+ instance_ = std::make_shared<MyClass>();
86
+ }
87
+ return instance_;
88
+ }
89
+
90
+ public:
91
+ static inline std::shared_ptr<MyClass> instance_;
92
+ };
93
+
94
+ class Sink
95
+ {
96
+ public:
97
+ long shareOwnership(std::shared_ptr<MyClass> ptr)
98
+ {
99
+ return ptr.use_count();
100
+ }
101
+
102
+ int updatePointer(std::shared_ptr<MyClass>& ptr, MyClass* myClass)
103
+ {
104
+ ptr.reset(myClass);
105
+ return ptr->flag;
106
+ }
107
+
108
+ std::shared_ptr<void> makeVoidShared(MyClass* myClass)
109
+ {
110
+ return std::shared_ptr<void>(myClass);
111
+ }
112
+
113
+ int handleVoid(std::shared_ptr<void>& ptr)
114
+ {
115
+ MyClass* myClass = static_cast<MyClass*>(ptr.get());
116
+ return myClass->flag;
117
+ }
118
+ };
119
+
120
+ int extractFlagSharedPtr(std::shared_ptr<MyClass> myClass)
121
+ {
122
+ return myClass->flag;
123
+ }
124
+
125
+ int extractFlagSharedPtrRef(std::shared_ptr<MyClass>& myClass)
126
+ {
127
+ return myClass->flag;
128
+ }
129
+ }
130
+
131
+ SETUP(SharedPtr)
132
+ {
133
+ embed_ruby();
134
+
135
+ #if RUBY_API_VERSION_MAJOR == 3 && RUBY_API_VERSION_MINOR >= 2
136
+ rb_eval_string("GC.stress = true");
137
+ #endif
138
+
139
+ define_class<MyClass>("MyClass").
140
+ define_constructor(Constructor<MyClass>()).
141
+ define_method("set_flag", &MyClass::setFlag);
142
+
143
+ define_class<Factory>("Factory").
144
+ define_constructor(Constructor<Factory>()).
145
+ define_method("share", &Factory::share).
146
+ define_method("share_ref", &Factory::share_ref);
147
+
148
+ define_class<Sink>("Sink").
149
+ define_constructor(Constructor<Sink>()).
150
+ define_method("share_ownership", &Sink::shareOwnership).
151
+ define_method("update_pointer", &Sink::updatePointer,
152
+ Arg("ptr"), Arg("myClass").takeOwnership()).
153
+ define_method("make_void_shared", &Sink::makeVoidShared,
154
+ Arg("myClass").takeOwnership()).
155
+ define_method<int(Sink::*)(std::shared_ptr<void>&)>("handle_void", &Sink::handleVoid);
156
+
157
+ define_global_function("extract_flag_shared_ptr", &extractFlagSharedPtr);
158
+ define_global_function("extract_flag_shared_ptr_ref", &extractFlagSharedPtrRef);
159
+
160
+ define_global_function("extract_flag_shared_ptr_with_default", &extractFlagSharedPtr,
161
+ Arg("myClass") = std::make_shared<MyClass>());
162
+ define_global_function("extract_flag_shared_ptr_ref_with_default", &extractFlagSharedPtrRef,
163
+ Arg("myClass") = std::make_shared<MyClass>());
164
+ }
165
+
166
+ TEARDOWN(SharedPtr)
167
+ {
168
+ rb_gc_start();
169
+ #if RUBY_API_VERSION_MAJOR == 3 && RUBY_API_VERSION_MINOR >= 2
170
+ rb_eval_string("GC.stress = false");
171
+ #endif
172
+ }
173
+
174
+ TESTCASE(ShareOwnership)
175
+ {
176
+ MyClass::reset();
177
+ Factory::reset();
178
+
179
+ Module m = define_module("TestingModule");
180
+
181
+ // Create ruby objects that point to the same instance of MyClass
182
+ std::string code = R"(ary = Array.new
183
+ factory = Factory.new
184
+ 10.times do |i|
185
+ my_class = factory.share
186
+ my_class.set_flag(i)
187
+ ary << my_class
188
+ end)";
189
+
190
+ ASSERT_EQUAL(0, Factory::instance_.use_count());
191
+ m.module_eval(code);
192
+
193
+ ASSERT_EQUAL(11, Factory::instance_.use_count());
194
+ rb_gc_start();
195
+ ASSERT_EQUAL(1, Factory::instance_.use_count());
196
+
197
+ ASSERT_EQUAL(1, MyClass::constructorCalls);
198
+ ASSERT_EQUAL(0, MyClass::copyConstructorCalls);
199
+ ASSERT_EQUAL(0, MyClass::moveConstructorCalls);
200
+ ASSERT_EQUAL(0, MyClass::destructorCalls);
201
+ ASSERT_EQUAL(9, Factory::instance_->flag);
202
+ }
203
+
204
+ TESTCASE(ShareOwnership2)
205
+ {
206
+ MyClass::reset();
207
+ Factory::reset();
208
+
209
+ Module m = define_module("TestingModule");
210
+
211
+ // Create ruby objects that point to the same instance of MyClass
212
+ std::string code = R"(factory = Factory.new
213
+ 10.times do |i|
214
+ my_class = factory.share
215
+ my_class.set_flag(i)
216
+ end)";
217
+
218
+ Factory factory;
219
+ std::shared_ptr<MyClass> myClass = factory.share();
220
+ ASSERT_EQUAL(2, Factory::instance_.use_count());
221
+
222
+ // Call some ruby code
223
+ Data_Object<Factory> wrapper(factory);
224
+ ASSERT_EQUAL(2, Factory::instance_.use_count());
225
+ wrapper.instance_eval("self.share_ref.set_flag(1)");
226
+
227
+ ASSERT_EQUAL(3, Factory::instance_.use_count());
228
+ rb_gc_start();
229
+ ASSERT_EQUAL(2, Factory::instance_.use_count());
230
+ }
231
+
232
+ TESTCASE(PtrParameter)
233
+ {
234
+ MyClass::reset();
235
+ Factory::reset();
236
+
237
+ Module m = define_module("TestingModule");
238
+
239
+ std::string code = R"(factory = Factory.new
240
+ my_class = factory.share
241
+ my_class.set_flag(8)
242
+ extract_flag_shared_ptr(my_class))";
243
+
244
+ Object result = m.module_eval(code);
245
+ ASSERT_EQUAL(8, detail::From_Ruby<int>().convert(result));
246
+ }
247
+
248
+ TESTCASE(RefParameter)
249
+ {
250
+ MyClass::reset();
251
+ Factory::reset();
252
+
253
+ Module m = define_module("TestingModule");
254
+
255
+ std::string code = R"(factory = Factory.new
256
+ my_class = factory.share
257
+ my_class.set_flag(9)
258
+ extract_flag_shared_ptr_ref(my_class))";
259
+
260
+ Object result = m.module_eval(code);
261
+ ASSERT_EQUAL(9, detail::From_Ruby<int>().convert(result));
262
+ }
263
+
264
+ TESTCASE(DefaultParameter)
265
+ {
266
+ MyClass::reset();
267
+ Factory::reset();
268
+
269
+ Module m = define_module("TestingModule");
270
+
271
+ std::string code = R"(factory = Factory.new
272
+ my_class = factory.share
273
+ my_class.set_flag(7)
274
+ extract_flag_shared_ptr_with_default())";
275
+
276
+ Object result = m.module_eval(code);
277
+ // The default value kicks in and ignores any previous pointer
278
+ ASSERT_EQUAL(0, detail::From_Ruby<int>().convert(result));
279
+ }
280
+
281
+ TESTCASE(RefDefaultParameter)
282
+ {
283
+ MyClass::reset();
284
+ Factory::reset();
285
+
286
+ Module m = define_module("TestingModule");
287
+
288
+ std::string code = R"(factory = Factory.new
289
+ my_class = factory.share
290
+ my_class.set_flag(7)
291
+ extract_flag_shared_ptr_ref_with_default())";
292
+
293
+ Object result = m.module_eval(code);
294
+ rb_gc_start();
295
+
296
+ // The default value kicks in and ignores any previous pointer
297
+ ASSERT_EQUAL(0, detail::From_Ruby<int>().convert(result));
298
+
299
+ ASSERT_EQUAL(1, MyClass::constructorCalls);
300
+ ASSERT_EQUAL(0, MyClass::copyConstructorCalls);
301
+ ASSERT_EQUAL(0, MyClass::moveConstructorCalls);
302
+ ASSERT_EQUAL(1, MyClass::destructorCalls);
303
+ }
304
+
305
+ TESTCASE(RoundTrip)
306
+ {
307
+ MyClass::reset();
308
+ Factory::reset();
309
+
310
+ Module m = define_module("TestingModule");
311
+
312
+ // Create ruby objects that point to the same instance of MyClass
313
+ std::string code = R"(factory = Factory.new
314
+ my_class = factory.share
315
+
316
+ sink = Sink.new
317
+ sink.share_ownership(my_class))";
318
+
319
+ Object result = m.instance_eval(code);
320
+ ASSERT_EQUAL(3, detail::From_Ruby<long>().convert(result.value()));
321
+
322
+ ASSERT_EQUAL(1, MyClass::constructorCalls);
323
+ ASSERT_EQUAL(0, MyClass::copyConstructorCalls);
324
+ ASSERT_EQUAL(0, MyClass::moveConstructorCalls);
325
+ ASSERT_EQUAL(1, MyClass::destructorCalls);
326
+ }
327
+
328
+ TESTCASE(Update)
329
+ {
330
+ MyClass::reset();
331
+ Factory::reset();
332
+
333
+ Module m = define_module("TestingModule");
334
+
335
+ // Create ruby objects that point to the same instance of MyClass
336
+ std::string code = R"(factory = Factory.new
337
+ my_class1 = factory.share
338
+ my_class1.set_flag(7)
339
+
340
+ my_class2 = MyClass.new
341
+ my_class2.set_flag(14)
342
+
343
+ sink = Sink.new
344
+ sink.update_pointer(my_class1, my_class2))";
345
+
346
+ Object result = m.instance_eval(code);
347
+ ASSERT_EQUAL(14, detail::From_Ruby<long>().convert(result.value()));
348
+ }
349
+
350
+ TESTCASE(Void)
351
+ {
352
+ MyClass::reset();
353
+ Factory::reset();
354
+
355
+ detail::Type<Buffer<void>>::verify();
356
+
357
+ Module m = define_module("TestingModule");
358
+
359
+ // Create ruby objects that point to the same instance of MyClass
360
+ std::string code = R"(my_class = MyClass.new
361
+ my_class.set_flag(9)
362
+ sink = Sink.new
363
+ void_ptr = sink.make_void_shared(my_class)
364
+ sink.handle_void(void_ptr))";
365
+
366
+ Object result = m.instance_eval(code);
367
+ ASSERT_EQUAL(9, detail::From_Ruby<long>().convert(result.value()));
368
+
369
+ rb_gc_start();
370
+
371
+ ASSERT_EQUAL(1, MyClass::constructorCalls);
372
+ ASSERT_EQUAL(0, MyClass::copyConstructorCalls);
373
+ ASSERT_EQUAL(0, MyClass::moveConstructorCalls);
374
+ // ASSERT_EQUAL(1, MyClass::destructorCalls);
375
+ }
376
+
377
+ namespace
378
+ {
379
+ std::shared_ptr<int> createPointer(int value)
380
+ {
381
+ int* sharedInt = new int(value);
382
+ std::shared_ptr<int> shared(sharedInt);
383
+ return shared;
384
+ }
385
+
386
+ int getPointerValue(std::shared_ptr<int> ptr)
387
+ {
388
+ return *ptr;
389
+ }
390
+
391
+ int updatePointerValue(std::shared_ptr<int> ptr)
392
+ {
393
+ *ptr = *ptr + 1;
394
+ return *ptr;
395
+ }
396
+ }
397
+
398
+ TESTCASE(PointerToInt)
399
+ {
400
+ detail::Type<Buffer<int>>::verify();
401
+
402
+ Module m = define_module("SharedPtrInt").
403
+ define_module_function("create_pointer", &createPointer).
404
+ define_module_function("get_pointer_value", &getPointerValue);
405
+
406
+ std::string code = R"(ptr = create_pointer(44)
407
+ get_pointer_value(ptr))";
408
+
409
+ Object result = m.instance_eval(code);
410
+ ASSERT_EQUAL(44, detail::From_Ruby<int>().convert(result.value()));
411
+ }
412
+
413
+ TESTCASE(CreatePointerToInt)
414
+ {
415
+ Module m = define_module("CreatePointerToInt").
416
+ define_module_function("get_pointer_value", &getPointerValue);
417
+
418
+ define_shared_ptr<int>("SharedPtrInt");
419
+
420
+ std::string code = R"(buffer = Rice::Buffer≺int≻.new(45)
421
+ ptr = Std::SharedPtrInt.new(buffer)
422
+ get_pointer_value(ptr))";
423
+
424
+ Object result = m.instance_eval(code);
425
+ ASSERT_EQUAL(45, detail::From_Ruby<int>().convert(result.value()));
426
+ }
427
+
428
+ TESTCASE(UpdatePointerToInt)
429
+ {
430
+ Module m = define_module("UpdatePointerToInt").
431
+ define_module_function("update_pointer_value", &updatePointerValue);
432
+
433
+ define_shared_ptr<int>();
434
+
435
+ std::string code = R"(buffer = Rice::Buffer≺int≻.new(45)
436
+ ptr = Std::SharedPtr≺int≻.new(buffer)
437
+ update_pointer_value(ptr))";
438
+
439
+ Object result = m.instance_eval(code);
440
+ ASSERT_EQUAL(46, detail::From_Ruby<int>().convert(result.value()));
441
+ }
442
+
443
+ TESTCASE(ReadPointerToInt)
444
+ {
445
+ detail::Type<Buffer<int>>::verify();
446
+
447
+ Module m = define_module("ReadPointerToInt").
448
+ define_module_function("create_pointer", &createPointer);
449
+
450
+ std::string code = R"(ptr = create_pointer(50)
451
+ buffer = Rice::Buffer≺int≻.new(ptr)
452
+ buffer.to_ary(1))";
453
+
454
+ Array array = m.instance_eval(code);
455
+ std::vector<int> actual = array.to_vector<int>();
456
+ std::vector<int> expected { 50 };
457
+ ASSERT_EQUAL(expected, actual);
458
+ }
@@ -15,6 +15,11 @@ SETUP(StlString)
15
15
  embed_ruby();
16
16
  }
17
17
 
18
+ TEARDOWN(StlString)
19
+ {
20
+ rb_gc_start();
21
+ }
22
+
18
23
  TESTCASE(std_string_to_ruby)
19
24
  {
20
25
  ASSERT(rb_equal(String("").value(), detail::to_ruby(std::string(""))));
@@ -15,6 +15,11 @@ SETUP(StlStringView)
15
15
  embed_ruby();
16
16
  }
17
17
 
18
+ TEARDOWN(StlStringView)
19
+ {
20
+ rb_gc_start();
21
+ }
22
+
18
23
  TESTCASE(std_string_view_to_ruby)
19
24
  {
20
25
  ASSERT(rb_equal(String("").value(), detail::to_ruby(std::string_view(""))));
@@ -0,0 +1,116 @@
1
+ #include "unittest.hpp"
2
+ #include "embed_ruby.hpp"
3
+ #include <rice/rice.hpp>
4
+ #include <rice/stl.hpp>
5
+
6
+ #include <tuple>
7
+ #include <complex>
8
+
9
+ using namespace Rice;
10
+ using namespace std::complex_literals;
11
+
12
+ TESTSUITE(Tuple);
13
+
14
+ SETUP(Tuple)
15
+ {
16
+ embed_ruby();
17
+ }
18
+
19
+ TEARDOWN(Tuple)
20
+ {
21
+ rb_gc_start();
22
+ }
23
+
24
+ namespace
25
+ {
26
+ /*inline std::ostream& operator<<(std::ostream& stream, Tuple_T const& tuple)
27
+ {
28
+ stream << "Tuple(" << "index: " << tuple.index() << ")";
29
+ return stream;
30
+ }*/
31
+
32
+ class MyTupleClass
33
+ {
34
+ public:
35
+ using Tuple_T = std::tuple<std::string,
36
+ std::complex<double>,
37
+ std::vector<int>,
38
+ double,
39
+ bool,
40
+ int>;
41
+
42
+ Tuple_T someTuple()
43
+ {
44
+ Tuple_T result{ "Hello", 1i, std::vector<int>{1, 2, 3}, 3.3, true, 5 };
45
+ return result;
46
+ }
47
+
48
+ bool validateTuple(Tuple_T tuple)
49
+ {
50
+ ASSERT_EQUAL("Ruby", std::get<0>(tuple).c_str());
51
+
52
+ std::complex<double> complex = std::get<1>(tuple);
53
+ ASSERT_EQUAL(2, complex.real());
54
+ ASSERT_EQUAL(3, complex.imag());
55
+
56
+ std::vector<int> vec = std::get<2>(tuple);
57
+ ASSERT_EQUAL(3, vec.size());
58
+ ASSERT_EQUAL(3, vec[0]);
59
+ ASSERT_EQUAL(4, vec[1]);
60
+ ASSERT_EQUAL(5, vec[2]);
61
+
62
+
63
+ ASSERT_EQUAL(4.4, std::get<3>(tuple));
64
+ ASSERT_EQUAL(false, std::get<4>(tuple));
65
+ ASSERT_EQUAL(9, std::get<5>(tuple));
66
+
67
+ return true;
68
+ }
69
+ };
70
+ }
71
+
72
+ Data_Type<MyTupleClass> defineMyTupleClass()
73
+ {
74
+ return define_class<MyTupleClass>("MyTupleClass").
75
+ define_constructor(Constructor<MyTupleClass>()).
76
+ define_method("some_tuple", &MyTupleClass::someTuple).
77
+ define_method("validate_tuple", &MyTupleClass::validateTuple);
78
+ }
79
+
80
+ TESTCASE(TupleToRuby)
81
+ {
82
+ Data_Type<MyTupleClass> klass = defineMyTupleClass();
83
+
84
+ Module m = define_module("TupleTesting");
85
+
86
+ std::string code = R"(my_class = MyTupleClass.new
87
+ my_class.some_tuple)";
88
+
89
+ Array result = m.module_eval(code);
90
+
91
+ ASSERT_EQUAL(6, result.size());
92
+
93
+ ASSERT_EQUAL("Hello", detail::From_Ruby<std::string>().convert(result[0].value()).c_str());
94
+ ASSERT_EQUAL(1i, detail::From_Ruby<std::complex<double>>().convert(result[1].value()));
95
+
96
+ std::vector<int> converted = detail::From_Ruby<std::vector<int>>().convert(result[2].value());
97
+ ASSERT_EQUAL(3, converted.size());
98
+
99
+ ASSERT_EQUAL(3.3, detail::From_Ruby<double>().convert(result[3].value()));
100
+ ASSERT_EQUAL(Qtrue, result[4].value());
101
+ ASSERT_EQUAL(5, detail::From_Ruby<int>().convert(result[5].value()));
102
+ }
103
+
104
+ TESTCASE(TupleFromRuby)
105
+ {
106
+ Data_Type<MyTupleClass> klass = defineMyTupleClass();
107
+
108
+ Module m = define_module("TupleTesting");
109
+
110
+ std::string code = R"(my_class = MyTupleClass.new
111
+ array = ["Ruby", Complex(2, 3), [3, 4, 5], 4.4, false, 9]
112
+ my_class.validate_tuple(array))";
113
+
114
+ Object result = m.module_eval(code);
115
+ ASSERT_EQUAL(Qtrue, result.value());
116
+ }