rice 4.5.0 → 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 (166) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +23 -0
  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/Rakefile +5 -4
  8. data/include/rice/rice.hpp +5436 -3201
  9. data/include/rice/stl.hpp +2355 -1269
  10. data/lib/make_rice_headers.rb +79 -0
  11. data/lib/mkmf-rice.rb +4 -0
  12. data/lib/rice/version.rb +3 -0
  13. data/lib/rice.rb +1 -0
  14. data/lib/rubygems/builder.rb +11 -0
  15. data/lib/rubygems/cmake_builder.rb +113 -0
  16. data/lib/rubygems_plugin.rb +9 -0
  17. data/rice/Arg.hpp +7 -1
  18. data/rice/Arg.ipp +11 -2
  19. data/rice/Buffer.hpp +123 -0
  20. data/rice/Buffer.ipp +599 -0
  21. data/rice/Constructor.ipp +3 -3
  22. data/rice/Data_Object.hpp +2 -3
  23. data/rice/Data_Object.ipp +188 -188
  24. data/rice/Data_Type.hpp +4 -5
  25. data/rice/Data_Type.ipp +42 -26
  26. data/rice/Enum.hpp +0 -1
  27. data/rice/Enum.ipp +26 -23
  28. data/rice/Init.hpp +8 -0
  29. data/rice/Init.ipp +8 -0
  30. data/rice/MemoryView.ipp +1 -41
  31. data/rice/Return.hpp +1 -1
  32. data/rice/Return.ipp +6 -0
  33. data/rice/cpp_api/Array.hpp +209 -0
  34. data/rice/cpp_api/Array.ipp +304 -0
  35. data/rice/cpp_api/Builtin_Object.hpp +31 -0
  36. data/rice/cpp_api/Builtin_Object.ipp +37 -0
  37. data/rice/cpp_api/Class.hpp +70 -0
  38. data/rice/cpp_api/Class.ipp +97 -0
  39. data/rice/cpp_api/Encoding.hpp +32 -0
  40. data/rice/cpp_api/Encoding.ipp +59 -0
  41. data/rice/cpp_api/Hash.hpp +194 -0
  42. data/rice/cpp_api/Hash.ipp +257 -0
  43. data/rice/cpp_api/Identifier.hpp +46 -0
  44. data/rice/cpp_api/Identifier.ipp +31 -0
  45. data/rice/cpp_api/Module.hpp +72 -0
  46. data/rice/cpp_api/Module.ipp +101 -0
  47. data/rice/cpp_api/Object.hpp +272 -0
  48. data/rice/cpp_api/Object.ipp +235 -0
  49. data/rice/cpp_api/String.hpp +74 -0
  50. data/rice/cpp_api/String.ipp +120 -0
  51. data/rice/cpp_api/Struct.hpp +113 -0
  52. data/rice/cpp_api/Struct.ipp +92 -0
  53. data/rice/cpp_api/Symbol.hpp +46 -0
  54. data/rice/cpp_api/Symbol.ipp +93 -0
  55. data/rice/cpp_api/shared_methods.hpp +134 -0
  56. data/rice/detail/MethodInfo.hpp +1 -9
  57. data/rice/detail/MethodInfo.ipp +5 -72
  58. data/rice/detail/Native.hpp +3 -2
  59. data/rice/detail/Native.ipp +32 -4
  60. data/rice/detail/NativeAttributeGet.hpp +3 -2
  61. data/rice/detail/NativeAttributeGet.ipp +8 -2
  62. data/rice/detail/NativeAttributeSet.hpp +3 -2
  63. data/rice/detail/NativeAttributeSet.ipp +8 -2
  64. data/rice/detail/NativeCallbackFFI.ipp +1 -1
  65. data/rice/detail/NativeFunction.hpp +17 -6
  66. data/rice/detail/NativeFunction.ipp +168 -64
  67. data/rice/detail/NativeIterator.hpp +3 -2
  68. data/rice/detail/NativeIterator.ipp +8 -2
  69. data/rice/detail/RubyType.hpp +2 -5
  70. data/rice/detail/RubyType.ipp +50 -5
  71. data/rice/detail/Type.hpp +3 -1
  72. data/rice/detail/Type.ipp +61 -31
  73. data/rice/detail/Wrapper.hpp +68 -33
  74. data/rice/detail/Wrapper.ipp +103 -113
  75. data/rice/detail/from_ruby.hpp +5 -4
  76. data/rice/detail/from_ruby.ipp +737 -365
  77. data/rice/detail/to_ruby.ipp +1092 -186
  78. data/rice/global_function.ipp +1 -1
  79. data/rice/libc/file.hpp +11 -0
  80. data/rice/libc/file.ipp +32 -0
  81. data/rice/rice.hpp +23 -16
  82. data/rice/stl/complex.hpp +6 -0
  83. data/rice/stl/complex.ipp +93 -0
  84. data/rice/stl/exception.hpp +11 -0
  85. data/rice/stl/exception.ipp +29 -0
  86. data/rice/stl/exception_ptr.hpp +6 -0
  87. data/rice/stl/exception_ptr.ipp +27 -0
  88. data/rice/stl/map.hpp +12 -0
  89. data/rice/stl/map.ipp +469 -0
  90. data/rice/stl/monostate.hpp +6 -0
  91. data/rice/stl/monostate.ipp +80 -0
  92. data/rice/stl/multimap.hpp +14 -0
  93. data/rice/stl/multimap.ipp +448 -0
  94. data/rice/stl/optional.hpp +6 -0
  95. data/rice/stl/optional.ipp +118 -0
  96. data/rice/stl/pair.hpp +13 -0
  97. data/rice/stl/pair.ipp +155 -0
  98. data/rice/stl/reference_wrapper.hpp +6 -0
  99. data/rice/stl/reference_wrapper.ipp +41 -0
  100. data/rice/stl/set.hpp +12 -0
  101. data/rice/stl/set.ipp +495 -0
  102. data/rice/stl/shared_ptr.hpp +28 -0
  103. data/rice/stl/shared_ptr.ipp +224 -0
  104. data/rice/stl/string.hpp +6 -0
  105. data/rice/stl/string.ipp +158 -0
  106. data/rice/stl/string_view.hpp +6 -0
  107. data/rice/stl/string_view.ipp +65 -0
  108. data/rice/stl/tuple.hpp +6 -0
  109. data/rice/stl/tuple.ipp +128 -0
  110. data/rice/stl/type_index.hpp +6 -0
  111. data/rice/stl/type_index.ipp +30 -0
  112. data/rice/stl/type_info.hpp +6 -0
  113. data/rice/stl/type_info.ipp +29 -0
  114. data/rice/stl/unique_ptr.hpp +22 -0
  115. data/rice/stl/unique_ptr.ipp +139 -0
  116. data/rice/stl/unordered_map.hpp +12 -0
  117. data/rice/stl/unordered_map.ipp +469 -0
  118. data/rice/stl/variant.hpp +6 -0
  119. data/rice/stl/variant.ipp +242 -0
  120. data/rice/stl/vector.hpp +12 -0
  121. data/rice/stl/vector.ipp +590 -0
  122. data/rice/stl.hpp +7 -3
  123. data/rice/traits/attribute_traits.hpp +26 -0
  124. data/rice/traits/function_traits.hpp +95 -0
  125. data/rice/traits/method_traits.hpp +47 -0
  126. data/rice/traits/rice_traits.hpp +160 -0
  127. data/rice.gemspec +85 -0
  128. data/test/embed_ruby.cpp +3 -0
  129. data/test/ruby/test_multiple_extensions_same_class.rb +14 -14
  130. data/test/test_Array.cpp +6 -3
  131. data/test/test_Attribute.cpp +34 -1
  132. data/test/test_Buffer.cpp +285 -0
  133. data/test/test_Callback.cpp +2 -3
  134. data/test/test_Data_Object.cpp +88 -34
  135. data/test/test_Data_Type.cpp +106 -65
  136. data/test/test_Director.cpp +7 -3
  137. data/test/test_Enum.cpp +5 -2
  138. data/test/test_File.cpp +1 -1
  139. data/test/test_From_Ruby.cpp +181 -114
  140. data/test/test_Iterator.cpp +1 -1
  141. data/test/{test_JumpException.cpp → test_Jump_Exception.cpp} +1 -0
  142. data/test/test_Keep_Alive.cpp +7 -18
  143. data/test/test_Keep_Alive_No_Wrapper.cpp +0 -1
  144. data/test/test_Module.cpp +13 -6
  145. data/test/test_Native_Registry.cpp +0 -1
  146. data/test/test_Overloads.cpp +180 -5
  147. data/test/test_Ownership.cpp +100 -57
  148. data/test/test_Proc.cpp +0 -1
  149. data/test/test_Self.cpp +4 -4
  150. data/test/test_Stl_Map.cpp +37 -39
  151. data/test/test_Stl_Multimap.cpp +693 -0
  152. data/test/test_Stl_Pair.cpp +8 -8
  153. data/test/test_Stl_Reference_Wrapper.cpp +4 -2
  154. data/test/test_Stl_Set.cpp +790 -0
  155. data/test/{test_Stl_SmartPointer.cpp → test_Stl_SharedPtr.cpp} +97 -127
  156. data/test/test_Stl_Tuple.cpp +116 -0
  157. data/test/test_Stl_Type.cpp +1 -1
  158. data/test/test_Stl_UniquePtr.cpp +202 -0
  159. data/test/test_Stl_Unordered_Map.cpp +28 -34
  160. data/test/test_Stl_Variant.cpp +217 -89
  161. data/test/test_Stl_Vector.cpp +209 -83
  162. data/test/test_To_Ruby.cpp +373 -1
  163. data/test/test_Type.cpp +85 -14
  164. data/test/test_global_functions.cpp +17 -4
  165. metadata +94 -10
  166. data/rice/detail/TupleIterator.hpp +0 -14
