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/Exception.cpp
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
#include "Exception.hpp"
|
2
|
+
#include "protect.hpp"
|
3
|
+
#include "to_from_ruby.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::Exception::
|
15
|
+
Exception(VALUE e)
|
16
|
+
: Exception_Base(e)
|
17
|
+
, message_(Qnil)
|
18
|
+
, message_guard_(&message_)
|
19
|
+
{
|
20
|
+
}
|
21
|
+
|
22
|
+
Rice::Exception::
|
23
|
+
Exception(Exception const & other)
|
24
|
+
: Exception_Base(other)
|
25
|
+
, message_(other.message_)
|
26
|
+
, message_guard_(&message_)
|
27
|
+
{
|
28
|
+
}
|
29
|
+
|
30
|
+
Rice::Exception::
|
31
|
+
Exception(Object exc, char const * fmt, ...)
|
32
|
+
: Exception_Base(Qnil)
|
33
|
+
, message_(Qnil)
|
34
|
+
, message_guard_(&message_)
|
35
|
+
{
|
36
|
+
va_list args;
|
37
|
+
char buf[BUFSIZ];
|
38
|
+
|
39
|
+
va_init_list(args, fmt);
|
40
|
+
vsnprintf(buf, BUFSIZ, fmt, args);
|
41
|
+
buf[BUFSIZ - 1] = '\0';
|
42
|
+
va_end(args);
|
43
|
+
|
44
|
+
set_value(protect(rb_exc_new2, exc, buf));
|
45
|
+
}
|
46
|
+
|
47
|
+
Rice::String Rice::Exception::
|
48
|
+
message() const
|
49
|
+
{
|
50
|
+
return protect(rb_funcall, value(), rb_intern("message"), 0);
|
51
|
+
}
|
52
|
+
|
53
|
+
char const * Rice::Exception::
|
54
|
+
what() const throw()
|
55
|
+
{
|
56
|
+
message_ = message();
|
57
|
+
return from_ruby<std::string>(message_).c_str();
|
58
|
+
}
|
59
|
+
|
data/rice/Exception.hpp
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
#ifndef Rice__Exception__hpp_
|
2
|
+
#define Rice__Exception__hpp_
|
3
|
+
|
4
|
+
namespace Rice {
|
5
|
+
class Exception;
|
6
|
+
}
|
7
|
+
|
8
|
+
#include "Exception_defn.hpp"
|
9
|
+
#include "Exception_Base.hpp"
|
10
|
+
#include "Address_Registration_Guard.hpp"
|
11
|
+
|
12
|
+
#endif // Rice__Exception__hpp_
|
13
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
#ifndef Rice__Exception_Base_defn__hpp_
|
2
|
+
#define Rice__Exception_Base_defn__hpp_
|
3
|
+
|
4
|
+
#include "Object_defn.hpp"
|
5
|
+
|
6
|
+
namespace Rice
|
7
|
+
{
|
8
|
+
|
9
|
+
//! An abstract interface for Exception
|
10
|
+
/*! This class exists to prevent a circular reference between
|
11
|
+
* Exception.hpp and ruby_try_catch.hpp
|
12
|
+
*/
|
13
|
+
class Exception_Base
|
14
|
+
: public std::exception
|
15
|
+
, public Object
|
16
|
+
{
|
17
|
+
public:
|
18
|
+
Exception_Base(VALUE v);
|
19
|
+
|
20
|
+
virtual ~Exception_Base() throw() = 0;
|
21
|
+
|
22
|
+
virtual char const * what() const throw() = 0;
|
23
|
+
};
|
24
|
+
|
25
|
+
} // Rice
|
26
|
+
|
27
|
+
#endif // Rice__Exception_Base_defn__hpp_
|
@@ -0,0 +1,69 @@
|
|
1
|
+
#ifndef Rice__Exception_defn__hpp_
|
2
|
+
#define Rice__Exception_defn__hpp_
|
3
|
+
|
4
|
+
#include "Exception_Base_defn.hpp"
|
5
|
+
#include "String.hpp"
|
6
|
+
#include "Address_Registration_Guard_defn.hpp"
|
7
|
+
|
8
|
+
#include <stdexcept>
|
9
|
+
#include "detail/ruby.hpp"
|
10
|
+
|
11
|
+
namespace Rice
|
12
|
+
{
|
13
|
+
|
14
|
+
//! A placeholder for Ruby exceptions.
|
15
|
+
/*! You can use this to safely throw a Ruby exception using C++ syntax:
|
16
|
+
* \code
|
17
|
+
* VALUE foo(VALUE self) {
|
18
|
+
* RUBY_TRY {
|
19
|
+
* throw Rice::Exception(rb_eMyException, "uh oh!");
|
20
|
+
* RUBY_CATCH
|
21
|
+
* }
|
22
|
+
* \endcode
|
23
|
+
*/
|
24
|
+
class Exception
|
25
|
+
: public Exception_Base
|
26
|
+
{
|
27
|
+
public:
|
28
|
+
//! Construct a Exception with the exception e.
|
29
|
+
explicit Exception(VALUE e);
|
30
|
+
|
31
|
+
//! Copy constructor.
|
32
|
+
Exception(Exception const & other);
|
33
|
+
|
34
|
+
//! Construct a Exception with printf-style formatting.
|
35
|
+
/*! \param exc either an exception object or a class that inherits
|
36
|
+
* from Exception.
|
37
|
+
* \param fmt a printf-style format string
|
38
|
+
* \param ... the arguments to the format string.
|
39
|
+
*/
|
40
|
+
Exception(Object exc, char const * fmt, ...);
|
41
|
+
|
42
|
+
//! Destructor
|
43
|
+
virtual ~Exception() throw() { }
|
44
|
+
|
45
|
+
//! Get the message the exception holds
|
46
|
+
/*! \return the result of calling message() on the underlying
|
47
|
+
* exception object.
|
48
|
+
*/
|
49
|
+
String message() const;
|
50
|
+
|
51
|
+
//! Get message as a char const *.
|
52
|
+
/*! If message is a non-string object, then this function will attempt
|
53
|
+
* to throw an exception (which it can't do because of the no-throw
|
54
|
+
* specification).
|
55
|
+
* \return the underlying C pointer of the underlying message object.
|
56
|
+
*/
|
57
|
+
virtual char const * what() const throw();
|
58
|
+
|
59
|
+
private:
|
60
|
+
// Keep message around in case someone calls what() and then the GC
|
61
|
+
// gets invoked.
|
62
|
+
mutable VALUE message_;
|
63
|
+
Address_Registration_Guard message_guard_;
|
64
|
+
};
|
65
|
+
|
66
|
+
} // namespace Rice
|
67
|
+
|
68
|
+
#endif // Rice__Exception_defn__hpp_
|
69
|
+
|
data/rice/Hash.hpp
ADDED
@@ -0,0 +1,227 @@
|
|
1
|
+
#ifndef Rice__Hash__hpp_
|
2
|
+
#define Rice__Hash__hpp_
|
3
|
+
|
4
|
+
#include "Builtin_Object_defn.hpp"
|
5
|
+
#include "Array.hpp"
|
6
|
+
#include "to_from_ruby_defn.hpp"
|
7
|
+
#include "detail/ruby.hpp"
|
8
|
+
#include "detail/traits.hpp"
|
9
|
+
#include <iterator>
|
10
|
+
|
11
|
+
namespace Rice
|
12
|
+
{
|
13
|
+
|
14
|
+
//! A wrapper for the ruby Hash class.
|
15
|
+
//! This class provides a C++-style interface to ruby's Hash class and
|
16
|
+
//! its associated rb_hash_* functions.
|
17
|
+
//! Example:
|
18
|
+
//! \code
|
19
|
+
//! Hash h;
|
20
|
+
//! h[42] = String("foo");
|
21
|
+
//! h[10] = String("bar");
|
22
|
+
//! std::cout << String(h[42]) << std::endl;
|
23
|
+
//! \endcode
|
24
|
+
class Hash
|
25
|
+
: public Builtin_Object<T_HASH>
|
26
|
+
{
|
27
|
+
public:
|
28
|
+
//! Construct a new hash.
|
29
|
+
Hash();
|
30
|
+
|
31
|
+
//! Wrap an existing hash.
|
32
|
+
/*! \param v the hash to wrap.
|
33
|
+
*/
|
34
|
+
Hash(Object v);
|
35
|
+
|
36
|
+
//! Return the number of elements in the hash.
|
37
|
+
size_t size() const;
|
38
|
+
|
39
|
+
private:
|
40
|
+
//! A helper class so hash[key]=value can work.
|
41
|
+
class Proxy;
|
42
|
+
|
43
|
+
public:
|
44
|
+
//! Get the value for the given key.
|
45
|
+
/*! \param key the key whose value should be retrieved from the hash.
|
46
|
+
* \return the value associated with the given key.
|
47
|
+
*/
|
48
|
+
template<typename Key_T>
|
49
|
+
Proxy const operator[](Key_T const & key) const;
|
50
|
+
|
51
|
+
//! Get the value for the given key.
|
52
|
+
/*! \param key the key whose value should be retrieved from the hash.
|
53
|
+
* \return the value associated with the given key.
|
54
|
+
*/
|
55
|
+
template<typename Key_T>
|
56
|
+
Proxy operator[](Key_T const & key);
|
57
|
+
|
58
|
+
//! Get the value for the given key
|
59
|
+
/*! The returned value is converted to the type given by Value_T.
|
60
|
+
* \param key the key whose value should be retrieved from the hash.
|
61
|
+
* \return the value associated with the given key, converted to C++
|
62
|
+
* type Value_T.
|
63
|
+
*/
|
64
|
+
template<typename Value_T, typename Key_T>
|
65
|
+
Value_T get(Key_T const & key);
|
66
|
+
|
67
|
+
//! A helper class for dereferencing iterators
|
68
|
+
class Entry;
|
69
|
+
|
70
|
+
//! A helper class for implementing iterators for a Hash.
|
71
|
+
template<typename Hash_Ref_T, typename Value_T>
|
72
|
+
class Iterator;
|
73
|
+
|
74
|
+
public:
|
75
|
+
//! An iterator.
|
76
|
+
typedef Iterator<Hash &, Entry> iterator;
|
77
|
+
|
78
|
+
//! A const iterator.
|
79
|
+
typedef Iterator<Hash const &, Entry const> const_iterator;
|
80
|
+
|
81
|
+
public:
|
82
|
+
//! Return an iterator to the beginning of the hash.
|
83
|
+
iterator begin();
|
84
|
+
|
85
|
+
//! Return a const iterator to the beginning of the hash.
|
86
|
+
const_iterator begin() const;
|
87
|
+
|
88
|
+
//! Return an iterator to the end of the hash.
|
89
|
+
iterator end();
|
90
|
+
|
91
|
+
//! Return a const to the end of the hash.
|
92
|
+
const_iterator end() const;
|
93
|
+
};
|
94
|
+
|
95
|
+
//! A helper class so hash[key]=value can work.
|
96
|
+
class Hash::Proxy
|
97
|
+
{
|
98
|
+
public:
|
99
|
+
//! Construct a new Proxy.
|
100
|
+
Proxy(Hash hash, Object key);
|
101
|
+
|
102
|
+
//! Implicit conversion to Object.
|
103
|
+
operator Object() const;
|
104
|
+
|
105
|
+
//! Explicit conversion to VALUE.
|
106
|
+
VALUE value() const;
|
107
|
+
|
108
|
+
//! Assignment operator.
|
109
|
+
template<typename T>
|
110
|
+
Object operator=(T const & value);
|
111
|
+
|
112
|
+
void swap(Proxy & proxy);
|
113
|
+
|
114
|
+
private:
|
115
|
+
Hash hash_;
|
116
|
+
Object key_;
|
117
|
+
};
|
118
|
+
|
119
|
+
//! A helper class for dereferencing iterators
|
120
|
+
/*! This class is intended to look similar to an std::pair.
|
121
|
+
*/
|
122
|
+
class Hash::Entry
|
123
|
+
{
|
124
|
+
public:
|
125
|
+
//! Construct a new Entry.
|
126
|
+
Entry(Hash hash, Object key);
|
127
|
+
|
128
|
+
//! Copy constructor.
|
129
|
+
Entry(Entry const & entry);
|
130
|
+
|
131
|
+
Object const key; //!< The key
|
132
|
+
Object const & first; //!< An alias for the key
|
133
|
+
|
134
|
+
Proxy value; //!< The value
|
135
|
+
Proxy & second; //!< An alias for the value
|
136
|
+
|
137
|
+
Entry & operator=(Entry const & rhs);
|
138
|
+
|
139
|
+
void swap(Entry & entry);
|
140
|
+
|
141
|
+
friend bool operator<(Entry const & lhs, Entry const & rhs);
|
142
|
+
};
|
143
|
+
|
144
|
+
bool operator<(Hash::Entry const & lhs, Hash::Entry const & rhs);
|
145
|
+
|
146
|
+
//! A helper class for implementing iterators for a Hash.
|
147
|
+
template<typename Hash_Ref_T, typename Value_T>
|
148
|
+
class Hash::Iterator
|
149
|
+
: public std::iterator<
|
150
|
+
std::input_iterator_tag,
|
151
|
+
Value_T>
|
152
|
+
{
|
153
|
+
public:
|
154
|
+
//! Construct a new Iterator.
|
155
|
+
Iterator(Hash_Ref_T hash);
|
156
|
+
|
157
|
+
//! Construct a new Iterator with a given start-at index point
|
158
|
+
Iterator(Hash_Ref_T hash, int start_at);
|
159
|
+
|
160
|
+
//! Copy construct an Iterator.
|
161
|
+
Iterator(Iterator const & iterator);
|
162
|
+
|
163
|
+
//! Construct an Iterator from another Iterator of a different const
|
164
|
+
//! qualification.
|
165
|
+
template<typename Iterator_T>
|
166
|
+
Iterator(Iterator_T const & iterator);
|
167
|
+
|
168
|
+
//! Assignment operator.
|
169
|
+
Iterator & operator=(Iterator const & rhs);
|
170
|
+
|
171
|
+
//! Preincrement operator.
|
172
|
+
Iterator & operator++();
|
173
|
+
|
174
|
+
//! Postincrement operator.
|
175
|
+
Iterator operator++(int);
|
176
|
+
|
177
|
+
//! Dereference operator.
|
178
|
+
Value_T operator*();
|
179
|
+
|
180
|
+
//! Dereference operator.
|
181
|
+
Value_T * operator->();
|
182
|
+
|
183
|
+
//! Equality operator.
|
184
|
+
bool operator==(Iterator const & rhs) const;
|
185
|
+
|
186
|
+
//! Inequality operator.
|
187
|
+
bool operator!=(Iterator const & rhs) const;
|
188
|
+
|
189
|
+
template<typename Hash_Ref_T_, typename Value_T_>
|
190
|
+
friend class Hash::Iterator;
|
191
|
+
|
192
|
+
//! Swap with another iterator of the same type.
|
193
|
+
void swap(Iterator & iterator);
|
194
|
+
|
195
|
+
protected:
|
196
|
+
Object current_key();
|
197
|
+
|
198
|
+
Array hash_keys();
|
199
|
+
|
200
|
+
private:
|
201
|
+
Hash hash_;
|
202
|
+
size_t current_index_;
|
203
|
+
VALUE keys_;
|
204
|
+
|
205
|
+
mutable typename detail::remove_const<Value_T>::Type tmp_;
|
206
|
+
};
|
207
|
+
|
208
|
+
} // namespace Rice
|
209
|
+
|
210
|
+
template<>
|
211
|
+
inline
|
212
|
+
Rice::Hash from_ruby<Rice::Hash>(Rice::Object x)
|
213
|
+
{
|
214
|
+
return Rice::Hash(x);
|
215
|
+
}
|
216
|
+
|
217
|
+
template<>
|
218
|
+
inline
|
219
|
+
Rice::Object to_ruby<Rice::Hash>(Rice::Hash const & x)
|
220
|
+
{
|
221
|
+
return x;
|
222
|
+
}
|
223
|
+
|
224
|
+
#include "Hash.ipp"
|
225
|
+
|
226
|
+
#endif // Rice__Hash__hpp_
|
227
|
+
|
data/rice/Hash.ipp
ADDED
@@ -0,0 +1,329 @@
|
|
1
|
+
#ifndef Rice__Hash__ipp_
|
2
|
+
#define Rice__Hash__ipp_
|
3
|
+
|
4
|
+
#include "protect.hpp"
|
5
|
+
#include "to_from_ruby.hpp"
|
6
|
+
#include "Exception.hpp"
|
7
|
+
#include "Builtin_Object.hpp"
|
8
|
+
#include "detail/st.hpp"
|
9
|
+
#include <algorithm>
|
10
|
+
|
11
|
+
inline Rice::Hash::
|
12
|
+
Hash()
|
13
|
+
: Builtin_Object<T_HASH>(protect(rb_hash_new))
|
14
|
+
{
|
15
|
+
}
|
16
|
+
|
17
|
+
inline Rice::Hash::
|
18
|
+
Hash(Object v)
|
19
|
+
: Builtin_Object<T_HASH>(v)
|
20
|
+
{
|
21
|
+
}
|
22
|
+
|
23
|
+
inline size_t Rice::Hash::
|
24
|
+
size() const
|
25
|
+
{
|
26
|
+
return RHASH_SIZE(this->value());
|
27
|
+
}
|
28
|
+
|
29
|
+
inline Rice::Hash::Proxy::
|
30
|
+
Proxy(Hash hash, Object key)
|
31
|
+
: hash_(hash)
|
32
|
+
, key_(key)
|
33
|
+
{
|
34
|
+
}
|
35
|
+
|
36
|
+
/*
|
37
|
+
inline Rice::Hash::Proxy::
|
38
|
+
operator VALUE() const
|
39
|
+
{
|
40
|
+
return value();
|
41
|
+
}
|
42
|
+
*/
|
43
|
+
|
44
|
+
inline Rice::Hash::Proxy::
|
45
|
+
operator Rice::Object() const
|
46
|
+
{
|
47
|
+
return value();
|
48
|
+
}
|
49
|
+
|
50
|
+
inline VALUE Rice::Hash::Proxy::
|
51
|
+
value() const
|
52
|
+
{
|
53
|
+
return protect(rb_hash_aref, hash_, key_);
|
54
|
+
}
|
55
|
+
|
56
|
+
template<typename T>
|
57
|
+
inline Rice::Object Rice::Hash::Proxy::
|
58
|
+
operator=(T const & value)
|
59
|
+
{
|
60
|
+
return protect(rb_hash_aset, hash_, key_, to_ruby(value));
|
61
|
+
}
|
62
|
+
|
63
|
+
inline void Rice::Hash::Proxy::
|
64
|
+
swap(Proxy & proxy)
|
65
|
+
{
|
66
|
+
hash_.swap(proxy.hash_);
|
67
|
+
key_.swap(proxy.key_);
|
68
|
+
}
|
69
|
+
|
70
|
+
template<typename Key_T>
|
71
|
+
inline Rice::Hash::Proxy const Rice::Hash::
|
72
|
+
operator[](Key_T const & key) const
|
73
|
+
{
|
74
|
+
return Proxy(*this, to_ruby(key));
|
75
|
+
}
|
76
|
+
|
77
|
+
template<typename Key_T>
|
78
|
+
inline Rice::Hash::Proxy Rice::Hash::
|
79
|
+
operator[](Key_T const & key)
|
80
|
+
{
|
81
|
+
return Proxy(*this, to_ruby(key));
|
82
|
+
}
|
83
|
+
|
84
|
+
template<typename Value_T, typename Key_T>
|
85
|
+
inline Value_T Rice::Hash::
|
86
|
+
get(Key_T const & key)
|
87
|
+
{
|
88
|
+
Object ruby_key(to_ruby(key));
|
89
|
+
Object value = operator[](ruby_key);
|
90
|
+
try
|
91
|
+
{
|
92
|
+
return from_ruby<Value_T>(value);
|
93
|
+
}
|
94
|
+
catch(Exception const & ex)
|
95
|
+
{
|
96
|
+
String s_key(ruby_key.to_s());
|
97
|
+
throw Exception(
|
98
|
+
ex,
|
99
|
+
"%s while converting value for key %s",
|
100
|
+
ex.what(),
|
101
|
+
s_key.c_str());
|
102
|
+
}
|
103
|
+
}
|
104
|
+
|
105
|
+
inline Rice::Hash::Entry::
|
106
|
+
Entry(Hash hash, Object key)
|
107
|
+
: key(key)
|
108
|
+
, first(Hash::Entry::key)
|
109
|
+
, value(hash, key)
|
110
|
+
, second(Hash::Entry::value)
|
111
|
+
{
|
112
|
+
}
|
113
|
+
|
114
|
+
inline Rice::Hash::Entry::
|
115
|
+
Entry(Entry const & entry)
|
116
|
+
: key(entry.key)
|
117
|
+
, first(Hash::Entry::key)
|
118
|
+
, value(entry.value)
|
119
|
+
, second(Hash::Entry::value)
|
120
|
+
{
|
121
|
+
}
|
122
|
+
|
123
|
+
inline Rice::Hash::Entry & Rice::Hash::Entry::
|
124
|
+
operator=(Rice::Hash::Entry const & rhs)
|
125
|
+
{
|
126
|
+
Entry tmp(rhs);
|
127
|
+
swap(tmp);
|
128
|
+
return *this;
|
129
|
+
}
|
130
|
+
|
131
|
+
inline void Rice::Hash::Entry::
|
132
|
+
swap(Rice::Hash::Entry & entry)
|
133
|
+
{
|
134
|
+
const_cast<Object &>(key).swap(const_cast<Object &>(entry.key));
|
135
|
+
value.swap(entry.value);
|
136
|
+
}
|
137
|
+
|
138
|
+
template<typename Hash_Ref_T, typename Value_T>
|
139
|
+
inline Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
|
140
|
+
Iterator(Hash_Ref_T hash)
|
141
|
+
: hash_(hash)
|
142
|
+
, current_index_(0)
|
143
|
+
, keys_(Qnil)
|
144
|
+
, tmp_(hash, Qnil)
|
145
|
+
{
|
146
|
+
}
|
147
|
+
|
148
|
+
template<typename Hash_Ref_T, typename Value_T>
|
149
|
+
inline Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
|
150
|
+
Iterator(Hash_Ref_T hash, int start_at)
|
151
|
+
: hash_(hash)
|
152
|
+
, current_index_(start_at)
|
153
|
+
, keys_(Qnil)
|
154
|
+
, tmp_(hash, Qnil)
|
155
|
+
{
|
156
|
+
}
|
157
|
+
|
158
|
+
template<typename Hash_Ref_T, typename Value_T>
|
159
|
+
inline Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
|
160
|
+
Iterator(Iterator const & iterator)
|
161
|
+
: hash_(iterator.hash_.value())
|
162
|
+
, current_index_(iterator.current_index_)
|
163
|
+
, keys_(Qnil)
|
164
|
+
, tmp_(iterator.hash_, Qnil)
|
165
|
+
{
|
166
|
+
}
|
167
|
+
|
168
|
+
template<typename Hash_Ref_T, typename Value_T>
|
169
|
+
template<typename Iterator_T>
|
170
|
+
inline Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
|
171
|
+
Iterator(Iterator_T const & iterator)
|
172
|
+
: hash_(iterator.hash_.value())
|
173
|
+
, current_index_(iterator.current_index_)
|
174
|
+
, keys_(Qnil)
|
175
|
+
, tmp_(iterator.hash_, Qnil)
|
176
|
+
{
|
177
|
+
}
|
178
|
+
|
179
|
+
template<typename Hash_Ref_T, typename Value_T>
|
180
|
+
inline Rice::Hash::Iterator<Hash_Ref_T, Value_T> &
|
181
|
+
Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
|
182
|
+
operator=(Iterator const & iterator)
|
183
|
+
{
|
184
|
+
Iterator tmp(iterator);
|
185
|
+
|
186
|
+
this->swap(tmp);
|
187
|
+
|
188
|
+
return *this;
|
189
|
+
}
|
190
|
+
|
191
|
+
template<typename Hash_Ref_T, typename Value_T>
|
192
|
+
inline Rice::Hash::Iterator<Hash_Ref_T, Value_T> &
|
193
|
+
Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
|
194
|
+
operator++()
|
195
|
+
{
|
196
|
+
// Ensure we're within the range
|
197
|
+
if(current_index_ < hash_keys().size()) {
|
198
|
+
current_index_++;
|
199
|
+
}
|
200
|
+
|
201
|
+
return *this;
|
202
|
+
}
|
203
|
+
|
204
|
+
template<typename Hash_Ref_T, typename Value_T>
|
205
|
+
inline Rice::Hash::Iterator<Hash_Ref_T, Value_T>
|
206
|
+
Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
|
207
|
+
operator++(int)
|
208
|
+
{
|
209
|
+
Iterator copy(*this);
|
210
|
+
++(*this);
|
211
|
+
return copy;
|
212
|
+
}
|
213
|
+
|
214
|
+
template<typename Hash_Ref_T, typename Value_T>
|
215
|
+
inline Value_T
|
216
|
+
Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
|
217
|
+
operator*()
|
218
|
+
{
|
219
|
+
return Value_T(hash_, current_key());
|
220
|
+
}
|
221
|
+
|
222
|
+
template<typename Hash_Ref_T, typename Value_T>
|
223
|
+
inline Value_T *
|
224
|
+
Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
|
225
|
+
operator->()
|
226
|
+
{
|
227
|
+
Entry tmp(hash_, current_key());
|
228
|
+
this->tmp_.swap(tmp);
|
229
|
+
return &tmp_;
|
230
|
+
}
|
231
|
+
|
232
|
+
template<typename Hash_Ref_T, typename Value_T>
|
233
|
+
inline bool Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
|
234
|
+
operator==(Iterator const & rhs) const
|
235
|
+
{
|
236
|
+
return hash_.value() == rhs.hash_.value()
|
237
|
+
&& current_index_ == rhs.current_index_;
|
238
|
+
}
|
239
|
+
|
240
|
+
template<typename Hash_Ref_T, typename Value_T>
|
241
|
+
inline bool Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
|
242
|
+
operator!=(Iterator const & rhs) const
|
243
|
+
{
|
244
|
+
return !(*this == rhs);
|
245
|
+
}
|
246
|
+
|
247
|
+
template<typename Hash_Ref_T, typename Value_T>
|
248
|
+
inline void
|
249
|
+
Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
|
250
|
+
swap(Iterator& iterator)
|
251
|
+
{
|
252
|
+
using namespace std;
|
253
|
+
|
254
|
+
hash_.swap(iterator.hash_);
|
255
|
+
swap(keys_, iterator.keys_);
|
256
|
+
swap(current_index_, iterator.current_index_);
|
257
|
+
}
|
258
|
+
|
259
|
+
template<typename Hash_Ref_T, typename Value_T>
|
260
|
+
inline Rice::Object
|
261
|
+
Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
|
262
|
+
current_key()
|
263
|
+
{
|
264
|
+
return hash_keys()[current_index_];
|
265
|
+
}
|
266
|
+
|
267
|
+
|
268
|
+
template<typename Hash_Ref_T, typename Value_T>
|
269
|
+
inline Rice::Array
|
270
|
+
Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
|
271
|
+
hash_keys()
|
272
|
+
{
|
273
|
+
if(NIL_P(keys_)) {
|
274
|
+
keys_ = rb_funcall(hash_, rb_intern("keys"), 0, 0);
|
275
|
+
}
|
276
|
+
|
277
|
+
return Rice::Array(keys_);
|
278
|
+
}
|
279
|
+
|
280
|
+
inline Rice::Hash::iterator Rice::Hash::
|
281
|
+
begin()
|
282
|
+
{
|
283
|
+
return iterator(*this);
|
284
|
+
}
|
285
|
+
|
286
|
+
inline Rice::Hash::const_iterator Rice::Hash::
|
287
|
+
begin() const
|
288
|
+
{
|
289
|
+
return const_iterator(*this);
|
290
|
+
}
|
291
|
+
|
292
|
+
inline Rice::Hash::iterator Rice::Hash::
|
293
|
+
end()
|
294
|
+
{
|
295
|
+
return iterator(*this, (int)size());
|
296
|
+
}
|
297
|
+
|
298
|
+
inline Rice::Hash::const_iterator Rice::Hash::
|
299
|
+
end() const
|
300
|
+
{
|
301
|
+
return const_iterator(*this, (int)size());
|
302
|
+
}
|
303
|
+
|
304
|
+
inline bool Rice::
|
305
|
+
operator<(
|
306
|
+
Hash::Entry const & lhs, Hash::Entry const & rhs)
|
307
|
+
{
|
308
|
+
Object lhs_key(lhs.key);
|
309
|
+
Object rhs_key(rhs.key);
|
310
|
+
if(lhs_key < rhs_key)
|
311
|
+
{
|
312
|
+
return true;
|
313
|
+
}
|
314
|
+
else if(lhs_key > rhs_key)
|
315
|
+
{
|
316
|
+
return false;
|
317
|
+
}
|
318
|
+
else if(Object(lhs.value.value()) < Object(rhs.value.value()))
|
319
|
+
{
|
320
|
+
return true;
|
321
|
+
}
|
322
|
+
else
|
323
|
+
{
|
324
|
+
return false;
|
325
|
+
}
|
326
|
+
}
|
327
|
+
|
328
|
+
#endif // Rice__Hash__ipp_
|
329
|
+
|