rice2 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/COPYING +23 -0
- data/Doxyfile +2268 -0
- data/Makefile.am +26 -0
- data/Makefile.in +929 -0
- data/README.md +1054 -0
- data/README.mingw +8 -0
- data/Rakefile +24 -0
- data/aclocal.m4 +1090 -0
- data/bootstrap +8 -0
- data/config.guess +1667 -0
- data/config.sub +1793 -0
- data/configure +8209 -0
- data/configure.ac +55 -0
- data/depcomp +791 -0
- data/doxygen.ac +314 -0
- data/doxygen.am +186 -0
- data/extconf.rb +66 -0
- data/install-sh +529 -0
- data/missing +215 -0
- data/post-autoconf.rb +22 -0
- data/post-automake.rb +28 -0
- data/rice/Address_Registration_Guard.cpp +22 -0
- data/rice/Address_Registration_Guard.hpp +7 -0
- data/rice/Address_Registration_Guard.ipp +37 -0
- data/rice/Address_Registration_Guard_defn.hpp +75 -0
- data/rice/Arg.hpp +8 -0
- data/rice/Arg_impl.hpp +129 -0
- data/rice/Arg_operators.cpp +21 -0
- data/rice/Arg_operators.hpp +19 -0
- data/rice/Array.hpp +214 -0
- data/rice/Array.ipp +256 -0
- data/rice/Builtin_Object.hpp +8 -0
- data/rice/Builtin_Object.ipp +50 -0
- data/rice/Builtin_Object_defn.hpp +50 -0
- data/rice/Class.cpp +57 -0
- data/rice/Class.hpp +8 -0
- data/rice/Class.ipp +6 -0
- data/rice/Class_defn.hpp +83 -0
- data/rice/Constructor.hpp +47 -0
- data/rice/Data_Object.hpp +8 -0
- data/rice/Data_Object.ipp +133 -0
- data/rice/Data_Object_defn.hpp +138 -0
- data/rice/Data_Type.cpp +54 -0
- data/rice/Data_Type.hpp +8 -0
- data/rice/Data_Type.ipp +365 -0
- data/rice/Data_Type_defn.hpp +261 -0
- data/rice/Data_Type_fwd.hpp +12 -0
- data/rice/Director.cpp +13 -0
- data/rice/Director.hpp +39 -0
- data/rice/Enum.hpp +117 -0
- data/rice/Enum.ipp +246 -0
- data/rice/Exception.cpp +59 -0
- data/rice/Exception.hpp +13 -0
- data/rice/Exception_Base.hpp +8 -0
- data/rice/Exception_Base.ipp +13 -0
- data/rice/Exception_Base_defn.hpp +27 -0
- data/rice/Exception_defn.hpp +69 -0
- data/rice/Hash.hpp +227 -0
- data/rice/Hash.ipp +329 -0
- data/rice/Identifier.cpp +8 -0
- data/rice/Identifier.hpp +50 -0
- data/rice/Identifier.ipp +33 -0
- data/rice/Jump_Tag.hpp +24 -0
- data/rice/Makefile.am +122 -0
- data/rice/Makefile.in +885 -0
- data/rice/Module.cpp +84 -0
- data/rice/Module.hpp +8 -0
- data/rice/Module.ipp +6 -0
- data/rice/Module_defn.hpp +88 -0
- data/rice/Module_impl.hpp +281 -0
- data/rice/Module_impl.ipp +345 -0
- data/rice/Object.cpp +169 -0
- data/rice/Object.hpp +8 -0
- data/rice/Object.ipp +33 -0
- data/rice/Object_defn.hpp +214 -0
- data/rice/Require_Guard.hpp +21 -0
- data/rice/String.cpp +94 -0
- data/rice/String.hpp +91 -0
- data/rice/Struct.cpp +117 -0
- data/rice/Struct.hpp +162 -0
- data/rice/Struct.ipp +26 -0
- data/rice/Symbol.cpp +25 -0
- data/rice/Symbol.hpp +66 -0
- data/rice/Symbol.ipp +44 -0
- data/rice/config.hpp +47 -0
- data/rice/config.hpp.in +46 -0
- data/rice/detail/Arguments.hpp +118 -0
- data/rice/detail/Auto_Function_Wrapper.hpp +898 -0
- data/rice/detail/Auto_Function_Wrapper.ipp +3694 -0
- data/rice/detail/Auto_Member_Function_Wrapper.hpp +897 -0
- data/rice/detail/Auto_Member_Function_Wrapper.ipp +2774 -0
- data/rice/detail/Caster.hpp +103 -0
- data/rice/detail/Exception_Handler.hpp +8 -0
- data/rice/detail/Exception_Handler.ipp +68 -0
- data/rice/detail/Exception_Handler_defn.hpp +96 -0
- data/rice/detail/Iterator.hpp +93 -0
- data/rice/detail/Not_Copyable.hpp +25 -0
- data/rice/detail/Wrapped_Function.hpp +33 -0
- data/rice/detail/cfp.hpp +24 -0
- data/rice/detail/cfp.ipp +51 -0
- data/rice/detail/check_ruby_type.cpp +27 -0
- data/rice/detail/check_ruby_type.hpp +23 -0
- data/rice/detail/creation_funcs.hpp +37 -0
- data/rice/detail/creation_funcs.ipp +36 -0
- data/rice/detail/default_allocation_func.hpp +23 -0
- data/rice/detail/default_allocation_func.ipp +11 -0
- data/rice/detail/define_method_and_auto_wrap.hpp +31 -0
- data/rice/detail/define_method_and_auto_wrap.ipp +30 -0
- data/rice/detail/demangle.cpp +56 -0
- data/rice/detail/demangle.hpp +19 -0
- data/rice/detail/env.hpp +11 -0
- data/rice/detail/from_ruby.hpp +43 -0
- data/rice/detail/from_ruby.ipp +60 -0
- data/rice/detail/method_data.cpp +92 -0
- data/rice/detail/method_data.hpp +21 -0
- data/rice/detail/node.hpp +13 -0
- data/rice/detail/protect.cpp +29 -0
- data/rice/detail/protect.hpp +34 -0
- data/rice/detail/ruby.hpp +74 -0
- data/rice/detail/ruby_version_code.hpp +6 -0
- data/rice/detail/ruby_version_code.hpp.in +6 -0
- data/rice/detail/st.hpp +22 -0
- data/rice/detail/to_ruby.hpp +22 -0
- data/rice/detail/to_ruby.ipp +36 -0
- data/rice/detail/traits.hpp +43 -0
- data/rice/detail/win32.hpp +16 -0
- data/rice/detail/wrap_function.hpp +66 -0
- data/rice/global_function.hpp +33 -0
- data/rice/global_function.ipp +22 -0
- data/rice/protect.hpp +38 -0
- data/rice/protect.ipp +1134 -0
- data/rice/ruby_mark.hpp +13 -0
- data/rice/ruby_try_catch.hpp +86 -0
- data/rice/rubypp.rb +97 -0
- data/rice/to_from_ruby.hpp +8 -0
- data/rice/to_from_ruby.ipp +418 -0
- data/rice/to_from_ruby_defn.hpp +70 -0
- data/ruby.ac +135 -0
- data/ruby/Makefile.am +1 -0
- data/ruby/Makefile.in +628 -0
- data/ruby/lib/Makefile.am +3 -0
- data/ruby/lib/Makefile.in +506 -0
- data/ruby/lib/mkmf-rice.rb.in +217 -0
- data/ruby/lib/version.rb +3 -0
- data/sample/Makefile.am +47 -0
- data/sample/Makefile.in +489 -0
- data/sample/enum/extconf.rb +3 -0
- data/sample/enum/sample_enum.cpp +54 -0
- data/sample/enum/test.rb +8 -0
- data/sample/inheritance/animals.cpp +98 -0
- data/sample/inheritance/extconf.rb +3 -0
- data/sample/inheritance/test.rb +7 -0
- data/sample/map/extconf.rb +3 -0
- data/sample/map/map.cpp +81 -0
- data/sample/map/test.rb +7 -0
- data/test/Makefile.am +72 -0
- data/test/Makefile.in +1213 -0
- data/test/ext/Makefile.am +41 -0
- data/test/ext/Makefile.in +483 -0
- data/test/ext/t1/Foo.hpp +10 -0
- data/test/ext/t1/extconf.rb +2 -0
- data/test/ext/t1/t1.cpp +15 -0
- data/test/ext/t2/extconf.rb +2 -0
- data/test/ext/t2/t2.cpp +11 -0
- data/test/test_Address_Registration_Guard.cpp +43 -0
- data/test/test_Array.cpp +248 -0
- data/test/test_Builtin_Object.cpp +71 -0
- data/test/test_Class.cpp +496 -0
- data/test/test_Constructor.cpp +128 -0
- data/test/test_Data_Object.cpp +275 -0
- data/test/test_Data_Type.cpp +348 -0
- data/test/test_Director.cpp +308 -0
- data/test/test_Enum.cpp +215 -0
- data/test/test_Exception.cpp +47 -0
- data/test/test_Hash.cpp +212 -0
- data/test/test_Identifier.cpp +70 -0
- data/test/test_Jump_Tag.cpp +17 -0
- data/test/test_Memory_Management.cpp +50 -0
- data/test/test_Module.cpp +497 -0
- data/test/test_Object.cpp +159 -0
- data/test/test_String.cpp +107 -0
- data/test/test_Struct.cpp +205 -0
- data/test/test_Symbol.cpp +63 -0
- data/test/test_To_From_Ruby.cpp +428 -0
- data/test/test_global_functions.cpp +114 -0
- data/test/test_rice.rb +41 -0
- data/test/unittest.cpp +136 -0
- data/test/unittest.hpp +294 -0
- metadata +297 -0
@@ -0,0 +1,261 @@
|
|
1
|
+
#ifndef Rice__Data_Type_defn__hpp_
|
2
|
+
#define Rice__Data_Type_defn__hpp_
|
3
|
+
|
4
|
+
#include "Class_defn.hpp"
|
5
|
+
#include "Data_Type_fwd.hpp"
|
6
|
+
#include "detail/ruby.hpp"
|
7
|
+
#include <memory>
|
8
|
+
#include <map>
|
9
|
+
#include <set>
|
10
|
+
|
11
|
+
/*!
|
12
|
+
* \example map/map.cpp
|
13
|
+
*/
|
14
|
+
|
15
|
+
namespace Rice
|
16
|
+
{
|
17
|
+
|
18
|
+
namespace detail
|
19
|
+
{
|
20
|
+
class Abstract_Caster;
|
21
|
+
}
|
22
|
+
|
23
|
+
class Module;
|
24
|
+
|
25
|
+
//! The base class for all instantiations of Data_Type.
|
26
|
+
class Data_Type_Base
|
27
|
+
: public Module_impl<Class, Data_Type_Base>
|
28
|
+
{
|
29
|
+
public:
|
30
|
+
//! Default constructor.
|
31
|
+
Data_Type_Base();
|
32
|
+
|
33
|
+
//! Constructor.
|
34
|
+
Data_Type_Base(VALUE v);
|
35
|
+
|
36
|
+
//! Destructor.
|
37
|
+
virtual ~Data_Type_Base() = 0;
|
38
|
+
|
39
|
+
// Must be public to workaround gcc 3.3
|
40
|
+
typedef std::map<VALUE, detail::Abstract_Caster *> Casters;
|
41
|
+
|
42
|
+
virtual detail::Abstract_Caster * caster() const = 0;
|
43
|
+
|
44
|
+
static Casters & casters();
|
45
|
+
|
46
|
+
private:
|
47
|
+
static Casters * casters_;
|
48
|
+
};
|
49
|
+
|
50
|
+
//! Define a new data class in the namespace given by module.
|
51
|
+
/*! The class will have a base class of Object.
|
52
|
+
* \param T the C++ type of the wrapped class.
|
53
|
+
* \param module the the Module in which to define the class.
|
54
|
+
* \return the new class.
|
55
|
+
*/
|
56
|
+
template<typename T>
|
57
|
+
Rice::Data_Type<T> define_class_under(
|
58
|
+
Object module,
|
59
|
+
char const * name);
|
60
|
+
|
61
|
+
//! Define a new data class in the namespace given by module.
|
62
|
+
/*! The class with have a base class determined by Base_T (specifically,
|
63
|
+
* Data_Type<Base_T>::klass). Therefore, the type Base_T must already
|
64
|
+
* have been registered using define_class<> or define_class_under<>.
|
65
|
+
* \param T the C++ type of the wrapped class.
|
66
|
+
* \param module the the Module in which to define the class.
|
67
|
+
* \return the new class.
|
68
|
+
*/
|
69
|
+
template<typename T, typename Base_T>
|
70
|
+
Rice::Data_Type<T> define_class_under(
|
71
|
+
Object module,
|
72
|
+
char const * name);
|
73
|
+
|
74
|
+
//! Define a new data class in the default namespace.
|
75
|
+
/*! The class will have a base class of Object.
|
76
|
+
* \param T the C++ type of the wrapped class.
|
77
|
+
* \return the new class.
|
78
|
+
*/
|
79
|
+
template<typename T>
|
80
|
+
Rice::Data_Type<T> define_class(
|
81
|
+
char const * name);
|
82
|
+
|
83
|
+
//! Define a new data class in the default namespace.
|
84
|
+
/*! The class with have a base class determined by Base_T (specifically,
|
85
|
+
* Data_Type<Base_T>::klass). Therefore, the type Base_T must already
|
86
|
+
* have been registered using define_class<> or define_class_under<>.
|
87
|
+
* \param T the C++ type of the wrapped class.
|
88
|
+
* \param module the the Module in which to define the class.
|
89
|
+
* \return the new class.
|
90
|
+
*/
|
91
|
+
template<typename T, typename Base_T>
|
92
|
+
Rice::Data_Type<T> define_class(
|
93
|
+
char const * name);
|
94
|
+
|
95
|
+
//! Define an implicit conversion rule between two types.
|
96
|
+
/*! Given two types, which can be custom types already
|
97
|
+
* wrapped into Rice or fundamental C++ types, this
|
98
|
+
* tells Rice that the two types can be used interchangably.
|
99
|
+
* \param From_T The type to convert from
|
100
|
+
* \param To_T The type to convert to
|
101
|
+
*/
|
102
|
+
template<typename From_T, typename To_T>
|
103
|
+
void define_implicit_cast();
|
104
|
+
|
105
|
+
//! A mechanism for binding ruby types to C++ types.
|
106
|
+
/*! This class binds run-time types (Ruby VALUEs) to compile-time types
|
107
|
+
* (C++ types). The binding can occur only once.
|
108
|
+
*/
|
109
|
+
template<typename T>
|
110
|
+
class Data_Type
|
111
|
+
: public Module_impl<Data_Type_Base, Data_Type<T> >
|
112
|
+
{
|
113
|
+
public:
|
114
|
+
//! The C++ type being held.
|
115
|
+
typedef T Type;
|
116
|
+
|
117
|
+
//! Default constructor which does not bind.
|
118
|
+
/*! No member functions must be called on this Data_Type except bind,
|
119
|
+
* until the type is bound.
|
120
|
+
*/
|
121
|
+
Data_Type();
|
122
|
+
|
123
|
+
//! Constructor which takes a Module.
|
124
|
+
/*! Binds the type to the given VALUE according to the rules given
|
125
|
+
* above.
|
126
|
+
* \param klass the module to which to bind.
|
127
|
+
*/
|
128
|
+
Data_Type(Module const & v);
|
129
|
+
|
130
|
+
//! Destructor.
|
131
|
+
virtual ~Data_Type();
|
132
|
+
|
133
|
+
//! Explictly return the Ruby type.
|
134
|
+
/*! \return the ruby class to which the type is bound.
|
135
|
+
*/
|
136
|
+
static Module klass();
|
137
|
+
|
138
|
+
//! Assignment operator which takes a Module
|
139
|
+
/*! \param klass must be the class to which this data type is already
|
140
|
+
* bound.
|
141
|
+
* \return *this
|
142
|
+
*/
|
143
|
+
virtual Data_Type & operator=(Module const & klass);
|
144
|
+
|
145
|
+
//! Define a constructor for the class.
|
146
|
+
/*! Creates a singleton method allocate and an instance method called
|
147
|
+
* initialize which together create a new instance of the class. The
|
148
|
+
* allocate method allocates memory for the object reference and the
|
149
|
+
* initialize method constructs the object.
|
150
|
+
* \param constructor an object that has a static member function
|
151
|
+
* construct() that constructs a new instance of T and sets the object's data
|
152
|
+
* member to point to the new instance. A helper class Constructor
|
153
|
+
* is provided that does precisely this.
|
154
|
+
* For example:
|
155
|
+
* \code
|
156
|
+
* define_class<Foo>("Foo")
|
157
|
+
* .define_constructor(Constructor<Foo>());
|
158
|
+
* \endcode
|
159
|
+
*/
|
160
|
+
template<typename Constructor_T>
|
161
|
+
Data_Type<T> & define_constructor(
|
162
|
+
Constructor_T constructor,
|
163
|
+
Arguments * arguments = 0);
|
164
|
+
|
165
|
+
template<typename Constructor_T>
|
166
|
+
Data_Type<T> & define_constructor(
|
167
|
+
Constructor_T constructor,
|
168
|
+
Arg const& arg);
|
169
|
+
|
170
|
+
//! Register a Director class for this class.
|
171
|
+
/*! For any class that uses Rice::Director to enable polymorphism
|
172
|
+
* across the languages, you need to register that director proxy
|
173
|
+
* class with this method. Not doing so will cause the resulting
|
174
|
+
* library to die at run time when it tries to convert the base
|
175
|
+
* type into the Director proxy type, and cannot find an appropriate Caster.
|
176
|
+
*
|
177
|
+
* This method takes no arguments, just needs the type of the
|
178
|
+
* Director proxy class.
|
179
|
+
*
|
180
|
+
* For example:
|
181
|
+
* \code
|
182
|
+
* class FooDirector : public Foo, public Rice::Director {
|
183
|
+
* ...
|
184
|
+
* };
|
185
|
+
*
|
186
|
+
* define_class<Foo>("Foo")
|
187
|
+
* .define_director<FooDirector>()
|
188
|
+
* .define_constructor(Constructor<FooDirector, Rice::Object>());
|
189
|
+
* \endcode
|
190
|
+
*/
|
191
|
+
template<typename Director_T>
|
192
|
+
Data_Type<T>& define_director();
|
193
|
+
|
194
|
+
//! Convert ruby object x to type T.
|
195
|
+
/*! \param x the object to convert.
|
196
|
+
* \return the C++ object wrapped inside object x.
|
197
|
+
*/
|
198
|
+
static T * from_ruby(Object x);
|
199
|
+
|
200
|
+
//! Determine if the type is bound.
|
201
|
+
/*! \return true if the object is bound, false otherwise.
|
202
|
+
*/
|
203
|
+
static bool is_bound();
|
204
|
+
|
205
|
+
virtual detail::Abstract_Caster * caster() const;
|
206
|
+
|
207
|
+
static std_unique_ptr<detail::Abstract_Caster> caster_;
|
208
|
+
|
209
|
+
protected:
|
210
|
+
//! Bind a Data_Type to a VALUE.
|
211
|
+
/*! Throws an exception if the Data_Type is already bound to a
|
212
|
+
* different class. Any existing instances of the Data_Type will be
|
213
|
+
* bound after this function returns.
|
214
|
+
* \param klass the ruby type to which to bind.
|
215
|
+
* \return *this
|
216
|
+
*/
|
217
|
+
template<typename Base_T>
|
218
|
+
static Data_Type bind(Module const & klass);
|
219
|
+
|
220
|
+
template<typename T_>
|
221
|
+
friend Rice::Data_Type<T_> define_class_under(
|
222
|
+
Object module,
|
223
|
+
char const * name);
|
224
|
+
|
225
|
+
template<typename T_, typename Base_T_>
|
226
|
+
friend Rice::Data_Type<T_> define_class_under(
|
227
|
+
Object module,
|
228
|
+
char const * name);
|
229
|
+
|
230
|
+
template<typename T_>
|
231
|
+
friend Rice::Data_Type<T_> Rice::define_class(
|
232
|
+
char const * name);
|
233
|
+
|
234
|
+
template<typename T_, typename Base_T_>
|
235
|
+
friend Rice::Data_Type<T_> define_class(
|
236
|
+
char const * name);
|
237
|
+
|
238
|
+
private:
|
239
|
+
template<typename T_>
|
240
|
+
friend class Data_Type;
|
241
|
+
|
242
|
+
static void check_is_bound();
|
243
|
+
|
244
|
+
static VALUE klass_;
|
245
|
+
|
246
|
+
typedef std::set<Data_Type<T> *> Instances;
|
247
|
+
|
248
|
+
static Instances & unbound_instances()
|
249
|
+
{
|
250
|
+
static Instances unbound_instances;
|
251
|
+
return unbound_instances;
|
252
|
+
}
|
253
|
+
};
|
254
|
+
|
255
|
+
|
256
|
+
} // namespace Rice
|
257
|
+
|
258
|
+
#include "Data_Type.ipp"
|
259
|
+
|
260
|
+
#endif // Rice__Data_Type_defn__hpp_
|
261
|
+
|
data/rice/Director.cpp
ADDED
data/rice/Director.hpp
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
#ifndef Rice__Director__hpp_
|
2
|
+
#define Rice__Director__hpp_
|
3
|
+
|
4
|
+
#include "Object.hpp"
|
5
|
+
|
6
|
+
namespace Rice {
|
7
|
+
|
8
|
+
/**
|
9
|
+
* A Director works exactly as a SWIG %director works (thus the name).
|
10
|
+
* You use this class to help build proxy classes so that polymorphism
|
11
|
+
* works from C++ into Ruby. See the main README for how this class works.
|
12
|
+
*/
|
13
|
+
class Director
|
14
|
+
{
|
15
|
+
public:
|
16
|
+
//! Construct new Director. Needs the Ruby object so that the
|
17
|
+
// proxy class can call methods on that object.
|
18
|
+
Director(Object self);
|
19
|
+
|
20
|
+
virtual ~Director() { }
|
21
|
+
|
22
|
+
//! Raise a ruby exception when a call comes through for a pure virtual method
|
23
|
+
/*! If a Ruby script calls 'super' on a method that's otherwise a pure virtual
|
24
|
+
* method, use this method to throw an exception in this case.
|
25
|
+
*/
|
26
|
+
void raisePureVirtual() const;
|
27
|
+
|
28
|
+
//! Get the Ruby object linked to this C++ instance
|
29
|
+
Object getSelf() const { return self_; }
|
30
|
+
|
31
|
+
private:
|
32
|
+
|
33
|
+
// Save the Ruby object related to the instance of this class
|
34
|
+
Object self_;
|
35
|
+
|
36
|
+
};
|
37
|
+
}
|
38
|
+
|
39
|
+
#endif // Rice__Director__hpp_
|
data/rice/Enum.hpp
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
#ifndef Rice__Enum__hpp_
|
2
|
+
#define Rice__Enum__hpp_
|
3
|
+
|
4
|
+
#include "to_from_ruby_defn.hpp"
|
5
|
+
#include "Address_Registration_Guard.hpp"
|
6
|
+
#include "Array.hpp"
|
7
|
+
#include "Hash.hpp"
|
8
|
+
#include "String.hpp"
|
9
|
+
#include "Module.hpp"
|
10
|
+
#include "Data_Type.hpp"
|
11
|
+
|
12
|
+
namespace Rice
|
13
|
+
{
|
14
|
+
|
15
|
+
//! Default traits for the Enum class template.
|
16
|
+
template<typename Enum_T>
|
17
|
+
struct Default_Enum_Traits
|
18
|
+
{
|
19
|
+
//! Converts the enum value to a long.
|
20
|
+
static long as_long(Enum_T value);
|
21
|
+
};
|
22
|
+
|
23
|
+
/*!
|
24
|
+
* \example enum/sample_enum.cpp
|
25
|
+
*/
|
26
|
+
|
27
|
+
//! A wrapper for enumerated types.
|
28
|
+
/*! Provides a simple type-safe wrapper for enumerated types. At the
|
29
|
+
* ruby level, the class will have convenience methods for iterating
|
30
|
+
* over all the defined enum values, converting the values to strings,
|
31
|
+
* and more.
|
32
|
+
*
|
33
|
+
* \param Enum_T the enumerated type
|
34
|
+
* \param Enum_Traits specifies the traits of the enumerated type.
|
35
|
+
*
|
36
|
+
* Example:
|
37
|
+
* \code
|
38
|
+
* enum Color { Red, Green, Blue };
|
39
|
+
* Enum<Color> rb_cColor = define_enum<Color>("Color")
|
40
|
+
* .define_value("Red", Red)
|
41
|
+
* .define_value("Green", Green)
|
42
|
+
* .define_value("Blue", Blue);
|
43
|
+
* \endcode
|
44
|
+
*/
|
45
|
+
template<typename Enum_T, typename Enum_Traits = Default_Enum_Traits<Enum_T> >
|
46
|
+
class Enum
|
47
|
+
: public Module_impl<Data_Type<Enum_T>, Enum<Enum_T, Enum_Traits> >
|
48
|
+
{
|
49
|
+
public:
|
50
|
+
//! Default constructor.
|
51
|
+
Enum();
|
52
|
+
|
53
|
+
//! Construct and initialize.
|
54
|
+
Enum(
|
55
|
+
char const * name,
|
56
|
+
Module module = rb_cObject);
|
57
|
+
|
58
|
+
//! Copy constructor.
|
59
|
+
Enum(Enum const & other);
|
60
|
+
|
61
|
+
//! Assignment operator.
|
62
|
+
Enum & operator=(Enum const & other);
|
63
|
+
|
64
|
+
//! Destructor.
|
65
|
+
virtual ~Enum();
|
66
|
+
|
67
|
+
//! Define a new enum value.
|
68
|
+
/*! \param name the name of the enum value.
|
69
|
+
* \param value the value to associate with name.
|
70
|
+
* \return *this
|
71
|
+
*/
|
72
|
+
Enum<Enum_T, Enum_Traits> & define_value(
|
73
|
+
char const * name,
|
74
|
+
Enum_T value);
|
75
|
+
|
76
|
+
void swap(Enum & other);
|
77
|
+
|
78
|
+
private:
|
79
|
+
//! Initialize the enum type.
|
80
|
+
/*! Must be called only once.
|
81
|
+
* \param name the name of the class to define
|
82
|
+
* \param module the module in which to place the enum class.
|
83
|
+
* \return *this
|
84
|
+
*/
|
85
|
+
Enum<Enum_T, Enum_Traits> & initialize(
|
86
|
+
char const * name,
|
87
|
+
Module module = rb_cObject);
|
88
|
+
|
89
|
+
private:
|
90
|
+
static Object each(Object self);
|
91
|
+
static Object to_s(Object self);
|
92
|
+
static Object to_i(Object self);
|
93
|
+
static Object inspect(Object self);
|
94
|
+
static Object compare(Object lhs, Object rhs);
|
95
|
+
static Object eql(Object lhs, Object rhs);
|
96
|
+
static Object hash(Object self);
|
97
|
+
static Object from_int(Class klass, Object i);
|
98
|
+
|
99
|
+
private:
|
100
|
+
Array enums_;
|
101
|
+
Address_Registration_Guard enums_guard_;
|
102
|
+
|
103
|
+
Hash names_;
|
104
|
+
Address_Registration_Guard names_guard_;
|
105
|
+
};
|
106
|
+
|
107
|
+
template<typename T>
|
108
|
+
Enum<T> define_enum(
|
109
|
+
char const * name,
|
110
|
+
Module module = rb_cObject);
|
111
|
+
|
112
|
+
} // namespace Rice
|
113
|
+
|
114
|
+
#include "Enum.ipp"
|
115
|
+
|
116
|
+
#endif // Rice__Enum__hpp_
|
117
|
+
|
data/rice/Enum.ipp
ADDED
@@ -0,0 +1,246 @@
|
|
1
|
+
#ifndef Rice__Enum__ipp_
|
2
|
+
#define Rice__Enum__ipp_
|
3
|
+
|
4
|
+
#include "Data_Object.hpp"
|
5
|
+
#include "Class.hpp"
|
6
|
+
#include "String.hpp"
|
7
|
+
#include "protect.hpp"
|
8
|
+
#include <memory>
|
9
|
+
|
10
|
+
template<typename Enum_T>
|
11
|
+
long Rice::Default_Enum_Traits<Enum_T>::as_long(Enum_T value)
|
12
|
+
{
|
13
|
+
return static_cast<long>(value);
|
14
|
+
}
|
15
|
+
|
16
|
+
template<typename Enum_T, typename Enum_Traits>
|
17
|
+
Rice::Enum<Enum_T, Enum_Traits>::
|
18
|
+
Enum()
|
19
|
+
: Module_impl<Data_Type<Enum_T>, Enum<Enum_T, Enum_Traits> >()
|
20
|
+
, enums_()
|
21
|
+
, enums_guard_(&enums_)
|
22
|
+
, names_()
|
23
|
+
, names_guard_(&names_)
|
24
|
+
{
|
25
|
+
}
|
26
|
+
|
27
|
+
template<typename Enum_T, typename Enum_Traits>
|
28
|
+
Rice::Enum<Enum_T, Enum_Traits>::
|
29
|
+
Enum(
|
30
|
+
char const * name,
|
31
|
+
Module module)
|
32
|
+
: Module_impl<Data_Type<Enum_T>, Enum<Enum_T, Enum_Traits> >()
|
33
|
+
, enums_()
|
34
|
+
, enums_guard_(&enums_)
|
35
|
+
, names_()
|
36
|
+
, names_guard_(&names_)
|
37
|
+
{
|
38
|
+
this->template bind<void>(initialize(name, module));
|
39
|
+
}
|
40
|
+
|
41
|
+
template<typename Enum_T, typename Enum_Traits>
|
42
|
+
Rice::Enum<Enum_T, Enum_Traits>::
|
43
|
+
Enum(Enum<Enum_T, Enum_Traits> const & other)
|
44
|
+
: Module_impl<Data_Type<Enum_T>, Enum<Enum_T, Enum_Traits> >(other)
|
45
|
+
, enums_(other.enums_)
|
46
|
+
, enums_guard_(&enums_)
|
47
|
+
, names_(other.names_)
|
48
|
+
, names_guard_(&names_)
|
49
|
+
{
|
50
|
+
}
|
51
|
+
|
52
|
+
template<typename Enum_T, typename Enum_Traits>
|
53
|
+
Rice::Enum<Enum_T, Enum_Traits> & Rice::Enum<Enum_T, Enum_Traits>::
|
54
|
+
operator=(Rice::Enum<Enum_T, Enum_Traits> const & other)
|
55
|
+
{
|
56
|
+
Rice::Enum<Enum_T, Enum_Traits> tmp(other);
|
57
|
+
this->swap(tmp);
|
58
|
+
return *this;
|
59
|
+
}
|
60
|
+
|
61
|
+
template<typename Enum_T, typename Enum_Traits>
|
62
|
+
Rice::Enum<Enum_T, Enum_Traits>::
|
63
|
+
~Enum()
|
64
|
+
{
|
65
|
+
}
|
66
|
+
|
67
|
+
template<typename Enum_T, typename Enum_Traits>
|
68
|
+
Rice::Enum<Enum_T, Enum_Traits> & Rice::Enum<Enum_T, Enum_Traits>::
|
69
|
+
initialize(
|
70
|
+
char const * name,
|
71
|
+
Module module)
|
72
|
+
{
|
73
|
+
Class c = Rice::define_class_under<Enum_T>(module, name)
|
74
|
+
.define_method("to_s", to_s)
|
75
|
+
.define_method("to_i", to_i)
|
76
|
+
.define_method("inspect", inspect)
|
77
|
+
.define_method("<=>", compare)
|
78
|
+
.define_method("hash", hash)
|
79
|
+
.define_method("eql?", eql)
|
80
|
+
.define_method("==", eql)
|
81
|
+
.define_method("===", eql)
|
82
|
+
.define_singleton_method("each", each)
|
83
|
+
.define_singleton_method("from_int", from_int)
|
84
|
+
.include_module(rb_mComparable);
|
85
|
+
|
86
|
+
// TODO: This should be unnecessary (it should be taken care of when
|
87
|
+
// define_class_under binds the C++ and ruby types)
|
88
|
+
this->set_value(c);
|
89
|
+
|
90
|
+
protect(rb_iv_set, c, "enums", enums_);
|
91
|
+
protect(rb_iv_set, c, "names", names_);
|
92
|
+
|
93
|
+
return *this;
|
94
|
+
}
|
95
|
+
|
96
|
+
template<typename Enum_T, typename Enum_Traits>
|
97
|
+
Rice::Enum<Enum_T, Enum_Traits> & Rice::Enum<Enum_T, Enum_Traits>::
|
98
|
+
define_value(
|
99
|
+
char const * name,
|
100
|
+
Enum_T value)
|
101
|
+
{
|
102
|
+
std_unique_ptr<Enum_T> copy(new Enum_T(value));
|
103
|
+
Rice::Data_Object<Enum_T> m(copy.get(), *this);
|
104
|
+
copy.release();
|
105
|
+
names_[m] = String(name);
|
106
|
+
this->const_set(name, m);
|
107
|
+
enums_.push(m);
|
108
|
+
|
109
|
+
return *this;
|
110
|
+
}
|
111
|
+
|
112
|
+
|
113
|
+
template<typename Enum_T, typename Enum_Traits>
|
114
|
+
void Rice::Enum<Enum_T, Enum_Traits>::
|
115
|
+
swap(Enum<Enum_T, Enum_Traits> & other)
|
116
|
+
{
|
117
|
+
Data_Type<Enum_T>::swap(other);
|
118
|
+
enums_.swap(other.enums_);
|
119
|
+
names_.swap(other.names_);
|
120
|
+
}
|
121
|
+
|
122
|
+
template<typename Enum_T, typename Enum_Traits>
|
123
|
+
Rice::Object Rice::Enum<Enum_T, Enum_Traits>::
|
124
|
+
each(Object self)
|
125
|
+
{
|
126
|
+
VALUE enums_v = rb_iv_get(self, "enums");
|
127
|
+
Check_Type(enums_v, T_ARRAY);
|
128
|
+
Array enums(enums_v);
|
129
|
+
for(size_t j = 0; j < enums.size(); ++j)
|
130
|
+
{
|
131
|
+
rb_yield(enums[j].value());
|
132
|
+
}
|
133
|
+
return Qnil;
|
134
|
+
}
|
135
|
+
|
136
|
+
template<typename Enum_T, typename Enum_Traits>
|
137
|
+
Rice::Object Rice::Enum<Enum_T, Enum_Traits>::
|
138
|
+
to_s(Object self)
|
139
|
+
{
|
140
|
+
Data_Type<Enum_T> klass;
|
141
|
+
Rice::Data_Object<Enum_T> m(self, klass);
|
142
|
+
Object enum_class = rb_class_of(m);
|
143
|
+
Hash names(rb_iv_get(enum_class, "names"));
|
144
|
+
Object name = names[m];
|
145
|
+
if(name.is_nil())
|
146
|
+
{
|
147
|
+
return String::format("INVALID(%d)", Enum_Traits::as_long(*m));
|
148
|
+
}
|
149
|
+
else
|
150
|
+
{
|
151
|
+
return String(name);
|
152
|
+
}
|
153
|
+
}
|
154
|
+
|
155
|
+
template<typename Enum_T, typename Enum_Traits>
|
156
|
+
Rice::Object Rice::Enum<Enum_T, Enum_Traits>::
|
157
|
+
inspect(Object self)
|
158
|
+
{
|
159
|
+
return String::format(
|
160
|
+
"#<%s::%s>",
|
161
|
+
String(self.class_of().name()).c_str(),
|
162
|
+
String(to_s(self)).c_str());
|
163
|
+
}
|
164
|
+
|
165
|
+
template<typename Enum_T, typename Enum_Traits>
|
166
|
+
Rice::Object Rice::Enum<Enum_T, Enum_Traits>::
|
167
|
+
compare(Object lhs, Object rhs)
|
168
|
+
{
|
169
|
+
if(lhs.class_of() != rhs.class_of())
|
170
|
+
{
|
171
|
+
String lhs_name(lhs.class_of().name());
|
172
|
+
String rhs_name(rhs.class_of().name());
|
173
|
+
rb_raise(
|
174
|
+
rb_eTypeError,
|
175
|
+
"Cannot compare %s to %s",
|
176
|
+
lhs_name.c_str(),
|
177
|
+
rhs_name.c_str());
|
178
|
+
}
|
179
|
+
|
180
|
+
Data_Type<Enum_T> klass;
|
181
|
+
Rice::Data_Object<Enum_T> l(lhs, klass);
|
182
|
+
Rice::Data_Object<Enum_T> r(rhs, klass);
|
183
|
+
|
184
|
+
Enum_T left(*l);
|
185
|
+
Enum_T right(*r);
|
186
|
+
|
187
|
+
if(left == right)
|
188
|
+
{
|
189
|
+
return INT2NUM(0);
|
190
|
+
}
|
191
|
+
else if(Enum_Traits::as_long(left) < Enum_Traits::as_long(right))
|
192
|
+
{
|
193
|
+
return INT2NUM(-1);
|
194
|
+
}
|
195
|
+
else
|
196
|
+
{
|
197
|
+
return INT2NUM(1);
|
198
|
+
}
|
199
|
+
}
|
200
|
+
|
201
|
+
template<typename Enum_T, typename Enum_Traits>
|
202
|
+
Rice::Object Rice::Enum<Enum_T, Enum_Traits>::
|
203
|
+
eql(Object lhs, Object rhs)
|
204
|
+
{
|
205
|
+
using ::from_ruby; // Workaround for g++ 3.3.3
|
206
|
+
bool is_equal = from_ruby<int>(compare(lhs, rhs)) == 0;
|
207
|
+
return to_ruby(is_equal);
|
208
|
+
}
|
209
|
+
|
210
|
+
template<typename Enum_T, typename Enum_Traits>
|
211
|
+
Rice::Object Rice::Enum<Enum_T, Enum_Traits>::
|
212
|
+
to_i(Object self)
|
213
|
+
{
|
214
|
+
Data_Type<Enum_T> klass;
|
215
|
+
Rice::Data_Object<Enum_T> m(self, klass);
|
216
|
+
return LONG2NUM(Enum_Traits::as_long(*m));
|
217
|
+
}
|
218
|
+
|
219
|
+
template<typename Enum_T, typename Enum_Traits>
|
220
|
+
Rice::Object Rice::Enum<Enum_T, Enum_Traits>::
|
221
|
+
hash(Object self)
|
222
|
+
{
|
223
|
+
return to_i(self);
|
224
|
+
}
|
225
|
+
|
226
|
+
template<typename Enum_T, typename Enum_Traits>
|
227
|
+
Rice::Object Rice::Enum<Enum_T, Enum_Traits>::
|
228
|
+
from_int(Class klass, Object i)
|
229
|
+
{
|
230
|
+
using ::from_ruby; // Workaround for g++ 3.3.3
|
231
|
+
Rice::Data_Object<Enum_T> m(
|
232
|
+
new Enum_T(static_cast<Enum_T>(from_ruby<long>(i))),
|
233
|
+
klass);
|
234
|
+
return m.value();
|
235
|
+
}
|
236
|
+
|
237
|
+
template<typename T>
|
238
|
+
Rice::Enum<T> Rice::
|
239
|
+
define_enum(
|
240
|
+
char const * name,
|
241
|
+
Module module)
|
242
|
+
{
|
243
|
+
return Enum<T>(name, module);
|
244
|
+
}
|
245
|
+
#endif // Rice__Enum__ipp_
|
246
|
+
|