rice 2.1.1 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (246) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +121 -0
  3. data/CONTRIBUTORS.md +19 -0
  4. data/COPYING +2 -2
  5. data/Gemfile +3 -0
  6. data/README.md +69 -0
  7. data/Rakefile +95 -12
  8. data/include/rice/rice.hpp +7766 -0
  9. data/lib/mkmf-rice.rb +127 -0
  10. data/lib/version.rb +3 -0
  11. data/rice/Address_Registration_Guard.ipp +75 -32
  12. data/rice/Address_Registration_Guard_defn.hpp +60 -56
  13. data/rice/Arg.hpp +80 -4
  14. data/rice/Arg.ipp +51 -0
  15. data/rice/Constructor.hpp +30 -376
  16. data/rice/Data_Object.ipp +234 -107
  17. data/rice/Data_Object_defn.hpp +77 -117
  18. data/rice/Data_Type.hpp +1 -2
  19. data/rice/Data_Type.ipp +251 -295
  20. data/rice/Data_Type_defn.hpp +175 -243
  21. data/rice/Director.hpp +14 -9
  22. data/rice/Enum.hpp +54 -104
  23. data/rice/Enum.ipp +104 -230
  24. data/rice/Exception.hpp +2 -8
  25. data/rice/Exception.ipp +65 -0
  26. data/rice/Exception_defn.hpp +46 -47
  27. data/rice/Identifier.hpp +28 -28
  28. data/rice/Identifier.ipp +23 -27
  29. data/rice/Return.hpp +39 -0
  30. data/rice/Return.ipp +33 -0
  31. data/rice/detail/Exception_Handler.ipp +22 -62
  32. data/rice/detail/Exception_Handler_defn.hpp +76 -91
  33. data/rice/detail/Iterator.hpp +18 -88
  34. data/rice/detail/Iterator.ipp +47 -0
  35. data/rice/detail/Jump_Tag.hpp +21 -0
  36. data/rice/detail/MethodInfo.hpp +44 -0
  37. data/rice/detail/MethodInfo.ipp +78 -0
  38. data/rice/detail/NativeAttribute.hpp +53 -0
  39. data/rice/detail/NativeAttribute.ipp +83 -0
  40. data/rice/detail/NativeFunction.hpp +69 -0
  41. data/rice/detail/NativeFunction.ipp +248 -0
  42. data/rice/detail/RubyFunction.hpp +39 -0
  43. data/rice/detail/RubyFunction.ipp +92 -0
  44. data/rice/detail/Type.hpp +29 -0
  45. data/rice/detail/Type.ipp +138 -0
  46. data/rice/detail/TypeRegistry.hpp +50 -0
  47. data/rice/detail/TypeRegistry.ipp +106 -0
  48. data/rice/detail/Wrapper.hpp +51 -0
  49. data/rice/detail/Wrapper.ipp +151 -0
  50. data/rice/detail/default_allocation_func.hpp +8 -19
  51. data/rice/detail/default_allocation_func.ipp +9 -8
  52. data/rice/detail/from_ruby.hpp +2 -37
  53. data/rice/detail/from_ruby.ipp +1020 -46
  54. data/rice/detail/from_ruby_defn.hpp +38 -0
  55. data/rice/detail/function_traits.hpp +124 -0
  56. data/rice/detail/method_data.hpp +23 -15
  57. data/rice/detail/method_data.ipp +53 -0
  58. data/rice/detail/rice_traits.hpp +116 -0
  59. data/rice/detail/ruby.hpp +9 -49
  60. data/rice/detail/to_ruby.hpp +3 -17
  61. data/rice/detail/to_ruby.ipp +409 -31
  62. data/rice/detail/to_ruby_defn.hpp +48 -0
  63. data/rice/forward_declares.ipp +82 -0
  64. data/rice/global_function.hpp +16 -20
  65. data/rice/global_function.ipp +8 -17
  66. data/rice/rice.hpp +59 -0
  67. data/rice/ruby_mark.hpp +5 -3
  68. data/rice/ruby_try_catch.hpp +4 -4
  69. data/rice/stl.hpp +11 -0
  70. data/sample/callbacks/extconf.rb +6 -0
  71. data/sample/callbacks/sample_callbacks.cpp +35 -0
  72. data/sample/callbacks/test.rb +28 -0
  73. data/sample/enum/extconf.rb +3 -0
  74. data/sample/enum/sample_enum.cpp +3 -17
  75. data/sample/enum/test.rb +2 -2
  76. data/sample/inheritance/animals.cpp +8 -24
  77. data/sample/inheritance/extconf.rb +3 -0
  78. data/sample/inheritance/test.rb +1 -1
  79. data/sample/map/extconf.rb +3 -0
  80. data/sample/map/map.cpp +10 -18
  81. data/sample/map/test.rb +1 -1
  82. data/test/embed_ruby.cpp +34 -0
  83. data/test/embed_ruby.hpp +4 -0
  84. data/test/ext/t1/extconf.rb +3 -0
  85. data/test/ext/t1/t1.cpp +1 -3
  86. data/test/ext/t2/extconf.rb +3 -0
  87. data/test/ext/t2/t2.cpp +1 -1
  88. data/test/extconf.rb +23 -0
  89. data/test/ruby/test_callbacks_sample.rb +28 -0
  90. data/test/ruby/test_multiple_extensions.rb +18 -0
  91. data/test/ruby/test_multiple_extensions_same_class.rb +14 -0
  92. data/test/ruby/test_multiple_extensions_with_inheritance.rb +20 -0
  93. data/test/test_Address_Registration_Guard.cpp +25 -11
  94. data/test/test_Array.cpp +131 -74
  95. data/test/test_Attribute.cpp +147 -0
  96. data/test/test_Builtin_Object.cpp +36 -15
  97. data/test/test_Class.cpp +151 -274
  98. data/test/test_Constructor.cpp +10 -9
  99. data/test/test_Data_Object.cpp +135 -193
  100. data/test/test_Data_Type.cpp +323 -252
  101. data/test/test_Director.cpp +56 -42
  102. data/test/test_Enum.cpp +230 -104
  103. data/test/test_Exception.cpp +7 -7
  104. data/test/test_Hash.cpp +33 -31
  105. data/test/test_Identifier.cpp +6 -6
  106. data/test/test_Inheritance.cpp +221 -0
  107. data/test/test_Iterator.cpp +161 -0
  108. data/test/test_Jump_Tag.cpp +1 -1
  109. data/test/test_Keep_Alive.cpp +161 -0
  110. data/test/test_Memory_Management.cpp +4 -5
  111. data/test/test_Module.cpp +169 -111
  112. data/test/test_Object.cpp +51 -19
  113. data/test/test_Ownership.cpp +275 -0
  114. data/test/test_Self.cpp +205 -0
  115. data/test/test_Stl_Optional.cpp +90 -0
  116. data/test/test_Stl_Pair.cpp +144 -0
  117. data/test/test_Stl_SmartPointer.cpp +200 -0
  118. data/test/test_Stl_String.cpp +74 -0
  119. data/test/test_Stl_Vector.cpp +652 -0
  120. data/test/test_String.cpp +3 -3
  121. data/test/test_Struct.cpp +31 -40
  122. data/test/test_Symbol.cpp +3 -3
  123. data/test/test_To_From_Ruby.cpp +283 -218
  124. data/test/test_global_functions.cpp +41 -20
  125. data/test/unittest.cpp +34 -8
  126. data/test/unittest.hpp +0 -4
  127. metadata +117 -137
  128. data/Doxyfile +0 -2280
  129. data/Makefile.am +0 -26
  130. data/Makefile.in +0 -920
  131. data/README +0 -1055
  132. data/README.mingw +0 -8
  133. data/aclocal.m4 +0 -1088
  134. data/bootstrap +0 -8
  135. data/check_stdcxx_11.ac +0 -142
  136. data/config.guess +0 -1421
  137. data/config.sub +0 -1807
  138. data/configure +0 -7481
  139. data/configure.ac +0 -55
  140. data/depcomp +0 -791
  141. data/doxygen.ac +0 -314
  142. data/doxygen.am +0 -186
  143. data/extconf.rb +0 -69
  144. data/install-sh +0 -501
  145. data/missing +0 -215
  146. data/post-autoconf.rb +0 -22
  147. data/post-automake.rb +0 -28
  148. data/rice/Address_Registration_Guard.cpp +0 -22
  149. data/rice/Arg_impl.hpp +0 -129
  150. data/rice/Arg_operators.cpp +0 -21
  151. data/rice/Arg_operators.hpp +0 -19
  152. data/rice/Array.hpp +0 -214
  153. data/rice/Array.ipp +0 -256
  154. data/rice/Builtin_Object.hpp +0 -8
  155. data/rice/Builtin_Object.ipp +0 -50
  156. data/rice/Builtin_Object_defn.hpp +0 -50
  157. data/rice/Class.cpp +0 -57
  158. data/rice/Class.hpp +0 -8
  159. data/rice/Class.ipp +0 -6
  160. data/rice/Class_defn.hpp +0 -83
  161. data/rice/Data_Type.cpp +0 -54
  162. data/rice/Data_Type_fwd.hpp +0 -12
  163. data/rice/Director.cpp +0 -13
  164. data/rice/Exception.cpp +0 -59
  165. data/rice/Exception_Base.hpp +0 -8
  166. data/rice/Exception_Base.ipp +0 -13
  167. data/rice/Exception_Base_defn.hpp +0 -27
  168. data/rice/Hash.hpp +0 -227
  169. data/rice/Hash.ipp +0 -329
  170. data/rice/Identifier.cpp +0 -8
  171. data/rice/Jump_Tag.hpp +0 -24
  172. data/rice/Makefile.am +0 -124
  173. data/rice/Makefile.in +0 -839
  174. data/rice/Module.cpp +0 -84
  175. data/rice/Module.hpp +0 -8
  176. data/rice/Module.ipp +0 -6
  177. data/rice/Module_defn.hpp +0 -88
  178. data/rice/Module_impl.hpp +0 -281
  179. data/rice/Module_impl.ipp +0 -345
  180. data/rice/Object.cpp +0 -169
  181. data/rice/Object.hpp +0 -8
  182. data/rice/Object.ipp +0 -19
  183. data/rice/Object_defn.hpp +0 -191
  184. data/rice/Require_Guard.hpp +0 -21
  185. data/rice/String.cpp +0 -94
  186. data/rice/String.hpp +0 -91
  187. data/rice/Struct.cpp +0 -117
  188. data/rice/Struct.hpp +0 -162
  189. data/rice/Struct.ipp +0 -26
  190. data/rice/Symbol.cpp +0 -25
  191. data/rice/Symbol.hpp +0 -66
  192. data/rice/Symbol.ipp +0 -44
  193. data/rice/config.hpp +0 -47
  194. data/rice/config.hpp.in +0 -46
  195. data/rice/detail/Arguments.hpp +0 -118
  196. data/rice/detail/Auto_Function_Wrapper.hpp +0 -898
  197. data/rice/detail/Auto_Function_Wrapper.ipp +0 -3694
  198. data/rice/detail/Auto_Member_Function_Wrapper.hpp +0 -897
  199. data/rice/detail/Auto_Member_Function_Wrapper.ipp +0 -2774
  200. data/rice/detail/Caster.hpp +0 -103
  201. data/rice/detail/Not_Copyable.hpp +0 -25
  202. data/rice/detail/Wrapped_Function.hpp +0 -33
  203. data/rice/detail/cfp.hpp +0 -24
  204. data/rice/detail/cfp.ipp +0 -51
  205. data/rice/detail/check_ruby_type.cpp +0 -27
  206. data/rice/detail/check_ruby_type.hpp +0 -23
  207. data/rice/detail/creation_funcs.hpp +0 -37
  208. data/rice/detail/creation_funcs.ipp +0 -36
  209. data/rice/detail/define_method_and_auto_wrap.hpp +0 -31
  210. data/rice/detail/define_method_and_auto_wrap.ipp +0 -30
  211. data/rice/detail/demangle.cpp +0 -56
  212. data/rice/detail/demangle.hpp +0 -19
  213. data/rice/detail/env.hpp +0 -11
  214. data/rice/detail/method_data.cpp +0 -86
  215. data/rice/detail/node.hpp +0 -13
  216. data/rice/detail/object_call.hpp +0 -69
  217. data/rice/detail/object_call.ipp +0 -131
  218. data/rice/detail/protect.cpp +0 -29
  219. data/rice/detail/protect.hpp +0 -34
  220. data/rice/detail/ruby_version_code.hpp +0 -6
  221. data/rice/detail/ruby_version_code.hpp.in +0 -6
  222. data/rice/detail/st.hpp +0 -22
  223. data/rice/detail/traits.hpp +0 -43
  224. data/rice/detail/win32.hpp +0 -16
  225. data/rice/detail/wrap_function.hpp +0 -341
  226. data/rice/detail/wrap_function.ipp +0 -514
  227. data/rice/protect.hpp +0 -92
  228. data/rice/protect.ipp +0 -1134
  229. data/rice/rubypp.rb +0 -97
  230. data/rice/to_from_ruby.hpp +0 -8
  231. data/rice/to_from_ruby.ipp +0 -294
  232. data/rice/to_from_ruby_defn.hpp +0 -70
  233. data/ruby.ac +0 -135
  234. data/ruby/Makefile.am +0 -1
  235. data/ruby/Makefile.in +0 -625
  236. data/ruby/lib/Makefile.am +0 -3
  237. data/ruby/lib/Makefile.in +0 -503
  238. data/ruby/lib/mkmf-rice.rb.in +0 -217
  239. data/ruby/lib/version.rb +0 -3
  240. data/sample/Makefile.am +0 -47
  241. data/sample/Makefile.in +0 -486
  242. data/test/Makefile.am +0 -72
  243. data/test/Makefile.in +0 -1150
  244. data/test/ext/Makefile.am +0 -41
  245. data/test/ext/Makefile.in +0 -480
  246. data/test/test_rice.rb +0 -41
