rice2 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- 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
data/rice/Object.hpp
ADDED
data/rice/Object.ipp
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
#ifndef Rice__Object__ipp_
|
2
|
+
#define Rice__Object__ipp_
|
3
|
+
|
4
|
+
#include "protect.hpp"
|
5
|
+
#include "detail/ruby.hpp"
|
6
|
+
#include "to_from_ruby.hpp"
|
7
|
+
|
8
|
+
#include <vector>
|
9
|
+
|
10
|
+
template<typename ...ArgT>
|
11
|
+
inline Rice::Object Rice::Object::
|
12
|
+
call(Identifier id, ArgT... args) const
|
13
|
+
{
|
14
|
+
auto asList = this->convert_args<ArgT...>(args...);
|
15
|
+
return protect(rb_funcall2, value(), id, (int)asList.size(), &asList[0]);
|
16
|
+
}
|
17
|
+
|
18
|
+
template<typename ...ArgT>
|
19
|
+
std::vector<VALUE> Rice::Object::convert_args(ArgT&... args) const {
|
20
|
+
return std::vector<VALUE>{ to_ruby(args)... };
|
21
|
+
}
|
22
|
+
|
23
|
+
template<typename T>
|
24
|
+
void Rice::Object::
|
25
|
+
iv_set(
|
26
|
+
Identifier name,
|
27
|
+
T const & value)
|
28
|
+
{
|
29
|
+
protect(rb_ivar_set, *this, name.id(), to_ruby(value));
|
30
|
+
}
|
31
|
+
|
32
|
+
#endif // Rice__Object__ipp_
|
33
|
+
|
@@ -0,0 +1,214 @@
|
|
1
|
+
#ifndef Rice__Object_defn__hpp_
|
2
|
+
#define Rice__Object_defn__hpp_
|
3
|
+
|
4
|
+
/*! \file Object.hpp
|
5
|
+
*/
|
6
|
+
|
7
|
+
#include "Identifier.hpp"
|
8
|
+
#include "detail/ruby.hpp"
|
9
|
+
|
10
|
+
#include <iosfwd>
|
11
|
+
#include <vector>
|
12
|
+
|
13
|
+
namespace Rice
|
14
|
+
{
|
15
|
+
|
16
|
+
class Class;
|
17
|
+
class String;
|
18
|
+
class Array;
|
19
|
+
|
20
|
+
//! The base class for all Objects
|
21
|
+
/*! Perhaps the name "Object" is a misnomer, because this class really
|
22
|
+
* holds an object reference, not an object.
|
23
|
+
*/
|
24
|
+
class Object
|
25
|
+
{
|
26
|
+
public:
|
27
|
+
//! Encapsulate an existing ruby object.
|
28
|
+
Object(VALUE value = Qnil) : value_(value) { }
|
29
|
+
|
30
|
+
//! Copy constructor
|
31
|
+
Object(Object const & other) : value_(other.value()) { }
|
32
|
+
|
33
|
+
//! Destructor
|
34
|
+
virtual ~Object() { }
|
35
|
+
|
36
|
+
//! Returns false if the object is nil or false; returns true
|
37
|
+
//! otherwise.
|
38
|
+
// Having this conversion also prevents accidental conversion to
|
39
|
+
// undesired integral types (e.g. long or int) by making the
|
40
|
+
// conversion ambiguous.
|
41
|
+
bool test() const { return RTEST(value_); }
|
42
|
+
|
43
|
+
//! Returns false if the object is nil or false; returns true
|
44
|
+
//! otherwise.
|
45
|
+
operator bool() const { return test(); }
|
46
|
+
|
47
|
+
//! Returns true if the object is nil, false otherwise.
|
48
|
+
bool is_nil() const { return NIL_P(value_); }
|
49
|
+
|
50
|
+
//! Implicit conversion to VALUE.
|
51
|
+
operator VALUE() const { return value_; }
|
52
|
+
|
53
|
+
//! Explicitly get the encapsulated VALUE.
|
54
|
+
// Returns a const ref so that Address_Registration_Guard can access
|
55
|
+
// the address where the VALUE is stored
|
56
|
+
VALUE const volatile & value() const { return value_; }
|
57
|
+
|
58
|
+
//! Get the class of an object.
|
59
|
+
/*! \return the object's Class.
|
60
|
+
*/
|
61
|
+
Class class_of() const;
|
62
|
+
|
63
|
+
//! Compare this object to another object.
|
64
|
+
/*! Gets the result of self <=> other and returns the result. The
|
65
|
+
* result will be less than zero if self < other, greater than zero
|
66
|
+
* if self > other, and equal to zero if self == other.
|
67
|
+
*/
|
68
|
+
int compare(Object const & other) const;
|
69
|
+
|
70
|
+
//! Return a string representation of an object.
|
71
|
+
/*! \return the result of calling to_s on the object. A String is not
|
72
|
+
* returned, because it is not possible to return an instance of a
|
73
|
+
* derived class.
|
74
|
+
*/
|
75
|
+
String to_s() const;
|
76
|
+
|
77
|
+
//! Inspect the object.
|
78
|
+
/*! \return the result of calling inspect on the object. A String is
|
79
|
+
* not returned, because it is not possible to return an instance of
|
80
|
+
* a derived class.
|
81
|
+
*/
|
82
|
+
String inspect() const;
|
83
|
+
|
84
|
+
//! Freeze the object.
|
85
|
+
void freeze();
|
86
|
+
|
87
|
+
//! Determine if the object is frozen.
|
88
|
+
/*! \return true if the object is frozen, false otherwise.
|
89
|
+
*/
|
90
|
+
bool is_frozen() const;
|
91
|
+
|
92
|
+
//! Swap with another Object.
|
93
|
+
void swap(Object & other);
|
94
|
+
|
95
|
+
//! Evaluate the given string in the context of the object.
|
96
|
+
/*! This is equivalant to calling obj.instance_eval(s) from inside the
|
97
|
+
* interpreter.
|
98
|
+
* \return the result of the expression.
|
99
|
+
*/
|
100
|
+
Object instance_eval(String const & s);
|
101
|
+
|
102
|
+
//! Return the type of the underlying C object.
|
103
|
+
/*! This is equivalent to calling rb_type(obj).
|
104
|
+
* \return the type of the underlying C object (e.g. T_DATA, T_ARRAY,
|
105
|
+
* etc.).
|
106
|
+
*/
|
107
|
+
int rb_type() const;
|
108
|
+
|
109
|
+
//! Determine whether the object is an instance of a class/module.
|
110
|
+
/*! \param klass a class or module.
|
111
|
+
* \return true if the object is an instance of the given
|
112
|
+
* class/module or one of its descendants.
|
113
|
+
*/
|
114
|
+
bool is_a(Object klass) const;
|
115
|
+
|
116
|
+
//! Determine if the objects responds to a method.
|
117
|
+
/*! \param id the name of the method
|
118
|
+
* \return true if the objects responds to the method, false
|
119
|
+
* otherwise.
|
120
|
+
*/
|
121
|
+
bool respond_to(Identifier id) const;
|
122
|
+
|
123
|
+
//! Determine whether class is the object's class.
|
124
|
+
/*! \param klass a class.
|
125
|
+
* \return true if the object is an instance of the given class.
|
126
|
+
*/
|
127
|
+
bool is_instance_of(Object klass) const;
|
128
|
+
|
129
|
+
//! Set an instance variable.
|
130
|
+
/*! \param name the name of the instance variable to set (including
|
131
|
+
* the leading @ sign)
|
132
|
+
* \param value the value of the variable, which will be converted to
|
133
|
+
* a Ruby type if necessary.
|
134
|
+
*/
|
135
|
+
template<typename T>
|
136
|
+
void iv_set(
|
137
|
+
Identifier name,
|
138
|
+
T const & value);
|
139
|
+
|
140
|
+
//! Get the value of an instance variable.
|
141
|
+
/*! \param name the name of the instance variable to get
|
142
|
+
* \return the value of the instance variable
|
143
|
+
*/
|
144
|
+
Object iv_get(
|
145
|
+
Identifier name) const;
|
146
|
+
|
147
|
+
//! Get the value of an instance variable, but don't warn if it is
|
148
|
+
//unset.
|
149
|
+
/*! \param name the name of the instance variable to get
|
150
|
+
* \return the value of the instance variable
|
151
|
+
*/
|
152
|
+
Object attr_get(
|
153
|
+
Identifier name) const;
|
154
|
+
|
155
|
+
//! Call the Ruby method specified by 'id' on object 'obj'.
|
156
|
+
/*! Pass in arguments (arg1, arg2, ...). The arguments will be converted to
|
157
|
+
* Ruby objects with to_ruby<>.
|
158
|
+
*
|
159
|
+
* E.g.:
|
160
|
+
* \code
|
161
|
+
* Rice::Object obj = x.call("foo", "one", 2);
|
162
|
+
* \endcode
|
163
|
+
*
|
164
|
+
* If a return type is specified, the return value will automatically be
|
165
|
+
* converted to that type as long as 'from_ruby' exists for that type.
|
166
|
+
*
|
167
|
+
* E.g.:
|
168
|
+
* \code
|
169
|
+
* float ret = x.call<float>("foo", z, 42);
|
170
|
+
* \endcode
|
171
|
+
*/
|
172
|
+
template<typename ...ArgT>
|
173
|
+
Object call(Identifier id, ArgT... args) const;
|
174
|
+
|
175
|
+
//! Vectorized call.
|
176
|
+
/*! Calls the method identified by id with the list of arguments
|
177
|
+
* identified by args.
|
178
|
+
* \param id the name of the method to call
|
179
|
+
* \param args the arguments to the method
|
180
|
+
* \return the return value of the method call
|
181
|
+
*/
|
182
|
+
Object vcall(Identifier id, Array args);
|
183
|
+
|
184
|
+
//! Mark the object with the garbage collector.
|
185
|
+
void mark() const;
|
186
|
+
|
187
|
+
protected:
|
188
|
+
//! Set the encapsulated value.
|
189
|
+
void set_value(VALUE v);
|
190
|
+
|
191
|
+
//! Unpack the provided arguments and convert them all to Ruby types.
|
192
|
+
template<typename ...ArgT>
|
193
|
+
std::vector<VALUE> convert_args(ArgT&... args) const;
|
194
|
+
|
195
|
+
private:
|
196
|
+
volatile VALUE value_;
|
197
|
+
};
|
198
|
+
|
199
|
+
std::ostream & operator<<(std::ostream & out, Object const & obj);
|
200
|
+
|
201
|
+
bool operator==(Object const & lhs, Object const & rhs);
|
202
|
+
bool operator!=(Object const & lhs, Object const & rhs);
|
203
|
+
bool operator<(Object const & lhs, Object const & rhs);
|
204
|
+
bool operator>(Object const & lhs, Object const & rhs);
|
205
|
+
|
206
|
+
extern Object const Nil;
|
207
|
+
extern Object const True;
|
208
|
+
extern Object const False;
|
209
|
+
extern Object const Undef;
|
210
|
+
|
211
|
+
} // namespace Rice
|
212
|
+
|
213
|
+
#endif // Rice__Object_defn__hpp_
|
214
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
#ifndef Rice__Require_Guard__hpp_
|
2
|
+
#define Rice__Require_Guard__hpp_
|
3
|
+
|
4
|
+
/*! \def RICE_REQUIRE_GUARD
|
5
|
+
* \brief Put this inside your Init_module function to keep it from
|
6
|
+
* being required more than once with the same name (if you use
|
7
|
+
* Rice, and your module is initialized more than once, an
|
8
|
+
* exception will be thrown).
|
9
|
+
*/
|
10
|
+
#define RICE_REQUIRE_GUARD \
|
11
|
+
static bool Rice__module_initialized = false; \
|
12
|
+
if(Rice__module_initialized) \
|
13
|
+
{ \
|
14
|
+
return; \
|
15
|
+
} \
|
16
|
+
/* TODO: If module initialization fails, it's not possible to */ \
|
17
|
+
/* retry */ \
|
18
|
+
Rice__module_initialized = true
|
19
|
+
|
20
|
+
#endif // Rice__Require_Guard__hpp_
|
21
|
+
|
data/rice/String.cpp
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
#include "String.hpp"
|
2
|
+
#include "Builtin_Object.hpp"
|
3
|
+
#include "protect.hpp"
|
4
|
+
#include "detail/ruby.hpp"
|
5
|
+
|
6
|
+
#ifdef HAVE_STDARG_PROTOTYPES
|
7
|
+
#include <stdarg.h>
|
8
|
+
#define va_init_list(a,b) va_start(a,b)
|
9
|
+
#else
|
10
|
+
#include <varargs.h>
|
11
|
+
#define va_init_list(a,b) va_start(a)
|
12
|
+
#endif
|
13
|
+
|
14
|
+
Rice::String::
|
15
|
+
String()
|
16
|
+
: Builtin_Object<T_STRING>(protect(rb_str_new2, ""))
|
17
|
+
{
|
18
|
+
}
|
19
|
+
|
20
|
+
Rice::String::
|
21
|
+
String(VALUE v)
|
22
|
+
: Builtin_Object<T_STRING>(v)
|
23
|
+
{
|
24
|
+
}
|
25
|
+
|
26
|
+
Rice::String::
|
27
|
+
String(Object v)
|
28
|
+
: Builtin_Object<T_STRING>(v)
|
29
|
+
{
|
30
|
+
}
|
31
|
+
|
32
|
+
Rice::String::
|
33
|
+
String(char const * s)
|
34
|
+
: Builtin_Object<T_STRING>(protect(rb_str_new2, s))
|
35
|
+
{
|
36
|
+
}
|
37
|
+
|
38
|
+
Rice::String::
|
39
|
+
String(std::string const & s)
|
40
|
+
: Builtin_Object<T_STRING>(protect(rb_str_new, s.data(), s.length()))
|
41
|
+
{
|
42
|
+
}
|
43
|
+
|
44
|
+
Rice::String::
|
45
|
+
String(Identifier id)
|
46
|
+
: Builtin_Object<T_STRING>(protect(rb_str_new2, id.c_str()))
|
47
|
+
{
|
48
|
+
}
|
49
|
+
|
50
|
+
Rice::String Rice::String::
|
51
|
+
format(char const * fmt, ...)
|
52
|
+
{
|
53
|
+
va_list args;
|
54
|
+
char buf[BUFSIZ];
|
55
|
+
|
56
|
+
va_init_list(args, fmt);
|
57
|
+
vsnprintf(buf, BUFSIZ, fmt, args);
|
58
|
+
buf[BUFSIZ - 1] = '\0';
|
59
|
+
va_end(args);
|
60
|
+
|
61
|
+
String s = String(buf);
|
62
|
+
return s;
|
63
|
+
}
|
64
|
+
|
65
|
+
size_t Rice::String::
|
66
|
+
length() const
|
67
|
+
{
|
68
|
+
return RSTRING_LEN(value());
|
69
|
+
}
|
70
|
+
|
71
|
+
char Rice::String::
|
72
|
+
operator[](ptrdiff_t index) const
|
73
|
+
{
|
74
|
+
return RSTRING_PTR(value())[index];
|
75
|
+
}
|
76
|
+
|
77
|
+
char const * Rice::String::
|
78
|
+
c_str() const
|
79
|
+
{
|
80
|
+
return RSTRING_PTR(value());
|
81
|
+
}
|
82
|
+
|
83
|
+
std::string Rice::String::
|
84
|
+
str() const
|
85
|
+
{
|
86
|
+
return std::string(RSTRING_PTR(value()), length());
|
87
|
+
}
|
88
|
+
|
89
|
+
Rice::Identifier Rice::String::
|
90
|
+
intern() const
|
91
|
+
{
|
92
|
+
return rb_intern(c_str());
|
93
|
+
}
|
94
|
+
|
data/rice/String.hpp
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
#ifndef Rice__String__hpp_
|
2
|
+
#define Rice__String__hpp_
|
3
|
+
|
4
|
+
#include "Identifier.hpp"
|
5
|
+
#include "Builtin_Object_defn.hpp"
|
6
|
+
#include "to_from_ruby_defn.hpp"
|
7
|
+
#include "detail/ruby.hpp"
|
8
|
+
#include <string>
|
9
|
+
|
10
|
+
namespace Rice
|
11
|
+
{
|
12
|
+
|
13
|
+
//! A Wraper for the ruby String class.
|
14
|
+
/*! This class provides a C++-style interface to ruby's String class and
|
15
|
+
* its associated rb_str_* functions.
|
16
|
+
*
|
17
|
+
* Example:
|
18
|
+
* \code
|
19
|
+
* String s(String::format("%s: %d", "foo", 42));
|
20
|
+
* std::cout << s.length() << std::endl;
|
21
|
+
* \endcode
|
22
|
+
*/
|
23
|
+
class String
|
24
|
+
: public Builtin_Object<T_STRING>
|
25
|
+
{
|
26
|
+
public:
|
27
|
+
//! Construct a new string.
|
28
|
+
String();
|
29
|
+
|
30
|
+
//! Wrap an existing string.
|
31
|
+
String(VALUE v);
|
32
|
+
|
33
|
+
//! Wrap an existing string.
|
34
|
+
String(Object v);
|
35
|
+
|
36
|
+
//! Construct a String from an Identifier.
|
37
|
+
String(Identifier id);
|
38
|
+
|
39
|
+
//! Construct a String from a null-terminated C string.
|
40
|
+
String(char const * s);
|
41
|
+
|
42
|
+
//! Construct a String from an std::string.
|
43
|
+
String(std::string const & s);
|
44
|
+
|
45
|
+
//! Format a string using printf-style formatting.
|
46
|
+
static String format(char const * s, ...);
|
47
|
+
|
48
|
+
//! Get the length of the String.
|
49
|
+
/*! \return the length of the string.
|
50
|
+
*/
|
51
|
+
size_t length() const;
|
52
|
+
|
53
|
+
//! Get the character at the given index.
|
54
|
+
/*! \param index the desired index.
|
55
|
+
* \return the character at the given index.
|
56
|
+
*/
|
57
|
+
char operator[](ptrdiff_t index) const;
|
58
|
+
|
59
|
+
//! Return a pointer to the beginning of the underlying C string.
|
60
|
+
char const * c_str() const;
|
61
|
+
|
62
|
+
//! Return a copy of the string as an std::string.
|
63
|
+
std::string str() const;
|
64
|
+
|
65
|
+
//! Create an Identifier from the String.
|
66
|
+
/*! Calls rb_intern to create an ID.
|
67
|
+
* \return an Identifier holding the ID returned from rb_intern.
|
68
|
+
*/
|
69
|
+
Identifier intern() const;
|
70
|
+
};
|
71
|
+
|
72
|
+
} // namespace Rice
|
73
|
+
|
74
|
+
template<>
|
75
|
+
inline
|
76
|
+
Rice::String from_ruby<Rice::String>(Rice::Object x)
|
77
|
+
{
|
78
|
+
return Rice::String(x);
|
79
|
+
}
|
80
|
+
|
81
|
+
template<>
|
82
|
+
inline
|
83
|
+
Rice::Object to_ruby<Rice::String>(Rice::String const & x)
|
84
|
+
{
|
85
|
+
return x;
|
86
|
+
}
|
87
|
+
|
88
|
+
#include "Builtin_Object.ipp"
|
89
|
+
|
90
|
+
#endif // Rice__String__hpp_
|
91
|
+
|