My Project
array.hpp
Go to the documentation of this file.
1 
28 #ifndef BSL_ARRAY_HPP
29 #define BSL_ARRAY_HPP
30 
31 #include "contiguous_iterator.hpp"
32 #include "cstdint.hpp"
33 #include "cstring.hpp"
35 #include "is_fundamental.hpp"
36 #include "numeric_limits.hpp"
37 #include "reverse_iterator.hpp"
38 
39 namespace bsl
40 {
62  template<typename T, bsl::uintmax N>
63  class array final
64  {
65  static_assert(N != 0, "arrays of size 0 are not supported");
66 
67  public:
69  T m_data[N]; // NOLINT
70 
72  using value_type = T;
78  using reference_type = T &;
80  using const_reference_type = T const &;
82  using pointer_type = T *;
84  using const_pointer_type = T const *;
93 
112  [[nodiscard]] constexpr pointer_type
113  at_if(size_type const index) noexcept
114  {
115  if (index >= N) {
116  return nullptr;
117  }
118 
119  return &m_data[index]; // PRQA S 4024 // NOLINT
120  }
121 
134  [[nodiscard]] constexpr const_pointer_type
135  at_if(size_type const index) const noexcept
136  {
137  if (index >= N) {
138  return nullptr;
139  }
140 
141  return &m_data[index]; // NOLINT
142  }
143 
151  [[nodiscard]] constexpr reference_type
152  front() noexcept
153  {
154  return *this->at_if(0U);
155  }
156 
164  [[nodiscard]] constexpr const_reference_type
165  front() const noexcept
166  {
167  return *this->at_if(0U);
168  }
169 
177  [[nodiscard]] constexpr pointer_type
178  front_if() noexcept
179  {
180  return this->at_if(0U);
181  }
182 
190  [[nodiscard]] constexpr const_pointer_type
191  front_if() const noexcept
192  {
193  return this->at_if(0U);
194  }
195 
203  [[nodiscard]] constexpr reference_type
204  back() noexcept
205  {
206  return *this->at_if(N - 1U);
207  }
208 
216  [[nodiscard]] constexpr const_reference_type
217  back() const noexcept
218  {
219  return *this->at_if(N - 1U);
220  }
221 
229  [[nodiscard]] constexpr pointer_type
230  back_if() noexcept
231  {
232  return this->at_if(N - 1U);
233  }
234 
242  [[nodiscard]] constexpr const_pointer_type
243  back_if() const noexcept
244  {
245  return this->at_if(N - 1U);
246  }
247 
255  [[nodiscard]] constexpr pointer_type
256  data() noexcept
257  {
258  return m_data; // NOLINT
259  }
260 
268  [[nodiscard]] constexpr const_pointer_type
269  data() const noexcept
270  {
271  return m_data; // NOLINT
272  }
273 
281  [[nodiscard]] constexpr iterator_type
282  begin() noexcept
283  {
284  return iterator_type{this->front_if(), N, 0U};
285  }
286 
294  [[nodiscard]] constexpr const_iterator_type
295  begin() const noexcept
296  {
297  return const_iterator_type{this->front_if(), N, 0U};
298  }
299 
307  [[nodiscard]] constexpr const_iterator_type
308  cbegin() const noexcept
309  {
310  return const_iterator_type{this->front_if(), N, 0U};
311  }
312 
321  [[nodiscard]] constexpr iterator_type
322  iter(size_type const i) noexcept
323  {
324  return iterator_type{this->front_if(), N, i};
325  }
326 
335  [[nodiscard]] constexpr const_iterator_type
336  iter(size_type const i) const noexcept
337  {
338  return const_iterator_type{this->front_if(), N, i};
339  }
340 
349  [[nodiscard]] constexpr const_iterator_type
350  citer(size_type const i) const noexcept
351  {
352  return const_iterator_type{this->front_if(), N, i};
353  }
354 
366  [[nodiscard]] constexpr iterator_type
367  end() noexcept
368  {
369  return iterator_type{this->front_if(), N, N};
370  }
371 
383  [[nodiscard]] constexpr const_iterator_type
384  end() const noexcept
385  {
386  return const_iterator_type{this->front_if(), N, N};
387  }
388 
400  [[nodiscard]] constexpr const_iterator_type
401  cend() const noexcept
402  {
403  return const_iterator_type{this->front_if(), N, N};
404  }
405 
419  [[nodiscard]] constexpr reverse_iterator_type
420  rbegin() noexcept
421  {
422  return reverse_iterator_type{this->end()};
423  }
424 
438  [[nodiscard]] constexpr const_reverse_iterator_type
439  rbegin() const noexcept
440  {
441  return const_reverse_iterator_type{this->end()};
442  }
443 
457  [[nodiscard]] constexpr const_reverse_iterator_type
458  crbegin() const noexcept
459  {
460  return const_reverse_iterator_type{this->cend()};
461  }
462 
477  [[nodiscard]] constexpr reverse_iterator_type
478  riter(size_type const i) noexcept
479  {
480  size_type const ai{(i >= N) ? N : (i + 1U)};
481  return reverse_iterator_type{this->iter(ai)};
482  }
483 
498  [[nodiscard]] constexpr const_reverse_iterator_type
499  riter(size_type const i) const noexcept
500  {
501  size_type const ai{(i >= N) ? N : (i + 1U)};
502  return const_reverse_iterator_type{this->iter(ai)};
503  }
504 
519  [[nodiscard]] constexpr const_reverse_iterator_type
520  criter(size_type const i) const noexcept
521  {
522  size_type const ai{(i >= N) ? N : (i + 1U)};
523  return const_reverse_iterator_type{this->citer(ai)};
524  }
525 
539  [[nodiscard]] constexpr reverse_iterator_type
540  rend() noexcept
541  {
542  return reverse_iterator_type{this->begin()};
543  }
544 
558  [[nodiscard]] constexpr const_reverse_iterator_type
559  rend() const noexcept
560  {
561  return const_reverse_iterator_type{this->begin()};
562  }
563 
577  [[nodiscard]] constexpr const_reverse_iterator_type
578  crend() const noexcept
579  {
580  return const_reverse_iterator_type{this->cbegin()};
581  }
582 
592  [[nodiscard]] static constexpr bool
593  empty() noexcept
594  {
595  return false;
596  }
597 
607  [[nodiscard]] static constexpr size_type
608  size() noexcept
609  {
610  return N;
611  }
612 
620  [[nodiscard]] static constexpr size_type
621  max_size() noexcept
622  {
623  return numeric_limits<size_type>::max() / sizeof(T);
624  }
625 
633  [[nodiscard]] static constexpr size_type
634  size_bytes() noexcept
635  {
636  return N * sizeof(T);
637  }
638  };
639 
654  template<typename T, bsl::uintmax N>
655  constexpr bool
656  operator==(bsl::array<T, N> const &lhs, bsl::array<T, N> const &rhs) noexcept
657  {
659  return bsl::builtin_memcmp(lhs.data(), rhs.data(), lhs.size_bytes()) == 0;
660  }
661 
662  for (bsl::uintmax i{}; i < lhs.size(); ++i) {
663  if (*lhs.at_if(i) != *rhs.at_if(i)) {
664  return false;
665  }
666  }
667 
668  return true;
669  }
670 
685  template<typename T, bsl::uintmax N>
686  constexpr bool
687  operator!=(bsl::array<T, N> const &lhs, bsl::array<T, N> const &rhs) noexcept
688  {
689  return !(lhs == rhs);
690  }
691 
693  template<typename T, typename... U>
694  array(T, U...) -> array<T, 1 + sizeof...(U)>;
695 }
696 
697 #endif
Provides a safe encapsulation for a C-style array, minicing the std::array APIs. This container is an...
Definition: array.hpp:63
T value_type
alias for: T
Definition: array.hpp:72
constexpr iterator_type end() noexcept
Returns an iterator to one past the last element of the array. If you attempt to access this iterator...
Definition: array.hpp:367
reverse_iterator< iterator_type > reverse_iterator_type
alias for: reverse_iterator<iterator>
Definition: array.hpp:90
If the provided type is a fundamental type, provides the member constant value equal to true....
Definition: is_fundamental.hpp:76
reverse_iterator< const_iterator_type > const_reverse_iterator_type
alias for: reverse_iterator<const_iterator>
Definition: array.hpp:92
bsl::uintmax difference_type
alias for: bsl::uintmax
Definition: array.hpp:76
constexpr bool operator!=(bsl::array< T, N > const &lhs, bsl::array< T, N > const &rhs) noexcept
Returns false if two arrays contain the same contents. Returns true otherwise.
Definition: array.hpp:687
constexpr pointer_type data() noexcept
Returns a pointer to the array being encapsulated.
Definition: array.hpp:256
constexpr const_iterator_type iter(size_type const i) const noexcept
Returns an iterator to the element "i" in the array.
Definition: array.hpp:336
constexpr const_reverse_iterator_type criter(size_type const i) const noexcept
Returns a reverse iterator element "i" in the array. When accessing the iterator, the iterator will a...
Definition: array.hpp:520
constexpr reverse_iterator_type riter(size_type const i) noexcept
Returns a reverse iterator element "i" in the array. When accessing the iterator, the iterator will a...
Definition: array.hpp:478
static constexpr bool empty() noexcept
Since arrays of size 0 are not allowed, always returns false.
Definition: array.hpp:593
constexpr iterator_type iter(size_type const i) noexcept
Returns an iterator to the element "i" in the array.
Definition: array.hpp:322
constexpr const_reverse_iterator_type crend() const noexcept
Returns a reverse iterator first element of the array. When accessing the iterator,...
Definition: array.hpp:578
constexpr pointer_type back_if() noexcept
Returns a pointer to the last element in the array.
Definition: array.hpp:230
static constexpr size_type size() noexcept
Returns the number of elements in the array being encapsulated.
Definition: array.hpp:608
T m_data[N]
stores the array being wrapped
Definition: array.hpp:65
constexpr const_reference_type back() const noexcept
Returns a reference to the last element in the array.
Definition: array.hpp:217
constexpr const_reference_type front() const noexcept
Returns a reference to the first element in the array.
Definition: array.hpp:165
constexpr const_iterator_type cend() const noexcept
Returns an iterator to one past the last element of the array. If you attempt to access this iterator...
Definition: array.hpp:401
constexpr pointer_type at_if(size_type const index) noexcept
Returns a pointer to the instance of T stored at index "index". If the index is out of bounds,...
Definition: array.hpp:113
static constexpr size_type max_size() noexcept
Returns the max number of elements the BSL supports.
Definition: array.hpp:621
constexpr bool operator==(bsl::array< T, N > const &lhs, bsl::array< T, N > const &rhs) noexcept
Returns true if two arrays contain the same contents. Returns false otherwise.
Definition: array.hpp:656
constexpr const_pointer_type front_if() const noexcept
Returns a pointer to the first element in the array.
Definition: array.hpp:191
constexpr const_pointer_type data() const noexcept
Returns a pointer to the array being encapsulated.
Definition: array.hpp:269
constexpr const_pointer_type at_if(size_type const index) const noexcept
Returns a pointer to the instance of T stored at index "index". If the index is out of bounds,...
Definition: array.hpp:135
T const & const_reference_type
alias for: T const &
Definition: array.hpp:80
static constexpr size_type size_bytes() noexcept
Returns size() * sizeof(T)
Definition: array.hpp:634
constexpr reference_type back() noexcept
Returns a reference to the last element in the array.
Definition: array.hpp:204
constexpr reverse_iterator_type rend() noexcept
Returns a reverse iterator first element of the array. When accessing the iterator,...
Definition: array.hpp:540
constexpr const_iterator_type cbegin() const noexcept
Returns an iterator to the first element of the array.
Definition: array.hpp:308
constexpr const_iterator_type end() const noexcept
Returns an iterator to one past the last element of the array. If you attempt to access this iterator...
Definition: array.hpp:384
constexpr const_iterator_type begin() const noexcept
Returns an iterator to the first element of the array.
Definition: array.hpp:295
T * pointer_type
alias for: T *
Definition: array.hpp:82
static constexpr T max() noexcept
Returns the max value of T.
Definition: numeric_limits.hpp:154
constexpr const_iterator_type citer(size_type const i) const noexcept
Returns an iterator to the element "i" in the array.
Definition: array.hpp:350
constexpr reverse_iterator_type rbegin() noexcept
Returns a reverse iterator to one past the last element of the array. When accessing the iterator,...
Definition: array.hpp:420
Provides a reverse iterator as defined by the C++ specification, with the follwing differences:
Definition: reverse_iterator.hpp:78
constexpr const_reverse_iterator_type rend() const noexcept
Returns a reverse iterator first element of the array. When accessing the iterator,...
Definition: array.hpp:559
contiguous_iterator< T const > const_iterator_type
alias for: contiguous_iterator<T const>
Definition: array.hpp:88
contiguous_iterator< T > iterator_type
alias for: contiguous_iterator<T>
Definition: array.hpp:86
constexpr pointer_type front_if() noexcept
Returns a pointer to the first element in the array.
Definition: array.hpp:178
constexpr const_reverse_iterator_type rbegin() const noexcept
Returns a reverse iterator to one past the last element of the array. When accessing the iterator,...
Definition: array.hpp:439
constexpr iterator_type begin() noexcept
Returns an iterator to the first element of the array.
Definition: array.hpp:282
constexpr reference_type front() noexcept
Returns a reference to the first element in the array.
Definition: array.hpp:152
constexpr const_pointer_type back_if() const noexcept
Returns a pointer to the last element in the array.
Definition: array.hpp:243
constexpr bool is_constant_evaluated() noexcept
Detects whether the function call occurs within a constant-evaluated context. Returns true if the eva...
Definition: is_constant_evaluated.hpp:46
T const * const_pointer_type
alias for: T const *
Definition: array.hpp:84
constexpr const_reverse_iterator_type crbegin() const noexcept
Returns a reverse iterator to one past the last element of the array. When accessing the iterator,...
Definition: array.hpp:458
::uintmax_t uintmax
defines a unsigned integer with the maximum possible size
Definition: cstdint.hpp:97
bsl::uintmax size_type
alias for: bsl::uintmax
Definition: array.hpp:74
T & reference_type
alias for: T &
Definition: array.hpp:78
Provides a contiguous iterator as defined by the C++ specification, with the follwing differences:
Definition: contiguous_iterator.hpp:76
bsl::int32 builtin_memcmp(void const *const lhs, void const *const rhs, bsl::uintmax const count) noexcept
Same as std::memcmp with parameter checks. If lhs, rhs are a nullptr, or count is 0,...
Definition: cstring.hpp:108
constexpr const_reverse_iterator_type riter(size_type const i) const noexcept
Returns a reverse iterator element "i" in the array. When accessing the iterator, the iterator will a...
Definition: array.hpp:499