From c0da44ee33e9fcf5116f1028c5d469d3122e2664 Mon Sep 17 00:00:00 2001 From: Maxim Sharabayko Date: Thu, 14 Apr 2022 16:25:10 +0200 Subject: [PATCH] [core] FixedArray: use a function to throw an exception. --- srtcore/utilities.h | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/srtcore/utilities.h b/srtcore/utilities.h index d0a8770db..b47e522e0 100644 --- a/srtcore/utilities.h +++ b/srtcore/utilities.h @@ -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]) { } @@ -433,7 +432,7 @@ 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]; } @@ -441,7 +440,7 @@ class FixedArray T& operator[](size_t index) { if (index >= m_size) - throw std::runtime_error(m_strIndexErr); + raise_expection(index); return m_entries[index]; } @@ -449,7 +448,7 @@ class FixedArray const T& operator[](int index) const { if (index < 0 || static_cast(index) >= m_size) - throw std::runtime_error(m_strIndexErr); + raise_expection(index); return m_entries[index]; } @@ -457,7 +456,7 @@ class FixedArray T& operator[](int index) { if (index < 0 || static_cast(index) >= m_size) - throw std::runtime_error(m_strIndexErr); + raise_expection(index); return m_entries[index]; } @@ -479,8 +478,14 @@ class FixedArray FixedArray(const FixedArray& ); FixedArray& operator=(const FixedArray&); + 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; };