wurlinc-rice 1.4.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (186) hide show
  1. data/COPYING +23 -0
  2. data/Doxyfile +1253 -0
  3. data/Makefile.am +26 -0
  4. data/README +1119 -0
  5. data/README.mingw +8 -0
  6. data/Rakefile +33 -0
  7. data/bootstrap +9 -0
  8. data/configure.ac +52 -0
  9. data/doxygen.ac +314 -0
  10. data/doxygen.am +186 -0
  11. data/extconf.rb +41 -0
  12. data/post-autoconf.rb +22 -0
  13. data/post-automake.rb +28 -0
  14. data/rice/Address_Registration_Guard.hpp +7 -0
  15. data/rice/Address_Registration_Guard.ipp +34 -0
  16. data/rice/Address_Registration_Guard_defn.hpp +65 -0
  17. data/rice/Allocation_Strategies.hpp +37 -0
  18. data/rice/Arg.hpp +8 -0
  19. data/rice/Arg_impl.hpp +127 -0
  20. data/rice/Arg_operators.cpp +21 -0
  21. data/rice/Arg_operators.hpp +19 -0
  22. data/rice/Array.hpp +220 -0
  23. data/rice/Array.ipp +263 -0
  24. data/rice/Builtin_Object.hpp +8 -0
  25. data/rice/Builtin_Object.ipp +50 -0
  26. data/rice/Builtin_Object_defn.hpp +51 -0
  27. data/rice/Class.cpp +57 -0
  28. data/rice/Class.hpp +8 -0
  29. data/rice/Class.ipp +6 -0
  30. data/rice/Class_defn.hpp +83 -0
  31. data/rice/Constructor.hpp +367 -0
  32. data/rice/Critical_Guard.hpp +40 -0
  33. data/rice/Critical_Guard.ipp +26 -0
  34. data/rice/Data_Object.hpp +8 -0
  35. data/rice/Data_Object.ipp +133 -0
  36. data/rice/Data_Object_defn.hpp +132 -0
  37. data/rice/Data_Type.cpp +54 -0
  38. data/rice/Data_Type.hpp +8 -0
  39. data/rice/Data_Type.ipp +365 -0
  40. data/rice/Data_Type_defn.hpp +261 -0
  41. data/rice/Data_Type_fwd.hpp +12 -0
  42. data/rice/Director.cpp +13 -0
  43. data/rice/Director.hpp +39 -0
  44. data/rice/Enum.hpp +117 -0
  45. data/rice/Enum.ipp +246 -0
  46. data/rice/Exception.cpp +59 -0
  47. data/rice/Exception.hpp +9 -0
  48. data/rice/Exception_Base.hpp +8 -0
  49. data/rice/Exception_Base.ipp +13 -0
  50. data/rice/Exception_Base_defn.hpp +27 -0
  51. data/rice/Exception_defn.hpp +69 -0
  52. data/rice/Hash.hpp +210 -0
  53. data/rice/Hash.ipp +338 -0
  54. data/rice/Identifier.cpp +8 -0
  55. data/rice/Identifier.hpp +50 -0
  56. data/rice/Identifier.ipp +33 -0
  57. data/rice/Jump_Tag.hpp +24 -0
  58. data/rice/Makefile.am +129 -0
  59. data/rice/Module.cpp +84 -0
  60. data/rice/Module.hpp +8 -0
  61. data/rice/Module.ipp +6 -0
  62. data/rice/Module_defn.hpp +88 -0
  63. data/rice/Module_impl.hpp +281 -0
  64. data/rice/Module_impl.ipp +348 -0
  65. data/rice/Object.cpp +160 -0
  66. data/rice/Object.hpp +8 -0
  67. data/rice/Object.ipp +19 -0
  68. data/rice/Object_defn.hpp +191 -0
  69. data/rice/Require_Guard.hpp +21 -0
  70. data/rice/String.cpp +94 -0
  71. data/rice/String.hpp +89 -0
  72. data/rice/Struct.cpp +117 -0
  73. data/rice/Struct.hpp +162 -0
  74. data/rice/Struct.ipp +26 -0
  75. data/rice/Symbol.cpp +25 -0
  76. data/rice/Symbol.hpp +66 -0
  77. data/rice/Symbol.ipp +44 -0
  78. data/rice/VM.cpp +92 -0
  79. data/rice/VM.hpp +32 -0
  80. data/rice/config.hpp.in +40 -0
  81. data/rice/detail/Arguments.hpp +118 -0
  82. data/rice/detail/Auto_Function_Wrapper.hpp +829 -0
  83. data/rice/detail/Auto_Function_Wrapper.ipp +3391 -0
  84. data/rice/detail/Auto_Member_Function_Wrapper.hpp +828 -0
  85. data/rice/detail/Auto_Member_Function_Wrapper.ipp +2503 -0
  86. data/rice/detail/Caster.hpp +103 -0
  87. data/rice/detail/Exception_Handler.hpp +8 -0
  88. data/rice/detail/Exception_Handler.ipp +68 -0
  89. data/rice/detail/Exception_Handler_defn.hpp +96 -0
  90. data/rice/detail/Iterator.hpp +93 -0
  91. data/rice/detail/Not_Copyable.hpp +25 -0
  92. data/rice/detail/Wrapped_Function.hpp +33 -0
  93. data/rice/detail/cfp.hpp +24 -0
  94. data/rice/detail/cfp.ipp +51 -0
  95. data/rice/detail/check_ruby_type.cpp +27 -0
  96. data/rice/detail/check_ruby_type.hpp +23 -0
  97. data/rice/detail/creation_funcs.hpp +37 -0
  98. data/rice/detail/creation_funcs.ipp +36 -0
  99. data/rice/detail/default_allocation_func.hpp +23 -0
  100. data/rice/detail/default_allocation_func.ipp +11 -0
  101. data/rice/detail/define_method_and_auto_wrap.hpp +31 -0
  102. data/rice/detail/define_method_and_auto_wrap.ipp +30 -0
  103. data/rice/detail/demangle.cpp +56 -0
  104. data/rice/detail/demangle.hpp +19 -0
  105. data/rice/detail/env.hpp +19 -0
  106. data/rice/detail/from_ruby.hpp +43 -0
  107. data/rice/detail/from_ruby.ipp +60 -0
  108. data/rice/detail/method_data.cpp +159 -0
  109. data/rice/detail/method_data.hpp +21 -0
  110. data/rice/detail/mininode.cpp +1220 -0
  111. data/rice/detail/mininode.hpp +320 -0
  112. data/rice/detail/node.hpp +13 -0
  113. data/rice/detail/object_call.hpp +68 -0
  114. data/rice/detail/object_call.ipp +131 -0
  115. data/rice/detail/protect.cpp +29 -0
  116. data/rice/detail/protect.hpp +34 -0
  117. data/rice/detail/ruby.hpp +93 -0
  118. data/rice/detail/ruby_version_code.hpp.in +6 -0
  119. data/rice/detail/rubysig.hpp +19 -0
  120. data/rice/detail/st.hpp +60 -0
  121. data/rice/detail/to_ruby.hpp +22 -0
  122. data/rice/detail/to_ruby.ipp +36 -0
  123. data/rice/detail/traits.hpp +43 -0
  124. data/rice/detail/win32.hpp +16 -0
  125. data/rice/detail/wrap_function.hpp +341 -0
  126. data/rice/detail/wrap_function.ipp +514 -0
  127. data/rice/global_function.hpp +33 -0
  128. data/rice/global_function.ipp +22 -0
  129. data/rice/protect.hpp +91 -0
  130. data/rice/protect.ipp +1133 -0
  131. data/rice/ruby_mark.hpp +13 -0
  132. data/rice/ruby_try_catch.hpp +86 -0
  133. data/rice/rubypp.rb +97 -0
  134. data/rice/to_from_ruby.hpp +8 -0
  135. data/rice/to_from_ruby.ipp +297 -0
  136. data/rice/to_from_ruby_defn.hpp +71 -0
  137. data/ruby/Makefile.am +1 -0
  138. data/ruby/lib/Makefile.am +3 -0
  139. data/ruby/lib/mkmf-rice.rb.in +216 -0
  140. data/ruby/lib/version.rb +3 -0
  141. data/ruby.ac +136 -0
  142. data/sample/Makefile.am +47 -0
  143. data/sample/enum/extconf.rb +3 -0
  144. data/sample/enum/sample_enum.cpp +54 -0
  145. data/sample/enum/test.rb +8 -0
  146. data/sample/inheritance/animals.cpp +98 -0
  147. data/sample/inheritance/extconf.rb +3 -0
  148. data/sample/inheritance/test.rb +7 -0
  149. data/sample/map/extconf.rb +3 -0
  150. data/sample/map/map.cpp +81 -0
  151. data/sample/map/test.rb +7 -0
  152. data/test/Makefile.am +78 -0
  153. data/test/ext/Makefile.am +43 -0
  154. data/test/ext/t1/Foo.hpp +10 -0
  155. data/test/ext/t1/extconf.rb +2 -0
  156. data/test/ext/t1/t1.cpp +15 -0
  157. data/test/ext/t2/extconf.rb +2 -0
  158. data/test/ext/t2/t2.cpp +11 -0
  159. data/test/test_Address_Registration_Guard.cpp +43 -0
  160. data/test/test_Allocation_Strategies.cpp +77 -0
  161. data/test/test_Array.cpp +241 -0
  162. data/test/test_Builtin_Object.cpp +72 -0
  163. data/test/test_Class.cpp +498 -0
  164. data/test/test_Constructor.cpp +128 -0
  165. data/test/test_Critical_Guard.cpp +51 -0
  166. data/test/test_Data_Object.cpp +275 -0
  167. data/test/test_Data_Type.cpp +348 -0
  168. data/test/test_Director.cpp +301 -0
  169. data/test/test_Enum.cpp +195 -0
  170. data/test/test_Exception.cpp +46 -0
  171. data/test/test_Hash.cpp +195 -0
  172. data/test/test_Identifier.cpp +70 -0
  173. data/test/test_Jump_Tag.cpp +17 -0
  174. data/test/test_Memory_Management.cpp +50 -0
  175. data/test/test_Module.cpp +481 -0
  176. data/test/test_Object.cpp +148 -0
  177. data/test/test_String.cpp +94 -0
  178. data/test/test_Struct.cpp +192 -0
  179. data/test/test_Symbol.cpp +63 -0
  180. data/test/test_To_From_Ruby.cpp +263 -0
  181. data/test/test_VM.cpp +26 -0
  182. data/test/test_global_functions.cpp +114 -0
  183. data/test/test_rice.rb +43 -0
  184. data/test/unittest.cpp +136 -0
  185. data/test/unittest.hpp +292 -0
  186. metadata +276 -0
