DDS-PSM-C++  1.0
ISO C++ API for OpenSplice DDS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
array.hpp
Go to the documentation of this file.
1 #ifndef ORG_OMG_DDS_CORE_ARRAY_HPP_
2 #define ORG_OMG_DDS_CORE_ARRAY_HPP_
3 
4 #include <string>
5 
6 // NOTICE:
7 // This array implementation is derived from the GNU C++ std library
8 // and is licensed under LGPL.
9 // This code is intented to show a possible implementation of the
10 // array class as well as to provide the mandatory API that compliant
11 // implementation of the DDS-PSM-C++ API have to conform with.
12 
13 namespace dds {
14  namespace core {
15  template <typename T> typename T::iterator begin(T& t) {
16  return t.begin();
17  }
18 
19  template <typename T> typename T::iterator end(T& t) {
20  return t.end();
21  }
22 
23  template <typename T> typename T::const_iterator begin(const T& t) {
24  return t.begin();
25  }
26 
27  template <typename T> typename T::const_iterator end(const T& t) {
28  return t.end();
29  }
30 
31 
46  template<typename _Tp, std::size_t _Nm>
47  struct array
48  {
49  typedef _Tp value_type;
50  typedef _Tp* pointer;
51  typedef const _Tp* const_pointer;
53  typedef const value_type& const_reference;
54  typedef value_type* iterator;
55  typedef const value_type* const_iterator;
56  typedef std::size_t size_type;
57  typedef std::ptrdiff_t difference_type;
58  typedef std::reverse_iterator<iterator> reverse_iterator;
59  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
60 
61  // Support for zero-sized arrays mandatory.
62  value_type _M_instance[_Nm ? _Nm : 1];
63 
64  // No explicit construct/copy/destroy for aggregate type.
65 
66  // DR 776.
67  void
68  fill(const value_type& __u)
69  { std::fill_n(begin(), size(), __u); }
70 
71  void
72  swap(array& __other)
73  { std::swap_ranges(begin(), end(), __other.begin()); }
74 
75  // Iterators.
76  iterator
78  { return iterator(&_M_instance[0]); }
79 
81  begin() const
82  { return const_iterator(&(_M_instance[0])); }
83 
84  iterator
85  end()
86  { return iterator(&(_M_instance[_Nm])); }
87 
89  end() const
90  { return const_iterator(&(_M_instance[_Nm])); }
91 
94  { return reverse_iterator(end()); }
95 
97  rbegin() const
98  { return const_reverse_iterator(end()); }
99 
102  { return reverse_iterator(begin()); }
103 
105  rend() const
106  { return const_reverse_iterator(begin()); }
107 
109  cbegin() const
110  { return const_iterator(&(_M_instance[0])); }
111 
113  cend() const
114  { return const_iterator(&(_M_instance[_Nm])); }
115 
117  crbegin() const
118  { return const_reverse_iterator(end()); }
119 
121  crend() const
122  { return const_reverse_iterator(begin()); }
123 
124  // Capacity.
125  size_type
126  size() const { return _Nm; }
127 
128  size_type
129  max_size() const { return _Nm; }
130 
131  bool
132  empty() const { return size() == 0; }
133 
134  // Element access.
135  reference
137  { return _M_instance[__n]; }
138 
141  { return _M_instance[__n]; }
142 
143  reference
145  {
146  if (__n >= _Nm)
147  std::__throw_out_of_range(__N("array::at"));
148  return _M_instance[__n];
149  }
150 
152  at(size_type __n) const
153  {
154  if (__n >= _Nm)
155  std::__throw_out_of_range(__N("array::at"));
156  return _M_instance[__n];
157  }
158 
159  reference
161  { return *begin(); }
162 
164  front() const
165  { return *begin(); }
166 
167  reference
169  { return _Nm ? *(end() - 1) : *end(); }
170 
172  back() const
173  { return _Nm ? *(end() - 1) : *end(); }
174 
175  _Tp*
177  { return &(_M_instance[0]); }
178 
179  const _Tp*
180  data() const
181  { return &(_M_instance[0]); }
182  };
183 
184  // Array comparisons.
185  template<typename _Tp, std::size_t _Nm>
186  inline bool
187  operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
188  { return std::equal(__one.begin(), __one.end(), __two.begin()); }
189 
190  template<typename _Tp, std::size_t _Nm>
191  inline bool
192  operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
193  { return !(__one == __two); }
194 
195  template<typename _Tp, std::size_t _Nm>
196  inline bool
197  operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
198  {
199  return std::lexicographical_compare(__a.begin(), __a.end(),
200  __b.begin(), __b.end());
201  }
202 
203  template<typename _Tp, std::size_t _Nm>
204  inline bool
205  operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
206  { return __two < __one; }
207 
208  template<typename _Tp, std::size_t _Nm>
209  inline bool
210  operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
211  { return !(__one > __two); }
212 
213  template<typename _Tp, std::size_t _Nm>
214  inline bool
215  operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
216  { return !(__one < __two); }
217 
219  template<std::size_t _Int, typename _Tp>
221 
222  template<typename _Tp, std::size_t _Nm>
223  struct tuple_size<array<_Tp, _Nm> >
224  { static const std::size_t value = _Nm; };
225 
226  template<typename _Tp, std::size_t _Nm>
227  const std::size_t
228  tuple_size<array<_Tp, _Nm> >::value;
229 
230  template<std::size_t _Int, typename _Tp, std::size_t _Nm>
231  struct tuple_element<_Int, array<_Tp, _Nm> >
232  { typedef _Tp type; };
233 
234  template<std::size_t _Int, typename _Tp, std::size_t _Nm>
235  inline _Tp&
236  get(array<_Tp, _Nm>& __arr)
237  { return __arr[_Int]; }
238 
239  template<std::size_t _Int, typename _Tp, std::size_t _Nm>
240  inline const _Tp&
241  get(const array<_Tp, _Nm>& __arr)
242  { return __arr[_Int]; }
243  }
244 }
245 
246 
247 #endif /* ORG_OMG_DDS_CORE_ARRAY_HPP_ */