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
@@ -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_
@@ -0,0 +1,97 @@
1
+
2
+ namespace Rice
3
+ {
4
+ inline Class::Class(VALUE value) : Module(value)
5
+ {
6
+ detail::protect(rb_check_type, value, (int)T_CLASS);
7
+ }
8
+
9
+ inline Class& Class::undef_creation_funcs()
10
+ {
11
+ detail::protect(rb_undef_alloc_func, value());
12
+ detail::protect(rb_undef_method, value(), "initialize");
13
+ return *this;
14
+ }
15
+
16
+ template<typename ...Arg_Ts>
17
+ inline Object Class::create(Arg_Ts ...args)
18
+ {
19
+ return this->call("new", args...);
20
+ }
21
+
22
+ inline const std::string Class::name() const
23
+ {
24
+ const char* buffer = rb_class2name(this->value());
25
+ return std::string(buffer);
26
+ }
27
+
28
+ inline const std::string Class::base_name() const
29
+ {
30
+ std::string name = this->name();
31
+ auto regex = std::regex("^.*::");
32
+ std::string result = std::regex_replace(name, regex, "");
33
+ return result;
34
+ }
35
+
36
+ inline Class define_class_under(Object parent, Identifier id, const Class& superclass)
37
+ {
38
+ VALUE klass = detail::protect(rb_define_class_id_under, parent.value(), id, superclass.value());
39
+
40
+ // We MUST reset the instance registry in case the user just redefined a class which resets it
41
+ detail::Registries::instance.natives.reset(klass);
42
+
43
+ return klass;
44
+ }
45
+
46
+ inline Class define_class_under(Object parent, char const* name, const Class& superclass)
47
+ {
48
+ Identifier id(name);
49
+ return define_class_under(parent, id, superclass);
50
+ }
51
+
52
+ inline Class define_class(char const* name, const Class& superclass)
53
+ {
54
+ VALUE klass = detail::protect(rb_define_class, name, superclass.value());
55
+
56
+ // We MUST reset the instance registry in case the user just redefined a class which resets it
57
+ detail::Registries::instance.natives.reset(klass);
58
+
59
+ return klass;
60
+ }
61
+
62
+ inline Class anonymous_class()
63
+ {
64
+ VALUE klass = detail::protect(rb_class_new, rb_cObject);
65
+ VALUE singleton = detail::protect(rb_singleton_class, klass);
66
+
67
+ // Ruby will reuse addresses previously assigned to other modules
68
+ // that have subsequently been garbage collected
69
+ detail::Registries::instance.natives.reset(klass);
70
+ detail::Registries::instance.natives.reset(singleton);
71
+
72
+ return klass;
73
+ }
74
+ }
75
+
76
+ namespace Rice::detail
77
+ {
78
+ template<>
79
+ class To_Ruby<Class>
80
+ {
81
+ public:
82
+ static VALUE convert(Class const& x)
83
+ {
84
+ return x.value();
85
+ }
86
+ };
87
+
88
+ template<>
89
+ class From_Ruby<Class>
90
+ {
91
+ public:
92
+ Class convert(VALUE value)
93
+ {
94
+ return Class(value);
95
+ }
96
+ };
97
+ }
@@ -0,0 +1,32 @@
1
+ #ifndef Rice__Encoding__hpp_
2
+ #define Rice__Encoding__hpp_
3
+
4
+ namespace Rice
5
+ {
6
+ //! A wrapper for a Ruby encoding
7
+ class Encoding
8
+ {
9
+ public:
10
+ static Encoding utf8();
11
+
12
+ //! Wrap an existing encoding.
13
+ Encoding(rb_encoding* encoding);
14
+
15
+ //! Implicit conversion to VALUE.
16
+ operator rb_encoding* () const
17
+ {
18
+ return this->encoding_;
19
+ }
20
+
21
+ operator VALUE () const
22
+ {
23
+ return detail::protect(rb_enc_from_encoding, this->encoding_);
24
+ }
25
+
26
+ private:
27
+ rb_encoding* encoding_;
28
+ };
29
+ } // namespace Rice
30
+
31
+ #endif // Rice__Encoding__hpp_
32
+
@@ -0,0 +1,59 @@
1
+ namespace Rice
2
+ {
3
+ inline Encoding Encoding::utf8()
4
+ {
5
+ return Encoding(rb_utf8_encoding());
6
+ }
7
+
8
+ inline Encoding::Encoding(rb_encoding* encoding) : encoding_(encoding)
9
+ {
10
+ }
11
+ }
12
+
13
+ /*namespace Rice::detail
14
+ {
15
+ template<>
16
+ struct Type<Encoding>
17
+ {
18
+ static bool verify()
19
+ {
20
+ return true;
21
+ }
22
+ };
23
+
24
+ template<>
25
+ class To_Ruby<Encoding>
26
+ {
27
+ public:
28
+ VALUE convert(const Encoding& encoding)
29
+ {
30
+ // return x.value();
31
+ }
32
+ };
33
+
34
+ template<>
35
+ class From_Ruby<Encoding>
36
+ {
37
+ public:
38
+ Convertible is_convertible(VALUE value)
39
+ {
40
+ switch (rb_type(value))
41
+ {
42
+ case RUBY_T_SYMBOL:
43
+ return Convertible::Exact;
44
+ break;
45
+ case RUBY_T_STRING:
46
+ return Convertible::Cast;
47
+ break;
48
+ default:
49
+ return Convertible::None;
50
+ }
51
+ }
52
+
53
+ Encoding convert(VALUE value)
54
+ {
55
+ // return Symbol(value);
56
+ }
57
+ };
58
+ }
59
+ */