jameskilton-rice 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (196) hide show
  1. data/COPYING +23 -0
  2. data/Doxyfile +1253 -0
  3. data/Makefile.am +26 -0
  4. data/Makefile.in +749 -0
  5. data/README +879 -0
  6. data/README.mingw +8 -0
  7. data/Rakefile +72 -0
  8. data/aclocal.m4 +891 -0
  9. data/bootstrap +8 -0
  10. data/config.guess +1526 -0
  11. data/config.sub +1658 -0
  12. data/configure +7668 -0
  13. data/configure.ac +52 -0
  14. data/depcomp +589 -0
  15. data/doxygen.ac +314 -0
  16. data/doxygen.am +186 -0
  17. data/extconf.rb +22 -0
  18. data/install-sh +519 -0
  19. data/missing +367 -0
  20. data/post-autoconf.rb +22 -0
  21. data/post-automake.rb +28 -0
  22. data/rice/Address_Registration_Guard.hpp +7 -0
  23. data/rice/Address_Registration_Guard.ipp +34 -0
  24. data/rice/Address_Registration_Guard_defn.hpp +65 -0
  25. data/rice/Allocation_Strategies.hpp +37 -0
  26. data/rice/Arg.hpp +8 -0
  27. data/rice/Arg_impl.hpp +116 -0
  28. data/rice/Arg_operators.cpp +21 -0
  29. data/rice/Arg_operators.hpp +18 -0
  30. data/rice/Array.hpp +220 -0
  31. data/rice/Array.ipp +263 -0
  32. data/rice/Builtin_Object.hpp +8 -0
  33. data/rice/Builtin_Object.ipp +50 -0
  34. data/rice/Builtin_Object_defn.hpp +51 -0
  35. data/rice/Class.cpp +57 -0
  36. data/rice/Class.hpp +8 -0
  37. data/rice/Class.ipp +6 -0
  38. data/rice/Class_defn.hpp +83 -0
  39. data/rice/Constructor.hpp +339 -0
  40. data/rice/Critical_Guard.hpp +40 -0
  41. data/rice/Critical_Guard.ipp +26 -0
  42. data/rice/Data_Object.hpp +8 -0
  43. data/rice/Data_Object.ipp +133 -0
  44. data/rice/Data_Object_defn.hpp +132 -0
  45. data/rice/Data_Type.cpp +21 -0
  46. data/rice/Data_Type.hpp +8 -0
  47. data/rice/Data_Type.ipp +306 -0
  48. data/rice/Data_Type_defn.hpp +219 -0
  49. data/rice/Data_Type_fwd.hpp +12 -0
  50. data/rice/Director.cpp +19 -0
  51. data/rice/Director.hpp +47 -0
  52. data/rice/Enum.hpp +118 -0
  53. data/rice/Enum.ipp +246 -0
  54. data/rice/Exception.cpp +59 -0
  55. data/rice/Exception.hpp +9 -0
  56. data/rice/Exception_Base.hpp +8 -0
  57. data/rice/Exception_Base.ipp +13 -0
  58. data/rice/Exception_Base_defn.hpp +27 -0
  59. data/rice/Exception_defn.hpp +69 -0
  60. data/rice/Hash.hpp +210 -0
  61. data/rice/Hash.ipp +338 -0
  62. data/rice/Identifier.cpp +8 -0
  63. data/rice/Identifier.hpp +50 -0
  64. data/rice/Identifier.ipp +33 -0
  65. data/rice/Jump_Tag.hpp +24 -0
  66. data/rice/Makefile.am +140 -0
  67. data/rice/Makefile.in +740 -0
  68. data/rice/Module.cpp +84 -0
  69. data/rice/Module.hpp +8 -0
  70. data/rice/Module.ipp +6 -0
  71. data/rice/Module_defn.hpp +88 -0
  72. data/rice/Module_impl.hpp +281 -0
  73. data/rice/Module_impl.ipp +348 -0
  74. data/rice/Object.cpp +153 -0
  75. data/rice/Object.hpp +8 -0
  76. data/rice/Object.ipp +19 -0
  77. data/rice/Object_defn.hpp +183 -0
  78. data/rice/Require_Guard.hpp +21 -0
  79. data/rice/String.cpp +94 -0
  80. data/rice/String.hpp +89 -0
  81. data/rice/Struct.cpp +117 -0
  82. data/rice/Struct.hpp +162 -0
  83. data/rice/Struct.ipp +26 -0
  84. data/rice/Symbol.cpp +25 -0
  85. data/rice/Symbol.hpp +66 -0
  86. data/rice/Symbol.ipp +44 -0
  87. data/rice/VM.cpp +92 -0
  88. data/rice/VM.hpp +32 -0
  89. data/rice/config.hpp +44 -0
  90. data/rice/config.hpp.in +43 -0
  91. data/rice/detail/Arguments.hpp +126 -0
  92. data/rice/detail/Auto_Function_Wrapper.hpp +861 -0
  93. data/rice/detail/Auto_Function_Wrapper.ipp +2929 -0
  94. data/rice/detail/Auto_Member_Function_Wrapper.hpp +828 -0
  95. data/rice/detail/Auto_Member_Function_Wrapper.ipp +2326 -0
  96. data/rice/detail/Caster.hpp +63 -0
  97. data/rice/detail/Exception_Handler.hpp +8 -0
  98. data/rice/detail/Exception_Handler.ipp +68 -0
  99. data/rice/detail/Exception_Handler_defn.hpp +96 -0
  100. data/rice/detail/Iterator.hpp +93 -0
  101. data/rice/detail/Not_Copyable.hpp +25 -0
  102. data/rice/detail/Wrapped_Function.hpp +33 -0
  103. data/rice/detail/check_ruby_type.cpp +27 -0
  104. data/rice/detail/check_ruby_type.hpp +23 -0
  105. data/rice/detail/creation_funcs.hpp +37 -0
  106. data/rice/detail/creation_funcs.ipp +36 -0
  107. data/rice/detail/default_allocation_func.hpp +23 -0
  108. data/rice/detail/default_allocation_func.ipp +11 -0
  109. data/rice/detail/define_method_and_auto_wrap.hpp +31 -0
  110. data/rice/detail/define_method_and_auto_wrap.ipp +30 -0
  111. data/rice/detail/demangle.cpp +56 -0
  112. data/rice/detail/demangle.hpp +19 -0
  113. data/rice/detail/env.hpp +19 -0
  114. data/rice/detail/from_ruby.hpp +43 -0
  115. data/rice/detail/from_ruby.ipp +60 -0
  116. data/rice/detail/method_data.cpp +392 -0
  117. data/rice/detail/method_data.cpp.rpp +301 -0
  118. data/rice/detail/method_data.hpp +21 -0
  119. data/rice/detail/mininode.cpp +1220 -0
  120. data/rice/detail/mininode.cpp.rpp +62 -0
  121. data/rice/detail/mininode.hpp +320 -0
  122. data/rice/detail/mininode.hpp.rpp +119 -0
  123. data/rice/detail/node.hpp +13 -0
  124. data/rice/detail/object_call.hpp +85 -0
  125. data/rice/detail/object_call.ipp +147 -0
  126. data/rice/detail/protect.cpp +29 -0
  127. data/rice/detail/protect.hpp +34 -0
  128. data/rice/detail/remove_const.hpp +21 -0
  129. data/rice/detail/ruby.hpp +89 -0
  130. data/rice/detail/ruby_version_code.hpp +6 -0
  131. data/rice/detail/ruby_version_code.hpp.in +6 -0
  132. data/rice/detail/rubysig.hpp +19 -0
  133. data/rice/detail/st.hpp +60 -0
  134. data/rice/detail/to_ruby.hpp +22 -0
  135. data/rice/detail/to_ruby.ipp +37 -0
  136. data/rice/detail/win32.hpp +16 -0
  137. data/rice/detail/wrap_function.hpp +345 -0
  138. data/rice/detail/wrap_function.ipp +531 -0
  139. data/rice/generate_code.rb +1311 -0
  140. data/rice/global_function.hpp +33 -0
  141. data/rice/global_function.ipp +22 -0
  142. data/rice/protect.hpp +91 -0
  143. data/rice/protect.ipp +803 -0
  144. data/rice/ruby_mark.hpp +13 -0
  145. data/rice/ruby_try_catch.hpp +86 -0
  146. data/rice/rubypp.rb +97 -0
  147. data/rice/to_from_ruby.hpp +8 -0
  148. data/rice/to_from_ruby.ipp +299 -0
  149. data/rice/to_from_ruby_defn.hpp +71 -0
  150. data/ruby.ac +150 -0
  151. data/ruby/Makefile.am +1 -0
  152. data/ruby/Makefile.in +497 -0
  153. data/ruby/lib/Makefile.am +3 -0
  154. data/ruby/lib/Makefile.in +374 -0
  155. data/ruby/lib/mkmf-rice.rb.in +209 -0
  156. data/ruby/lib/version.rb +3 -0
  157. data/sample/Makefile.am +47 -0
  158. data/sample/Makefile.in +380 -0
  159. data/sample/enum/extconf.rb +3 -0
  160. data/sample/enum/sample_enum.cpp +54 -0
  161. data/sample/enum/test.rb +8 -0
  162. data/sample/inheritance/animals.cpp +98 -0
  163. data/sample/inheritance/extconf.rb +3 -0
  164. data/sample/inheritance/test.rb +7 -0
  165. data/sample/map/extconf.rb +3 -0
  166. data/sample/map/map.cpp +81 -0
  167. data/sample/map/test.rb +7 -0
  168. data/test/Makefile.am +49 -0
  169. data/test/Makefile.in +603 -0
  170. data/test/test_Address_Registration_Guard.cpp +43 -0
  171. data/test/test_Allocation_Strategies.cpp +77 -0
  172. data/test/test_Array.cpp +241 -0
  173. data/test/test_Builtin_Object.cpp +72 -0
  174. data/test/test_Class.cpp +398 -0
  175. data/test/test_Constructor.cpp +238 -0
  176. data/test/test_Critical_Guard.cpp +51 -0
  177. data/test/test_Data_Object.cpp +275 -0
  178. data/test/test_Data_Type.cpp +121 -0
  179. data/test/test_Director.cpp +225 -0
  180. data/test/test_Enum.cpp +162 -0
  181. data/test/test_Exception.cpp +46 -0
  182. data/test/test_Hash.cpp +195 -0
  183. data/test/test_Identifier.cpp +70 -0
  184. data/test/test_Jump_Tag.cpp +17 -0
  185. data/test/test_Module.cpp +428 -0
  186. data/test/test_Object.cpp +148 -0
  187. data/test/test_String.cpp +94 -0
  188. data/test/test_Struct.cpp +192 -0
  189. data/test/test_Symbol.cpp +63 -0
  190. data/test/test_To_From_Ruby.cpp +263 -0
  191. data/test/test_VM.cpp +26 -0
  192. data/test/test_global_functions.cpp +97 -0
  193. data/test/test_rice.rb +35 -0
  194. data/test/unittest.cpp +136 -0
  195. data/test/unittest.hpp +292 -0
  196. metadata +247 -0
