// SPDX-License-Identifier: GPL-2.0-or-later #ifndef EXIV2_INCLUDE_SLICE_HPP #define EXIV2_INCLUDE_SLICE_HPP #include #include #include #include namespace Exiv2 { namespace Internal { /*! * Common base class of all slice implementations. * * Implements only the most basic functions, which do not require any * knowledge about the stored data. */ struct SliceBase { inline SliceBase(size_t begin, size_t end) : begin_(begin), end_(end) { if (begin >= end) { throw std::out_of_range("Begin must be smaller than end"); } } /*! * Return the number of elements in the slice. */ [[nodiscard]] inline size_t size() const noexcept { // cannot underflow, as we know that begin < end return end_ - begin_; } protected: /*! * Throw an exception when index is too large. * * @throw std::out_of_range when `index` will access an element * outside of the slice */ inline void rangeCheck(size_t index) const { if (index >= size()) { throw std::out_of_range("Index outside of the slice"); } } /*! * lower and upper bounds of the slice with respect to the * container/array stored in storage_ */ size_t begin_; size_t end_; }; /*! * @brief This class provides the public-facing const-qualified methods * of a slice. * * The public methods are implemented in a generic fashion using a * storage_type. This type contains the actual reference to the data to * which the slice points and provides the following methods: * * - (const) value_type& unsafeAt(size_t index) (const) * Return the value at the given index of the underlying container, * without promising to perform a range check and without any * knowledge of the slices' size * * - const_iterator/iterator unsafeGetIteratorAt(size_t index) (const) * Return a (constant) iterator at the given index of the underlying * container. Again, no range checks are promised. * * - Constructor(data_type& data, size_t begin, size_t end) * Can use `begin` & `end` to perform range checks on `data`, but * should not store both values. Must not take ownership of `data`! * * - Must save data as a public member named `data_`. * * - Must provide appropriate typedefs for iterator, const_iterator and * value_type */ template