diff --git a/include/loki/details/pddl/factory.hpp b/include/loki/details/pddl/factory.hpp index e6d9bcdd..920617f2 100644 --- a/include/loki/details/pddl/factory.hpp +++ b/include/loki/details/pddl/factory.hpp @@ -48,6 +48,15 @@ class PDDLFactory size_t m_count = 0; + void range_check(size_t pos) const + { + if (pos >= size()) + { + throw std::out_of_range("SegmentedVector::range_check: pos (which is " + std::to_string(pos) + ") >= this->size() (which is " + + std::to_string(size()) + ")"); + } + } + public: explicit PDDLFactory(size_t elements_per_segment) : m_persistent_vector(SegmentedVector(elements_per_segment)) {} PDDLFactory(const PDDLFactory& other) = delete; @@ -101,7 +110,17 @@ class PDDLFactory */ /// @brief Returns a pointer to an existing object with the given identifier. - [[nodiscard]] HolderType const* get(size_t identifier) const { return &(m_persistent_vector.at(identifier)); } + [[nodiscard]] HolderType const* operator[](size_t identifier) const + { + assert(identifier < size()); + return &(m_persistent_vector.at(identifier)); + } + + [[nodiscard]] HolderType const* at(size_t identifier) const + { + range_check(identifier); + return &(m_persistent_vector.at(identifier)); + } /** * Iterators diff --git a/include/loki/details/utils/segmented_vector.hpp b/include/loki/details/utils/segmented_vector.hpp index d7b9f5ce..f08de2be 100644 --- a/include/loki/details/utils/segmented_vector.hpp +++ b/include/loki/details/utils/segmented_vector.hpp @@ -56,6 +56,15 @@ class SegmentedVector size_t element_index(size_t pos) const { return pos % m_elements_per_segment; } + void range_check(size_t pos) const + { + if (pos >= size()) + { + throw std::out_of_range("SegmentedVector::range_check: pos (which is " + std::to_string(pos) + ") >= this->size() (which is " + + std::to_string(size()) + ")"); + } + } + public: explicit SegmentedVector(size_t elements_per_segment) : m_elements_per_segment(elements_per_segment), m_size(0), m_capacity(0) {} @@ -105,13 +114,13 @@ class SegmentedVector T& at(size_t pos) { - assert(pos < size()); + range_check(pos); return m_data[segment_index(pos)].at(element_index(pos)); } const T& at(size_t pos) const { - assert(pos < size()); + range_check(pos); return m_data[segment_index(pos)].at(element_index(pos)); }