@@ -0,0 +1,132 @@
1
+ #ifndef Rice__Data_Object_defn__hpp_
2
+ #define Rice__Data_Object_defn__hpp_
3
+
4
+ #include "Object_defn.hpp"
5
+ #include "Data_Type_fwd.hpp"
6
+ #include "Allocation_Strategies.hpp"
7
+ #include "ruby_mark.hpp"
8
+ #include "detail/to_ruby.hpp"
9
+ #include "detail/ruby.hpp"
10
+
11
+ /*! \file
12
+ * \brief Provides a helper class for wrapping and unwrapping C++
13
+ * objects as Ruby objects.
14
+ */
15
+
16
+ namespace Rice
17
+ {
18
+
19
+ template<typename T>
20
+ struct Default_Mark_Function
21
+ {
22
+ typedef void (*Ruby_Data_Func)(T * obj);
23
+ static const Ruby_Data_Func mark;
24
+ };
25
+
26
+ //! A smartpointer-like wrapper for Ruby data objects.
27
+ /*! A data object is a ruby object of type T_DATA, which is usually
28
+ * created by using the Data_Wrap_Struct or Data_Make_Struct macro.
29
+ * This class wraps creation of the data structure, providing a
30
+ * type-safe object-oriented interface to the underlying C interface.
31
+ * This class works in conjunction with the Data_Type class to ensure
32
+ * type safety.
33
+ *
34
+ * Example:
35
+ * \code
36
+ * class Foo { };
37
+ * ...
38
+ * Data_Type<Foo> rb_cFoo = define_class("Foo");
39
+ * ...
40
+ * // Wrap:
41
+ * Data_Object<Foo> foo1(new Foo);
42
+ *
43
+ * // Get value to return:
44
+ * VALUE v = foo1.value()
45
+ *
46
+ * // Unwrap:
47
+ * Data_Object<Foo> foo2(v, rb_cFoo);
48
+ * \endcode
49
+ */
50
+ template<typename T>
51
+ class Data_Object
52
+ : public Object
53
+ {
54
+ public:
55
+ //! A function that takes a T* and returns void.
56
+ typedef void (*Ruby_Data_Func)(T * obj);
57
+
58
+ //! Wrap a C++ object.
59
+ /*! This constructor is analgous to calling Data_Wrap_Struct. Be
60
+ * careful not to call this function more than once for the same
61
+ * pointer (in general, it should only be called for newly
62
+ * constructed objects that need to be managed by Ruby's garbage
63
+ * collector).
64
+ * \param obj the object to wrap.
65
+ * \param klass the Ruby class to use for the newly created Ruby
66
+ * object.
67
+ * \param mark_func a function that gets called by the garbage
68
+ * collector to mark the object's children.
69
+ * \param free_func a function that gets called by the garbage
70
+ * collector to free the object.
71
+ */
72
+ Data_Object(
73
+ T * obj,
74
+ VALUE klass = Data_Type<T>::klass(),
75
+ Ruby_Data_Func mark_func = Default_Mark_Function<T>::mark,
76
+ Ruby_Data_Func free_func = Default_Allocation_Strategy<T>::free);
77
+
78
+ //! Unwrap a Ruby object.
79
+ /*! This constructor is analgous to calling Data_Get_Struct. Uses
80
+ * Data_Type<T>::klass as the class of the object.
81
+ * \param value the Ruby object to unwrap.
82
+ */
83
+ Data_Object(
84
+ Object value);
85
+
86
+ //! Unwrap a Ruby object.
87
+ /*! This constructor is analgous to calling Data_Get_Struct. Will
88
+ * throw an exception if the class of the object differs from the
89
+ * specified class.
90
+ * \param value the Ruby object to unwrap.
91
+ * \param klass the expected class of the object.
92
+ */
93
+ template<typename U>
94
+ Data_Object(
95
+ Object value,
96
+ Data_Type<U> const & klass = Data_Type<T>::klass());
97
+
98
+ //! Make a copy of a Data_Object
99
+ /*! \param other the Data_Object to copy.
100
+ */
101
+ Data_Object(Data_Object const & other);
102
+
103
+ T & operator*() const { return *obj_; } //!< Return a reference to obj_
104
+ T * operator->() const { return obj_; } //!< Return a pointer to obj_
105
+ T * get() const { return obj_; } //!< Return a pointer to obj_
106
+
107
+ //! Swap with another data object of the same type
108
+ /*! \param ref the object with which to swap.
109
+ */
110
+ template<typename U>
111
+ void swap(Data_Object<U> & ref);
112
+
113
+ private:
114
+ static void check_cpp_type(Data_Type<T> const & klass);
115
+
116
+ private:
117
+ T * obj_;
118
+ };
119
+
120
+ namespace detail
121
+ {
122
+ template<typename T>
123
+ struct to_ruby_<Data_Object<T> >
124
+ {
125
+ static Rice::Object convert(Data_Object<T> const & x);
126
+ };
127
+ }
128
+
129
+ } // namespace Rice
130
+
131
+ #endif // Rice__Data_Object_defn__hpp_
132
+
@@ -0,0 +1,54 @@
1
+ #include "Data_Type.hpp"
2
+
3
+ Rice::Data_Type_Base::Casters * Rice::Data_Type_Base::casters_ = 0;
4
+
5
+ Rice::Data_Type_Base::
6
+ Data_Type_Base()
7
+ : Module_impl<Class, Data_Type_Base>()
8
+ {
9
+ }
10
+
11
+ Rice::Data_Type_Base::
12
+ Data_Type_Base(VALUE v)
13
+ : Module_impl<Class, Data_Type_Base>(v)
14
+ {
15
+ }
16
+
17
+ Rice::Data_Type_Base::
18
+ ~Data_Type_Base()
19
+ {
20
+ }
21
+
22
+ Rice::Data_Type_Base::Casters &
23
+ Rice::Data_Type_Base::
24
+ casters()
25
+ {
26
+ // Initialize the casters_ if it is null
27
+ if (!casters_)
28
+ {
29
+ // First, see if it has been previously registered with the
30
+ // interpreter (possibly by another extension)
31
+ Class object(rb_cObject);
32
+ Object casters_object(object.attr_get("__rice_casters__"));
33
+
34
+ if (casters_object.is_nil())
35
+ {
36
+ // If it is unset, then set it for the first time
37
+ Data_Object<Casters> casters(
38
+ new Casters,
39
+ rb_cObject);
40
+ object.iv_set("__rice_casters__", casters);
41
+ casters_ = casters.get();
42
+ }
43
+ else
44
+ {
45
+ // If it is set, then use the existing value
46
+ Data_Object<Casters> casters(
47
+ casters_object);
48
+ casters_ = casters.get();
49
+ }
50
+ }
51
+
52
+ return *casters_;
53
+ }
54
+
@@ -0,0 +1,8 @@
1
+ #ifndef Rice__Data_Type__hpp_
2
+ #define Rice__Data_Type__hpp_
3
+
4
+ #include "Data_Type_defn.hpp"
5
+ #include "Data_Type.ipp"
6
+
7
+ #endif // Rice__Data_Type__hpp_
8
+
@@ -0,0 +1,365 @@
1
+ #ifndef Rice__Data_Type__ipp_
2
+ #define Rice__Data_Type__ipp_
3
+
4
+ #include "Class.hpp"
5
+ #include "String.hpp"
6
+ #include "Data_Object.hpp"
7
+ #include "detail/default_allocation_func.hpp"
8
+ #include "detail/creation_funcs.hpp"
9
+ #include "detail/method_data.hpp"
10
+ #include "detail/Caster.hpp"
11
+ #include "detail/demangle.hpp"
12
+
13
+ #include <stdexcept>
14
+ #include <typeinfo>
15
+
16
+ template<typename T>
17
+ VALUE Rice::Data_Type<T>::klass_ = Qnil;
18
+
19
+ template<typename T>
20
+ std::auto_ptr<Rice::detail::Abstract_Caster> Rice::Data_Type<T>::caster_;
21
+
22
+ template<typename T>
23
+ template<typename Base_T>
24
+ inline Rice::Data_Type<T> Rice::Data_Type<T>::
25
+ bind(Module const & klass)
26
+ {
27
+ if(klass.value() == klass_)
28
+ {
29
+ return Data_Type<T>();
30
+ }
31
+
32
+ if(is_bound())
33
+ {
34
+ std::string s;
35
+ s = "Data type ";
36
+ s += detail::demangle(typeid(T).name());
37
+ s += " is already bound to a different type";
38
+ throw std::runtime_error(s.c_str());
39
+ }
40
+
41
+ // TODO: Make sure base type is bound; throw an exception otherwise.
42
+ // We can't do this just yet, because we don't have a specialization
43
+ // for binding to void.
44
+ klass_ = klass;
45
+
46
+ // TODO: do we need to unregister when the program exits? we have to
47
+ // be careful if we do, because the ruby interpreter might have
48
+ // already shut down. The correct behavior is probably to register an
49
+ // exit proc with the interpreter, so the proc gets called before the
50
+ // GC shuts down.
51
+ rb_gc_register_address(&klass_);
52
+
53
+ for(typename Instances::iterator it = unbound_instances().begin(),
54
+ end = unbound_instances().end();
55
+ it != end;
56
+ unbound_instances().erase(it++))
57
+ {
58
+ (*it)->set_value(klass);
59
+ }
60
+
61
+ detail::Abstract_Caster * base_caster = Data_Type<Base_T>().caster();
62
+ caster_.reset(new detail::Caster<T, Base_T>(base_caster, klass));
63
+ Data_Type_Base::casters().insert(std::make_pair(klass, caster_.get()));
64
+ return Data_Type<T>();
65
+ }
66
+
67
+ template<typename T>
68
+ inline Rice::Data_Type<T>::
69
+ Data_Type()
70
+ : Module_impl<Data_Type_Base, Data_Type<T> >(
71
+ klass_ == Qnil ? rb_cObject : klass_)
72
+ {
73
+ if(!is_bound())
74
+ {
75
+ unbound_instances().insert(this);
76
+ }
77
+ }
78
+
79
+ template<typename T>
80
+ inline Rice::Data_Type<T>::
81
+ Data_Type(Module const & klass)
82
+ : Module_impl<Data_Type_Base, Data_Type<T> >(
83
+ klass)
84
+ {
85
+ this->bind<void>(klass);
86
+ }
87
+
88
+ template<typename T>
89
+ inline Rice::Data_Type<T>::
90
+ ~Data_Type()
91
+ {
92
+ unbound_instances().erase(this);
93
+ }
94
+
95
+ template<typename T>
96
+ Rice::Module
97
+ Rice::Data_Type<T>::
98
+ klass() {
99
+ if(is_bound())
100
+ {
101
+ return klass_;
102
+ }
103
+ else
104
+ {
105
+ std::string s;
106
+ s += detail::demangle(typeid(T *).name());
107
+ s += " is unbound";
108
+ throw std::runtime_error(s.c_str());
109
+ }
110
+ }
111
+
112
+ template<typename T>
113
+ Rice::Data_Type<T> & Rice::Data_Type<T>::
114
+ operator=(Module const & klass)
115
+ {
116
+ this->bind<void>(klass);
117
+ return *this;
118
+ }
119
+
120
+ template<typename T>
121
+ template<typename Constructor_T>
122
+ inline Rice::Data_Type<T> & Rice::Data_Type<T>::
123
+ define_constructor(
124
+ Constructor_T /* constructor */,
125
+ Arguments* arguments)
126
+ {
127
+ check_is_bound();
128
+
129
+ // Normal constructor pattern with new/initialize
130
+ rb_define_alloc_func(
131
+ static_cast<VALUE>(*this),
132
+ detail::default_allocation_func<T>);
133
+ define_method(
134
+ "initialize",
135
+ &Constructor_T::construct,
136
+ arguments
137
+ );
138
+
139
+ return *this;
140
+ }
141
+
142
+ template<typename T>
143
+ template<typename Constructor_T>
144
+ inline Rice::Data_Type<T> & Rice::Data_Type<T>::
145
+ define_constructor(
146
+ Constructor_T constructor,
147
+ Arg const& arg)
148
+ {
149
+ Arguments* args = new Arguments();
150
+ args->add(arg);
151
+ return define_constructor(constructor, args);
152
+ }
153
+
154
+
155
+ template<typename T>
156
+ template<typename Director_T>
157
+ inline Rice::Data_Type<T>& Rice::Data_Type<T>::
158
+ define_director()
159
+ {
160
+ Rice::Data_Type<Director_T>::template bind<T>(*this);
161
+ return *this;
162
+ }
163
+
164
+ template<typename T>
165
+ inline T * Rice::Data_Type<T>::
166
+ from_ruby(Object x)
167
+ {
168
+ check_is_bound();
169
+
170
+ void * v = DATA_PTR(x.value());
171
+ Class klass = x.class_of();
172
+
173
+ if(klass.value() == klass_)
174
+ {
175
+ // Great, not converting to a base/derived type
176
+ Data_Type<T> data_klass;
177
+ Data_Object<T> obj(x, data_klass);
178
+ return obj.get();
179
+ }
180
+
181
+ Data_Type_Base::Casters::const_iterator it = Data_Type_Base::casters().begin();
182
+ Data_Type_Base::Casters::const_iterator end = Data_Type_Base::casters().end();
183
+
184
+ // Finding the bound type that relates to the given klass is
185
+ // a two step process. We iterate over the list of known type casters,
186
+ // looking for:
187
+ //
188
+ // 1) casters that handle this direct type
189
+ // 2) casters that handle types that are ancestors of klass
190
+ //
191
+ // Step 2 allows us to handle the case where a Rice-wrapped class
192
+ // is subclassed in Ruby but then an instance of that class is passed
193
+ // back into C++ (say, in a Listener / callback construction)
194
+ //
195
+
196
+ VALUE ancestors = rb_mod_ancestors(klass.value());
197
+
198
+ long earliest = RARRAY_LEN(ancestors) + 1;
199
+
200
+ int index;
201
+ VALUE indexFound;
202
+ Data_Type_Base::Casters::const_iterator toUse = end;
203
+
204
+ for(; it != end; it++) {
205
+ // Do we match directly?
206
+ if(klass.value() == it->first) {
207
+ toUse = it;
208
+ break;
209
+ }
210
+
211
+ // Check for ancestors. Trick is, we need to find the lowest
212
+ // ancestor that does have a Caster to make sure that we're casting
213
+ // to the closest C++ type that the Ruby class is subclassing.
214
+ // There might be multiple ancestors that are also wrapped in
215
+ // the extension, so find the earliest in the list and use that one.
216
+ indexFound = rb_funcall(ancestors, rb_intern("index"), 1, it->first);
217
+
218
+ if(indexFound != Qnil) {
219
+ index = NUM2INT(indexFound);
220
+
221
+ if(index < earliest) {
222
+ earliest = index;
223
+ toUse = it;
224
+ }
225
+ }
226
+ }
227
+
228
+ if(toUse == end)
229
+ {
230
+ std::string s = "Class ";
231
+ s += klass.name().str();
232
+ s += " is not registered/bound in Rice";
233
+ throw std::runtime_error(s);
234
+ }
235
+
236
+ detail::Abstract_Caster * caster = toUse->second;
237
+ if(caster)
238
+ {
239
+ T * result = static_cast<T *>(caster->cast_to_base(v, klass_));
240
+ return result;
241
+ }
242
+ else
243
+ {
244
+ return static_cast<T *>(v);
245
+ }
246
+ }
247
+
248
+ template<typename T>
249
+ inline bool Rice::Data_Type<T>::
250
+ is_bound()
251
+ {
252
+ return klass_ != Qnil;
253
+ }
254
+
255
+ template<typename T>
256
+ inline Rice::detail::Abstract_Caster * Rice::Data_Type<T>::
257
+ caster() const
258
+ {
259
+ check_is_bound();
260
+ return caster_.get();
261
+ }
262
+
263
+ namespace Rice
264
+ {
265
+
266
+ template<>
267
+ inline detail::Abstract_Caster * Data_Type<void>::
268
+ caster() const
269
+ {
270
+ return 0;
271
+ }
272
+
273
+ template<typename T>
274
+ void Data_Type<T>::
275
+ check_is_bound()
276
+ {
277
+ if(!is_bound())
278
+ {
279
+ std::string s;
280
+ s = "Data type ";
281
+ s += detail::demangle(typeid(T).name());
282
+ s += " is not bound";
283
+ throw std::runtime_error(s.c_str());
284
+ }
285
+ }
286
+
287
+ } // Rice
288
+
289
+ template<typename T>
290
+ inline Rice::Data_Type<T> Rice::
291
+ define_class_under(
292
+ Object module,
293
+ char const * name)
294
+ {
295
+ Class c(define_class_under(module, name, rb_cObject));
296
+ c.undef_creation_funcs();
297
+ return Data_Type<T>::template bind<void>(c);
298
+ }
299
+
300
+ template<typename T, typename Base_T>
301
+ inline Rice::Data_Type<T> Rice::
302
+ define_class_under(
303
+ Object module,
304
+ char const * name)
305
+ {
306
+ Data_Type<Base_T> base_dt;
307
+ Class c(define_class_under(module, name, base_dt));
308
+ c.undef_creation_funcs();
309
+ return Data_Type<T>::template bind<Base_T>(c);
310
+ }
311
+
312
+ template<typename T>
313
+ inline Rice::Data_Type<T> Rice::
314
+ define_class(
315
+ char const * name)
316
+ {
317
+ Class c(define_class(name, rb_cObject));
318
+ c.undef_creation_funcs();
319
+ return Data_Type<T>::template bind<void>(c);
320
+ }
321
+
322
+ template<typename T, typename Base_T>
323
+ inline Rice::Data_Type<T> Rice::
324
+ define_class(
325
+ char const * name)
326
+ {
327
+ Data_Type<Base_T> base_dt;
328
+ Class c(define_class(name, base_dt));
329
+ c.undef_creation_funcs();
330
+ return Data_Type<T>::template bind<Base_T>(c);
331
+ }
332
+
333
+ template<typename From_T, typename To_T>
334
+ inline void
335
+ Rice::define_implicit_cast()
336
+ {
337
+ // As Rice currently expects only one entry into
338
+ // this list for a given klass VALUE, we need to get
339
+ // the current caster for From_T and insert in our
340
+ // new caster as the head of the caster list
341
+
342
+ Class from_class = Data_Type<From_T>::klass().value();
343
+ Class to_class = Data_Type<To_T>::klass().value();
344
+
345
+ detail::Abstract_Caster* from_caster =
346
+ Data_Type<From_T>::caster_.release();
347
+
348
+ detail::Abstract_Caster* new_caster =
349
+ new detail::Implicit_Caster<To_T, From_T>(from_caster, to_class);
350
+
351
+ // Insert our new caster into the list for the from class
352
+ Data_Type_Base::casters().erase(from_class);
353
+ Data_Type_Base::casters().insert(
354
+ std::make_pair(
355
+ from_class,
356
+ new_caster
357
+ )
358
+ );
359
+
360
+ // And make sure the from_class has direct access to the
361
+ // updated caster list
362
+ Data_Type<From_T>::caster_.reset(new_caster);
363
+ }
364
+
365
+ #endif // Rice__Data_Type__ipp_