Skip to content

Commit c0da44e

Browse files
committed
[core] FixedArray: use a function to throw an exception.
1 parent 29d56be commit c0da44e

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

srtcore/utilities.h

+12-7
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,7 @@ class FixedArray
418418
{
419419
public:
420420
FixedArray(size_t size)
421-
: m_strIndexErr("FixedArray: invalid index")
422-
, m_size(size)
421+
: m_size(size)
423422
, m_entries(new T[size])
424423
{
425424
}
@@ -433,31 +432,31 @@ class FixedArray
433432
const T& operator[](size_t index) const
434433
{
435434
if (index >= m_size)
436-
throw std::runtime_error(m_strIndexErr);
435+
raise_expection(index);
437436

438437
return m_entries[index];
439438
}
440439

441440
T& operator[](size_t index)
442441
{
443442
if (index >= m_size)
444-
throw std::runtime_error(m_strIndexErr);
443+
raise_expection(index);
445444

446445
return m_entries[index];
447446
}
448447

449448
const T& operator[](int index) const
450449
{
451450
if (index < 0 || static_cast<size_t>(index) >= m_size)
452-
throw std::runtime_error(m_strIndexErr);
451+
raise_expection(index);
453452

454453
return m_entries[index];
455454
}
456455

457456
T& operator[](int index)
458457
{
459458
if (index < 0 || static_cast<size_t>(index) >= m_size)
460-
throw std::runtime_error(m_strIndexErr);
459+
raise_expection(index);
461460

462461
return m_entries[index];
463462
}
@@ -479,8 +478,14 @@ class FixedArray
479478
FixedArray(const FixedArray<T>& );
480479
FixedArray<T>& operator=(const FixedArray<T>&);
481480

481+
void raise_expection(int i) const
482+
{
483+
std::stringstream ss;
484+
ss << "Index " << i << "out of range";
485+
throw std::runtime_error(ss.str());
486+
}
487+
482488
private:
483-
const char* m_strIndexErr;
484489
size_t m_size;
485490
T* const m_entries;
486491
};

0 commit comments

Comments
 (0)