data/rice/Buffer.ipp ADDED
@@ -0,0 +1,599 @@
1
+ namespace Rice
2
+ {
3
+ // ---- Buffer<T> -------
4
+ template<typename T>
5
+ inline Buffer<T>::Buffer(T* pointer) : m_buffer(pointer)
6
+ {
7
+ }
8
+
9
+ template<typename T>
10
+ inline Buffer<T>::Buffer(T* pointer, size_t size) : m_size(size), m_buffer(pointer)
11
+ {
12
+ }
13
+
14
+ template <typename T>
15
+ inline Buffer<T>::Buffer(VALUE value)
16
+ {
17
+ if constexpr (std::is_fundamental_v<T>)
18
+ {
19
+ this->fromRubyType(value);
20
+ }
21
+ else
22
+ {
23
+ this->fromDataType(value);
24
+ }
25
+ }
26
+
27
+ template <typename T>
28
+ inline void Buffer<T>::fromRubyType(VALUE value)
29
+ {
30
+ using Intrinsic_T = typename detail::intrinsic_type<T>;
31
+ using RubyType_T = typename detail::RubyType<Intrinsic_T>;
32
+ ruby_value_type valueType = rb_type(value);
33
+
34
+ switch (valueType)
35
+ {
36
+ case RUBY_T_ARRAY:
37
+ {
38
+ Array array(value);
39
+ this->m_size = array.size();
40
+ this->m_buffer = new T[this->m_size]();
41
+
42
+ String packed = array.pack<Intrinsic_T>();
43
+ memcpy(this->m_buffer, RSTRING_PTR(packed.value()), RSTRING_LEN(packed.value()));
44
+
45
+ this->m_owner = true;
46
+ break;
47
+ }
48
+ case RUBY_T_STRING:
49
+ {
50
+ this->m_size = RSTRING_LEN(value);
51
+ if constexpr (std::is_same_v<T, char>)
52
+ {
53
+ // Add 2 for null characters (string and wstring)
54
+ this->m_buffer = new T[this->m_size + 2]();
55
+ }
56
+ else
57
+ {
58
+ this->m_buffer = new T[this->m_size]();
59
+ }
60
+ memcpy(this->m_buffer, RSTRING_PTR(value), this->m_size);
61
+
62
+ this->m_owner = true;
63
+ break;
64
+ }
65
+ case RUBY_T_DATA:
66
+ {
67
+ if (Data_Type<T>::is_descendant(value))
68
+ {
69
+ this->m_size = 1;
70
+ this->m_buffer = new T[this->m_size]();
71
+ this->m_buffer[0] = *detail::unwrap<T>(value, Data_Type<T>::ruby_data_type(), false);
72
+ this->m_owner = false;
73
+ break;
74
+ }
75
+ }
76
+ default:
77
+ {
78
+ if (RubyType_T::Exact.find(valueType) != RubyType_T::Exact.end() ||
79
+ RubyType_T::Castable.find(valueType) != RubyType_T::Castable.end() ||
80
+ RubyType_T::Narrowable.find(valueType) != RubyType_T::Narrowable.end())
81
+ {
82
+ T data = detail::protect(RubyType_T::fromRuby, value);
83
+ this->m_size = 1;
84
+ this->m_buffer = new T[this->m_size]();
85
+ memcpy(this->m_buffer, &data, sizeof(T));
86
+ this->m_owner = true;
87
+ break;
88
+ }
89
+ else
90
+ {
91
+ std::string typeName = detail::typeName(typeid(T));
92
+ throw Exception(rb_eTypeError, "wrong argument type %s (expected % s*)",
93
+ detail::protect(rb_obj_classname, value), typeName.c_str());
94
+ }
95
+ }
96
+ }
97
+ }
98
+
99
+ template <typename T>
100
+ inline void Buffer<T>::fromDataType(VALUE value)
101
+ {
102
+ using Intrinsic_T = typename detail::intrinsic_type<T>;
103
+
104
+ switch (rb_type(value))
105
+ {
106
+ case RUBY_T_ARRAY:
107
+ {
108
+ Array array(value);
109
+ this->m_size = array.size();
110
+
111
+ // Use operator new[] to allocate memory but not call constructors
112
+ this->m_buffer = static_cast<T*>(operator new[](sizeof(T)* this->m_size));
113
+
114
+ detail::From_Ruby<Intrinsic_T> fromRuby;
115
+
116
+ for (size_t i = 0; i < this->m_size; i++)
117
+ {
118
+ this->m_buffer[i] = fromRuby.convert(array[i].value());
119
+ }
120
+ break;
121
+ }
122
+ default:
123
+ {
124
+ std::string typeName = detail::typeName(typeid(T));
125
+ throw Exception(rb_eTypeError, "wrong argument type %s (expected % s*)",
126
+ detail::protect(rb_obj_classname, value), typeName.c_str());
127
+ }
128
+ }
129
+ }
130
+
131
+ template <typename T>
132
+ inline Buffer<T>::~Buffer()
133
+ {
134
+ if constexpr (std::is_destructible_v<T>)
135
+ {
136
+ if (this->m_owner)
137
+ {
138
+ delete[] this->m_buffer;
139
+ }
140
+ }
141
+ }
142
+
143
+ template <typename T>
144
+ inline Buffer<T>::Buffer(Buffer<T>&& other) : m_owner(other.m_owner), m_size(other.m_size), m_buffer(other.m_buffer)
145
+ {
146
+ other.m_buffer = nullptr;
147
+ other.m_size = 0;
148
+ other.m_owner = false;
149
+ }
150
+
151
+ template <typename T>
152
+ inline Buffer<T>& Buffer<T>::operator=(Buffer<T>&& other)
153
+ {
154
+ this->m_buffer = other.m_buffer;
155
+ other.m_buffer = nullptr;
156
+
157
+ this->m_size = other.m_size;
158
+ other.m_size = 0;
159
+
160
+ this->m_owner = other.m_owner;
161
+ other.m_owner = false;
162
+
163
+ return *this;
164
+ }
165
+
166
+ template <typename T>
167
+ inline size_t Buffer<T>::size() const
168
+ {
169
+ return this->m_size;
170
+ }
171
+
172
+ template <typename T>
173
+ void Buffer<T>::setSize(size_t value)
174
+ {
175
+ this->m_size = value;
176
+ }
177
+
178
+ template <typename T>
179
+ inline T* Buffer<T>::ptr()
180
+ {
181
+ return this->m_buffer;
182
+ }
183
+
184
+ template <typename T>
185
+ inline T& Buffer<T>::reference()
186
+ {
187
+ return *this->m_buffer;
188
+ }
189
+
190
+ template <typename T>
191
+ inline bool Buffer<T>::isOwner() const
192
+ {
193
+ return this->m_owner;
194
+ }
195
+
196
+ template <typename T>
197
+ inline void Buffer<T>::setOwner(bool value)
198
+ {
199
+ this->m_owner = value;
200
+ }
201
+
202
+ template <typename T>
203
+ inline void Buffer<T>::release()
204
+ {
205
+ this->m_owner = false;
206
+ }
207
+
208
+ /* template<typename T>
209
+ inline VALUE Buffer<T>::toString() const
210
+ {
211
+ std::string name = detail::typeName(typeid(T));
212
+ std::string result = "Buffer<type: " + detail::cppClassName(name) + ", size: " + std::to_string(this->m_size) + ">";
213
+ return detail::To_Ruby<std::string>().convert(result);
214
+ }*/
215
+
216
+ template<typename T>
217
+ inline VALUE Buffer<T>::bytes(size_t count) const
218
+ {
219
+ if (!this->m_buffer)
220
+ {
221
+ return Qnil;
222
+ }
223
+ else
224
+ {
225
+ long length = (long)(count * sizeof(T));
226
+ return detail::protect(rb_str_new_static, (const char*)this->m_buffer, length);
227
+ }
228
+ }
229
+
230
+ template<typename T>
231
+ inline VALUE Buffer<T>::bytes() const
232
+ {
233
+ return this->bytes(this->m_size);
234
+ }
235
+
236
+ template<typename T>
237
+ inline Array Buffer<T>::toArray(size_t count) const
238
+ {
239
+ if (!this->m_buffer)
240
+ {
241
+ return Qnil;
242
+ }
243
+ else if constexpr (std::is_fundamental_v<T>)
244
+ {
245
+ VALUE string = this->bytes(count);
246
+ return String(string).unpack<T>();
247
+ }
248
+ else
249
+ {
250
+ Array result;
251
+
252
+ T* ptr = this->m_buffer;
253
+ T* end = this->m_buffer + count;
254
+
255
+ for (; ptr < end; ptr++)
256
+ {
257
+ result.push(*ptr);
258
+ }
259
+ return result;
260
+ }
261
+ }
262
+
263
+ template<typename T>
264
+ inline Array Buffer<T>::toArray() const
265
+ {
266
+ return this->toArray(this->m_size);
267
+ }
268
+
269
+ template<typename T>
270
+ inline T Buffer<T>::get(size_t index) const
271
+ {
272
+ if (index >= this->m_size)
273
+ {
274
+ throw Exception(rb_eIndexError, "index %ld outside of bounds: 0..%ld", index, this->m_size);
275
+ }
276
+
277
+ return this->m_buffer[index];
278
+ }
279
+
280
+ template<typename T>
281
+ inline void Buffer<T>::set(size_t index, T element)
282
+ {
283
+ if (index >= this->m_size)
284
+ {
285
+ throw Exception(rb_eIndexError, "index %ld outside of bounds: 0..%ld", index, this->m_size);
286
+ }
287
+
288
+ this->m_buffer[index] = element;
289
+ }
290
+
291
+ // ---- Buffer<T*> -------
292
+ template<typename T>
293
+ inline Buffer<T*>::Buffer(T** pointer) : m_outer(pointer)
294
+ {
295
+ }
296
+
297
+ template<typename T>
298
+ inline Buffer<T*>::Buffer(T** pointer, size_t size) : m_outer(pointer), m_size(size)
299
+ {
300
+ }
301
+
302
+ template <typename T>
303
+ inline Buffer<T*>::Buffer(VALUE value)
304
+ {
305
+ ruby_value_type valueType = rb_type(value);
306
+
307
+ switch (valueType)
308
+ {
309
+ case RUBY_T_ARRAY:
310
+ {
311
+ Array outer(value);
312
+ this->m_size = outer.size();
313
+ this->m_outer = new T * [this->m_size]();
314
+
315
+ for (size_t i = 0; i < this->m_size; i++)
316
+ {
317
+ // Check the inner value is also an array
318
+ Array inner(outer[i].value());
319
+
320
+ // Wrap it with a buffer and add it our list of inner buffers
321
+ Buffer<T> buffer(inner.value());
322
+
323
+ // And update the outer array
324
+ this->m_outer[i] = buffer.ptr();
325
+
326
+ // Now move the buffer into the affer, not the buffer pointer is still valid (it just got moved)
327
+ this->m_inner.push_back(std::move(buffer));
328
+ }
329
+
330
+ this->m_owner = true;
331
+ break;
332
+ }
333
+ default:
334
+ {
335
+ std::string typeName = detail::typeName(typeid(T));
336
+ throw Exception(rb_eTypeError, "wrong argument type %s (expected % s*)",
337
+ detail::protect(rb_obj_classname, value), typeName.c_str());
338
+ }
339
+ }
340
+ }
341
+
342
+ template <typename T>
343
+ inline Buffer<T*>::~Buffer()
344
+ {
345
+ if (this->m_owner)
346
+ {
347
+ delete[] this->m_outer;
348
+ }
349
+ }
350
+
351
+ template <typename T>
352
+ inline Buffer<T*>::Buffer(Buffer<T*>&& other) : m_owner(other.m_owner), m_size(other.m_size),
353
+ m_outer(other.m_outer), m_inner(std::move(other.m_inner))
354
+ {
355
+ other.m_outer = nullptr;
356
+ other.m_inner.clear();
357
+ other.m_size = 0;
358
+ other.m_owner = false;
359
+ }
360
+
361
+ template <typename T>
362
+ inline Buffer<T*>& Buffer<T*>::operator=(Buffer<T*>&& other)
363
+ {
364
+ this->m_outer = other.m_outer;
365
+ other.m_outer = nullptr;
366
+
367
+ this->m_inner = std::move(other.m_inner);
368
+ other.m_inner.clear();
369
+
370
+ this->m_size = other.m_size;
371
+ other.m_size = 0;
372
+
373
+ this->m_owner = other.m_owner;
374
+ other.m_owner = false;
375
+
376
+ return *this;
377
+ }
378
+
379
+ template <typename T>
380
+ inline const Buffer<T>& Buffer<T*>::operator[](size_t index)
381
+ {
382
+ return this->m_inner[index];
383
+ }
384
+
385
+ template <typename T>
386
+ inline size_t Buffer<T*>::size() const
387
+ {
388
+ return this->m_size;
389
+ }
390
+
391
+ template <typename T>
392
+ void Buffer<T*>::setSize(size_t value)
393
+ {
394
+ this->m_size = value;
395
+ }
396
+
397
+ template <typename T>
398
+ inline T** Buffer<T*>::ptr()
399
+ {
400
+ return this->m_outer;
401
+ }
402
+
403
+ template <typename T>
404
+ inline bool Buffer<T*>::isOwner() const
405
+ {
406
+ return this->m_owner;
407
+ }
408
+
409
+ template <typename T>
410
+ inline void Buffer<T*>::setOwner(bool value)
411
+ {
412
+ this->m_owner = value;
413
+ }
414
+
415
+ template <typename T>
416
+ inline void Buffer<T*>::release()
417
+ {
418
+ this->m_owner = false;
419
+ }
420
+
421
+ /*template<typename T>
422
+ inline VALUE Buffer<T*>::toString() const
423
+ {
424
+ std::string name = detail::typeName(typeid(T*));
425
+ std::string result = "Buffer<type: " + detail::cppClassName(name) + ", size: " + std::to_string(this->m_size) + ">";
426
+ return detail::To_Ruby<std::string>().convert(result);
427
+ }*/
428
+
429
+ template<typename T>
430
+ inline VALUE Buffer<T*>::bytes(size_t count) const
431
+ {
432
+ if (!this->m_outer)
433
+ {
434
+ return Qnil;
435
+ }
436
+ else
437
+ {
438
+ T** begin = this->m_outer;
439
+ long length = (long)(count * sizeof(T*));
440
+ return detail::protect(rb_str_new_static, (const char*)*begin, length);
441
+ }
442
+ }
443
+
444
+ template<typename T>
445
+ inline VALUE Buffer<T*>::bytes() const
446
+ {
447
+ return this->bytes(this->m_size);
448
+ }
449
+
450
+ template<typename T>
451
+ inline Array Buffer<T*>::toArray(size_t count) const
452
+ {
453
+ if (!this->m_outer)
454
+ {
455
+ return Qnil;
456
+ }
457
+ else
458
+ {
459
+ Array result;
460
+
461
+ T** ptr = this->m_outer;
462
+ T** end = this->m_outer + count;
463
+
464
+ for (; ptr < end; ptr++)
465
+ {
466
+ Buffer<T> buffer(*ptr);
467
+ result.push(std::move(buffer));
468
+ }
469
+ return result;
470
+ }
471
+ }
472
+
473
+ template<typename T>
474
+ inline Array Buffer<T*>::toArray() const
475
+ {
476
+ return this->toArray(this->m_size);
477
+ }
478
+
479
+ // ---- Buffer<void> -------
480
+ inline Buffer<void>::Buffer(void* pointer) : m_buffer(pointer)
481
+ {
482
+ }
483
+
484
+ inline Buffer<void>::Buffer(Buffer<void>&& other) : m_buffer(other.m_buffer)
485
+ {
486
+ other.m_buffer = nullptr;
487
+ }
488
+
489
+ inline Buffer<void>& Buffer<void>::operator=(Buffer<void>&& other)
490
+ {
491
+ this->m_buffer = other.m_buffer;
492
+ other.m_buffer = nullptr;
493
+
494
+ return *this;
495
+ }
496
+
497
+ inline void* Buffer<void>::ptr()
498
+ {
499
+ return this->m_buffer;
500
+ }
501
+
502
+ // ------ define_buffer ----------
503
+ template<typename T>
504
+ inline Data_Type<Buffer<T>> define_buffer(std::string klassName)
505
+ {
506
+ using Buffer_T = Buffer<T>;
507
+
508
+ if (klassName.empty())
509
+ {
510
+ std::string typeName = detail::typeName(typeid(Buffer_T));
511
+ klassName = detail::rubyClassName(typeName);
512
+ }
513
+
514
+ Module rb_mRice = define_module("Rice");
515
+
516
+ if constexpr (std::is_void_v<T>)
517
+ {
518
+ return define_class_under<Buffer_T>(rb_mRice, klassName);
519
+ }
520
+ else
521
+ {
522
+ Data_Type<Buffer_T> klass = define_class_under<Buffer_T>(rb_mRice, klassName).
523
+ define_constructor(Constructor<Buffer_T, VALUE>(), Arg("value").setValue()).
524
+ define_method("size", &Buffer_T::size).
525
+ define_method("size=", &Buffer_T::setSize).
526
+ // template define_method<VALUE(Buffer_T::*)() const>("to_s", &Buffer_T::toString, Return().setValue()).
527
+ template define_method<VALUE(Buffer_T::*)(size_t) const>("bytes", &Buffer_T::bytes, Return().setValue()).
528
+ template define_method<VALUE(Buffer_T::*)() const>("bytes", &Buffer_T::bytes, Return().setValue()).
529
+ template define_method<Array(Buffer_T::*)(size_t) const>("to_ary", &Buffer_T::toArray, Return().setValue()).
530
+ template define_method<Array(Buffer_T::*)() const>("to_ary", &Buffer_T::toArray, Return().setValue());
531
+
532
+ if constexpr (!std::is_pointer_v<T>)
533
+ {
534
+ klass.
535
+ define_method("[]", [](const Buffer_T& self, size_t index) -> T
536
+ {
537
+ return self.get(index);
538
+ }).
539
+ define_method("[]=", [](Buffer_T& self, size_t index, T element) -> void
540
+ {
541
+ self.set(index, element);
542
+ });
543
+ }
544
+
545
+ return klass;
546
+ }
547
+ }
548
+
549
+ inline void define_fundamental_buffer_types()
550
+ {
551
+ define_buffer<bool>();
552
+ define_buffer<int>();
553
+ define_buffer<int*>();
554
+ define_buffer<unsigned int>();
555
+ define_buffer<unsigned int*>();
556
+ define_buffer<char>();
557
+ define_buffer<char*>();
558
+ define_buffer<unsigned char>();
559
+ define_buffer<unsigned char*>();
560
+ define_buffer<signed char>();
561
+ define_buffer<signed char*>();
562
+ define_buffer<double>();
563
+ define_buffer<double*>();
564
+ define_buffer<float>();
565
+ define_buffer<float*>();
566
+ define_buffer<long>();
567
+ define_buffer<long*>();
568
+ define_buffer<unsigned long>();
569
+ define_buffer<unsigned long*>();
570
+ define_buffer<long long>();
571
+ define_buffer<long long*>();
572
+ define_buffer<unsigned long long>();
573
+ define_buffer<unsigned long long*>();
574
+ define_buffer<short>();
575
+ define_buffer<short*>();
576
+ define_buffer<unsigned short>();
577
+ define_buffer<unsigned short*>();
578
+ define_buffer<void>();
579
+ }
580
+ }
581
+
582
+ namespace Rice::detail
583
+ {
584
+ template<typename T>
585
+ struct Type<Buffer<T>>
586
+ {
587
+ static bool verify()
588
+ {
589
+ Type<intrinsic_type<T>>::verify();
590
+
591
+ if (!Data_Type<Buffer<T>>::is_defined())
592
+ {
593
+ define_buffer<T>();
594
+ }
595
+
596
+ return true;
597
+ }
598
+ };
599
+ }
data/rice/Constructor.ipp CHANGED
@@ -42,14 +42,14 @@ namespace Rice
42
42
  {
43
43
  // Call C++ constructor
44
44
  T* data = new T(args...);
45
- detail::replace<T>(self, Data_Type<T>::ruby_data_type(), data, true);
45
+ detail::wrapConstructed<T>(self, Data_Type<T>::ruby_data_type(), data, true);
46
46
  }
