Exiv2::Slice< container > Struct Template Reference

Slice (= view) for STL containers. More...

#include <slice.hpp>

Inheritance diagram for Exiv2::Slice< container >:

Public Types

using iterator = typename container::iterator
 
using const_iterator = typename container::const_iterator
 
using value_type = typename std::remove_cv< typename container::value_type >::type
 
- Public Types inherited from Exiv2::Internal::MutableSliceBase< Internal::ContainerStorage, container >
using iterator = typename ConstSliceBase< Internal::ContainerStorage, container >::iterator
 
using const_iterator = typename ConstSliceBase< Internal::ContainerStorage, container >::const_iterator
 
using value_type = typename ConstSliceBase< Internal::ContainerStorage, container >::value_type
 
- Public Types inherited from Exiv2::Internal::ConstSliceBase< storage_type, data_type >
using iterator = typename storage_type< data_type >::iterator
 
using const_iterator = typename storage_type< data_type >::const_iterator
 
using value_type = typename storage_type< data_type >::value_type
 

Public Member Functions

Slice subSlice (size_t begin, size_t end)
 
Slice< const container > subSlice (size_t begin, size_t end) const
 
- Public Member Functions inherited from Exiv2::Internal::MutableSliceBase< Internal::ContainerStorage, container >
value_type & at (size_t index)
 
const value_type & at (size_t index) const
 
iterator begin () noexcept
 
iterator end () noexcept
 
- Public Member Functions inherited from Exiv2::Internal::ConstSliceBase< storage_type, data_type >
 ConstSliceBase (data_type &data, size_t begin, size_t end)
 
const value_type & at (size_t index) const
 
const_iterator cbegin () const noexcept
 
const_iterator cend () const noexcept
 
template<typename slice_type >
slice_type subSlice (size_t begin, size_t end) const
 
- Public Member Functions inherited from Exiv2::Internal::SliceBase
 SliceBase (size_t begin, size_t end)
 
size_t size () const noexcept
 

Additional Inherited Members

- Protected Types inherited from Exiv2::Internal::MutableSliceBase< Internal::ContainerStorage, container >
using base_type = ConstSliceBase< Internal::ContainerStorage, container >
 
- Protected Member Functions inherited from Exiv2::Internal::MutableSliceBase< Internal::ContainerStorage, container >
ConstSliceBase< Internal::ContainerStorage, const container > to_const_base () const noexcept
 
slice_type subSlice (size_t begin, size_t end)
 
- Protected Member Functions inherited from Exiv2::Internal::SliceBase
void rangeCheck (size_t index) const
 
- Protected Attributes inherited from Exiv2::Internal::ConstSliceBase< storage_type, data_type >
storage_type< data_type > storage_
 
- Protected Attributes inherited from Exiv2::Internal::SliceBase
size_t begin_
 
size_t end_
 

Detailed Description

template<typename container>
struct Exiv2::Slice< container >

Slice (= view) for STL containers.

This is a very simple implementation of slices (i.e. views of sub-arrays) for STL containers that support O(1) element access and random access iterators (like std::vector, std::array and std::string).

A slice represents the semi-open interval [begin, end) and provides a (mutable) view, it does however not own the data! It can be used to conveniently pass parts of containers into functions without having to use iterators or offsets.

In contrast to C++20's std::span<T> it is impossible to read beyond the container's bounds and unchecked access is not-possible (by design).

Example usage:

std::vector<int> vec = {0, 1, 2, 3, 4};
slice<std::vector<int> > one_two(vec, 1, 3);
assert(one_two.size() == 2);
assert(one_two.at(0) == 1 && one_two.at(1) == 2);
// mutate the contents:
one_two.at(0) *= 2;
one_two.at(1) *= 3;
assert(one_two.at(0) == 2 && one_two.at(1) == 6);

Slices also offer access via iterators of the same type as the underlying container, so that they can be used in a comparable fashion:

std::vector<int> vec = {0, 1, 2, 3, 4};
slice<std::vector<int>> three_four(vec, 3, 5);
assert(*three_four.begin() == 3 && *three_four.end() == 4);
// this prints:
// 3
// 4
for (const auto & elem : three_four) {
std::cout << elem << std::endl;
}
Template Parameters
containerA STL container type, like vector or array. Must support array-like access via the at() method.

Member Function Documentation

◆ subSlice() [1/2]

template<typename container >
Slice Exiv2::Slice< container >::subSlice ( size_t  begin,
size_t  end 
)
inline

Construct a sub-slice of this slice with the given bounds. The bounds are evaluated with respect to the current slice.

Parameters
[in]beginFirst element in the new slice.
[in]endFirst element beyond the new slice.
Exceptions
std::out_of_rangewhen begin or end are invalid

References Exiv2::Internal::MutableSliceBase< Internal::ContainerStorage, container >::begin(), and Exiv2::Internal::MutableSliceBase< Internal::ContainerStorage, container >::end().

◆ subSlice() [2/2]

template<typename container >
Slice<const container> Exiv2::Slice< container >::subSlice ( size_t  begin,
size_t  end 
) const
inline

The documentation for this struct was generated from the following file: