Rice  3.0.0
Hash.hpp
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 <iterator>
9 #include <type_traits>
10 
11 namespace Rice
12 {
13 
24 class Hash
25  : public Builtin_Object<T_HASH>
26 {
27 public:
29  Hash();
30 
32 
35 
37  size_t size() const;
38 
39 private:
41  class Proxy;
42 
43 public:
45 
48  template<typename Key_T>
49  Proxy const operator[](Key_T const & key) const;
50 
52 
55  template<typename Key_T>
56  Proxy operator[](Key_T const & key);
57 
59 
64  template<typename Value_T, typename Key_T>
65  Value_T get(Key_T const & key);
66 
68  class Entry;
69 
71  template<typename Hash_Ref_T, typename Value_T>
72  class Iterator;
73 
74 public:
77 
80 
81 public:
84 
87 
90 
93 };
94 
97 {
98 public:
100  Proxy(Hash hash, Object key);
101 
103  operator Object() const;
104 
106  VALUE value() const;
107 
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 
120 
123 {
124 public:
127 
129  Entry(Entry const & entry);
130 
131  Object const key;
132  Object const & first;
133 
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 
147 template<typename Hash_Ref_T, typename Value_T>
149 {
150 public:
151  using iterator_category = std::input_iterator_tag;
152  using value_type = Value_T;
153  using difference_type = long;
154  using pointer = Object*;
155  using reference = Value_T&;
156 
158  Iterator(Hash_Ref_T hash);
159 
161  Iterator(Hash_Ref_T hash, int start_at);
162 
165 
168  template<typename Iterator_T>
169  Iterator(Iterator_T const & iterator);
170 
172  Iterator & operator=(Iterator const & rhs);
173 
176 
179 
181  Value_T operator*();
182 
184  Value_T * operator->();
185 
187  bool operator==(Iterator const & rhs) const;
188 
190  bool operator!=(Iterator const & rhs) const;
191 
192  template<typename Hash_Ref_T_, typename Value_T_>
193  friend class Hash::Iterator;
194 
197 
198 protected:
199  Object current_key();
200 
201  Array hash_keys();
202 
203 private:
204  Hash hash_;
205  size_t current_index_;
206  VALUE keys_;
207 
208  mutable typename std::remove_const<Value_T>::type tmp_;
209 };
210 
211 } // namespace Rice
212 
213 template<>
214 inline
215 Rice::Hash from_ruby<Rice::Hash>(Rice::Object x)
216 {
217  return Rice::Hash(x);
218 }
219 
220 template<>
221 inline
222 Rice::Object to_ruby<Rice::Hash>(Rice::Hash const & x)
223 {
224  return x;
225 }
226 
227 #include "Hash.ipp"
228 
229 #endif // Rice__Hash__hpp_
230 
A wrapper for the ruby Array class.
Definition: Array.hpp:24
A smartpointer-like wrapper for Ruby builtin objects.
Definition: Builtin_Object_defn.hpp:20
A helper class for dereferencing iterators.
Definition: Hash.hpp:123
Object const key
The key.
Definition: Hash.hpp:131
Proxy value
The value.
Definition: Hash.hpp:134
Entry(Entry const &entry)
Copy constructor.
Proxy & second
An alias for the value.
Definition: Hash.hpp:135
Object const & first
An alias for the key.
Definition: Hash.hpp:132
Entry(Hash hash, Object key)
Construct a new Entry.
A helper class for implementing iterators for a Hash.
Definition: Hash.hpp:149
Iterator & operator++()
Preincrement operator.
Iterator(Hash_Ref_T hash, int start_at)
Construct a new Iterator with a given start-at index point.
Iterator(Iterator_T const &iterator)
Iterator(Hash_Ref_T hash)
Construct a new Iterator.
Value_T * operator->()
Dereference operator.
Iterator & operator=(Iterator const &rhs)
Assignment operator.
Iterator operator++(int)
Postincrement operator.
bool operator!=(Iterator const &rhs) const
Inequality operator.
Value_T operator*()
Dereference operator.
void swap(Iterator &iterator)
Swap with another iterator of the same type.
bool operator==(Iterator const &rhs) const
Equality operator.
Iterator(Iterator const &iterator)
Copy construct an Iterator.
A helper class so hash[key]=value can work.
Definition: Hash.hpp:97
Object operator=(T const &value)
Assignment operator.
Proxy(Hash hash, Object key)
Construct a new Proxy.
VALUE value() const
Explicit conversion to VALUE.
Definition: Hash.hpp:26
size_t size() const
Return the number of elements in the hash.
Value_T get(Key_T const &key)
Get the value for the given key.
Hash(Object v)
Wrap an existing hash.
Iterator< Hash &, Entry > iterator
An iterator.
Definition: Hash.hpp:72
iterator end()
Return an iterator to the end of the hash.
Proxy const operator[](Key_T const &key) const
Get the value for the given key.
iterator begin()
Return an iterator to the beginning of the hash.
Iterator< Hash const &, Entry const > const_iterator
A const iterator.
Definition: Hash.hpp:79
Hash()
Construct a new hash.
const_iterator end() const
Return a const to the end of the hash.
Proxy operator[](Key_T const &key)
Get the value for the given key.
const_iterator begin() const
Return a const iterator to the beginning of the hash.
The base class for all Objects.
Definition: Object_defn.hpp:25
Object(VALUE value=Qnil)
Encapsulate an existing ruby object.
Definition: Object_defn.hpp:28