X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fcommon%2Fcontainer-wrapper.hpp;h=7f9678d08d3cb8252de645bd3731434d73b4e52a;hb=69e98ad60c01ddfbfa6eb843960f77804173dd0f;hp=bccb23ddaeaf05efc27c51985ddd5a67004ceaa9;hpb=f74e820c5e8f81d6416f4557bce8b56ee6be4746;p=lttng-tools.git diff --git a/src/common/container-wrapper.hpp b/src/common/container-wrapper.hpp index bccb23dda..7f9678d08 100644 --- a/src/common/container-wrapper.hpp +++ b/src/common/container-wrapper.hpp @@ -8,6 +8,8 @@ #ifndef LTTNG_CONTAINER_WRAPPER_H #define LTTNG_CONTAINER_WRAPPER_H +#include +#include #include #include @@ -71,7 +73,7 @@ class random_access_container_wrapper { typename std::conditional::value, IteratorElementType, IteratorElementType&>::type - operator*() const noexcept + operator*() const { return _container[_index]; } @@ -95,9 +97,9 @@ public: return iterator(*this); } - iterator end() noexcept + iterator end() { - return iterator(*this, ContainerOperations::size(_container)); + return iterator(*this, size()); } const_iterator begin() const noexcept @@ -105,12 +107,12 @@ public: return const_iterator(*this); } - const_iterator end() const noexcept + const_iterator end() const { - return const_iterator(*this, ContainerOperations::size(_container)); + return const_iterator(*this, size()); } - std::size_t size() const noexcept + std::size_t size() const { return ContainerOperations::size(_container); } @@ -118,8 +120,22 @@ public: typename std::conditional::value, ElementType, ElementType&>::type operator[](std::size_t index) { - LTTNG_ASSERT(index < ContainerOperations::size(_container)); - return ContainerOperations::get(_container, index); + /* + * To share code between the const and mutable versions of this operator, 'this' + * is casted to a const reference. A const_cast then ensures that a mutable + * reference (or pointer) is returned. + * + * We typically avoid const_cast, but this is safe: if the user is calling the + * mutable version of this operator, it had a mutable object anyhow. + * + * For more information, see Item 3 of Effective C++. + */ + const auto& const_this = static_cast(*this); + + /* NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) */ + return const_cast::value, + ElementType, + ElementType&>::type>(const_this[index]); } typename std::conditional::value, @@ -127,7 +143,13 @@ public: const ElementType&>::type operator[](std::size_t index) const { - LTTNG_ASSERT(index < ContainerOperations::size(_container)); + if (index >= ContainerOperations::size(_container)) { + LTTNG_THROW_INVALID_ARGUMENT_ERROR(lttng::format( + "Out of bound access through random_access_container_wrapper: index={}, size={}", + index, + size())); + } + return ContainerOperations::get(_container, index); }