Skip to content

Commit

Permalink
use using in slice
Browse files Browse the repository at this point in the history
  • Loading branch information
neheb committed Apr 1, 2023
1 parent 0dd0689 commit b72ac4b
Showing 1 changed file with 7 additions and 62 deletions.
69 changes: 7 additions & 62 deletions include/exiv2/slice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,11 @@ struct ConstSliceBase : SliceBase {
*/
template <template <typename> class storage_type, typename data_type>
struct MutableSliceBase : public ConstSliceBase<storage_type, data_type> {
using ConstSliceBase<storage_type, data_type>::ConstSliceBase;
using iterator = typename ConstSliceBase<storage_type, data_type>::iterator;
using const_iterator = typename ConstSliceBase<storage_type, data_type>::const_iterator;
using value_type = typename ConstSliceBase<storage_type, data_type>::value_type;

/*!
* Forwards everything to the constructor of const_slice_base
*
* @todo use using once we have C++11
*/
MutableSliceBase(data_type& data, size_t begin, size_t end) :
ConstSliceBase<storage_type, data_type>(data, begin, end) {
}

/*!
* Obtain a reference to the element with the specified index in the
* slice.
Expand Down Expand Up @@ -427,8 +419,9 @@ struct PtrSliceStorage {
*/
template <typename container>
struct Slice : public Internal::MutableSliceBase<Internal::ContainerStorage, container> {
using Internal::MutableSliceBase<Internal::ContainerStorage, container>::MutableSliceBase;
using Internal::MutableSliceBase<Internal::ContainerStorage, container>::subSlice;
using iterator = typename container::iterator;

using const_iterator = typename container::const_iterator;

#if __cplusplus >= 201402L || _MSVC_LANG >= 201402L
Expand All @@ -437,39 +430,6 @@ struct Slice : public Internal::MutableSliceBase<Internal::ContainerStorage, con
using value_type = typename std::remove_cv<typename container::value_type>::type;
#endif

/*!
* @brief Construct a slice of the container `cont` starting at `begin`
* (including) and ending before `end`.
*
* @param[in] cont Reference to the container
* @param[in] begin First element of the slice.
* @param[in] end First element beyond the slice.
*
* @throws std::out_of_range For invalid slice bounds: when end is not
* larger than begin or when the slice's bounds are larger than the
* container's size.
*
* Please note that due to the requirement that `end` must be larger
* than `begin` (they cannot be equal) it is impossible to construct a
* slice with zero length.
*/
Slice(container& cont, size_t begin, size_t end) :
Internal::MutableSliceBase<Internal::ContainerStorage, container>(cont, begin, end) {
}

/*!
* Construct a sub-slice of this slice with the given bounds. The bounds
* are evaluated with respect to the current slice.
*
* @param[in] begin First element in the new slice.
* @param[in] end First element beyond the new slice.
*
* @throw std::out_of_range when begin or end are invalid
*/
Slice subSlice(size_t begin, size_t end) {
return Internal::MutableSliceBase<Internal::ContainerStorage, container>::template subSlice<Slice>(begin, end);
}

/*!
* Constructs a new constant subSlice. Behaves otherwise exactly like
* the non-const version.
Expand All @@ -484,24 +444,16 @@ struct Slice : public Internal::MutableSliceBase<Internal::ContainerStorage, con
*/
template <typename container>
struct Slice<const container> : public Internal::ConstSliceBase<Internal::ContainerStorage, const container> {
using Internal::ConstSliceBase<Internal::ContainerStorage, const container>::ConstSliceBase;
using Internal::ConstSliceBase<Internal::ContainerStorage, const container>::subSlice;
using iterator = typename container::iterator;

using const_iterator = typename container::const_iterator;

#if __cplusplus >= 201402L || _MSVC_LANG >= 201402L
using value_type = std::remove_cv_t<typename container::value_type>;
#else
using value_type = typename std::remove_cv<typename container::value_type>::type;
#endif

Slice(const container& cont, size_t begin, size_t end) :
Internal::ConstSliceBase<Internal::ContainerStorage, const container>(cont, begin, end) {
}

Slice subSlice(size_t begin, size_t end) const {
return Internal::ConstSliceBase<Internal::ContainerStorage,
const container>::template subSlice<Slice<const container>>(begin, end);
}
};

/*!
Expand All @@ -514,6 +466,7 @@ struct Slice<const container> : public Internal::ConstSliceBase<Internal::Contai
*/
template <typename T>
struct Slice<const T*> : public Internal::ConstSliceBase<Internal::PtrSliceStorage, const T*> {
using Internal::ConstSliceBase<Internal::PtrSliceStorage, const T*>::subSlice;
/*!
* Constructor.
*
Expand All @@ -530,26 +483,18 @@ struct Slice<const T*> : public Internal::ConstSliceBase<Internal::PtrSliceStora
Internal::ConstSliceBase<Internal::PtrSliceStorage, const T*>(ptr, begin, end) {
// TODO: use using in C++11
}

Slice<const T*> subSlice(size_t begin, size_t end) const {
return Internal::ConstSliceBase<Internal::PtrSliceStorage, const T*>::template subSlice<Slice<const T*>>(begin,
end);
}
};

/*!
* Specialization of slices for (mutable) C-arrays.
*/
template <typename T>
struct Slice<T*> : public Internal::MutableSliceBase<Internal::PtrSliceStorage, T*> {
using Internal::MutableSliceBase<Internal::PtrSliceStorage, T*>::subSlice;
Slice(T* ptr, size_t begin, size_t end) : Internal::MutableSliceBase<Internal::PtrSliceStorage, T*>(ptr, begin, end) {
// TODO: use using in C++11
}

Slice<T*> subSlice(size_t begin, size_t end) {
return Internal::MutableSliceBase<Internal::PtrSliceStorage, T*>::template subSlice<Slice<T*>>(begin, end);
}

[[nodiscard]] Slice<const T*> subSlice(size_t begin, size_t end) const {
return this->to_const_base().template subSlice<Slice<const T*>>(begin, end);
}
Expand Down

0 comments on commit b72ac4b

Please sign in to comment.