rice 4.3.3 → 4.6.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.
Files changed (237) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +86 -26
  3. data/CMakeLists.txt +31 -0
  4. data/CMakePresets.json +75 -0
  5. data/COPYING +3 -2
  6. data/FindRuby.cmake +437 -0
  7. data/README.md +7 -2
  8. data/Rakefile +12 -5
  9. data/include/rice/rice.hpp +9522 -4426
  10. data/include/rice/stl.hpp +2831 -1198
  11. data/lib/make_rice_headers.rb +79 -0
  12. data/lib/mkmf-rice.rb +40 -94
  13. data/lib/rice/version.rb +3 -0
  14. data/lib/rice.rb +1 -0
  15. data/lib/rubygems/builder.rb +11 -0
  16. data/lib/rubygems/cmake_builder.rb +113 -0
  17. data/lib/rubygems_plugin.rb +9 -0
  18. data/rice/Address_Registration_Guard.hpp +72 -3
  19. data/rice/Arg.hpp +26 -6
  20. data/rice/Arg.ipp +35 -2
  21. data/rice/Buffer.hpp +123 -0
  22. data/rice/Buffer.ipp +599 -0
  23. data/rice/Callback.hpp +21 -0
  24. data/rice/Callback.ipp +13 -0
  25. data/rice/Constructor.hpp +4 -27
  26. data/rice/Constructor.ipp +79 -0
  27. data/rice/Data_Object.hpp +73 -3
  28. data/rice/Data_Object.ipp +388 -96
  29. data/rice/Data_Type.hpp +214 -3
  30. data/rice/Data_Type.ipp +144 -67
  31. data/rice/Director.hpp +0 -2
  32. data/rice/Enum.hpp +4 -7
  33. data/rice/Enum.ipp +102 -55
  34. data/rice/Exception.hpp +62 -2
  35. data/rice/Exception.ipp +7 -12
  36. data/rice/Init.hpp +8 -0
  37. data/rice/Init.ipp +8 -0
  38. data/rice/JumpException.hpp +44 -0
  39. data/rice/JumpException.ipp +48 -0
  40. data/rice/MemoryView.hpp +11 -0
  41. data/rice/MemoryView.ipp +3 -0
  42. data/rice/Return.hpp +7 -27
  43. data/rice/Return.ipp +13 -13
  44. data/rice/cpp_api/Array.hpp +209 -0
  45. data/rice/cpp_api/Array.ipp +304 -0
  46. data/rice/cpp_api/Builtin_Object.hpp +31 -0
  47. data/rice/cpp_api/Builtin_Object.ipp +37 -0
  48. data/rice/cpp_api/Class.hpp +70 -0
  49. data/rice/cpp_api/Class.ipp +97 -0
  50. data/rice/cpp_api/Encoding.hpp +32 -0
  51. data/rice/cpp_api/Encoding.ipp +59 -0
  52. data/rice/cpp_api/Hash.hpp +194 -0
  53. data/rice/cpp_api/Hash.ipp +257 -0
  54. data/rice/{Identifier.hpp → cpp_api/Identifier.hpp} +2 -6
  55. data/rice/{Identifier.ipp → cpp_api/Identifier.ipp} +4 -2
  56. data/rice/cpp_api/Module.hpp +72 -0
  57. data/rice/cpp_api/Module.ipp +101 -0
  58. data/rice/cpp_api/Object.hpp +272 -0
  59. data/rice/cpp_api/Object.ipp +235 -0
  60. data/rice/cpp_api/String.hpp +74 -0
  61. data/rice/cpp_api/String.ipp +120 -0
  62. data/rice/cpp_api/Struct.hpp +113 -0
  63. data/rice/cpp_api/Struct.ipp +92 -0
  64. data/rice/cpp_api/Symbol.hpp +46 -0
  65. data/rice/cpp_api/Symbol.ipp +93 -0
  66. data/rice/cpp_api/shared_methods.hpp +134 -0
  67. data/rice/detail/DefaultHandler.hpp +12 -0
  68. data/rice/detail/DefaultHandler.ipp +8 -0
  69. data/rice/detail/HandlerRegistry.hpp +5 -35
  70. data/rice/detail/HandlerRegistry.ipp +7 -11
  71. data/rice/detail/InstanceRegistry.hpp +1 -4
  72. data/rice/detail/MethodInfo.hpp +12 -10
  73. data/rice/detail/MethodInfo.ipp +26 -21
  74. data/rice/detail/Native.hpp +33 -0
  75. data/rice/detail/Native.ipp +157 -0
  76. data/rice/detail/NativeAttributeGet.hpp +52 -0
  77. data/rice/detail/NativeAttributeGet.ipp +57 -0
  78. data/rice/detail/NativeAttributeSet.hpp +44 -0
  79. data/rice/detail/NativeAttributeSet.ipp +88 -0
  80. data/rice/detail/NativeCallbackFFI.hpp +55 -0
  81. data/rice/detail/NativeCallbackFFI.ipp +151 -0
  82. data/rice/detail/NativeCallbackSimple.hpp +30 -0
  83. data/rice/detail/NativeCallbackSimple.ipp +29 -0
  84. data/rice/detail/NativeFunction.hpp +33 -23
  85. data/rice/detail/NativeFunction.ipp +309 -70
  86. data/rice/detail/NativeIterator.hpp +9 -11
  87. data/rice/detail/NativeIterator.ipp +33 -31
  88. data/rice/detail/NativeRegistry.hpp +24 -15
  89. data/rice/detail/NativeRegistry.ipp +23 -48
  90. data/rice/detail/Proc.hpp +4 -0
  91. data/rice/detail/Proc.ipp +85 -0
  92. data/rice/detail/Registries.hpp +0 -7
  93. data/rice/detail/Registries.ipp +0 -18
  94. data/rice/detail/RubyFunction.hpp +0 -3
  95. data/rice/detail/RubyFunction.ipp +4 -8
  96. data/rice/detail/RubyType.hpp +16 -0
  97. data/rice/detail/RubyType.ipp +232 -0
  98. data/rice/detail/Type.hpp +7 -6
  99. data/rice/detail/Type.ipp +192 -45
  100. data/rice/detail/TypeRegistry.hpp +15 -7
  101. data/rice/detail/TypeRegistry.ipp +105 -12
  102. data/rice/detail/Wrapper.hpp +68 -32
  103. data/rice/detail/Wrapper.ipp +121 -109
  104. data/rice/detail/cpp_protect.hpp +5 -6
  105. data/rice/detail/default_allocation_func.ipp +0 -2
  106. data/rice/detail/from_ruby.hpp +38 -3
  107. data/rice/detail/from_ruby.ipp +1321 -492
  108. data/rice/detail/ruby.hpp +18 -0
  109. data/rice/detail/to_ruby.hpp +41 -3
  110. data/rice/detail/to_ruby.ipp +1424 -194
  111. data/rice/global_function.hpp +0 -4
  112. data/rice/global_function.ipp +0 -1
  113. data/rice/libc/file.hpp +11 -0
  114. data/rice/libc/file.ipp +32 -0
  115. data/rice/rice.hpp +116 -26
  116. data/rice/ruby_mark.hpp +4 -3
  117. data/rice/stl/complex.hpp +6 -0
  118. data/rice/stl/complex.ipp +93 -0
  119. data/rice/stl/exception.hpp +11 -0
  120. data/rice/stl/exception.ipp +29 -0
  121. data/rice/stl/exception_ptr.hpp +6 -0
  122. data/rice/stl/exception_ptr.ipp +27 -0
  123. data/rice/stl/map.hpp +12 -0
  124. data/rice/stl/map.ipp +469 -0
  125. data/rice/stl/monostate.hpp +6 -0
  126. data/rice/stl/monostate.ipp +80 -0
  127. data/rice/stl/multimap.hpp +14 -0
  128. data/rice/stl/multimap.ipp +448 -0
  129. data/rice/stl/optional.hpp +6 -0
  130. data/rice/stl/optional.ipp +118 -0
  131. data/rice/stl/pair.hpp +13 -0
  132. data/rice/stl/pair.ipp +155 -0
  133. data/rice/stl/reference_wrapper.hpp +6 -0
  134. data/rice/stl/reference_wrapper.ipp +41 -0
  135. data/rice/stl/set.hpp +12 -0
  136. data/rice/stl/set.ipp +495 -0
  137. data/rice/stl/shared_ptr.hpp +28 -0
  138. data/rice/stl/shared_ptr.ipp +224 -0
  139. data/rice/stl/string.hpp +6 -0
  140. data/rice/stl/string.ipp +158 -0
  141. data/rice/stl/string_view.hpp +6 -0
  142. data/rice/stl/string_view.ipp +65 -0
  143. data/rice/stl/tuple.hpp +6 -0
  144. data/rice/stl/tuple.ipp +128 -0
  145. data/rice/stl/type_index.hpp +6 -0
  146. data/rice/stl/type_index.ipp +30 -0
  147. data/rice/stl/type_info.hpp +6 -0
  148. data/rice/stl/type_info.ipp +29 -0
  149. data/rice/stl/unique_ptr.hpp +22 -0
  150. data/rice/stl/unique_ptr.ipp +139 -0
  151. data/rice/stl/unordered_map.hpp +12 -0
  152. data/rice/stl/unordered_map.ipp +469 -0
  153. data/rice/stl/variant.hpp +6 -0
  154. data/rice/stl/variant.ipp +242 -0
  155. data/rice/stl/vector.hpp +12 -0
  156. data/rice/stl/vector.ipp +590 -0
  157. data/rice/stl.hpp +11 -3
  158. data/rice/traits/attribute_traits.hpp +26 -0
  159. data/rice/traits/function_traits.hpp +95 -0
  160. data/rice/traits/method_traits.hpp +47 -0
  161. data/rice/traits/rice_traits.hpp +160 -0
  162. data/rice.gemspec +85 -0
  163. data/test/embed_ruby.cpp +7 -1
  164. data/test/extconf.rb +2 -0
  165. data/test/test_Address_Registration_Guard.cpp +5 -0
  166. data/test/test_Array.cpp +18 -4
  167. data/test/test_Attribute.cpp +136 -21
  168. data/test/test_Buffer.cpp +285 -0
  169. data/test/test_Builtin_Object.cpp +5 -0
  170. data/test/test_Callback.cpp +230 -0
  171. data/test/test_Class.cpp +5 -31
  172. data/test/test_Constructor.cpp +69 -6
  173. data/test/test_Data_Object.cpp +97 -38
  174. data/test/test_Data_Type.cpp +470 -65
  175. data/test/test_Director.cpp +17 -8
  176. data/test/test_Enum.cpp +155 -40
  177. data/test/test_Exception.cpp +235 -0
  178. data/test/test_File.cpp +70 -0
  179. data/test/test_From_Ruby.cpp +609 -0
  180. data/test/test_Hash.cpp +5 -0
  181. data/test/test_Identifier.cpp +5 -0
  182. data/test/test_Inheritance.cpp +6 -1
  183. data/test/test_Iterator.cpp +6 -1
  184. data/test/test_Jump_Exception.cpp +23 -0
  185. data/test/test_Keep_Alive.cpp +13 -19
  186. data/test/test_Keep_Alive_No_Wrapper.cpp +5 -1
  187. data/test/test_Memory_Management.cpp +5 -0
  188. data/test/test_Module.cpp +128 -67
  189. data/test/test_Native_Registry.cpp +2 -34
  190. data/test/test_Object.cpp +5 -0
  191. data/test/test_Overloads.cpp +806 -0
  192. data/test/test_Ownership.cpp +160 -54
  193. data/test/test_Proc.cpp +44 -0
  194. data/test/test_Self.cpp +9 -4
  195. data/test/test_Stl_Exception.cpp +109 -0
  196. data/test/test_Stl_Map.cpp +54 -42
  197. data/test/test_Stl_Multimap.cpp +693 -0
  198. data/test/test_Stl_Optional.cpp +5 -0
  199. data/test/test_Stl_Pair.cpp +14 -9
  200. data/test/test_Stl_Reference_Wrapper.cpp +9 -2
  201. data/test/test_Stl_Set.cpp +790 -0
  202. data/test/test_Stl_SharedPtr.cpp +458 -0
  203. data/test/test_Stl_String.cpp +5 -0
  204. data/test/test_Stl_String_View.cpp +5 -0
  205. data/test/test_Stl_Tuple.cpp +116 -0
  206. data/test/test_Stl_Type.cpp +147 -0
  207. data/test/test_Stl_UniquePtr.cpp +202 -0
  208. data/test/test_Stl_Unordered_Map.cpp +43 -38
  209. data/test/test_Stl_Variant.cpp +217 -84
  210. data/test/test_Stl_Vector.cpp +306 -58
  211. data/test/test_String.cpp +5 -0
  212. data/test/test_Struct.cpp +5 -0
  213. data/test/test_Symbol.cpp +5 -0
  214. data/test/test_Template.cpp +192 -0
  215. data/test/test_To_Ruby.cpp +524 -0
  216. data/test/test_Tracking.cpp +1 -0
  217. data/test/test_Type.cpp +171 -0
  218. data/test/test_global_functions.cpp +67 -7
  219. data/test/unittest.cpp +8 -0
  220. metadata +127 -26
  221. data/lib/version.rb +0 -3
  222. data/rice/Address_Registration_Guard_defn.hpp +0 -79
  223. data/rice/Data_Object_defn.hpp +0 -84
  224. data/rice/Data_Type_defn.hpp +0 -190
  225. data/rice/Exception_defn.hpp +0 -68
  226. data/rice/HandlerRegistration.hpp +0 -15
  227. data/rice/detail/ExceptionHandler.hpp +0 -8
  228. data/rice/detail/ExceptionHandler.ipp +0 -28
  229. data/rice/detail/ExceptionHandler_defn.hpp +0 -77
  230. data/rice/detail/Jump_Tag.hpp +0 -21
  231. data/rice/detail/NativeAttribute.hpp +0 -64
  232. data/rice/detail/NativeAttribute.ipp +0 -112
  233. data/rice/detail/from_ruby_defn.hpp +0 -38
  234. data/rice/detail/to_ruby_defn.hpp +0 -48
  235. data/test/test_Jump_Tag.cpp +0 -17
  236. data/test/test_Stl_SmartPointer.cpp +0 -283
  237. data/test/test_To_From_Ruby.cpp +0 -399
