Skip to content

Commit

Permalink
[core] FixedArray: use a function to throw an exception.
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsharabayko committed Apr 20, 2022
1 parent 29d56be commit c0da44e
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions srtcore/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,7 @@ class FixedArray
{
public:
FixedArray(size_t size)
: m_strIndexErr("FixedArray: invalid index")
, m_size(size)
: m_size(size)
, m_entries(new T[size])
{
}
Expand All @@ -433,31 +432,31 @@ class FixedArray
const T& operator[](size_t index) const
{
if (index >= m_size)
throw std::runtime_error(m_strIndexErr);
raise_expection(index);

return m_entries[index];
}

T& operator[](size_t index)
{
if (index >= m_size)
throw std::runtime_error(m_strIndexErr);
raise_expection(index);

return m_entries[index];
}

const T& operator[](int index) const
{
if (index < 0 || static_cast<size_t>(index) >= m_size)
throw std::runtime_error(m_strIndexErr);
raise_expection(index);

return m_entries[index];
}

T& operator[](int index)
{
if (index < 0 || static_cast<size_t>(index) >= m_size)
throw std::runtime_error(m_strIndexErr);
raise_expection(index);

return m_entries[index];
}
Expand All @@ -479,8 +478,14 @@ class FixedArray
FixedArray(const FixedArray<T>& );
FixedArray<T>& operator=(const FixedArray<T>&);

void raise_expection(int i) const
{
std::stringstream ss;
ss << "Index " << i << "out of range";
throw std::runtime_error(ss.str());
}

private:
const char* m_strIndexErr;
size_t m_size;
T* const m_entries;
};
Expand Down

0 comments on commit c0da44e

Please sign in to comment.