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,74 @@
1
+ #ifndef Rice__String__hpp_
2
+ #define Rice__String__hpp_
3
+
4
+ namespace Rice
5
+ {
6
+ //! A Wraper for the ruby String class.
7
+ /*! This class provides a C++-style interface to ruby's String class and
8
+ * its associated rb_str_* functions.
9
+ *
10
+ * Example:
11
+ * \code
12
+ * String s(String::format("%s: %d", "foo", 42));
13
+ * std::cout << s.length() << std::endl;
14
+ * \endcode
15
+ */
16
+ class String
17
+ : public Builtin_Object<T_STRING>
18
+ {
19
+ public:
20
+ //! Construct a new string.
21
+ String();
22
+
23
+ //! Wrap an existing string.
24
+ String(VALUE v);
25
+
26
+ //! Wrap an existing string.
27
+ String(Object v);
28
+
29
+ //! Construct a String from an Identifier.
30
+ String(Identifier id);
31
+
32
+ //! Construct a String from a null-terminated C string.
33
+ String(char const* s);
34
+
35
+ //! Construct a String from an std::string.
36
+ String(std::string const& s);
37
+
38
+ //! Construct a String from an std::string_view.
39
+ String(std::string_view const& s);
40
+
41
+ //! Format a string using printf-style formatting.
42
+ template <typename... Arg_Ts>
43
+ static inline String format(char const* fmt, Arg_Ts&&...args);
44
+
45
+ //! Get the length of the String.
46
+ /*! \return the length of the string.
47
+ */
48
+ size_t length() const;
49
+
50
+ //! Get the character at the given index.
51
+ /*! \param index the desired index.
52
+ * \return the character at the given index.
53
+ */
54
+ char operator[](ptrdiff_t index) const;
55
+
56
+ //! Return a pointer to the beginning of the underlying C string.
57
+ char const* c_str() const;
58
+
59
+ //! Return a copy of the string as an std::string.
60
+ std::string str() const;
61
+
62
+ //! Return an array from a string by unpacking it
63
+ template<typename T>
64
+ Array unpack() const;
65
+
66
+ //! Create an Identifier from the String.
67
+ /*! Calls rb_intern to create an ID.
68
+ * \return an Identifier holding the ID returned from rb_intern.
69
+ */
70
+ Identifier intern() const;
71
+ };
72
+ } // namespace Rice
73
+
74
+ #endif // Rice__String__hpp_
@@ -0,0 +1,120 @@
1
+ namespace Rice
2
+ {
3
+ inline String::String() : Builtin_Object<T_STRING>(detail::protect(rb_str_new2, ""))
4
+ {
5
+ }
6
+
7
+ inline String::String(VALUE v) : Builtin_Object<T_STRING>(v)
8
+ {
9
+ }
10
+
11
+ inline String::String(Object v) : Builtin_Object<T_STRING>(v)
12
+ {
13
+ }
14
+
15
+ inline String::String(char const* s) : Builtin_Object<T_STRING>(detail::protect(rb_utf8_str_new_cstr, s))
16
+ {
17
+ }
18
+
19
+ inline String::String(std::string const& s) : Builtin_Object<T_STRING>(detail::protect(rb_utf8_str_new, s.data(), (long)s.length()))
20
+ {
21
+ }
22
+
23
+ inline String::String(std::string_view const& s) : Builtin_Object<T_STRING>(detail::protect(rb_utf8_str_new, s.data(), (long)s.length()))
24
+ {
25
+ }
26
+
27
+ inline String::String(Identifier id) : Builtin_Object<T_STRING>(detail::protect(rb_utf8_str_new_cstr, id.c_str()))
28
+ {
29
+ }
30
+
31
+ template <typename... Arg_Ts>
32
+ inline String String::format(char const* fmt, Arg_Ts&&...args)
33
+ {
34
+ size_t size = std::snprintf(nullptr, 0, fmt, std::forward<Arg_Ts>(args)...);
35
+ std::string temp(size, '\0');
36
+
37
+ // size+1 avoids truncating the string. Otherwise snprintf writes n - 1 characters
38
+ // to allow space for null character but we don't need that since std::string
39
+ // will add a null character internally at n + 1
40
+ std::snprintf(&temp[0], size + 1, fmt, std::forward<Arg_Ts>(args)...);
41
+
42
+ String s = String(temp.c_str());
43
+ return s;
44
+ }
45
+
46
+ inline size_t String::length() const
47
+ {
48
+ return RSTRING_LEN(value());
49
+ }
50
+
51
+ inline char String::operator[](ptrdiff_t index) const
52
+ {
53
+ return RSTRING_PTR(value())[index];
54
+ }
55
+
56
+ inline char const* String::c_str() const
57
+ {
58
+ return RSTRING_PTR(value());
59
+ }
60
+
61
+ inline std::string String::str() const
62
+ {
63
+ return std::string(RSTRING_PTR(value()), length());
64
+ }
65
+
66
+ template<typename T>
67
+ inline Array String::unpack() const
68
+ {
69
+ return this->call("unpack", detail::RubyType<T>::packTemplate.c_str());
70
+ }
71
+
72
+ inline Identifier String::intern() const
73
+ {
74
+ return rb_intern(c_str());
75
+ }
76
+ }
77
+
78
+ namespace Rice::detail
79
+ {
80
+ template<>
81
+ struct Type<String>
82
+ {
83
+ static bool verify()
84
+ {
85
+ return true;
86
+ }
87
+ };
88
+
89
+ template<>
90
+ class To_Ruby<String>
91
+ {
92
+ public:
93
+ VALUE convert(String const& x)
94
+ {
95
+ return x.value();
96
+ }
97
+ };
98
+
99
+ template<>
100
+ class From_Ruby<String>
101
+ {
102
+ public:
103
+ Convertible is_convertible(VALUE value)
104
+ {
105
+ switch (rb_type(value))
106
+ {
107
+ case RUBY_T_STRING:
108
+ return Convertible::Exact;
109
+ break;
110
+ default:
111
+ return Convertible::None;
112
+ }
113
+ }
114
+
115
+ String convert(VALUE value)
116
+ {
117
+ return String(value);
118
+ }
119
+ };
120
+ }
@@ -0,0 +1,113 @@
1
+ #ifndef Rice__ruby_struct__hpp_
2
+ #define Rice__ruby_struct__hpp_
3
+
4
+ namespace Rice
5
+ {
6
+ //! A wrapper for creating Struct classes.
7
+ /*! The Struct class is used for creating new Classes. Note that the
8
+ * notation used here differs slightly from the notation inside the
9
+ * interpreter.
10
+ *
11
+ * Inside the interpreter, calling Struct.new creates a new Class:
12
+ * \code
13
+ * irb(main):001:0> MyStruct = Struct.new(:a, :b, :c)
14
+ * => S
15
+ * irb(main):002:0> MyStruct.class
16
+ * => Class
17
+ * \endcode
18
+ *
19
+ * Instantiating that Class creates an instance of that Class:
20
+ * \code
21
+ * irb(main):003:0> mystruct_instance = MyStruct.new
22
+ * => #<struct MyStruct a=nil, b=nil, c=nil>
23
+ * irb(main):004:0> mystruct_instance.class
24
+ * => MyStruct
25
+ * irb(main):005:0> mystruct_instance.class.ancestors
26
+ * => [MyStruct, Struct, Enumerable, Object, Kernel]
27
+ * \endcode
28
+ *
29
+ * Thus, inside the interpreter, MyStruct is a Class which inherits
30
+ * from Struct, and mystruct_instance is an instance of MyStruct.
31
+ *
32
+ * At the C++ level, we might do this instead:
33
+ * \code
34
+ * Struct rb_cMyStruct = define_struct()
35
+ * .define_member("a")
36
+ * .define_member("b")
37
+ * .define_member("c")
38
+ * .initialize("MyStruct");
39
+ * Struct::Instance mystruct_instance(rb_cMyStruct.new_instance());
40
+ * \endcode
41
+ *
42
+ * Here rb_cMyStruct is an instance of Struct and that mystruct_instance
43
+ * is an instance of Struct::Instance.
44
+ */
45
+ class Struct : public Class
46
+ {
47
+ public:
48
+ //! Define a new Struct member.
49
+ /*! Defines a new member of the Struct. Must be called before the
50
+ * Struct is initialized.
51
+ * \return *this
52
+ */
53
+ Struct& define_member(Identifier name);
54
+
55
+ //! Initialize the Struct class.
56
+ /*! Must be called after all Struct members have been defined.
57
+ * \param module the module under which to define the Struct.
58
+ * \param name the name of the Class at the ruby level.
59
+ */
60
+ Struct& initialize(Module module, Identifier name);
61
+
62
+ //! Get the members in Struct.
63
+ Array members() const;
64
+
65
+ class Instance;
66
+ friend class Instance;
67
+ //friend Struct Rice::define_struct();
68
+
69
+ //! Create a new instance of the Struct
70
+ /*! \param args the arguments to the constructor.
71
+ * \return a new Struct::Instance
72
+ */
73
+ Instance new_instance(Array args = Array()) const;
74
+
75
+ private:
76
+ std::vector<Symbol> members_;
77
+ };
78
+
79
+ //! An instance of a Struct
80
+ //! \sa Struct
81
+ class Struct::Instance : public Builtin_Object<T_STRUCT>
82
+ {
83
+ public:
84
+ //! Create a new Instance of a Struct.
85
+ /*! \param type the Struct type to create.
86
+ * \param args the initial values for the objects of the instance.
87
+ */
88
+ Instance(Struct const& type,Array args = Array());
89
+
90
+ //! Encapsulate an existing Struct instance.
91
+ /*! \param type the Struct type to encapsulate.
92
+ * \param s the instance to encapsulate.
93
+ */
94
+ Instance(Struct const& type, Object s);
95
+
96
+ //! Get a member, given its offset.
97
+ /*! \param index the (integral) index into the Struct's internal
98
+ * array or its name (an Identifier or char const *)
99
+ * \return the member.
100
+ */
101
+ template<typename T>
102
+ Object operator[](T index);
103
+
104
+ private:
105
+ Struct type_;
106
+ };
107
+
108
+ //! Define a new Struct
109
+ Struct define_struct();
110
+
111
+ } // namespace Rice
112
+
113
+ #endif // Rice__ruby_struct__hpp_
@@ -0,0 +1,92 @@
1
+
2
+ namespace Rice
3
+ {
4
+ inline Struct& Struct::initialize(Module module, Identifier name)
5
+ {
6
+ Class struct_class(rb_cStruct);
7
+
8
+ Object type = struct_class.vcall("new", this->members());
9
+
10
+ set_value(type);
11
+ module.const_set(name, type);
12
+
13
+ return *this;
14
+ }
15
+
16
+ inline Struct& Struct::define_member(Identifier name)
17
+ {
18
+ if (value() != rb_cObject)
19
+ {
20
+ throw std::runtime_error("struct is already initialized");
21
+ }
22
+
23
+ members_.push_back(name.to_sym());
24
+
25
+ return *this;
26
+ }
27
+
28
+ inline Array Struct::members() const
29
+ {
30
+ if (value() == rb_cObject)
31
+ {
32
+ // Struct is not yet defined
33
+ return Array(members_.begin(), members_.end());
34
+ }
35
+ else
36
+ {
37
+ // Struct is defined, call Ruby API
38
+ return rb_struct_s_members(this->value());
39
+ }
40
+ }
41
+
42
+ inline Struct::Instance Struct::new_instance(Array args) const
43
+ {
44
+ Object instance = const_cast<Struct*>(this)->vcall("new", args);
45
+ return Instance(*this, instance);
46
+ }
47
+
48
+ inline Struct::Instance::Instance(Struct const& type, Array args) :
49
+ Builtin_Object<T_STRUCT>(type.new_instance(args)), type_(type)
50
+ {
51
+ }
52
+
53
+ inline Struct::Instance::Instance(Struct const& type, Object s) :
54
+ Builtin_Object<T_STRUCT>(s), type_(type)
55
+ {
56
+ }
57
+
58
+ inline Struct define_struct()
59
+ {
60
+ return Struct();
61
+ }
62
+
63
+ template<typename T>
64
+ inline Object Struct::Instance::operator[](T index)
65
+ {
66
+ return rb_struct_aref(value(), ULONG2NUM(index));
67
+ }
68
+
69
+ template<>
70
+ inline Object Struct::Instance::operator[]<Identifier>(Identifier member)
71
+ {
72
+ return rb_struct_aref(value(), Symbol(member));
73
+ }
74
+
75
+ template<>
76
+ inline Object Struct::Instance::operator[]<char const*>(char const* name)
77
+ {
78
+ return (*this)[Identifier(name)];
79
+ }
80
+ }
81
+
82
+ namespace Rice::detail
83
+ {
84
+ template<>
85
+ struct Type<Struct>
86
+ {
87
+ static bool verify()
88
+ {
89
+ return true;
90
+ }
91
+ };
92
+ }
@@ -0,0 +1,46 @@
1
+ #ifndef Rice__Symbol__hpp_
2
+ #define Rice__Symbol__hpp_
3
+
4
+ namespace Rice
5
+ {
6
+ //! A wrapper for ruby's Symbol class.
7
+ /*! Symbols are internal identifiers in ruby. They are singletons and
8
+ * can be thought of as frozen strings. They differ from an Identifier
9
+ * in that they are in fact real Objects, but they can be converted
10
+ * back and forth between Identifier and Symbol.
11
+ */
12
+ class Symbol
13
+ : public Object
14
+ {
15
+ public:
16
+ //! Wrap an existing symbol.
17
+ Symbol(VALUE v);
18
+
19
+ //! Wrap an existing symbol.
20
+ Symbol(Object v);
21
+
22
+ //! Construct a Symbol from an Identifier.
23
+ Symbol(Identifier id);
24
+
25
+ //! Construct a Symbol from a null-terminated C string.
26
+ Symbol(char const* s = "");
27
+
28
+ //! Construct a Symbol from an std::string.
29
+ Symbol(std::string const& s);
30
+
31
+ //! Construct a Symbol from an std::string_view.
32
+ Symbol(std::string_view const& s);
33
+
34
+ //! Return a string representation of the Symbol.
35
+ char const* c_str() const;
36
+
37
+ //! Return a string representation of the Symbol.
38
+ std::string str() const;
39
+
40
+ //! Return the Symbol as an Identifier.
41
+ Identifier to_id() const;
42
+ };
43
+ } // namespace Rice
44
+
45
+ #endif // Rice__Symbol__hpp_
46
+
@@ -0,0 +1,93 @@
1
+ namespace Rice
2
+ {
3
+ inline Symbol::Symbol(VALUE value) : Object(value)
4
+ {
5
+ detail::protect(rb_check_type, value, (int)T_SYMBOL);
6
+ }
7
+
8
+ inline Symbol::Symbol(Object value) : Object(value)
9
+ {
10
+ detail::protect(rb_check_type, value.value(), (int)T_SYMBOL);
11
+ }
12
+
13
+ inline Symbol::Symbol(char const* s)
14
+ : Object(detail::protect(rb_id2sym, detail::protect(rb_intern, s)))
15
+ {
16
+ }
17
+
18
+ inline Symbol::Symbol(std::string const& s)
19
+ : Object(detail::protect(rb_id2sym, detail::protect(rb_intern2, s.c_str(), (long)s.length())))
20
+ {
21
+ }
22
+
23
+ inline Symbol::Symbol(std::string_view const& view)
24
+ : Object(detail::protect(rb_id2sym, detail::protect(rb_intern2, view.data(), (long)view.length())))
25
+ {
26
+ }
27
+
28
+ inline Symbol::Symbol(Identifier id) : Object(detail::protect(rb_id2sym, id))
29
+ {
30
+ }
31
+
32
+ inline char const* Symbol::c_str() const
33
+ {
34
+ return to_id().c_str();
35
+ }
36
+
37
+ inline std::string Symbol::str() const
38
+ {
39
+ return to_id().str();
40
+ }
41
+
42
+ inline Identifier Symbol::to_id() const
43
+ {
44
+ return rb_to_id(value());
45
+ }
46
+ }
47
+
48
+ namespace Rice::detail
49
+ {
50
+ template<>
51
+ struct Type<Symbol>
52
+ {
53
+ static bool verify()
54
+ {
55
+ return true;
56
+ }
57
+ };
58
+
59
+ template<>
60
+ class To_Ruby<Symbol>
61
+ {
62
+ public:
63
+ VALUE convert(Symbol const& x)
64
+ {
65
+ return x.value();
66
+ }
67
+ };
68
+
69
+ template<>
70
+ class From_Ruby<Symbol>
71
+ {
72
+ public:
73
+ Convertible is_convertible(VALUE value)
74
+ {
75
+ switch (rb_type(value))
76
+ {
77
+ case RUBY_T_SYMBOL:
78
+ return Convertible::Exact;
79
+ break;
80
+ case RUBY_T_STRING:
81
+ return Convertible::Cast;
82
+ break;
83
+ default:
84
+ return Convertible::None;
85
+ }
86
+ }
87
+
88
+ Symbol convert(VALUE value)
89
+ {
90
+ return Symbol(value);
91
+ }
92
+ };
93
+ }
@@ -0,0 +1,134 @@
1
+ // Include these methods to call methods from Module but return
2
+ // an instance of the current classes. This is an alternative to
3
+ // using CRTP.
4
+
5
+
6
+ //! Include a module.
7
+ /*! \param inc the module to be included.
8
+ * \return *this
9
+ */
10
+ inline auto& include_module(Module const& inc)
11
+ {
12
+ detail::protect(rb_include_module, this->value(), inc.value());
13
+ return *this;
14
+ }
15
+
16
+ //! Define an instance method.
17
+ /*! The method's implementation can be a member function, plain function
18
+ * or lambda. The easiest case is a member function where the Ruby
19
+ * method maps one-to-one to the C++ method. In the case of a
20
+ * plain function or lambda, the first argument must be SELF - ie,
21
+ * the current object. If the type of the first argument is VALUE, then
22
+ * the current Ruby object is passed. If the type is a C++ class,
23
+ * then the C++ object is passed. If you don't want to include the
24
+ * SELF argument see define_function.
25
+ * Rice will automatically convert method parameters from Ruby to C++ and
26
+ * convert the return value from C++ to Ruby.
27
+ * \param name the name of the method
28
+ * \param func the implementation of the function, either a function
29
+ * pointer or a member function pointer.
30
+ * \param args a list of Arg instance used to define default parameters.
31
+ * \return *this
32
+ */
33
+ template<typename Function_T, typename...Arg_Ts>
34
+ inline auto& define_method(std::string name, Function_T&& func, const Arg_Ts&...args)
35
+ {
36
+ MethodInfo* methodInfo = new MethodInfo(detail::method_traits<Function_T, true>::arity, args...);
37
+ this->wrap_native_call<true>(this->value(), name, std::forward<Function_T>(func), methodInfo);
38
+ return *this;
39
+ }
40
+
41
+ //! Define a function.
42
+ /*! The function implementation is a plain function or a static
43
+ * member function.
44
+ * Rice will automatically convert method method from Ruby to C++ and
45
+ * then convert the return value from C++ to Ruby.
46
+ * \param name the name of the method
47
+ * \param func the implementation of the function, either a function
48
+ * pointer or a member function pointer.
49
+ * \param args a list of Arg instance used to define default parameters (optional)
50
+ * \return *this
51
+ */
52
+ template<typename Function_T, typename...Arg_Ts>
53
+ inline auto& define_function(std::string name, Function_T&& func, const Arg_Ts&...args)
54
+ {
55
+ MethodInfo* methodInfo = new MethodInfo(detail::method_traits<Function_T, false>::arity, args...);
56
+ this->wrap_native_call<false>(this->value(), name, std::forward<Function_T>(func), methodInfo);
57
+ return *this;
58
+ }
59
+
60
+ //! Define a singleton method.
61
+ /*! The method's implementation can be a static member function,
62
+ * plain function or lambda. In all cases the first argument
63
+ * must be SELF - ie, the current object. If it is specified as a VALUE, then
64
+ * the current Ruby object is passed. If it is specified as a C++ class,
65
+ * then the C++ object is passed. If you don't want to include the
66
+ * SELF argument see define_singleton_function.
67
+ * Rice will automatically convert method method from Ruby to C++ and
68
+ * then convert the return value from C++ to Ruby.
69
+ * \param name the name of the method
70
+ * \param func the implementation of the function, either a function
71
+ * pointer or a member function pointer.
72
+ * \param args a list of Arg instance used to define default parameters (optional)
73
+ * \return *this
74
+ */
75
+ template<typename Function_T, typename...Arg_Ts>
76
+ inline auto& define_singleton_method(std::string name, Function_T&& func, const Arg_Ts&...args)
77
+ {
78
+ MethodInfo* methodInfo = new MethodInfo(detail::method_traits<Function_T, true>::arity, args...);
79
+ this->wrap_native_call<true>(rb_singleton_class(*this), name, std::forward<Function_T>(func), methodInfo);
80
+ return *this;
81
+ }
82
+
83
+ //! Define a singleton method.
84
+ /*! The method's implementation can be a static member function, plain
85
+ * function or lambda. A wrapper will be generated which will convert the method
86
+ * from ruby types to C++ types before calling the function. The return
87
+ * value will be converted back to ruby.
88
+ * \param name the name of the method
89
+ * \param func the implementation of the function, either a function
90
+ * pointer or a member function pointer.
91
+ * \param args a list of Arg instance used to define default parameters (optional)
92
+ * \return *this
93
+ */
94
+ template<typename Function_T, typename...Arg_Ts>
95
+ inline auto& define_singleton_function(std::string name, Function_T&& func, const Arg_Ts& ...args)
96
+ {
97
+ MethodInfo* methodInfo = new MethodInfo(detail::method_traits<Function_T, false>::arity, args...);
98
+ this->wrap_native_call<false>(rb_singleton_class(*this), name, std::forward<Function_T>(func), methodInfo);
99
+ return *this;
100
+ }
101
+
102
+ //! Define a module function.
103
+ /*! A module function is a function that can be accessed either as a
104
+ * singleton method or as an instance method. It wrap a plain
105
+ * function, static member function or lambda.
106
+ * Rice will automatically convert method method from Ruby to C++ and
107
+ * then convert the return value from C++ to Ruby.
108
+ * \param name the name of the method
109
+ * \param func the implementation of the function, either a function
110
+ * pointer or a member function pointer.
111
+ * \param args a list of Arg instance used to define default parameters (optional)
112
+ * \return *this
113
+ */
114
+ template<typename Function_T, typename...Arg_Ts>
115
+ inline auto& define_module_function(std::string name, Function_T&& func, const Arg_Ts& ...args)
116
+ {
117
+ if (this->rb_type() != T_MODULE)
118
+ {
119
+ throw std::runtime_error("can only define module functions for modules");
120
+ }
121
+
122
+ define_function(name, std::forward<Function_T>(func), args...);
123
+ define_singleton_function(name, std::forward<Function_T>(func), args...);
124
+ return *this;
125
+ }
126
+
127
+ //! Define a constant
128
+ template<typename Constant_T>
129
+ inline auto& define_constant(std::string name, Constant_T value)
130
+ {
131
+ using Base_T = detail::remove_cv_recursive_t<Constant_T>;
132
+ detail::protect(rb_define_const, this->value(), name.c_str(), detail::To_Ruby<Base_T>().convert(value));
133
+ return *this;
134
+ }
@@ -0,0 +1,12 @@
1
+ #ifndef Rice__detail__DefaultHandler__hpp_
2
+ #define Rice__detail__DefaultHandler__hpp_
3
+
4
+ namespace Rice::detail
5
+ {
6
+ class DefaultHandler
7
+ {
8
+ public:
9
+ void operator()() const;
10
+ };
11
+ }
12
+ #endif // Rice__detail__DefaultHandler__hpp_
@@ -0,0 +1,8 @@
1
+ namespace Rice::detail
2
+ {
3
+ inline void Rice::detail::DefaultHandler::operator()() const
4
+ {
5
+ // This handler does nothing, it just rethrows the exception so it can be handled
6
+ throw;
7
+ }
8
+ }