CatapultServer  v0.5.0.1 (Elephant)
CheckedArray.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "catapult/exceptions.h"
23 
24 namespace catapult { namespace utils {
25 
27  template<typename T, size_t N>
28  class CheckedArray {
29  public:
31  {}
32 
33  public:
35  bool empty() const {
36  return 0 == m_size;
37  }
38 
40  size_t size() const {
41  return m_size;
42  }
43 
45  size_t capacity() const {
46  return N;
47  }
48 
51  void push_back(T val) {
52  if (N <= m_size)
53  CATAPULT_THROW_OUT_OF_RANGE("array bounds exeeded");
54 
55  m_data[m_size++] = val;
56  }
57 
58  public:
60  const T* begin() const {
61  return &m_data[0];
62  }
63 
65  const T* end() const {
66  return &m_data[m_size];
67  }
68 
70  const T* cbegin() const {
71  return &m_data[0];
72  }
73 
75  const T* cend() const {
76  return &m_data[m_size];
77  }
78 
79  public:
82  T& operator[](size_t index) {
83  if (index >= m_size)
84  CATAPULT_THROW_OUT_OF_RANGE("index out of range");
85 
86  return m_data[index];
87  }
88 
91  const T& operator[](size_t index) const {
92  if (index >= m_size)
93  CATAPULT_THROW_OUT_OF_RANGE("index out of range");
94 
95  return m_data[index];
96  }
97 
99  constexpr bool operator==(const CheckedArray& rhs) const {
100  return m_size == rhs.m_size && 0 == std::memcmp(m_data, rhs.m_data, m_size * sizeof(T));
101  }
102 
104  constexpr bool operator!=(const CheckedArray& rhs) const {
105  return !(*this == rhs);
106  }
107 
108  private:
109  T m_data[N];
110  size_t m_size;
111  };
112 }}
catapult::utils::CheckedArray::operator!=
constexpr bool operator!=(const CheckedArray &rhs) const
Returns true if this array is not equal to rhs.
Definition: CheckedArray.h:104
exceptions.h
catapult::utils::CheckedArray::empty
bool empty() const
Returns true if the array is empty, false otherwise.
Definition: CheckedArray.h:35
catapult::utils::CheckedArray::cend
const T * cend() const
Returns a const iterator that represents one past last element.
Definition: CheckedArray.h:75
catapult::utils::CheckedArray::m_size
size_t m_size
Definition: CheckedArray.h:110
catapult::utils::CheckedArray::capacity
size_t capacity() const
Gets the capacity of the array.
Definition: CheckedArray.h:45
catapult::utils::CheckedArray::operator[]
const T & operator[](size_t index) const
Definition: CheckedArray.h:91
catapult::utils::CheckedArray::end
const T * end() const
Returns a const iterator that represents one past last element.
Definition: CheckedArray.h:65
catapult::utils::CheckedArray
An array that checks for overflow.
Definition: CheckedArray.h:28
catapult::utils::CheckedArray::size
size_t size() const
Gets the size of the array.
Definition: CheckedArray.h:40
catapult::utils::CheckedArray::operator==
constexpr bool operator==(const CheckedArray &rhs) const
Returns true if this array is equal to rhs.
Definition: CheckedArray.h:99
CATAPULT_THROW_OUT_OF_RANGE
#define CATAPULT_THROW_OUT_OF_RANGE(MESSAGE)
Macro used to throw a catapult out of range.
Definition: exceptions.h:191
catapult::utils::CheckedArray::m_data
T m_data[N]
Definition: CheckedArray.h:109
catapult::utils::CheckedArray::CheckedArray
CheckedArray()
Definition: CheckedArray.h:30
catapult::utils::CheckedArray::operator[]
T & operator[](size_t index)
Definition: CheckedArray.h:82
catapult::utils::CheckedArray::push_back
void push_back(T val)
Definition: CheckedArray.h:51
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::utils::CheckedArray::cbegin
const T * cbegin() const
Returns a const iterator that represents first element.
Definition: CheckedArray.h:70
catapult::utils::CheckedArray::begin
const T * begin() const
Returns a const iterator that represents first element.
Definition: CheckedArray.h:60