@@ -0,0 +1,97 @@
1
+
2
+ namespace Rice
3
+ {
4
+ inline Class::Class(VALUE value) : Module(value)
5
+ {
6
+ detail::protect(rb_check_type, value, (int)T_CLASS);
7
+ }
8
+
9
+ inline Class& Class::undef_creation_funcs()
10
+ {
11
+ detail::protect(rb_undef_alloc_func, value());
12
+ detail::protect(rb_undef_method, value(), "initialize");
13
+ return *this;
14
+ }
15
+
16
+ template<typename ...Arg_Ts>
17
+ inline Object Class::create(Arg_Ts ...args)
18
+ {
19
+ return this->call("new", args...);
20
+ }
21
+
22
+ inline const std::string Class::name() const
23
+ {
24
+ const char* buffer = rb_class2name(this->value());
25
+ return std::string(buffer);
26
+ }
27
+
28
+ inline const std::string Class::base_name() const
29
+ {
30
+ std::string name = this->name();
31
+ auto regex = std::regex("^.*::");
32
+ std::string result = std::regex_replace(name, regex, "");
33
+ return result;
34
+ }
35
+
36
+ inline Class define_class_under(Object parent, Identifier id, const Class& superclass)
37
+ {
38
+ VALUE klass = detail::protect(rb_define_class_id_under, parent.value(), id, superclass.value());
39
+
40
+ // We MUST reset the instance registry in case the user just redefined a class which resets it
41
+ detail::Registries::instance.natives.reset(klass);
42
+
43
+ return klass;
44
+ }
45
+
46
+ inline Class define_class_under(Object parent, char const* name, const Class& superclass)
47
+ {
48
+ Identifier id(name);
49
+ return define_class_under(parent, id, superclass);
50
+ }
51
+
52
+ inline Class define_class(char const* name, const Class& superclass)
53
+ {
54
+ VALUE klass = detail::protect(rb_define_class, name, superclass.value());
55
+
56
+ // We MUST reset the instance registry in case the user just redefined a class which resets it
57
+ detail::Registries::instance.natives.reset(klass);
58
+
59
+ return klass;
60
+ }
61
+
62
+ inline Class anonymous_class()
63
+ {
64
+ VALUE klass = detail::protect(rb_class_new, rb_cObject);
65
+ VALUE singleton = detail::protect(rb_singleton_class, klass);
66
+
67
+ // Ruby will reuse addresses previously assigned to other modules
68
+ // that have subsequently been garbage collected
69
+ detail::Registries::instance.natives.reset(klass);
70
+ detail::Registries::instance.natives.reset(singleton);
71
+
72
+ return klass;
73
+ }
74
+ }
75
+
76
+ namespace Rice::detail
77
+ {
78
+ template<>
79
+ class To_Ruby<Class>
80
+ {
81
+ public:
82
+ static VALUE convert(Class const& x)
83
+ {
84
+ return x.value();
85
+ }
86
+ };
87
+
88
+ template<>
89
+ class From_Ruby<Class>
90
+ {
91
+ public:
92
+ Class convert(VALUE value)
93
+ {
94
+ return Class(value);
95
+ }
96
+ };
97
+ }
@@ -0,0 +1,32 @@
1
+ #ifndef Rice__Encoding__hpp_
2
+ #define Rice__Encoding__hpp_
3
+
4
+ namespace Rice
5
+ {
6
+ //! A wrapper for a Ruby encoding
7
+ class Encoding
8
+ {
9
+ public:
10
+ static Encoding utf8();
11
+
12
+ //! Wrap an existing encoding.
13
+ Encoding(rb_encoding* encoding);
14
+
15
+ //! Implicit conversion to VALUE.
16
+ operator rb_encoding* () const
17
+ {
18
+ return this->encoding_;
19
+ }
20
+
21
+ operator VALUE () const
22
+ {
23
+ return detail::protect(rb_enc_from_encoding, this->encoding_);
24
+ }
25
+
26
+ private:
27
+ rb_encoding* encoding_;
28
+ };
29
+ } // namespace Rice
30
+
31
+ #endif // Rice__Encoding__hpp_
32
+
@@ -0,0 +1,59 @@
1
+ namespace Rice
2
+ {
3
+ inline Encoding Encoding::utf8()
4
+ {
5
+ return Encoding(rb_utf8_encoding());
6
+ }
7
+
8
+ inline Encoding::Encoding(rb_encoding* encoding) : encoding_(encoding)
9
+ {
10
+ }
11
+ }
12
+
13
+ /*namespace Rice::detail
14
+ {
15
+ template<>
16
+ struct Type<Encoding>
17
+ {
18
+ static bool verify()
19
+ {
20
+ return true;
21
+ }
22
+ };
23
+
24
+ template<>
25
+ class To_Ruby<Encoding>
26
+ {
27
+ public:
28
+ VALUE convert(const Encoding& encoding)
29
+ {
30
+ // return x.value();
31
+ }
32
+ };
33
+
34
+ template<>
35
+ class From_Ruby<Encoding>
36
+ {
37
+ public:
38
+ Convertible is_convertible(VALUE value)
39
+ {
40
+ switch (rb_type(value))
41
+ {
42
+ case RUBY_T_SYMBOL:
43
+ return Convertible::Exact;
44
+ break;
45
+ case RUBY_T_STRING:
46
+ return Convertible::Cast;
47
+ break;
48
+ default:
49
+ return Convertible::None;
50
+ }
51
+ }
52
+
53
+ Encoding convert(VALUE value)
54
+ {
55
+ // return Symbol(value);
56
+ }
57
+ };
58
+ }
59
+ */
@@ -0,0 +1,194 @@
1
+ #ifndef Rice__Hash__hpp_
2
+ #define Rice__Hash__hpp_
3
+
4
+ #include <iterator>
5
+ #include <type_traits>
6
+
7
+ namespace Rice
8
+ {
9
+ //! A wrapper for the ruby Hash class.
10
+ //! This class provides a C++-style interface to ruby's Hash class and
11
+ //! its associated rb_hash_* functions.
12
+ //! Example:
13
+ //! \code
14
+ //! Hash h;
15
+ //! h[42] = String("foo");
16
+ //! h[10] = String("bar");
17
+ //! std::cout << String(h[42]) << std::endl;
18
+ //! \endcode
19
+ class Hash: public Builtin_Object<T_HASH>
20
+ {
21
+ public:
22
+ //! Construct a new hash.
23
+ Hash();
24
+
25
+ //! Wrap an existing hash.
26
+ /*! \param v the hash to wrap.
27
+ */
28
+ Hash(Object v);
29
+
30
+ //! Return the number of elements in the hash.
31
+ size_t size() const;
32
+
33
+ private:
34
+ //! A helper class so hash[key]=value can work.
35
+ class Proxy;
36
+
37
+ public:
38
+ //! Get the value for the given key.
39
+ /*! \param key the key whose value should be retrieved from the hash.
40
+ * \return the value associated with the given key.
41
+ */
42
+ template<typename Key_T>
43
+ Proxy const operator[](Key_T const& key) const;
44
+
45
+ //! Get the value for the given key.
46
+ /*! \param key the key whose value should be retrieved from the hash.
47
+ * \return the value associated with the given key.
48
+ */
49
+ template<typename Key_T>
50
+ Proxy operator[](Key_T const& key);
51
+
52
+ //! Get the value for the given key
53
+ /*! The returned value is converted to the type given by Value_T.
54
+ * \param key the key whose value should be retrieved from the hash.
55
+ * \return the value associated with the given key, converted to C++
56
+ * type Value_T.
57
+ */
58
+ template<typename Value_T, typename Key_T>
59
+ Value_T get(Key_T const& key);
60
+
61
+ //! A helper class for dereferencing iterators
62
+ class Entry;
63
+
64
+ //! A helper class for implementing iterators for a Hash.
65
+ template<typename Hash_Ptr_T, typename Value_T>
66
+ class Iterator;
67
+
68
+ public:
69
+ //! An iterator.
70
+ typedef Iterator<Hash*, Entry> iterator;
71
+
72
+ //! A const iterator.
73
+ typedef Iterator<Hash const*, Entry const> const_iterator;
74
+
75
+ public:
76
+ //! Return an iterator to the beginning of the hash.
77
+ iterator begin();
78
+
79
+ //! Return a const iterator to the beginning of the hash.
80
+ const_iterator begin() const;
81
+
82
+ //! Return an iterator to the end of the hash.
83
+ iterator end();
84
+
85
+ //! Return a const to the end of the hash.
86
+ const_iterator end() const;
87
+ };
88
+
89
+ //! A helper class so hash[key]=value can work.
90
+ class Hash::Proxy
91
+ {
92
+ public:
93
+ //! Construct a new Proxy.
94
+ Proxy(Hash* hash, Object key);
95
+
96
+ //! Implicit conversion to Object.
97
+ operator Object() const;
98
+
99
+ //! Explicit conversion to VALUE.
100
+ VALUE value() const;
101
+
102
+ //! Assignment operator.
103
+ template<typename T>
104
+ Object operator=(T const& value);
105
+
106
+ private:
107
+ Hash* hash_;
108
+ Object key_;
109
+ };
110
+
111
+ //! A helper class for dereferencing iterators
112
+ /*! This class is intended to look similar to an std::pair.
113
+ */
114
+ class Hash::Entry
115
+ {
116
+ public:
117
+ //! Construct a new Entry.
118
+ Entry(Hash* hash, Object key);
119
+
120
+ //! Copy constructor.
121
+ Entry(Entry const& entry);
122
+
123
+ Object const key; //!< The key
124
+ Object const& first; //!< An alias for the key
125
+
126
+ Proxy value; //!< The value
127
+ Proxy& second; //!< An alias for the value
128
+
129
+ Entry& operator=(Entry const& rhs);
130
+
131
+ friend bool operator<(Entry const& lhs, Entry const& rhs);
132
+ };
133
+
134
+ bool operator<(Hash::Entry const& lhs, Hash::Entry const& rhs);
135
+
136
+ //! A helper class for implementing iterators for a Hash.
137
+ template<typename Hash_Ptr_T, typename Value_T>
138
+ class Hash::Iterator
139
+ {
140
+ public:
141
+ using iterator_category = std::input_iterator_tag;
142
+ using value_type = Value_T;
143
+ using difference_type = long;
144
+ using pointer = Object*;
145
+ using reference = Value_T&;
146
+
147
+ //! Construct a new Iterator.
148
+ Iterator(Hash_Ptr_T hash);
149
+
150
+ //! Construct a new Iterator with a given start-at index point
151
+ Iterator(Hash_Ptr_T hash, int start_at);
152
+
153
+ //! Construct an Iterator from another Iterator of a different const
154
+ //! qualification.
155
+ template<typename Iterator_T>
156
+ Iterator(Iterator_T const& iterator);
157
+
158
+ //! Preincrement operator.
159
+ Iterator& operator++();
160
+
161
+ //! Postincrement operator.
162
+ Iterator operator++(int);
163
+
164
+ //! Dereference operator.
165
+ Value_T operator*();
166
+
167
+ //! Dereference operator.
168
+ Value_T* operator->();
169
+
170
+ //! Equality operator.
171
+ bool operator==(Iterator const& rhs) const;
172
+
173
+ //! Inequality operator.
174
+ bool operator!=(Iterator const& rhs) const;
175
+
176
+ template<typename Hash_Ptr_T_, typename Value_T_>
177
+ friend class Hash::Iterator;
178
+
179
+ protected:
180
+ Object current_key();
181
+
182
+ Array hash_keys();
183
+
184
+ private:
185
+ Hash_Ptr_T hash_;
186
+ long current_index_;
187
+ VALUE keys_;
188
+
189
+ mutable typename std::remove_const<Value_T>::type tmp_;
190
+ };
191
+ } // namespace Rice
192
+
193
+ #endif // Rice__Hash__hpp_
194
+
@@ -0,0 +1,257 @@
1
+ #include <algorithm>
2
+
3
+ namespace Rice
4
+ {
5
+ inline Hash::Hash() : Builtin_Object<T_HASH>(detail::protect(rb_hash_new))
6
+ {
7
+ }
8
+
9
+ inline Hash::Hash(Object v) : Builtin_Object<T_HASH>(v)
10
+ {
11
+ }
12
+
13
+ inline size_t Hash::size() const
14
+ {
15
+ return RHASH_SIZE(this->value());
16
+ }
17
+
18
+ inline Hash::Proxy::Proxy(Hash* hash, Object key) : hash_(hash), key_(key)
19
+ {
20
+ }
21
+
22
+ inline Hash::Proxy::operator Object() const
23
+ {
24
+ return value();
25
+ }
26
+
27
+ inline VALUE Hash::Proxy::value() const
28
+ {
29
+ return detail::protect(rb_hash_aref, hash_->value(), key_.value());
30
+ }
31
+
32
+ template<typename T>
33
+ inline Object Hash::Proxy::operator=(T const& value)
34
+ {
35
+ return detail::protect(rb_hash_aset, hash_->value(), key_.value(), detail::To_Ruby<T>().convert(value));
36
+ }
37
+
38
+ template<typename Key_T>
39
+ inline Hash::Proxy const Hash::operator[](Key_T const& key) const
40
+ {
41
+ return Proxy(*this, detail::To_Ruby<Key_T>().convert(key));
42
+ }
43
+
44
+ template<typename Key_T>
45
+ inline Hash::Proxy Hash::operator[](Key_T const& key)
46
+ {
47
+ return Proxy(this, detail::To_Ruby<Key_T>().convert(key));
48
+ }
49
+
50
+ template<typename Value_T, typename Key_T>
51
+ inline Value_T Hash::get(Key_T const& key)
52
+ {
53
+ Object ruby_key(detail::To_Ruby<Key_T>().convert(key));
54
+ Object value = operator[](ruby_key);
55
+ try
56
+ {
57
+ return detail::From_Ruby<Value_T>().convert(value);
58
+ }
59
+ catch (Exception const& ex)
60
+ {
61
+ String s_key(ruby_key.to_s());
62
+ throw Exception(
63
+ ex,
64
+ "%s while converting value for key %s",
65
+ ex.what(),
66
+ s_key.c_str());
67
+ }
68
+ }
69
+
70
+ inline Hash::Entry::Entry(Hash* hash, Object key) :
71
+ key(key), first(Hash::Entry::key), value(hash, key), second(Hash::Entry::value)
72
+ {
73
+ }
74
+
75
+ inline Hash::Entry::Entry(Entry const& entry) :
76
+ key(entry.key), first(Hash::Entry::key), value(entry.value), second(Hash::Entry::value)
77
+ {
78
+ }
79
+
80
+ inline Hash::Entry& Hash::Entry::operator=(Hash::Entry const& other)
81
+ {
82
+ const_cast<Object&>(key) = const_cast<Object&>(other.key);
83
+
84
+ this->value = other.value;
85
+ this->second = this->value;
86
+
87
+ return *this;
88
+ }
89
+
90
+ template<typename Hash_Ptr_T, typename Value_T>
91
+ inline Hash::Iterator<Hash_Ptr_T, Value_T>::Iterator(Hash_Ptr_T hash)
92
+ : hash_(hash), current_index_(0), keys_(Qnil), tmp_(const_cast<Hash*>(hash), Qnil)
93
+ {
94
+ }
95
+
96
+ template<typename Hash_Ptr_T, typename Value_T>
97
+ inline Hash::Iterator<Hash_Ptr_T, Value_T>::Iterator(Hash_Ptr_T hash, int start_at)
98
+ : hash_(hash), current_index_(start_at), keys_(Qnil), tmp_(const_cast<Hash*>(hash), Qnil)
99
+ {
100
+ }
101
+
102
+ template<typename Hash_Ptr_T, typename Value_T>
103
+ template<typename Iterator_T>
104
+ inline Hash::Iterator<Hash_Ptr_T, Value_T>::Iterator(Iterator_T const& iterator) :
105
+ hash_(iterator.hash_), current_index_(iterator.current_index_), keys_(Qnil), tmp_(iterator.hash_, Qnil)
106
+ {
107
+ }
108
+
109
+ template<typename Hash_Ptr_T, typename Value_T>
110
+ inline Hash::Iterator<Hash_Ptr_T, Value_T>& Hash::Iterator<Hash_Ptr_T, Value_T>::operator++()
111
+ {
112
+ // Ensure we're within the range
113
+ if (current_index_ < hash_keys().size())
114
+ {
115
+ current_index_++;
116
+ }
117
+
118
+ return *this;
119
+ }
120
+
121
+ template<typename Hash_Ptr_T, typename Value_T>
122
+ inline Hash::Iterator<Hash_Ptr_T, Value_T> Hash::Iterator<Hash_Ptr_T, Value_T>::operator++(int)
123
+ {
124
+ Iterator copy(*this);
125
+ ++(*this);
126
+ return copy;
127
+ }
128
+
129
+ template<typename Hash_Ptr_T, typename Value_T>
130
+ inline Value_T Hash::Iterator<Hash_Ptr_T, Value_T>::operator*()
131
+ {
132
+ return Value_T(const_cast<Hash*>(hash_), current_key());
133
+ }
134
+
135
+ template<typename Hash_Ptr_T, typename Value_T>
136
+ inline Value_T* Hash::Iterator<Hash_Ptr_T, Value_T>::operator->()
137
+ {
138
+ this->tmp_ = Entry(const_cast<Hash*>(hash_), current_key());
139
+ return &tmp_;
140
+ }
141
+
142
+ template<typename Hash_Ptr_T, typename Value_T>
143
+ inline bool Hash::Iterator<Hash_Ptr_T, Value_T>::operator==(Iterator const& rhs) const
144
+ {
145
+ return hash_->value() == rhs.hash_->value()
146
+ && current_index_ == rhs.current_index_;
147
+ }
148
+
149
+ template<typename Hash_Ptr_T, typename Value_T>
150
+ inline bool Hash::Iterator<Hash_Ptr_T, Value_T>::operator!=(Iterator const& rhs) const
151
+ {
152
+ return !(*this == rhs);
153
+ }
154
+
155
+ template<typename Hash_Ptr_T, typename Value_T>
156
+ inline Object Hash::Iterator<Hash_Ptr_T, Value_T>::current_key()
157
+ {
158
+ return hash_keys()[current_index_];
159
+ }
160
+
161
+ template<typename Hash_Ptr_T, typename Value_T>
162
+ inline Array Hash::Iterator<Hash_Ptr_T, Value_T>::hash_keys()
163
+ {
164
+ if (NIL_P(keys_))
165
+ {
166
+ keys_ = rb_funcall(hash_->value(), rb_intern("keys"), 0, 0);
167
+ }
168
+
169
+ return Array(keys_);
170
+ }
171
+
172
+ inline Hash::iterator Hash::begin()
173
+ {
174
+ return iterator(this);
175
+ }
176
+
177
+ inline Hash::const_iterator Hash::begin() const
178
+ {
179
+ return const_iterator(this);
180
+ }
181
+
182
+ inline Hash::iterator Hash::end()
183
+ {
184
+ return iterator(this, (int)size());
185
+ }
186
+
187
+ inline Hash::const_iterator Hash::end() const
188
+ {
189
+ return const_iterator(this, (int)size());
190
+ }
191
+
192
+ inline bool operator<(Rice::Hash::Entry const& lhs, Rice::Hash::Entry const& rhs)
193
+ {
194
+ Rice::Object lhs_key(lhs.key);
195
+ Rice::Object rhs_key(rhs.key);
196
+ if (lhs_key < rhs_key)
197
+ {
198
+ return true;
199
+ }
200
+ else if (lhs_key > rhs_key)
201
+ {
202
+ return false;
203
+ }
204
+ else if (Rice::Object(lhs.value.value()) < Rice::Object(rhs.value.value()))
205
+ {
206
+ return true;
207
+ }
208
+ else
209
+ {
210
+ return false;
211
+ }
212
+ }
213
+ }
214
+
215
+ namespace Rice::detail
216
+ {
217
+ template<>
218
+ struct Type<Hash>
219
+ {
220
+ static bool verify()
221
+ {
222
+ return true;
223
+ }
224
+ };
225
+
226
+ template<>
227
+ class To_Ruby<Hash>
228
+ {
229
+ public:
230
+ VALUE convert(Hash const& x)
231
+ {
232
+ return x.value();
233
+ }
234
+ };
235
+
236
+ template<>
237
+ class From_Ruby<Hash>
238
+ {
239
+ public:
240
+ Convertible is_convertible(VALUE value)
241
+ {
242
+ switch (rb_type(value))
243
+ {
244
+ case RUBY_T_HASH:
245
+ return Convertible::Exact;
246
+ break;
247
+ default:
248
+ return Convertible::None;
249
+ }
250
+ }
251
+
252
+ Hash convert(VALUE value)
253
+ {
254
+ return Hash(value);
255
+ }
256
+ };
257
+ }
@@ -1,8 +1,6 @@
1
1
  #ifndef Rice__Identifier__hpp_
