Vector.hpp
1 #ifndef OPALS_VECTOR_HPP_INCLUDED
2 #define OPALS_VECTOR_HPP_INCLUDED
3 
4 #include <opals/fwd.hpp>
5 
6 #include <cstddef>
7 
8 namespace std {
9  struct random_access_iterator_tag;
10 }
11 
12 namespace opals {
13 
14  /**
15  \class Vector
16  \tparam T the element type
17 
18  \brief A template class representing an STL-conforming std::vector<T>
19 
20  Symbols for Vector are exported to opals DLLs as required by their modules' interfaces,
21  meaning that DLL users are able to instantiate Vector<T> only with the corresponding template arguments.
22  The interface of opals::Vector differs slightly from std::vector:
23  no template constructors, no template functions are provided, but these are represented
24  as non-template overloads for Vector::iterator, Vector::const_iterator, Vector::reverse_iterator, and Vector::const_reverse_iterator.
25 
26  \author wk
27  \date 02.02.2011
28 
29  \internal
30  Conversion constructors and operators for std::vector<T> and its iterators are provided for internal use only.
31  If T is another interface class (e.g. opals::String),
32  then use function toInternal() to convert to std::vector<std::string>.
33  opals::Vector encapsulates STL's std::vector, whose implementation it hides from the
34  interface in order to avoid problems when using the interface / opals-DLLs
35  with different STL-implementations.
36  As opals data types are not required to be comparable,
37  no non-template member operators are provided in the interface.
38  */
39 
40  template<class T>
41  class Vector
42  {
43  class Impl;
44  Impl *pimpl_;
45 
46  public:
47 
48  typedef T value_type;
49  typedef T* pointer;
50  typedef T& reference;
51  typedef T const& const_reference;
52  typedef std::size_t size_type;
53  typedef std::ptrdiff_t difference_type;
54 
55  template<bool Const,bool Forward>
56  class Iterator;
57 
62 
63  /// \name construction / destruction
64  ///@{
65  Vector();
66  Vector( size_type n );
67  Vector( size_type n, const T& val );
68  Vector( const Vector& other );
69  Vector( iterator beg, iterator end );
73 
74  ~Vector();
75  ///}@
76 
77  /// \name assignment, swapping
78  ///@{
79  Vector& operator=( const Vector& other );
80  void swap( Vector& other );
81  ///}@
82 
83  /// \name iterator traversal
84  /// forward/reverse, const/non-const
85  ///@{
86  iterator begin();
87  iterator end();
88  const_iterator begin() const;
89  const_iterator end() const;
90 
91  reverse_iterator rbegin();
92  reverse_iterator rend();
93  const_reverse_iterator rbegin() const;
94  const_reverse_iterator rend() const;
95  ///}@
96 
97  /// \name direct data access
98  ///@{
99  reference operator[]( size_type n );
100  const_reference operator[]( size_type n ) const;
101 
102  reference front();
103  reference back();
104  const_reference front() const;
105  const_reference back() const;
106  ///}@
107 
108  /// \name size
109  ///@{
110  size_type size() const;
111  size_type max_size() const;
112  size_type capacity() const;
113  bool empty() const;
114  void clear();
115  void resize( size_type n, T val = T() );
116  void reserve( size_type n );
117  ///}@
118 
119  /// \name insertion
120  ///@{
121  void push_back( const T& val );
122 
123  iterator insert( iterator pos, const T& val );
124  void insert( iterator pos, size_type n, const T& val );
125  void insert( iterator pos, iterator beg, iterator end );
126  void insert( iterator pos, const_iterator beg, const_iterator end );
127  void insert( iterator pos, reverse_iterator beg, reverse_iterator end );
128  void insert( iterator pos, const_reverse_iterator beg, const_reverse_iterator end );
129  ///}@
130 
131  /// \name removal
132  ///@{
133  void pop_back();
134  iterator erase( iterator pos );
135  iterator erase( iterator beg, iterator end );
136  ///}@
137 
138  /**
139  \class Iterator
140  \tparam Const constant-ness
141  \tparam Forward forward/reverse traversal
142 
143  \brief A random access iterator class for Vector
144 
145  \author wk
146  \date 02.02.2011
147  */
148  template<bool Const,bool Forward>
149  class Iterator
150  {
151  class Impl;
152  Impl *pimpl_;
153  public:
154  template<class T1,bool Const1>
155  struct ConstSel { typedef T1 Type; };
156  template<class T1>
157  struct ConstSel<T1,true> { typedef const T1 Type; };
158 
159  public:
160  typedef std::random_access_iterator_tag iterator_category;
161  typedef T value_type;
162  typedef typename Vector<T>::difference_type difference_type;
163  typedef typename ConstSel<T,Const>::Type& reference;
164  typedef typename ConstSel<T,Const>::Type* pointer;
166 
167  Iterator();
168  Iterator( const Iterator& other );
169  operator ConstIterator() const;
170  ~Iterator();
171  Iterator &operator= ( const Iterator& other );
172  reference operator* () const;
173  pointer operator->() const;
174  Iterator& operator++();
175  Iterator operator++(int);
176  Iterator& operator--();
177  Iterator operator--(int);
178  Iterator& operator+=( difference_type offset );
179  Iterator operator+ ( difference_type offset ) const;
180  Iterator& operator-=( difference_type offset );
181  Iterator operator- ( difference_type offset ) const;
182  difference_type operator-( const Iterator<true, Forward>& other ) const;
183  difference_type operator-( const Iterator<false,Forward>& other ) const;
184  reference operator[]( difference_type offset ) const;
185 
186  bool operator==( const Iterator<true, Forward>& other ) const;
187  bool operator==( const Iterator<false,Forward>& other ) const;
188  bool operator!=( const Iterator<true, Forward>& other ) const;
189  bool operator!=( const Iterator<false,Forward>& other ) const;
190  bool operator< ( const Iterator<true, Forward>& other ) const;
191  bool operator< ( const Iterator<false,Forward>& other ) const;
192  bool operator> ( const Iterator<true, Forward>& other ) const;
193  bool operator> ( const Iterator<false,Forward>& other ) const;
194  bool operator<=( const Iterator<true, Forward>& other ) const;
195  bool operator<=( const Iterator<false,Forward>& other ) const;
196  bool operator>=( const Iterator<true, Forward>& other ) const;
197  bool operator>=( const Iterator<false,Forward>& other ) const;
198 
199  };
200 
201  };
202 
203 }
204 
205 #endif