Rice  3.0.0
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<T_ARRAY>
24 {
25 public:
27  Array();
28 
30 
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, long n>
50  Array(T const (& a)[n]);
51 
52 public:
54  long size() const;
55 
57 
62  Object operator[](long index) const;
63 
64 private:
66  class Proxy;
67 
68 public:
70 
75  Proxy operator[](long index);
76 
78 
81  template<typename T>
82  Object push(T const & obj);
83 
85 
89 
91 
94  template<typename T>
95  Object unshift(T const & obj);
96 
98 
101 
102 private:
103  template<typename Array_Ref_T, typename Value_T>
104  class Iterator;
105 
106  long position_of(long index) const;
107 
108 public:
110  typedef Iterator<Array &, Proxy> iterator;
111 
113  typedef Iterator<Array const &, Object> const_iterator;
114 
117 
120 
123 
126 };
127 
130 {
131 public:
133  Proxy(Array array, long index);
134 
136  operator Object() const;
137 
139  VALUE value() const;
140 
142  template<typename T>
143  Object operator=(T const & value);
144 
145 private:
146  Array array_;
147  long index_;
148 };
149 
151 // TODO: This really should be a random-access iterator.
152 template<typename Array_Ref_T, typename Value_T>
153 class Array::Iterator
154 {
155 public:
156  using iterator_category = std::forward_iterator_tag;
157  using value_type = Value_T;
158  using difference_type = long;
159  using pointer = Object*;
160  using reference = Value_T&;
161 
162  Iterator(Array_Ref_T array, long index);
163 
164  template<typename Array_Ref_T_, typename Value_T_>
165  Iterator(Iterator<Array_Ref_T_, Value_T_> const & rhs);
166 
167  template<typename Array_Ref_T_, typename Value_T_>
168  Iterator & operator=(Iterator<Array_Ref_T_, Value_T_> const & rhs);
169 
170  Iterator & operator++();
171  Iterator operator++(int);
172  Value_T operator*();
173  Object * operator->();
174 
175  template<typename Array_Ref_T_, typename Value_T_>
176  bool operator==(Iterator<Array_Ref_T_, Value_T_> const & rhs) const;
177 
178  template<typename Array_Ref_T_, typename Value_T_>
179  bool operator!=(Iterator<Array_Ref_T_, Value_T_> const & rhs) const;
180 
181  // Causes ICE on g++ 3.3.3
182  // template<typename Array_Ref_T_, typename Value_T_>
183  // friend class Iterator;
184 
185  Array_Ref_T array() const;
186  long index() const;
187 
188 private:
189  Array_Ref_T array_;
190  long index_;
191 
192  Object tmp_;
193 };
194 
195 } // namespace Rice
196 
197 template<>
198 inline
199 Rice::Array from_ruby<Rice::Array>(Rice::Object x)
200 {
201  return Rice::Array(x);
202 }
203 
204 template<>
205 inline
206 Rice::Object to_ruby<Rice::Array>(Rice::Array const & x)
207 {
208  return x;
209 }
210 
211 #include "Array.ipp"
212 
213 #endif // Rice__Array__hpp_
214 
A helper class so array[index]=value can work.
Definition: Array.hpp:130
VALUE value() const
Explicit conversion to VALUE.
Object operator=(T const &value)
Assignment operator.
Proxy(Array array, long index)
Construct a new Proxy.
A wrapper for the ruby Array class.
Definition: Array.hpp:24
Array()
Construct a new array.
Array(Iter_T begin, Iter_T end)
Construct an array from a sequence.
Array(VALUE v)
Wrap an existing array.
long size() const
Return the size of the array.
iterator end()
Return an iterator to the end of the array.
Array(Object v)
Wrap an existing array.
Object unshift(T const &obj)
Unshift an element onto the beginning of the array.
Object operator[](long index) const
Return the element at the given index.
const_iterator begin() const
Return a const iterator to the beginning of the array.
const_iterator end() const
Return a const iterator to the end of the array.
Proxy operator[](long index)
Return a reference to the element at the given index.
iterator begin()
Return an iterator to the beginning of the array.
Object push(T const &obj)
Push an element onto the end of the array.
Object pop()
Pop an element from the end of the array.
Object shift()
Shift an element from the beginning of the array.
Iterator< Array const &, Object > const_iterator
A const iterator.
Definition: Array.hpp:113
Iterator< Array &, Proxy > iterator
An iterator.
Definition: Array.hpp:110
Array(T const (&a)[n])
Construct an Array from a C array.
A smartpointer-like wrapper for Ruby builtin objects.
Definition: Builtin_Object_defn.hpp:20
RObject & operator*() const
Return a reference to obj_.
Definition: Builtin_Object_defn.hpp:34
RObject * operator->() const
Return a pointer to obj_.
Definition: Builtin_Object_defn.hpp:35
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