2
2
  #define Rice__Identifier__hpp_
3
3
 
4
- #include <string>
5
-
6
4
  namespace Rice
7
5
  {
8
6
  class Symbol;
@@ -20,10 +18,10 @@ namespace Rice
20
18
  Identifier(Symbol const& symbol);
21
19
 
22
20
  //! Construct a new Identifier from a c string.
23
- Identifier(char const* s);
21
+ Identifier(char const* s, Encoding encoding = Encoding::utf8());
24
22
 
25
23
  //! Construct a new Identifier from a string.
26
- Identifier(std::string const& string);
24
+ Identifier(std::string const& string, Encoding encoding = Encoding::utf8());
27
25
 
28
26
  //! Return a string representation of the Identifier.
29
27
  char const* c_str() const;
@@ -45,6 +43,4 @@ namespace Rice
45
43
  };
46
44
  } // namespace Rice
47
45
 
48
- #include "Identifier.ipp"
49
-
50
46
  #endif // Rice__Identifier__hpp_
@@ -4,12 +4,14 @@ namespace Rice
4
4
  {
5
5
  }
6
6
 
7
- inline Identifier::Identifier(char const* s) : id_(rb_intern(s))
7
+ inline Identifier::Identifier(char const* name, Encoding encoding)
8
8
  {
9
+ this->id_ = detail::protect(rb_intern3, name, (long)strlen(name), encoding);
9
10
  }
10
11
 
11
- inline Identifier::Identifier(std::string const& s) : id_(rb_intern2(s.c_str(), s.size()))
12
+ inline Identifier::Identifier(const std::string& name, Encoding encoding)
12
13
  {
14
+ this->id_ = detail::protect(rb_intern3, name.c_str(), (long)name.size(), encoding);
13
15
  }
14
16
 
15
17
  inline char const* Identifier::c_str() const