data/README DELETED
@@ -1,1055 +0,0 @@
1
- \mainpage Rice - Ruby Interface for C++ Extensions
2
-
3
-
4
- \section intro Introduction
5
-
6
- Rice is a C++ interface to Ruby's C API. It provides a type-safe and
7
- exception-safe interface in order to make embedding Ruby and writing
8
- Ruby extensions with C++ easier. It is similar to Boost.Python in many
9
- ways, but also attempts to provide an object-oriented interface to all
10
- of the Ruby C API.
11
-
12
- What Rice gives you:
13
- \li A simple C++-based syntax for wrapping and defining classes
14
- \li Automatic conversion of exceptions between C++ and Ruby
15
- \li Smart pointers for handling garbage collection
16
- \li Wrappers for most builtin types to simplify calling code
17
-
18
- \section project Project Details
19
-
20
- The source is hosted on github: http://github.com/jasonroelofs/rice
21
-
22
- Bug tracking: http://github.com/jasonroelofs/rice/issues
23
-
24
- \section installation Installation
25
-
26
- \code
27
- gem install rice
28
- \endcode
29
-
30
- Building it locally from a clone of the repository is as follows:
31
-
32
- \code
33
- ./bootstrap
34
- ruby extconf.rb
35
- make
36
- \endcode
37
-
38
- Rice is known to work on *nix and OSX. Windows is not currently
39
- supported.
40
-
41
- \section tutorial Tutorial
42
-
43
- \subsection geting_started Getting started
44
-
45
- Writing an extension with Rice is very similar to writing an extension
46
- with the C API.
47
-
48
- The first step is to create an extconf.rb file:
49
-
50
- \code
51
- require 'mkmf-rice'
52
- create_makefile('test')
53
- \endcode
54
-
55
- Note that we use mkmf-rice instead of mkmf. This will ensure that the
56
- extension will be linked with standard C++ library along with the Rice
57
- library, and allow access to the Rice header files.
58
-
59
- Next we create our extension and save it to test.cpp:
60
-
61
- \code
62
- extern "C"
63
- void Init_test()
64
- {
65
- }
66
- \endcode
67
-
68
- Note the extern "C" line above. This tells the compiler that the
69
- function Init_test should have C linkage and calling convention. This
70
- turns off name mangling so that the Ruby interpreter will be able to
71
- find the function (remember that Ruby is written in C, not C++).
72
-
73
- So far we haven't put anything into the extension, so it isn't
74
- particularly useful. The next step is to define a class so we can add
75
- methods to it.
76
-
77
-
78
- \subsection classes Defining clases
79
-
80
- Defining a class in Rice is easy:
81
-
82
- \code
83
- #include "rice/Class.hpp"
84
-
85
- using namespace Rice;
86
-
87
- extern "C"
88
- void Init_test()
89
- {
90
- Class rb_cTest = define_class("Test");
91
- }
92
- \endcode
93
-
94
- This will create a class called Test that inherits from Object. If we
95
- wanted to inherit from a different class, we could easily do so:
96
-
97
- \code
98
- #include "rice/Class.hpp"
99
-
100
- using namespace Rice;
101
-
102
- extern "C"
103
- void Init_test()
104
- {
105
- Class rb_cMySocket = define_class("MySocket", rb_cIO);
106
- }
107
- \endcode
108
-
109
- Note the prefix rb_c on the name of the class. This is a convention
110
- that the Ruby interpreter and many extensions tend to use. It signifies
111
- that this is a class and not some other type of object. Some other
112
- naming conventions that are commonly used:
113
-
114
- \li rb_c variable name prefix for a Class
115
- \li rb_m variable name prefix for a Module
116
- \li rb_e variable name prefix for an Exception type
117
- \li rb_ function prefix for a function in the Ruby C API
118
- \li rb_f_ function prefix to differentiate between an API function that
119
- takes Ruby objects as arguments and one that takes C argument types
120
- \li rb_*_s_ indicates the function is a singleton function
121
- \li *_m suffix to indicate the function takes variable number of
122
- arguments
123
-
124
-
125
- Also note that we don't include "ruby.h" directly. Rice has a wrapper
126
- for ruby.h that handles some compatibility issues across platforms and
127
- Ruby versions. Always include Rice headers before including anything
128
- that might include "ruby.h".
129
-
130
- \subsection methods Defining methods
131
-
132
- Now let's add a method to our class:
133
-
134
- \code
135
- #include "rice/Class.hpp"
136
- #include "rice/String.hpp"
137
-
138
- using namespace Rice;
139
-
140
- Object test_hello(Object /* self */)
141
- {
142
- String str("hello, world");
143
- return str;
144
- }
145
-
146
- extern "C"
147
- void Init_test()
148
- {
149
- Class rb_cTest =
150
- define_class("Test")
151
- .define_method("hello", &test_hello);
152
- }
153
- \endcode
154
-
155
- Here we add a method Test#hello that simply returns the string
156
- "Hello, World". The method takes self as an implicit parameter, but
157
- isn't used, so we comment it out to prevent a compiler warning.
158
-
159
- We could also add an #initialize method to our class:
160
-
161
- \code
162
- #include "rice/Class.hpp"
163
- #include "rice/String.hpp"
164
-
165
- using namespace Rice;
166
-
167
- Object test_initialize(Object self)
168
- {
169
- self.iv_set("@foo", 42);
170
- }
171
-
172
- Object test_hello(Object /* self */)
173
- {
174
- String str("hello, world");
175
- return str;
176
- }
177
-
178
- extern "C"
179
- void Init_test()
180
- {
181
- Class rb_cTest =
182
- define_class("Test")
183
- .define_method("initialize", &test_initialize);
184
- .define_method("hello", &test_hello);
185
- }
186
- \endcode
187
-
188
- The initialize method sets an instance variable @foo to the value 42.
189
- The number is automatically converted to a Fixnum before doing the
190
- assignment.
191
-
192
- Note that we're chaining calls on the Class object. Most member
193
- functions in Module and Class return a reference to self, so we can
194
- chain as many calls as we want to define as many methods as we want.
195
-
196
-
197
- \subsection data_types Wrapping C++ Types
198
-
199
- It's useful to be able to define Ruby classes in a C++ style rather than
200
- using the Ruby API directly, but the real power Rice is in wrapping
201
- already-defined C++ types.
202
-
203
- Let's assume we have the following C++ class that we want to wrap:
204
-
205
- \code
206
- class Test
207
- {
208
- public:
209
- Test();
210
- std::string hello();
211
- };
212
- \endcode
213
-
214
- This is a C++ version of the Ruby class we just created in the previous
215
- section. To wrap it:
216
-
217
- \code
218
- #include "rice/Data_Type.hpp"
219
- #include "rice/Constructor.hpp"
220
-
221
- using namespace Rice;
222
-
223
- extern "C"
224
- void Init_test()
225
- {
226
- Data_Type<Test> rb_cTest =
227
- define_class<Test>("Test")
228
- .define_constructor(Constructor<Test>())
229
- .define_method("hello", &Test::hello);
230
- }
231
- \endcode
232
-
233
- This example is similar to the one before, but we use Data_Type<>
234
- instead of Class and the template version of define_class() instead of
235
- the non-template version. This creates a binding in the Rice library
236
- between the Ruby class Test and the C++ class Test such that Rice passes
237
- member function pointers to define_method().
238
-
239
- It is possible to write the conversion functions ourself (as we'll see
240
- below), but Rice does all the dirty work for us.
241
-
242
-
243
- \subsection conversions Type conversions
244
-
245
- Let's look again at our example class:
246
-
247
- \code
248
- class Test
249
- {
250
- public:
251
- Test();
252
- std::string hello();
253
- };
254
- \endcode
255
-
256
- When we wrote our class, we never wrote a single line of code to convert
257
- the std::string returned by hello() into a Ruby type. Neverthless, the
258
- conversion works, and when we write:
259
-
260
- \code
261
- test = Test.new
262
- puts test.hello
263
- \endcode
264
-
265
- We get the expected result.
266
-
267
- Rice has two template conversion functions to convert between C++ and
268
- Ruby types:
269
-
270
- \code
271
- template<typename T>
272
- T from_ruby(Object x);
273
-
274
- template<typename T>
275
- Object to_ruby(T const & x);
276
- \endcode
277
-
278
- Rice has included by default specializations for many of the builtin
279
- types. To define your own conversion, you can write a specialization:
280
-
281
- \code
282
- template<>
283
- Foo from_ruby<Foo>(Object x)
284
- {
285
- // ...
286
- }
287
-
288
- template<>
289
- Object to_ruby<Foo>(Foo const & x)
290
- {
291
- // ...
292
- }
293
- \endcode
294
-
295
- The implementation of these functions would, of course, depend on the
296
- implementation of Foo.
297
-
298
-
299
- \subsection data_conversions Conversions for wrapped C++ types
300
-
301
- Take another look at the wrapper we wrote for the Test class:
302
-
303
- \code
304
- extern "C"
305
- void Init_test()
306
- {
307
- Data_Type<Test> rb_cTest =
308
- define_class<Test>("Test")
309
- .define_constructor(Constructor<Test>())
310
- .define_method("hello", &Test::hello);
311
- }
312
- \endcode
313
-
314
- When we called define_class<Test>, it created a Class for us and
315
- automatically registered the new Class with the type system, so that the
316
- calls:
317
-
318
- \code
319
- Data_Object<Foo> obj(new Foo);
320
- Foo * f = from_ruby<Foo *>(obj);
321
- Foo const * f = from_ruby<Foo const *>(obj);
322
- \endcode
323
-
324
- work as expected.
325
-
326
- The Data_Object class is a wrapper for the Data_Wrap_Struct and the
327
- Data_Get_Struct macros in C extensions. It can be used to wrap or
328
- unwrap any class that has been assigned to a Data_Type. It inherits
329
- from Object, so any member functions we can call on an Object we can
330
- also call on a Data_Object:
331
-
332
- \code
333
- Object object_id = obj.call("object_id");
334
- std::cout << object_id << std::endl;
335
- \endcode
336
-
337
- The Data_Object class can be used to wrap a newly-created object:
338
-
339
- \code
340
- Data_Object<Foo> foo(new Foo);
341
- \endcode
342
-
343
- or to unwrap an already-created object:
344
-
345
- \code
346
- VALUE obj = ...;
347
- Data_Object<Foo> foo(obj);
348
- \endcode
349
-
350
- A Data_Object functions like a smart pointer:
351
-
352
- \code
353
- Data_Object<Foo> foo(obj);
354
- foo->foo();
355
- std::cout << *foo << std::endl;
356
- \endcode
357
-
358
- Like a VALUE or an Object, data stored in a Data_Object will be marked
359
- by the garbage collector as long as the Data_Object is on the stack.
360
-
361
-
362
- \subsection exception Exceptions
363
-
364
- Suppose we added a member function to our example class that throws an
365
- exception:
366
-
367
- \code
368
- class MyException
369
- : public std::exception
370
- {
371
- };
372
-
373
- class Test
374
- {
375
- public:
376
- Test();
377
- std::string hello();
378
- void error();
379
- };
380
- \endcode
381
-
382
- If we were to wrap this function:
383
-
384
- \code
385
- extern "C"
386
- void Init_test()
387
- {
388
- Data_Type<Test> rb_cTest =
389
- define_class<Test>("Test")
390
- .define_constructor(Constructor<Test>())
391
- .define_method("hello", &Test::hello)
392
- .define_method("error", &Test::error);
393
- }
394
- \endcode
395
-
396
- and call it from inside Ruby:
397
-
398
- \code
399
- test = Test.new
400
- test.error()
401
- \endcode
402
-
403
- we would get an exception. Rice will automatically convert any
404
- C++ exception it catches into a Ruby exception. But what if we wanted
405
- to use a custom error message when we convert the exception, or what if
406
- we wanted to convert to a different type of exception? We can write
407
- this:
408
-
409
- \code
410
- extern "C"
411
- void Init_test()
412
- {
413
- Data_Type<Test> rb_cTest =
414
- define_class<Test>("Test")
415
- .add_handler<MyException>(handle_my_exception)
416
- .define_constructor(Constructor<Test>())
417
- .define_method("hello", &Test::hello)
418
- .define_method("error", &Test::error);
419
- }
420
- \endcode
421
-
422
- The handle_my_exception function need only rethrow the exception as a
423
- Rice::Exception:
424
-
425
- \code
426
- void handle_my_exception(MyException const & ex)
427
- {
428
- throw Exception(rb_eRuntimeError, "Goodnight, moon");
429
- }
430
- \endcode
431
-
432
- And what if we want to call Ruby code from C++? These exceptions are
433
- also converted:
434
-
435
- \code
436
- Object o;
437
- o.call("some_function_that_raises", 42);
438
-
439
- protect(rb_raise, rb_eRuntimeError, "some exception msg");
440
- \endcode
441
-
442
- Internally whenever Rice catches a C++ or a Ruby exception, it converts
443
- it to an Exception object. This object will later be re-raised as a
444
- Ruby exception when control is returned to the Ruby VM.
445
-
446
- Rice uses a similar class called Jump_Tag to handle symbols thrown by
447
- Ruby's throw/catch or other non-local jumps from inside the Ruby VM.
448
-
449
-
450
- \subsection builtin Builtin Types
451
-
452
- You've seen this example:
453
-
454
- \code
455
- Object object_id = obj.call("object_id");
456
- std::cout << object_id << std::endl;
457
- \endcode
458
-
459
- Rice mimics the Ruby class hierarchy as closely as it can.
460
- In fact, the above code also works for Classes:
461
-
462
- \code
463
- Class rb_cTest = define_class<Test>("Test");
464
- Object object_id = rb_cTest.call("object_id");
465
- std::cout << object_id << std::endl;
466
- \endcode
467
-
468
- Rice provides builtin wrappers for many builtin Ruby types, including:
469
-
470
- \li Object
471
- \li Module
472
- \li Class
473
- \li String
474
- \li Array
475
- \li Hash
476
- \li Struct
477
- \li Symbol
478
- \li Exception
479
-
480
- The Array and Hash types can even be iterated over the same way one
481
- would iterate over an STL container:
482
-
483
- \code
484
- Array a;
485
- a.push(to_ruby(42));
486
- a.push(to_ruby(43));
487
- a.push(to_ruby(44));
488
- Array::iterator it = a.begin();
489
- Array::iterator end = a.end();
490
- for(; it != end; ++it)
491
- {
492
- std::cout << *it << std::endl;
493
- }
494
- \endcode
495
-
496
- STL algorithms should also work as expected on Array and Hash containers.
497
-
498
-
499
- \subsection inheritance Inheritance
500
-
501
- Inheritance is a tricky problem to solve in extensions. This is because
502
- wrapper functions for base classes typically don't know how to accept
503
- pointers to derived classes. It is possible to write this logic, but
504
- the code is nontrivial.
505
-
506
- Forunately Rice handles this gracefully:
507
-
508
- \code
509
- class Base
510
- {
511
- public:
512
- virtual void foo();
513
- };
514
-
515
- class Derived
516
- : public Base
517
- {
518
- };
519
-
520
- extern "C"
521
- void Init_test()
522
- {
523
- Data_Type<Base> rb_cBase =
524
- define_class<Base>("Base")
525
- .define_method("foo", &Base::foo);
526
- Data_Type<Derived> rb_cDerived =
527
- define_class<Derived, Base>("Derived");
528
- }
529
- \endcode
530
-
531
- The second template parameter to define_class indicates that Derived
532
- inherits from Base.
533
-
534
- Rice does not support multiple inheritance.
535
-
536
-
537
- \subsection overloading Overloaded functions
538
-
539
- If you try to create a member function pointer to an overloaded
540
- function, you will get an error. So how do we wrap classes that have
541
- overloaded functions?
542
-
543
- Consider a class that uses this idiom for accessors:
544
-
545
- \code
546
- class Container
547
- {
548
- size_t capacity(); // Get the capacity
549
- void capacity(size_t cap); // Set the capacity
550
- };
551
- \endcode
552
-
553
- We can wrap this class by using typedefs:
554
-
555
- \code
556
- extern "C"
557
- void Init_Container()
558
- {
559
- typedef size_t (Container::*get_capacity)();
560
- typedef void (Container::*set_capacity)(size_t);
561
-
562
- Data_Type<Container> rb_cContainer =
563
- define_class<Container>("Container")
564
- .define_method("capacity", get_capacity(&Container::capacity))
565
- .define_method("capacity=", set_capacity(&Container::capacity))
566
- }
567
- \endcode
568
-
569
-
570
- \subsection user_defined_conversions User-defined type conversions
571
-
572
- Rice provides default conversions for many built-in types. Sometimes,
573
- however, the default conversion is not what is expected. For
574
- example, consider a function:
575
-
576
- \code
577
- void foo(char * x);
578
- \endcode
579
-
580
- Is x a pointer to a single character or a pointer to the first character
581
- of a null-terminated string or a pointer to the first character of an
582
- array of char?
583
-
584
- Because the second case is the most common use case (a pointer to the
585
- first character of a C string), Rice provides a default conversion that
586
- treats a char * as a C string. But suppose the above function takes a
587
- pointer to a char instead?
588
-
589
- If we write this:
590
-
591
- \comment : -- this comment is to satisfy vim syntax highlighting --
592
-
593
- \code
594
- extern "C"
595
- void Init_test()
596
- {
597
- define_global_function("foo", &foo);
598
- }
599
- \endcode
600
-
601
- It will likely have the wrong behavior.
602
-
603
- To avoid this problem, it is necessary to write a wrapper function:
604
-
605
- \code
606
- Object wrap_foo(Object o)
607
- {
608
- char c = from_ruby<char>(o);
609
- foo(&c);
610
- return to_ruby(c);
611
- }
612
-
613
- extern "C"
614
- void Init_test()
615
- {
616
- define_global_function("foo", &wrap_foo);
617
- }
618
- \endcode
619
-
620
- Note that the out parameter is returned from wrap_foo, as Ruby does not
621
- have pass-by-variable-reference (it uses pass-by-object-reference).
622
-
623
-
624
- \subsection default_arguments Default Arguments
625
-
626
- Going back to our initial C++ class example, lets say that hello() now
627
- takes more arguments, one of which has a default value:
628
-
629
- \code
630
- class Test
631
- {
632
- public:
633
- Test();
634
- std::string hello(std::string first, std::string second = "world");
635
- };
636
- \endcode
637
-
638
- As default parameter information is not available through templates,
639
- it is necessary to define this in Rice explicitly using Rice::Arg:
640
-
641
- \code
642
- #include "rice/Data_Type.hpp"
643
- #include "rice/Constructor.hpp"
644
-
645
- using namespace Rice;
646
-
647
- extern "C"
648
- void Init_test()
649
- {
650
- Data_Type<Test> rb_cTest =
651
- define_class<Test>("Test")
652
- .define_constructor(Constructor<Test>())
653
- .define_method("hello",
654
- &Test::hello,
655
- (Arg("hello"), Arg("second") = "world")
656
- );
657
- }
658
- \endcode
659
-
660
- The syntax here is simply Arg(nameOfParameter)[ = defaultValue]. The name of the
661
- parameter is not important here (a readability tool), but the value set via operator=
662
- must match the type of the parameter. As such it may be necessary to
663
- explicitly cast the default value.
664
-
665
- \code
666
- .define_method("hello",
667
- &Test::hello,
668
- (Arg("hello"), Arg("second") = (std::string)"world")
669
- );
670
- \endcode
671
-
672
- These Rice::Arg objects must be in the correct order and must be
673
- surrounded with parentheses if more than one exists.
674
-
675
- Now, Ruby will now know about the default arguments, and this wrapper
676
- can be used as expected:
677
-
678
- \code
679
- t = Test.new
680
- t.hello("hello")
681
- t.hello("goodnight", "moon")
682
- \endcode
683
-
684
- This also works with Constructors:
685
-
686
- \code
687
- .define_constructor(Constructor<SomeClass, int, int>(),
688
- ( Arg("arg1") = 1, Arg("otherArg") = 12 );
689
- \endcode
690
-
691
- \subsection director Director
692
-
693
- As polymorphism is the most important tennant of Object Oriented Programming,
694
- it is important that Rice supports polymorphic calls travelling between C++
695
- and Ruby seemlessly. Super calls from Ruby subclasses back into C++ already work,
696
- but enabling the other direction requires some extra effort. Rice
697
- suppplies the the Rice::Director class and
698
- Rice::Data_Type::define_director to expose this functionality.
699
-
700
- Like SWIG_Director, Rice::Director is a class that is used to build a proxy class
701
- to properly send execution up or down the object heiarchy for that class. Take
702
- the following class:
703
-
704
- \code
705
- class VirtualBase {
706
- public:
707
- VirtualBase();
708
- virtual int doWork();
709
- virtual int processWorker() = 0;
710
- };
711
- \endcode
712
-
713
- Due to the abstract nature of this class, it will not work at all with Rice
714
- in its current form. Any attempt to do so will cause a compilation error due to
715
- this class not being constructable. Even without the pure virtual function, any
716
- call to VirtualBase::doWork will stop at the C++ level and will not pass down into
717
- any Ruby subclasses.
718
-
719
- To properly wrap both of these methods, use a Rice::Director subclass as a proxy:
720
-
721
- \code
722
- #include "rice/Director.hpp"
723
-
724
- class VirtualBaseProxy : public VirtualBase, public Rice::Director {
725
- public:
726
- VirtualBaseProxy(Object self) : Rice::Director(self) { }
727
-
728
- virtual int doWork() {
729
- return from_ruby<int>( getSelf().call("do_work") );
730
- }
731
-
732
- int default_doWork() {
733
- return VirtualBase::doWork();
734
- }
735
-
736
- virtual int processWorker() {
737
- return from_ruby<int>( getSelf().call("process_worker") );
738
- }
739
-
740
- int default_processWorker() {
741
- raisePureVirtual();
742
- }
743
- };
744
- \endcode
745
-
746
- There is a lot going on here, so we'll go through each part.
747
-
748
- \code
749
- class VirtualBaseProxy : public Virtualbase, public Rice::Director {
750
- \endcode
751
-
752
- First, the class needs to subclass both the virtual class in question and Rice::Director.
753
-
754
- \code
755
- public:
756
- VirtualBaseProxy(Object self) : Rice::Director(self) { }
757
- \endcode
758
-
759
- For Rice::Director to work its magic, every instance of this class needs to
760
- have a handle to the Ruby instance. The constructor
761
- must take a Rice::Object as the first argument and pass it up into
762
- Rice::Director. The code here is the minimum required for a Rice::Director proxy.
763
-
764
- \code
765
- virtual int doWork() {
766
- return from_ruby<int>( getSelf().call("do_work") );
767
- }
768
-
769
- int default_doWork() {
770
- return VirtualBase::doWork();
771
- }
772
- \endcode
773
-
774
- Here the directory proxy overrides the methods for Ruby exposure and
775
- implements the required actions to pass flow around the heirarchy
776
- appropriately. The pattern shown here is that the actual override will
777
- call down into Ruby, handling any type conversions, while a
778
- default_methodName method handles calling up into C++ and will be the
779
- method wrapped into Rice.
780
-
781
- The default_doWork method will be used as Rice's hookup of calling back up the
782
- heirarchy (wrapping is below). This method needs to do one of two things: call
783
- up the class heirarchy, as seen here, or call raisePureVirtual() as seen in the
784
- processWorker example:
785
-
786
- \code
787
- int default_processWorker() {
788
- raisePureVirtual();
789
- }
790
- \endcode
791
-
792
- The method raisePureVirtual() exists to allow wrapping a pure virtual method into Ruby
793
- (and ensuring compliation is possible) but making sure any users of this extension are
794
- informed quickly that there's nothing callable in the C++ side of the library.
795
-
796
- Once the proxy class is built, it's time to wrap it into Ruby:
797
-
798
- \code
799
- extern "C"
800
- void Init_virtual() {
801
- define_class<VirtualBase>("VirtualBase")
802
- .define_director<VirtualBaseProxy>()
803
- .define_constructor(Constructor<VirtualBaseProxy, Rice::Object>())
804
- .define_method("do_work", &VirtualBaseProxy::default_doWork)
805
- .define_method("process_worker", &VirtualBaseProxy::default_processWorker);
806
- }
807
- \endcode
808
-
809
- The wrapping is the same as is described earlier in this document. Expose the class
810
- VirtualBase, and register VirtualBaseProxy as a director proxy of VirtualBase with
811
- Rice::Data_Type::define_director, then define methods pointing to the proxy object as necessary.
812
-
813
- You must use the Rice::Director proxy class in the Constructor line, this allows proper
814
- object construction / destruction of the types in question.
815
-
816
- \subsection implicit_cast Implicit Casting
817
-
818
- There are times when a library exposes classes that, while unrelated, are
819
- built to be interchangeable across the library. One example of this is found in
820
- the Open Source 3d rendering engine <a
821
- href="http://www.ogre3d.org/">OGRE</a>: Ogre::Degree and Ogre::Radian.
822
- When a given method takes a Radian, you're free to pass in a Degree, and vice versa.
823
-
824
- Rice cannot automatically figure out if this kind of functionality is
825
- possible in a given library but it does provide an API for defining
826
- these relationships: Rice::define_implicit_cast<From, To>().
827
-
828
- \code
829
- class Degree { ... };
830
- class Radian { ... };
831
-
832
- extern "C"
833
- void Init_implicit() {
834
- define_class<Degree>()
835
- ...;
836
- define_class<Radian>()
837
- ...;
838
-
839
- define_implicit_cast<Degree, Radian>();
840
- define_implicit_cast<Radian, Degree>();
841
- }
842
- \endcode
843
-
844
- Using Rice::define_implicit_cast has the following requirements:
845
-
846
- \li The two types must be bound in Rice before defining the cast.
847
- \li The classes must have constructors that take the other type.
848
- \li This feature cannot be used with fundamental types.
849
-
850
- To see a full example of this feature, please check out
851
- test/test_Data_Type.cpp.
852
-
853
- \section motivation Motivation
854
-
855
- There are a number of common problems when writing C or C++ extensions
856
- for Ruby:
857
-
858
- \li Type safety. It is easy to mix-up integral types such as ID and
859
- VALUE. Some of the functions in the Ruby API are not consistent with
860
- which types they take (e.g. rb_const_defined takes an ID and
861
- rb_mod_remove_const takes a Symbol).
862
-
863
- \li DRY principle. Specifying the number of arguments that each wrapped
864
- function takes is easy to get wrong. Adding a new argument to the
865
- function means that the number of arguments passed to rb_define_method
866
- must also be updated.
867
-
868
- \li Type conversion. There are many different functions to convert data
869
- to and from ruby types. Many of them have different semantics or
870
- different forms. For example, to convert a string, one might use the
871
- StringValue macro, but to convert a fixnum, one might use FIX2INT.
872
- Unwrapping previously wrapped C data uses yet another form.
873
-
874
- \li Exception safety. It is imperative that C++ exceptions never make
875
- their way into C code, and it is also imperative that a Ruby exception
876
- never escape while there are objects on the stack with nontrivial
877
- destructors. Rules for when it is okay to use which exceptions are
878
- difficult to get right, especially as code is maintained through time.
879
-
880
- \li Thread safety. Because the Ruby interpreter is not threads-safe,
881
- the Ruby interpreter must not be run from more than one thread.
882
- Because of tricks the GC and scheduler play with the C stack, it's not
883
- enough to ensure that only one thread runs the interpreter at any
884
- given time; once the interpreter has been run from one thread, it must
885
- only ever be run from that thread in the future. Additionally,
886
- because Ruby copies the stack when it switches threads, C++ code must
887
- be careful not to access objects in one Ruby thread that were created
888
- on the stack in another Ruby thread.
889
-
890
- \li C-based API. The Ruby API is not always convenient for accessing
891
- Ruby data structurs such as Hash and Array, especially when writing C++
892
- code, as the interface for these containers is not consistent with
893
- standard containers.
894
-
895
- \li Calling convention. Function pointers passed into the Ruby API must
896
- follow the C calling convention. This means that it is not possible to
897
- pass a pointer to a template function or static member function (that
898
- is, it will work on some platforms, but isn't portable).
899
-
900
- \li Inheritance. When wrapping C++ objects, it is easy to store a
901
- pointer to a derived class, but then methods in the base class must have
902
- knowledge of the derived class in order to unwrap the object. It is
903
- possible to always store a pointer to the base class and then
904
- dynamic_cast the pointer to the derived type when necessary, but this
905
- can be slow and cumbersome, and it isn't likely to work with multiple
906
- inheritance. A system that properly handles inheritance for all corner
907
- cases is nontrivial.
908
-
909
- \li Multiple inheritance. C++ supports true multiple inheritance, but
910
- the Ruby object model uses single inheritance with mixins. When
911
- wrapping a library whose public interface uses multiple inheritance,
912
- care must be taken in constructing the mapping.
913
-
914
- \li GC safety. All live Ruby objects must be marked during the garbage
915
- collector's mark phase, otherwise they will be prematurely destroyed.
916
- The general rule is that object references stored on the heap should be
917
- either registered with rb_gc_register_address or marked by a data
918
- object's mark function; object references stored on the stack will be
919
- automatically marked, provided the Ruby interpreter was properly
920
- initialized at startup.
921
-
922
- \li Callbacks. C implements callbacks via function pointers, while ruby
923
- typically implements callbacks via procs. Writing an adapter function
924
- to call the proc is not difficult, but there is much opportunity for
925
- error (particularly with exception-safety).
926
-
927
- \li Data serialization. By default data objects defined at the C layer
928
- are not marshalable. The user must explicitly define functions to
929
- marshal the data member-by-member.
930
-
931
- Rice addresses these issues in many ways:
932
-
933
- \li Type safety. Rice provides encapsulation for all builtin types,
934
- such as Object, Identifier, Class, Module, and String. It
935
- automatically checks the dynamic type of an object before constructing
936
- an instance of a wrapper.
937
-
938
- \li DRY principle. Rice uses introspection through the use of templates
939
- and function overloading to automatically determine the number and types
940
- of arguments to functions. Default arguments must still be handled
941
- explicitly, however.
942
-
943
- \li Type conversions. Rice provides cast-style to_ruby<> and
944
- from_ruby<> template functions to simplify explicit type conversions.
945
- Automatic type conversions for parameters and return values are
946
- generated for all wrapped functions.
947
-
948
- \li Exception safety. Rice automatically converts common exceptions and
949
- provides a mechanism for converting user-defined exception types. Rice
950
- also provides convenience functions for converting exceptions when
951
- calling back into ruby code.
952
-
953
- \li Thread safety. Rice provides no mechanisms for dealing with thread
954
- safety. Many common thread safety issues should be alleviated by YARV,
955
- which supports POSIX threads.
956
-
957
- \li C++-based API. Rice provides an object-oriented C++-style API to
958
- most common functions in the Ruby C API.
959
-
960
- \li Calling convention. Rice automatically uses C calling convention
961
- for all function pointers passed into the Ruby API.
962
-
963
- \li Inheritance. Rice provides automatic conversion to the base class
964
- type when a wrapped member function is called on the base class.
965
-
966
- \li Multiple inheritance. Rice provides no mechanism for multiple
967
- inheritance. Multiple inheritance can be simulated via mixins, though
968
- this is not yet as easy as it could be.
969
-
970
- \li GC safety. Rice provides a handful of convenience classes for
971
- interacting with the garbage collector. There are still basic rules
972
- which must be followed to ensure that objects get properly destroyed.
973
-
974
- \li Callbacks. Rice provides a handful of convenience classes for
975
- dealing with callbacks.
976
-
977
- \li Data serialization. Rice provides no mechanism for data
978
- serialization, but it is likely this may be added in a future release.
979
-
980
-
981
- \section what_not What Rice is Not
982
-
983
- There are a number projects which server similar functions to Rice. Two
984
- such popular projects are SWIG and Boost.Python. Rice has some
985
- distinct features which set it apart from both of these projects.
986
-
987
- Rice is not trying to replace SWIG. Rice is not a generic wrapper
988
- interface generator. Rice is a C++ library for interfacing with the
989
- Ruby C API. This provides a very natural way for C++ programmers to
990
- wrap their C++ code, without having to learn a new domain-specific
991
- language. However, there is no reason why SWIG and Rice could not work
992
- together; a SWIG module could be written to generate Rice code. Such a
993
- module would combine the portability of SWIG with the maintainability of
994
- Rice (I have written extensions using both, and I have found Rice
995
- extensions to be more maintainable when the interface is constantly
996
- changing. Your mileage may vary).
997
-
998
- Rice is also not trying to simply be a Ruby version of Boost.Python.
999
- Rice does use some of the same template tricks that Boost.Python uses,
1000
- however there are some important distinctions. First of all,
1001
- Boost.Python attempts to create a declarative DSL in C++ using
1002
- templates. Rice is a wrapper around the Ruby C API and attempts to make
1003
- its interface look like an OO version of the API; this means that class
1004
- declarations look procedural rather than declarative. Secondly, the
1005
- Ruby object model is different from the python object model. This is
1006
- reflected in the interface to Rice; it mimics the Ruby object model at
1007
- the C++ level. Thirdly, Rice uses Ruby as a code generator; I find this
1008
- to be much more readable than using the Boost preprocessor library.
1009
-
1010
-
1011
- \section history History
1012
-
1013
- Rice originated as Excruby, a project to interface with C++-based trading
1014
- software at Automated Trading Desk in Mount Pleasant, South Carolina.
1015
- The Ruby bindings for Swig were at the time less mature than they are
1016
- today, and did not suit the needs of the project.
1017
-
1018
- Excruby was written not as a wrapper for the Ruby API, but rather as a
1019
- set of helper functions and classes for interfacing with the Ruby
1020
- interpreter in an exception-safe manner. Over the course of five years,
1021
- the project grew into wrappers for pieces of the API, but the original
1022
- helper functions remained as part of the public interface.
1023
-
1024
- This created confusion for the users of the library, because there were
1025
- multiple ways of accomplishing most tasks -- directly through the C API,
1026
- through a low-level wrapper around the C API, and through a high-level
1027
- abstraction of the lower-level interfaces.
1028
-
1029
- Rice was then born in an attempt to clean up the interface. Rice keeps
1030
- the lower-level wrappers, but as an implementation detail; the public
1031
- interface is truly a high-level abstraction around the Ruby C API.
1032
-
1033
-
1034
- \section gc The GC
1035
-
1036
- \li Objects are not automatically registered with the garbage collector.
1037
-
1038
- \li If an Object is on the stack, it does not need to be registered with
1039
- the garbage collector.
1040
-
1041
- \li If an Object is allocated on the heap or if it is a member of an
1042
- object that might be allocated on the heap, use an
1043
- Rice::Address_Registration_Guard to register the object with the garbage
1044
- collector.
1045
-
1046
- \li If a reference counted object is being wrapped, or if another type
1047
- of smart pointer is wrapped, ensure that only one mechanism is used to
1048
- destroy the object. In general, the smart pointer manages the
1049
- allocation of the object, and Ruby should hold only a reference to the
1050
- smart pointer. When the garbage collector determines that it is time to
1051
- clean up the object, the smart pointer will be destroyed, decrementing
1052
- the reference count; when the reference count drops to 0, underlying
1053
- object will be destroyed.
1054
-
1055
- vim:ft=cpp:tw=72:ts=2:sw=2:fo=cqrtn:noci:si