@@ -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<char const *>(message_);
58
+ }
59
+
@@ -0,0 +1,9 @@
1
+ #ifndef Rice__Exception__hpp_
2
+ #define Rice__Exception__hpp_
3
+
4
+ #include "Exception_defn.hpp"
5
+ #include "Exception_Base.hpp"
6
+ #include "Address_Registration_Guard.hpp"
7
+
8
+ #endif // Rice__Exception__hpp_
9
+
@@ -0,0 +1,8 @@
1
+ #ifndef Rice__Exception_Base__hpp_
2
+ #define Rice__Exception_Base__hpp_
3
+
4
+ #include "Exception_Base_defn.hpp"
5
+ #include "Exception_Base.ipp"
6
+
7
+ #endif // Rice__Exception_Base__hpp_
8
+
@@ -0,0 +1,13 @@
1
+ #include "Object.hpp"
2
+
3
+ inline Rice::Exception_Base::
4
+ Exception_Base(VALUE v)
5
+ : Object(v)
6
+ {
7
+ }
8
+
9
+ inline Rice::Exception_Base::
10
+ ~Exception_Base() throw()
11
+ {
12
+ }
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,210 @@
1
+ #ifndef Rice__Hash__hpp_
2
+ #define Rice__Hash__hpp_
3
+
4
+ #include "Builtin_Object_defn.hpp"
5
+ #include "to_from_ruby_defn.hpp"
6
+ #include "detail/ruby.hpp"
7
+ #include "detail/st.hpp"
8
+ #include "detail/remove_const.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<RHash, 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, st_data_t bin, st_table_entry * ptr);
156
+
157
+ //! Copy construct an Iterator.
158
+ Iterator(Iterator const & iterator);
159
+
160
+ //! Construct an Iterator from another Iterator of a different const
161
+ //! qualification.
162
+ template<typename Iterator_T>
163
+ Iterator(Iterator_T const & iterator);
164
+
165
+ //! Assignment operator.
166
+ Iterator & operator=(Iterator const & rhs);
167
+
168
+ //! Preincrement operator.
169
+ Iterator & operator++();
170
+
171
+ //! Postincrement operator.
172
+ Iterator operator++(int);
173
+
174
+ //! Dereference operator.
175
+ Value_T operator*();
176
+
177
+ //! Dereference operator.
178
+ Value_T * operator->();
179
+
180
+ //! Equality operator.
181
+ bool operator==(Iterator const & rhs) const;
182
+
183
+ //! Inequality operator.
184
+ bool operator!=(Iterator const & rhs) const;
185
+
186
+ template<typename Hash_Ref_T_, typename Value_T_>
187
+ friend class Hash::Iterator;
188
+
189
+ //! Swap with another iterator of the same type.
190
+ void swap(Iterator & iterator);
191
+
192
+ private:
193
+ Hash hash_;
194
+ st_table * tbl_;
195
+ #if RICE__RUBY_VERSION_CODE >= 190
196
+ st_index_t bin_;
197
+ #else
198
+ int bin_;
199
+ #endif
200
+ st_table_entry * ptr_;
201
+
202
+ mutable typename detail::remove_const<Value_T>::Type tmp_;
203
+ };
204
+
205
+ } // namespace Rice
206
+
207
+ #include "Hash.ipp"
208
+
209
+ #endif // Rice__Hash__hpp_
210
+
data/rice/Hash.ipp ADDED
@@ -0,0 +1,338 @@
1
+ #ifndef Rice__Hash__ipp_
2
+ #define Rice__Hash__ipp_
3
+
4
+ #include "protect.hpp"
5
+ #include "to_from_ruby.hpp"
6
+ #include "Builtin_Object.hpp"
7
+ #include "Exception.hpp"
8
+ #include "Builtin_Object.hpp"
9
+ #include <algorithm>
10
+
11
+ // TODO: Evil hack
12
+ struct st_table_entry {
13
+ unsigned int hash;
14
+ st_data_t key;
15
+ st_data_t record;
16
+ st_table_entry *next;
17
+ };
18
+
19
+ inline Rice::Hash::
20
+ Hash()
21
+ : Builtin_Object<RHash, T_HASH>(protect(rb_hash_new))
22
+ {
23
+ }
24
+
25
+ inline Rice::Hash::
26
+ Hash(Object v)
27
+ : Builtin_Object<RHash, T_HASH>(v)
28
+ {
29
+ }
30
+
31
+ inline size_t Rice::Hash::
32
+ size() const
33
+ {
34
+ return RHASH_TBL(this->value())->num_entries;
35
+ }
36
+
37
+ inline Rice::Hash::Proxy::
38
+ Proxy(Hash hash, Object key)
39
+ : hash_(hash)
40
+ , key_(key)
41
+ {
42
+ }
43
+
44
+ /*
45
+ inline Rice::Hash::Proxy::
46
+ operator VALUE() const
47
+ {
48
+ return value();
49
+ }
50
+ */
51
+
52
+ inline Rice::Hash::Proxy::
53
+ operator Rice::Object() const
54
+ {
55
+ return value();
56
+ }
57
+
58
+ inline VALUE Rice::Hash::Proxy::
59
+ value() const
60
+ {
61
+ return protect(rb_hash_aref, hash_, key_);
62
+ }
63
+
64
+ template<typename T>
65
+ inline Rice::Object Rice::Hash::Proxy::
66
+ operator=(T const & value)
67
+ {
68
+ return protect(rb_hash_aset, hash_, key_, to_ruby(value));
69
+ }
70
+
71
+ inline void Rice::Hash::Proxy::
72
+ swap(Proxy & proxy)
73
+ {
74
+ hash_.swap(proxy.hash_);
75
+ key_.swap(proxy.key_);
76
+ }
77
+
78
+ template<typename Key_T>
79
+ inline Rice::Hash::Proxy const Rice::Hash::
80
+ operator[](Key_T const & key) const
81
+ {
82
+ return Proxy(*this, to_ruby(key));
83
+ }
84
+
85
+ template<typename Key_T>
86
+ inline Rice::Hash::Proxy Rice::Hash::
87
+ operator[](Key_T const & key)
88
+ {
89
+ return Proxy(*this, to_ruby(key));
90
+ }
91
+
92
+ template<typename Value_T, typename Key_T>
93
+ inline Value_T Rice::Hash::
94
+ get(Key_T const & key)
95
+ {
96
+ Object ruby_key(to_ruby(key));
97
+ Object value = operator[](ruby_key);
98
+ try
99
+ {
100
+ return from_ruby<Value_T>(value);
101
+ }
102
+ catch(Exception const & ex)
103
+ {
104
+ String s_key(ruby_key.to_s());
105
+ throw Exception(
106
+ ex,
107
+ "%s while converting value for key %s",
108
+ ex.what(),
109
+ s_key.c_str());
110
+ }
111
+ }
112
+
113
+ inline Rice::Hash::Entry::
114
+ Entry(Hash hash, Object key)
115
+ : key(key)
116
+ , first(Hash::Entry::key)
117
+ , value(hash, key)
118
+ , second(Hash::Entry::value)
119
+ {
120
+ }
121
+
122
+ inline Rice::Hash::Entry::
123
+ Entry(Entry const & entry)
124
+ : key(entry.key)
125
+ , first(Hash::Entry::key)
126
+ , value(entry.value)
127
+ , second(Hash::Entry::value)
128
+ {
129
+ }
130
+
131
+ inline Rice::Hash::Entry & Rice::Hash::Entry::
132
+ operator=(Rice::Hash::Entry const & rhs)
133
+ {
134
+ Entry tmp(rhs);
135
+ swap(tmp);
136
+ return *this;
137
+ }
138
+
139
+ inline void Rice::Hash::Entry::
140
+ swap(Rice::Hash::Entry & entry)
141
+ {
142
+ const_cast<Object &>(key).swap(const_cast<Object &>(entry.key));
143
+ value.swap(entry.value);
144
+ }
145
+
146
+ template<typename Hash_Ref_T, typename Value_T>
147
+ inline Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
148
+ Iterator(Hash_Ref_T hash, st_data_t bin, st_table_entry * ptr)
149
+ : hash_(hash)
150
+ , tbl_(RHASH_TBL(hash.value()))
151
+ , bin_(bin)
152
+ , ptr_(ptr)
153
+ , tmp_(hash, Qnil)
154
+ {
155
+ // If we aren't already at the end, then use the increment operator to
156
+ // point to the first element
157
+ if(!ptr_ && bin_ < tbl_->num_bins)
158
+ {
159
+ operator++();
160
+ }
161
+ }
162
+
163
+ template<typename Hash_Ref_T, typename Value_T>
164
+ inline Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
165
+ Iterator(Iterator const & iterator)
166
+ : hash_(iterator.hash_.value())
167
+ , tbl_(iterator.tbl_)
168
+ , bin_(iterator.bin_)
169
+ , ptr_(iterator.ptr_)
170
+ , tmp_(iterator.hash_, Qnil)
171
+ {
172
+ }
173
+
174
+ template<typename Hash_Ref_T, typename Value_T>
175
+ template<typename Iterator_T>
176
+ inline Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
177
+ Iterator(Iterator_T const & iterator)
178
+ : hash_(iterator.hash_.value())
179
+ , tbl_(iterator.tbl_)
180
+ , bin_(iterator.bin_)
181
+ , ptr_(iterator.ptr_)
182
+ , tmp_(iterator.hash_, Qnil)
183
+ {
184
+ }
185
+
186
+ template<typename Hash_Ref_T, typename Value_T>
187
+ inline Rice::Hash::Iterator<Hash_Ref_T, Value_T> &
188
+ Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
189
+ operator=(Iterator const & iterator)
190
+ {
191
+ Iterator tmp(iterator);
192
+
193
+ this->swap(tmp);
194
+
195
+ return *this;
196
+ }
197
+
198
+ template<typename Hash_Ref_T, typename Value_T>
199
+ inline Rice::Hash::Iterator<Hash_Ref_T, Value_T> &
200
+ Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
201
+ operator++()
202
+ {
203
+ // Go to the next element in the bin; this will be a no-op if we were
204
+ // called from the constructor, because ptr_ will be 0 (and if its
205
+ // not, this function won't get called).
206
+ if(ptr_)
207
+ {
208
+ ptr_ = ptr_->next;
209
+ }
210
+
211
+ // If we've reached the end of the bin, then try the next bin until
212
+ // we have run out of bins
213
+ while(ptr_ == 0)
214
+ {
215
+ ++bin_;
216
+ if(bin_ == tbl_->num_bins)
217
+ {
218
+ // At the end..
219
+ return *this;
220
+ }
221
+ ptr_ = tbl_->bins[bin_];
222
+ }
223
+
224
+ return *this;
225
+ }
226
+
227
+ template<typename Hash_Ref_T, typename Value_T>
228
+ inline Rice::Hash::Iterator<Hash_Ref_T, Value_T>
229
+ Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
230
+ operator++(int)
231
+ {
232
+ Iterator copy(*this);
233
+ ++(*this);
234
+ return copy;
235
+ }
236
+
237
+ template<typename Hash_Ref_T, typename Value_T>
238
+ inline Value_T
239
+ Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
240
+ operator*()
241
+ {
242
+ return Value_T(hash_, ptr_->key);
243
+ }
244
+
245
+ template<typename Hash_Ref_T, typename Value_T>
246
+ inline Value_T *
247
+ Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
248
+ operator->()
249
+ {
250
+ Entry tmp(hash_, ptr_->key);
251
+ this->tmp_.swap(tmp);
252
+ return &tmp_;
253
+ }
254
+
255
+ template<typename Hash_Ref_T, typename Value_T>
256
+ inline bool Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
257
+ operator==(Iterator const & rhs) const
258
+ {
259
+ return hash_.value() == rhs.hash_.value()
260
+ && tbl_ == rhs.tbl_
261
+ && bin_ == rhs.bin_
262
+ && ptr_ == rhs.ptr_;
263
+ }
264
+
265
+ template<typename Hash_Ref_T, typename Value_T>
266
+ inline bool Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
267
+ operator!=(Iterator const & rhs) const
268
+ {
269
+ return !(*this == rhs);
270
+ }
271
+
272
+ template<typename Hash_Ref_T, typename Value_T>
273
+ inline void
274
+ Rice::Hash::Iterator<Hash_Ref_T, Value_T>::
275
+ swap(Iterator& iterator)
276
+ {
277
+ using namespace std;
278
+
279
+ hash_.swap(iterator.hash_);
280
+ swap(tbl_, iterator.tbl_);
281
+ swap(bin_, iterator.bin_);
282
+ swap(ptr_, iterator.ptr_);
283
+ }
284
+
285
+ inline Rice::Hash::iterator Rice::Hash::
286
+ begin()
287
+ {
288
+ st_table * tbl(RHASH_TBL(value()));
289
+ return iterator(*this, 0, tbl->bins[0]);
290
+ }
291
+
292
+ inline Rice::Hash::const_iterator Rice::Hash::
293
+ begin() const
294
+ {
295
+ st_table * tbl(RHASH_TBL(value()));
296
+ return const_iterator(*this, 0, tbl->bins[0]);
297
+ }
298
+
299
+ inline Rice::Hash::iterator Rice::Hash::
300
+ end()
301
+ {
302
+ st_table * tbl(RHASH_TBL(value()));
303
+ return iterator(*this, tbl->num_bins, 0);
304
+ }
305
+
306
+ inline Rice::Hash::const_iterator Rice::Hash::
307
+ end() const
308
+ {
309
+ st_table * tbl(RHASH_TBL(value()));
310
+ return const_iterator(*this, tbl->num_bins, 0);
311
+ }
312
+
313
+ inline bool Rice::
314
+ operator<(
315
+ Hash::Entry const & lhs, Hash::Entry const & rhs)
316
+ {
317
+ Object lhs_key(lhs.key);
318
+ Object rhs_key(rhs.key);
319
+ if(lhs_key < rhs_key)
320
+ {
321
+ return true;
322
+ }
323
+ else if(lhs_key > rhs_key)
324
+ {
325
+ return false;
326
+ }
327
+ else if(Object(lhs.value.value()) < Object(rhs.value.value()))
328
+ {
329
+ return true;
330
+ }
331
+ else
332
+ {
333
+ return false;
334
+ }
335
+ }
336
+
337
+ #endif // Rice__Hash__ipp_
338
+