47
47
 
48
48
  static void initialize_copy(VALUE self, const T& other)
49
49
  {
50
50
  // Call C++ copy constructor
51
51
  T* data = new T(other);
52
- detail::replace<T>(self, Data_Type<T>::ruby_data_type(), data, true);
52
+ detail::wrapConstructed<T>(self, Data_Type<T>::ruby_data_type(), data, true);
53
53
  }
54
54
 
55
55
  };
@@ -73,7 +73,7 @@ namespace Rice
73
73
  {
74
74
  // Call C++ constructor
75
75
  T* data = new T(self, args...);
76
- detail::replace<T>(self.value(), Data_Type<T>::ruby_data_type(), data, true);
76
+ detail::wrapConstructed<T>(self.value(), Data_Type<T>::ruby_data_type(), data, true);
77
77
  }
78
78
  };
79
79
  }
data/rice/Data_Object.hpp CHANGED
@@ -41,9 +41,6 @@ namespace Rice
41
41
  static_assert(!std::is_volatile_v<T>);
42
42
  static_assert(!std::is_void_v<T>);
43
43
 
44
- public:
45
- static T* from_ruby(VALUE value, bool takeOwnership = false);
46
-
47
44
  public:
48
45
  //! Wrap a C++ object.
49
46
  /*! This constructor is analogous to calling Data_Wrap_Struct. Be
@@ -58,6 +55,7 @@ namespace Rice
58
55
  */
59
56
  Data_Object(T* obj, bool isOwner = false, Class klass = Data_Type<T>::klass());
60
57
  Data_Object(T& obj, bool isOwner = false, Class klass = Data_Type<T>::klass());
58
+ Data_Object(T&& obj, Class klass = Data_Type<T>::klass());
61
59
  Data_Object(const T& obj, bool isOwner = false, Class klass = Data_Type<T>::klass());
62
60
 
63
61
  //! Unwrap a Ruby object.
@@ -66,6 +64,7 @@ namespace Rice
66
64
  * \param value the Ruby object to unwrap.
67
65
  */
68
66
  Data_Object(Object value);
67
+ Data_Object(VALUE value);
69
68
 
70
69
  T& operator*() const; //!< Return a reference to obj_
71
70
  T* operator->() const; //!< Return a pointer to obj_