Rice  1.5.2
 All Classes Files Functions Variables Typedefs Friends Pages
Array.hpp
1 #ifndef Rice__Array__hpp_
2 #define Rice__Array__hpp_
3 
4 #include "Builtin_Object.hpp"
5 #include "to_from_ruby_defn.hpp"
6 #include "detail/ruby.hpp"
7 #include <iterator>
8 
9 namespace Rice
10 {
11 
13 
22 class Array
23  : public Builtin_Object<RArray, T_ARRAY>
24 {
25 public:
27  Array();
28 
30 
32  Array(Object v);
33 
35 
37  Array(VALUE v);
38 
40 
43  template<typename Iter_T>
44  Array(Iter_T begin, Iter_T end);
45 
47 
49  template<typename T, size_t n>
50  Array(T const (& a)[n]);
51 
52 public:
54  size_t size() const;
55 
57 
62  Object operator[](ptrdiff_t index) const;
63 
64 private:
66  class Proxy;
67 
68 public:
70 
75  Proxy operator[](ptrdiff_t index);
76 
78 
81  template<typename T>
82  Object push(T const & obj);
83 
85 
88  Object pop();
89 
91 
94  template<typename T>
95  Object unshift(T const & obj);
96 
98 
100  Object shift();
101 
104 
106  VALUE * to_c_array();
107 
108 private:
109  template<typename Array_Ref_T, typename Value_T>
110  class Iterator;
111 
112  size_t position_of(ptrdiff_t index) const;
113 
114 public:
116  typedef Iterator<Array &, Proxy> iterator;
117 
119  typedef Iterator<Array const &, Object> const_iterator;
120 
122  iterator begin();
123 
125  const_iterator begin() const;
126 
128  iterator end();
129 
131  const_iterator end() const;
132 };
133 
136 {
137 public:
139  Proxy(Array array, size_t index);
140 
142  operator Object() const;
143 
145  VALUE value() const;
146 
148  template<typename T>
149  Object operator=(T const & value);
150 
151 private:
152  Array array_;
153  size_t index_;
154 };
155 
157 // TODO: This really should be a random-access iterator.
158 template<typename Array_Ref_T, typename Value_T>
159 class Array::Iterator
160  : public std::iterator<
161  std::forward_iterator_tag,
162  Value_T, // Type
163  ptrdiff_t, // Distance type
164  Object *, // Pointer type
165  Value_T &> // Reference type
166 {
167 public:
168  Iterator(Array_Ref_T array, size_t index);
169 
170  template<typename Array_Ref_T_, typename Value_T_>
171  Iterator(Iterator<Array_Ref_T_, Value_T_> const & rhs);
172 
173  template<typename Array_Ref_T_, typename Value_T_>
174  Iterator & operator=(Iterator<Array_Ref_T_, Value_T_> const & rhs);
175 
176  Iterator & operator++();
177  Iterator operator++(int);
178  Value_T operator*();
179  Object * operator->();
180 
181  template<typename Array_Ref_T_, typename Value_T_>
182  bool operator==(Iterator<Array_Ref_T_, Value_T_> const & rhs) const;
183 
184  template<typename Array_Ref_T_, typename Value_T_>
185  bool operator!=(Iterator<Array_Ref_T_, Value_T_> const & rhs) const;
186 
187  // Causes ICE on g++ 3.3.3
188  // template<typename Array_Ref_T_, typename Value_T_>
189  // friend class Iterator;
190 
191  Array_Ref_T array() const;
192  size_t index() const;
193 
194 private:
195  Array_Ref_T array_;
196  size_t index_;
197 
198  Object tmp_;
199 };
200 
201 } // namespace Rice
202 
203 template<>
204 inline
205 Rice::Array from_ruby<Rice::Array>(Rice::Object x)
206 {
207  return Rice::Array(x);
208 }
209 
210 template<>
211 inline
212 Rice::Object to_ruby<Rice::Array>(Rice::Array const & x)
213 {
214  return x;
215 }
216 
217 #include "Array.ipp"
218 
219 #endif // Rice__Array__hpp_
220 
size_t size() const
Return the size of the array.
iterator end()
Return an iterator to the end of the array.
Object(VALUE value=Qnil)
Encapsulate an existing ruby object.
Definition: Object_defn.hpp:27
A smartpointer-like wrapper for Ruby builtin objects.
Definition: Builtin_Object_defn.hpp:18
Object operator[](ptrdiff_t index) const
Return the element at the given index.
RArray * operator->() const
Return a pointer to obj_.
Definition: Builtin_Object_defn.hpp:35
A helper class so array[index]=value can work.
Definition: Array.hpp:135
iterator begin()
Return an iterator to the beginning of the array.
Object pop()
Pop an element from the end of the array.
Object unshift(T const &obj)
Unshift an element onto the beginning of the array.
VALUE * to_c_array()
Object operator=(T const &value)
Assignment operator.
The base class for all Objects.
Definition: Object_defn.hpp:23
Proxy(Array array, size_t index)
Construct a new Proxy.
Iterator< Array const &, Object > const_iterator
A const iterator.
Definition: Array.hpp:119
Object push(T const &obj)
Push an element onto the end of the array.
A wrapper for the ruby Array class.
Definition: Array.hpp:22
Array()
Construct a new array.
Iterator< Array &, Proxy > iterator
An iterator.
Definition: Array.hpp:116
VALUE value() const
Explicit conversion to VALUE.
Object shift()
Shift an element from the beginning of the array.
RArray & operator*() const
Return a reference to obj_.
Definition: Builtin_Object_defn.hpp:34