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,209 @@
1
+ #ifndef Rice__Array__hpp_
2
+ #define Rice__Array__hpp_
3
+
4
+ #include <iterator>
5
+ #include <memory>
6
+
7
+ namespace Rice
8
+ {
9
+ //! A wrapper for the ruby Array class.
10
+ /*! This class provides a C++-style interface to ruby's Array class and
11
+ * its associated rb_ary_* functions.
12
+ * Example:
13
+ * \code
14
+ * Array a;
15
+ * a.push(String("some string"));
16
+ * a.push(42);
17
+ * \endcode
18
+ */
19
+ class Array
20
+ : public Builtin_Object<T_ARRAY>
21
+ {
22
+ public:
23
+ //! Construct a new array
24
+ Array();
25
+
26
+ //! Construct a new array with specified size
27
+ Array(long capacity);
28
+
29
+ //! Wrap an existing array
30
+ /*! \param v a ruby object, which must be of type T_ARRAY.
31
+ */
32
+ Array(Object v);
33
+
34
+ //! Wrap an existing array
35
+ /*! \param v a ruby object, which must be of type T_ARRAY.
36
+ */
37
+ Array(VALUE v);
38
+
39
+ //! Construct an array from a sequence.
40
+ /*! \param begin an iterator to the beginning of the sequence.
41
+ * \param end an iterator to the end of the sequence.
42
+ */
43
+ template<typename Iter_T>
44
+ Array(Iter_T begin, Iter_T end);
45
+
46
+ //! Construct an Array from a C array.
47
+ /*! \param a a C array of type T and size n.
48
+ */
49
+ template<typename T, long n>
50
+ Array(T const (&a)[n]);
51
+
52
+ public:
53
+ //! Return the size of the array.
54
+ long size() const;
55
+
56
+ //! Return the element at the given index.
57
+ /*! \param index The index of the desired element. The index may be
58
+ * negative, to indicate an offset from the end of the array. If the
59
+ * index is out of bounds, this function has undefined behavior.
60
+ * \return the element at the given index.
61
+ */
62
+ Object operator[](long index) const;
63
+
64
+ //! Converts a Ruby Array into a C++ array where the elements are
65
+ //! contiguous. This is similar to a std::vector, but instead a
66
+ //! std::unique_ptr<T> is returned. This allows the calling code to
67
+ //! pass ownership to its callers if needed (for exmaple, From_Ruby<T>#convert)
68
+ //! Note this method is designed convenience, not for speed or efficieny.
69
+ //! C++ APIs that take large chunks of memory should not be passed Ruby Arrrays.
70
+ //! \return std::unique_ptr that is owned by the caller.
71
+ template<typename T>
72
+ String pack();
73
+
74
+ // Join elements together
75
+ String join(const char* separator);
76
+
77
+ private:
78
+ //! A helper class so array[index]=value can work.
79
+ class Proxy;
80
+
81
+ public:
82
+ //! Return a reference to the element at the given index.
83
+ /*! \param index The index of the desired element. The index may be
84
+ * negative, to indicate an offset from the end of the array. If the
85
+ * index is out of bounds, this function has undefined behavior.
86
+ * \return the element at the given index.
87
+ */
88
+ Proxy operator[](long index);
89
+
90
+ //! Push an element onto the end of the array
91
+ /*! \param v an object to push onto the array.
92
+ * \return the object which was pushed onto the array.
93
+ */
94
+ template<typename T>
95
+ Object push(T obj);
96
+
97
+ //! Pop an element from the end of the array
98
+ /*! \return the object which was popped from the array, or Qnil if
99
+ * the array was empty.
100
+ */
101
+ Object pop();
102
+
103
+ //! Unshift an element onto the beginning of the array
104
+ /*! \param v an object to unshift onto the array.
105
+ * \return the object which was unshifted onto the array.
106
+ */
107
+ template<typename T>
108
+ Object unshift(T const& obj);
109
+
110
+ //! Shift an element from the beginning of the array
111
+ /*! \return the object which was shifted from the array.
112
+ */
113
+ Object shift();
114
+
115
+ private:
116
+ template<typename Array_Ptr_T, typename Value_T>
117
+ class Iterator;
118
+
119
+ long position_of(long index) const;
120
+
121
+ public:
122
+ //! An iterator.
123
+ typedef Iterator<Array*, Proxy> iterator;
124
+
125
+ //! A const iterator.
126
+ typedef Iterator<Array const*, Object> const_iterator;
127
+
128
+ //! Return an iterator to the beginning of the array.
129
+ iterator begin();
130
+
131
+ //! Return a const iterator to the beginning of the array.
132
+ const_iterator begin() const;
133
+
134
+ //! Return an iterator to the end of the array.
135
+ iterator end();
136
+
137
+ //! Return a const iterator to the end of the array.
138
+ const_iterator end() const;
139
+
140
+ //! Return the content of the array as a std::vector
141
+ template<typename T>
142
+ std::vector<T> to_vector();
143
+ };
144
+
145
+ //! A helper class so array[index]=value can work.
146
+ class Array::Proxy
147
+ {
148
+ public:
149
+ //! Construct a new Proxy
150
+ Proxy(Array array, long index);
151
+
152
+ //! Implicit conversions
153
+ operator Object() const;
154
+
155
+ //! Explicit conversion to VALUE.
156
+ VALUE value() const;
157
+
158
+ //! Assignment operator.
159
+ template<typename T>
160
+ Object operator=(T const& value);
161
+
162
+ private:
163
+ Array array_;
164
+ long index_;
165
+ };
166
+
167
+ //! A helper class for implementing iterators for a Array.
168
+ // TODO: This really should be a random-access iterator.
169
+ template<typename Array_Ptr_T, typename Value_T>
170
+ class Array::Iterator
171
+ {
172
+ public:
173
+ using iterator_category = std::forward_iterator_tag;
174
+ using value_type = Value_T;
175
+ using difference_type = long;
176
+ using pointer = Object*;
177
+ using reference = Value_T&;
178
+
179
+ Iterator(Array_Ptr_T array, long index);
180
+
181
+ template<typename Array_Ptr_T_, typename Value_T_>
182
+ Iterator(Iterator<Array_Ptr_T_, Value_T_> const& rhs);
183
+
184
+ template<typename Array_Ptr_T_, typename Value_T_>
185
+ Iterator& operator=(Iterator<Array_Ptr_T_, Value_T_> const& rhs);
186
+
187
+ Iterator& operator++();
188
+ Iterator operator++(int);
189
+ Value_T operator*();
190
+ Object* operator->();
191
+
192
+ template<typename Array_Ptr_T_, typename Value_T_>
193
+ bool operator==(Iterator<Array_Ptr_T_, Value_T_> const& rhs) const;
194
+
195
+ template<typename Array_Ptr_T_, typename Value_T_>
196
+ bool operator!=(Iterator<Array_Ptr_T_, Value_T_> const& rhs) const;
197
+
198
+ Array_Ptr_T array() const;
199
+ long index() const;
200
+
201
+ private:
202
+ Array_Ptr_T array_;
203
+ long index_;
204
+
205
+ Object tmp_;
206
+ };
207
+ } // namespace Rice
208
+
209
+ #endif // Rice__Array__hpp_
@@ -0,0 +1,304 @@
1
+ #ifndef Rice__Array__ipp_
2
+ #define Rice__Array__ipp_
3
+
4
+ namespace Rice
5
+ {
6
+ inline Array::Array() : Builtin_Object<T_ARRAY>(detail::protect(rb_ary_new))
7
+ {
8
+ }
9
+
10
+ inline Array::Array(long capacity) : Builtin_Object<T_ARRAY>(detail::protect(rb_ary_new_capa, capacity))
11
+ {
12
+ }
13
+
14
+ inline Array::Array(Object v) : Builtin_Object<T_ARRAY>(v)
15
+ {
16
+ }
17
+
18
+ inline Array::Array(VALUE v) : Builtin_Object<T_ARRAY>(v)
19
+ {
20
+ }
21
+
22
+ template<typename Iter_T>
23
+ inline Array::Array(Iter_T it, Iter_T end) : Builtin_Object<T_ARRAY>(detail::protect(rb_ary_new))
24
+ {
25
+ for (; it != end; ++it)
26
+ {
27
+ push(*it);
28
+ }
29
+ }
30
+
31
+ template<typename T, long n>
32
+ inline Array::Array(T const (&a)[n]) : Builtin_Object<T_ARRAY>(detail::protect(rb_ary_new))
33
+ {
34
+ for (long j = 0; j < n; ++j)
35
+ {
36
+ push(a[j]);
37
+ }
38
+ }
39
+
40
+ inline long Array::size() const
41
+ {
42
+ return RARRAY_LEN(this->value());
43
+ }
44
+
45
+ inline String Array::join(const char* separator)
46
+ {
47
+ return this->call("join", separator);
48
+ }
49
+
50
+ template<typename T>
51
+ String Array::pack()
52
+ {
53
+ // Use .c_str so that the stl.hpp header is not required
54
+ return this->call("pack", detail::RubyType<T>::packTemplate.c_str());
55
+ }
56
+
57
+ inline Object Array::operator[](long index) const
58
+ {
59
+ return detail::protect(rb_ary_entry, value(), position_of(index));
60
+ }
61
+
62
+ inline Array::Proxy Array::operator[](long index)
63
+ {
64
+ return Proxy(*this, position_of(index));
65
+ }
66
+
67
+ template<typename T>
68
+ inline Object Array::push(T obj)
69
+ {
70
+ return detail::protect(rb_ary_push, value(), detail::To_Ruby<T>().convert(std::forward<T>(obj)));
71
+ }
72
+
73
+ inline Object Array::pop()
74
+ {
75
+ return detail::protect(rb_ary_pop, value());
76
+ }
77
+
78
+ template<typename T>
79
+ inline Object Array::unshift(T const& obj)
80
+ {
81
+ return detail::protect(rb_ary_unshift, value(), detail::To_Ruby<T>().convert(obj));
82
+ }
83
+
84
+ inline Object Array::shift()
85
+ {
86
+ return detail::protect(rb_ary_shift, value());
87
+ }
88
+
89
+ inline long Array::position_of(long index) const
90
+ {
91
+ if (index < 0)
92
+ {
93
+ return size() + index;
94
+ }
95
+ else
96
+ {
97
+ return static_cast<long>(index);
98
+ }
99
+ }
100
+
101
+ template<typename T>
102
+ std::vector<T> Array::to_vector()
103
+ {
104
+ std::vector<T> result;
105
+
106
+ long size = this->size();
107
+ result.reserve(size);
108
+
109
+ detail::From_Ruby<T> fromRuby;
110
+ for (long i = 0; i < size; i++)
111
+ {
112
+ VALUE element = detail::protect(rb_ary_entry, this->value(), i);
113
+ result.push_back(fromRuby.convert(element));
114
+ }
115
+
116
+ return result;
117
+ }
118
+
119
+ inline Array::Proxy::Proxy(Array array, long index)
120
+ : array_(array)
121
+ , index_(index)
122
+ {
123
+ }
124
+
125
+ inline VALUE Array::Proxy::value() const
126
+ {
127
+ return detail::protect(rb_ary_entry, array_.value(), index_);
128
+ }
129
+
130
+ inline Array::Proxy::operator Object() const
131
+ {
132
+ return Object(this->value());
133
+ }
134
+
135
+ template<typename T>
136
+ Object Array::Proxy::operator=(T const& value)
137
+ {
138
+ Object o = detail::To_Ruby<T>().convert(value);
139
+ detail::protect(rb_ary_store, array_.value(), index_, o.value());
140
+ return o;
141
+ }
142
+
143
+ template<typename Array_Ptr_T, typename Value_T>
144
+ inline Array::Iterator<Array_Ptr_T, Value_T>::Iterator(Array_Ptr_T array, long index) :
145
+ array_(array), index_(index)
146
+ {
147
+ }
148
+
149
+ template<typename Array_Ptr_T, typename Value_T>
150
+ template<typename Array_Ptr_T_, typename Value_T_>
151
+ inline
152
+ Array::Iterator<Array_Ptr_T, Value_T>::Iterator(Iterator<Array_Ptr_T_, Value_T_> const& rhs) :
153
+ array_(rhs.array()) , index_(rhs.index()), tmp_()
154
+ {
155
+ }
156
+
157
+ template<typename Array_Ptr_T, typename Value_T>
158
+ template<typename Array_Ptr_T_, typename Value_T_>
159
+ inline Array::Iterator<Array_Ptr_T, Value_T>& Array::Iterator<Array_Ptr_T, Value_T>::operator=(Iterator<Array_Ptr_T_, Value_T_> const& rhs)
160
+ {
161
+ array_ = rhs.array_;
162
+ index_ = rhs.index_;
163
+ return *this;
164
+ }
165
+
166
+ template<typename Array_Ptr_T, typename Value_T>
167
+ inline Array::Iterator<Array_Ptr_T, Value_T>& Array::Iterator<Array_Ptr_T, Value_T>::operator++()
168
+ {
169
+ ++index_;
170
+ return *this;
171
+ }
172
+
173
+ template<typename Array_Ptr_T, typename Value_T>
174
+ inline Array::Iterator<Array_Ptr_T, Value_T> Array::Iterator<Array_Ptr_T, Value_T>::operator++(int)
175
+ {
176
+ Array copy(*this);
177
+ ++(*this);
178
+ return *this;
179
+ }
180
+
181
+ template<typename Array_Ptr_T, typename Value_T>
182
+ inline Value_T Array::Iterator<Array_Ptr_T, Value_T>::operator*()
183
+ {
184
+ return (*array_)[index_];
185
+ }
186
+
187
+ template<typename Array_Ptr_T, typename Value_T>
188
+ inline Object* Array::Iterator<Array_Ptr_T, Value_T>::operator->()
189
+ {
190
+ tmp_ = (*array_)[index_];
191
+ return &tmp_;
192
+ }
193
+
194
+ template<typename Array_Ptr_T, typename Value_T>
195
+ template<typename Array_Ptr_T_, typename Value_T_>
196
+ inline bool Array::Iterator<Array_Ptr_T, Value_T>::operator==(Iterator<Array_Ptr_T_, Value_T_> const& rhs) const
197
+ {
198
+ return array_->value() == rhs.array_->value() && index_ == rhs.index_;
199
+ }
200
+
201
+ template<typename Array_Ptr_T, typename Value_T>
202
+ template<typename Array_Ptr_T_, typename Value_T_>
203
+ inline bool Array::Iterator<Array_Ptr_T, Value_T>::operator!=(Iterator<Array_Ptr_T_, Value_T_> const& rhs) const
204
+ {
205
+ return !(*this == rhs);
206
+ }
207
+
208
+ template<typename Array_Ptr_T, typename Value_T>
209
+ Array_Ptr_T Array::Iterator<Array_Ptr_T, Value_T>::array() const
210
+ {
211
+ return array_;
212
+ }
213
+
214
+ template<typename Array_Ptr_T, typename Value_T>
215
+ long Array::Iterator<Array_Ptr_T, Value_T>::index() const
216
+ {
217
+ return index_;
218
+ }
219
+
220
+ inline Array::iterator Array::begin()
221
+ {
222
+ return iterator(this, 0);
223
+ }
224
+
225
+ inline Array::const_iterator Array::begin() const
226
+ {
227
+ return const_iterator(this, 0);
228
+ }
229
+
230
+ inline Array::iterator Array::end()
231
+ {
232
+ return iterator(this, size());
233
+ }
234
+
235
+ inline Array::const_iterator Array::end() const
236
+ {
237
+ return const_iterator(this, size());
238
+ }
239
+ }
240
+
241
+ namespace Rice::detail
242
+ {
243
+ template<>
244
+ struct Type<Array>
245
+ {
246
+ static bool verify()
247
+ {
248
+ return true;
249
+ }
250
+ };
251
+
252
+ template<>
253
+ class To_Ruby<Array>
254
+ {
255
+ public:
256
+ VALUE convert(Array const& x)
257
+ {
258
+ return x.value();
259
+ }
260
+ };
261
+
262
+ template<>
263
+ class To_Ruby<Array&>
264
+ {
265
+ public:
266
+ VALUE convert(Array const& x)
267
+ {
268
+ return x.value();
269
+ }
270
+ };
271
+
272
+ template<>
273
+ class To_Ruby<Array*>
274
+ {
275
+ public:
276
+ VALUE convert(Array const* x)
277
+ {
278
+ return x->value();
279
+ }
280
+ };
281
+
282
+ template<>
283
+ class From_Ruby<Array>
284
+ {
285
+ public:
286
+ Convertible is_convertible(VALUE value)
287
+ {
288
+ switch (rb_type(value))
289
+ {
290
+ case RUBY_T_ARRAY:
291
+ return Convertible::Exact;
292
+ break;
293
+ default:
294
+ return Convertible::None;
295
+ }
296
+ }
297
+
298
+ Array convert(VALUE value)
299
+ {
300
+ return Array(value);
301
+ }
302
+ };
303
+ }
304
+ #endif // Rice__Array__ipp_
@@ -0,0 +1,31 @@
1
+ #ifndef Rice__Builtin_Object__hpp_
2
+ #define Rice__Builtin_Object__hpp_
3
+
4
+ namespace Rice
5
+ {
6
+ //! A smartpointer-like wrapper for Ruby builtin objects.
7
+ /*! A builtin object is one of Ruby's internal types, e.g. RArray or
8
+ * RString. Every builtin type structure has a corresponding integer
9
+ * type number (e.g T_ARRAY for RArray or T_STRING for RString). This
10
+ * class is a wrapper for those types of objects, primarily useful as a
11
+ * base class for other wrapper classes like Array and Hash.
12
+ */
13
+ template<int Builtin_Type>
14
+ class Builtin_Object
15
+ : public Object
16
+ {
17
+ public:
18
+ //! Wrap an already allocated Ruby object.
19
+ /*! Checks to see if the object is an object of type Builtin_Type; a
20
+ * C++ exception is thrown if this is not the case.
21
+ * \param value the object to be wrapped.
22
+ */
23
+ Builtin_Object(Object value);
24
+
25
+ RObject& operator*() const; //!< Return a reference to obj_
26
+ RObject* operator->() const; //!< Return a pointer to obj_
27
+ RObject* get() const; //!< Return a pointer to obj_
28
+ };
29
+ } // namespace Rice
30
+
31
+ #endif // Rice__Builtin_Object__hpp_
@@ -0,0 +1,37 @@
1
+ #include <algorithm>
2
+
3
+ namespace Rice
4
+ {
5
+ namespace detail
6
+ {
7
+ inline VALUE check_type(Object value, int type)
8
+ {
9
+ detail::protect(rb_check_type, value.value(), type);
10
+ return Qnil;
11
+ }
12
+ }
13
+
14
+ template<int Builtin_Type>
15
+ inline Builtin_Object<Builtin_Type>::Builtin_Object(Object value) : Object(value)
16
+ {
17
+ detail::check_type(value, Builtin_Type);
18
+ }
19
+
20
+ template<int Builtin_Type>
21
+ inline RObject& Builtin_Object<Builtin_Type>::operator*() const
22
+ {
23
+ return *ROBJECT(this->value());
24
+ }
25
+
26
+ template<int Builtin_Type>
27
+ inline RObject* Builtin_Object<Builtin_Type>::operator->() const
28
+ {
29
+ return ROBJECT(this->value());
30
+ }
31
+
32
+ template<int Builtin_Type>
33
+ inline RObject* Builtin_Object<Builtin_Type>::get() const
34
+ {
35
+ return ROBJECT(this->value());
36
+ }
37
+ } // namespace Rice
@@ -0,0 +1,70 @@
1
+ #ifndef Rice__Class__hpp_
2
+ #define Rice__Class__hpp_
3
+
4
+ /*!
5
+ * \example inheritance/animals.cpp
6
+ * \example callbacks/sample_callbacks.cpp
7
+ */
8
+
9
+ namespace Rice
10
+ {
11
+ //! A helper for defining a Class and its methods.
12
+ /*! This class provides a C++-style interface to ruby's Class class and
13
+ * for defining methods on that class.
14
+ */
15
+ class Class: public Module
16
+ {
17
+ public:
18
+ //! Default construct a new class wrapper and initialize it to
19
+ //! rb_cObject.
20
+ Class() = default;
21
+
22
+ //! Construct a new class wrapper from a ruby object of type T_CLASS.
23
+ Class(VALUE v);
24
+
25
+ //! Disallow creation of an instance from Ruby code.
26
+ /*! Undefines the singleton method allocate (or new, if using a
27
+ * version of ruby prior to 1.7) and the instance method initialize.
28
+ */
29
+ Class & undef_creation_funcs();
30
+
31
+ // Create a new instance
32
+ template<typename ...Arg_Ts>
33
+ Object create(Arg_Ts ...args);
34
+
35
+ //! Class name
36
+ /*! \return std::string.
37
+ */
38
+ const std::string name() const;
39
+
40
+ //! Base class name - does not include any parent modules
41
+ /*! \return std::string.
42
+ */
43
+ const std::string base_name() const;
44
+
45
+ #include "shared_methods.hpp"
46
+ };
47
+
48
+ //! Define a new class in the namespace given by module.
49
+ /*! \param module the Module in which to define the class.
50
+ * \param name the name of the class.
51
+ * \param superclass the base class to use.
52
+ * \return the new class.
53
+ */
54
+ Class define_class_under(Object parent, char const * name, const Class& superclass = rb_cObject);
55
+ Class define_class_under(Object parent, Identifier id, const Class& superclass = rb_cObject);
56
+
57
+ //! Define a new class in the default namespace.
58
+ /*! \param name the name of the class.
59
+ * \param superclass the base class to use.
60
+ * \return the new class.
61
+ */
62
+ Class define_class(char const * name, const Class& superclass = rb_cObject);
63
+
64
+ //! Create a new anonymous class.
65
+ /*! \return the new class.
66
+ */
67
+ Class anonymous_class();
68
+ } // namespace Rice
69
+
70
+ #endif // Rice__Class__hpp_