Rice  1.5.2
 All Classes Files Functions Variables Typedefs Friends Pages
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 "detail/traits.hpp"
9 #include <iterator>
10 
11 namespace Rice
12 {
13 
24 class Hash
25  : public Builtin_Object<RHash, T_HASH>
26 {
27 public:
29  Hash();
30 
32 
34  Hash(Object v);
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:
83  iterator begin();
84 
86  const_iterator begin() const;
87 
89  iterator end();
90 
92  const_iterator end() const;
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:
126  Entry(Hash hash, Object key);
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>
148 class Hash::Iterator
149  : public std::iterator<
150  std::input_iterator_tag,
151  Value_T>
152 {
153 public:
155  Iterator(Hash_Ref_T hash);
156 
158  Iterator(Hash_Ref_T hash, int start_at);
159 
161  Iterator(Iterator const & iterator);
162 
165  template<typename Iterator_T>
166  Iterator(Iterator_T const & iterator);
167 
169  Iterator & operator=(Iterator const & rhs);
170 
172  Iterator & operator++();
173 
175  Iterator operator++(int);
176 
178  Value_T operator*();
179 
181  Value_T * operator->();
182 
184  bool operator==(Iterator const & rhs) const;
185 
187  bool operator!=(Iterator const & rhs) const;
188 
189  template<typename Hash_Ref_T_, typename Value_T_>
190  friend class Hash::Iterator;
191 
193  void swap(Iterator & iterator);
194 
195 protected:
196  Object current_key();
197 
198  Array hash_keys();
199 
200 private:
201  Hash hash_;
202  int 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 
Object operator=(T const &value)
Assignment operator.
Iterator< Hash &, Entry > iterator
An iterator.
Definition: Hash.hpp:72
Proxy & second
An alias for the value.
Definition: Hash.hpp:135
Object(VALUE value=Qnil)
Encapsulate an existing ruby object.
Definition: Object_defn.hpp:27
VALUE value() const
Explicit conversion to VALUE.
A smartpointer-like wrapper for Ruby builtin objects.
Definition: Builtin_Object_defn.hpp:18
Value_T * operator->()
Dereference operator.
Proxy(Hash hash, Object key)
Construct a new Proxy.
Iterator< Hash const &, Entry const > const_iterator
A const iterator.
Definition: Hash.hpp:79
Value_T operator*()
Dereference operator.
size_t size() const
Return the number of elements in the hash.
Definition: Hash.hpp:24
Iterator & operator++()
Preincrement operator.
iterator begin()
Return an iterator to the beginning of the hash.
Proxy value
The value.
Definition: Hash.hpp:134
A helper class so hash[key]=value can work.
Definition: Hash.hpp:96
Iterator & operator=(Iterator const &rhs)
Assignment operator.
A helper class for dereferencing iterators.
Definition: Hash.hpp:122
Object const key
The key.
Definition: Hash.hpp:131
Hash()
Construct a new hash.
A helper class for implementing iterators for a Hash.
Definition: Hash.hpp:72
bool operator==(Iterator const &rhs) const
Equality operator.
The base class for all Objects.
Definition: Object_defn.hpp:23
bool operator!=(Iterator const &rhs) const
Inequality operator.
void swap(Iterator &iterator)
Swap with another iterator of the same type.
Entry(Hash hash, Object key)
Construct a new Entry.
Iterator(Hash_Ref_T hash)
Construct a new Iterator.
iterator end()
Return an iterator to the end of the hash.
A wrapper for the ruby Array class.
Definition: Array.hpp:22
Proxy const operator[](Key_T const &key) const
Get the value for the given key.
Object const & first
An alias for the key.
Definition: Hash.hpp:132