keyme-rice 1.5.1.keyme

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