Skip to content

Commit

Permalink
Made OwningView, using it in view::all
Browse files Browse the repository at this point in the history
Also MoveView, TransformView::base() const & noexcept
  • Loading branch information
OleErikPeistorpet committed Sep 29, 2023
1 parent 118e394 commit efb0c09
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 16 deletions.
19 changes: 7 additions & 12 deletions view/all.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "counted.h"
#include "subrange.h"
#include "detail/owning.h"

/** @file
*/
Expand All @@ -26,25 +27,19 @@ namespace _detail
}
#endif

template< typename I >
constexpr auto operator()(view::counted<I> v) const { return v; }

template< typename I, typename S >
constexpr auto operator()(view::subrange<I, S> v) const { return v; }

template< typename SizedRange >
constexpr auto operator()(SizedRange & r) const
-> decltype( view::counted(begin(r), oel::ssize(r)) )
{ return view::counted(begin(r), oel::ssize(r)); }

template< typename Range, typename... None >
constexpr auto operator()(Range & r, None...) const
template< typename Range >
constexpr auto operator()(Range && r) const
{
return view::subrange(begin(r), end(r));
if constexpr (std::is_lvalue_reference_v<Range>)
return view::subrange(begin(r), end(r));
else
return OwningView{static_cast<Range &&>(r)};
}

template< typename R >
void operator()(R &&) const = delete;
};
}

Expand Down
67 changes: 67 additions & 0 deletions view/detail/owning.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#pragma once

// Copyright 2020 Ole Erik Peistorpet
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)


#include "../../util.h"

namespace oel::_detail
{
template< typename Range >
class OwningView
{
using _iter = iterator_t<Range>;

Range _r;

public:
using difference_type = iter_difference_t<_iter>;

OwningView() = default;
OwningView(OwningView &&) = default;
OwningView(const OwningView &) = delete;
OwningView & operator =(OwningView &&) = default;
OwningView & operator =(const OwningView &) = delete;

constexpr explicit OwningView(Range && r) : _r{std::move(r)} {}

OEL_ALWAYS_INLINE constexpr _iter begin() { return adl_begin(_r); }

template< typename R = Range >
OEL_ALWAYS_INLINE constexpr auto end()
-> decltype( adl_end(std::declval<R &>()) ) { return adl_end(_r); }

template< typename R = Range >
OEL_ALWAYS_INLINE constexpr auto size()
-> decltype(as_unsigned( _detail::Size(std::declval<R &>()) ))
{
return _detail::Size(_r);
}

// Might want to be more generic here, don't know if _r.empty exists
constexpr bool empty() { return _r.empty(); }

constexpr decltype(auto) operator[](difference_type index)
OEL_REQUIRES(iter_is_random_access<_iter>)
{
return adl_begin(_r)[index];
}

constexpr Range && base() && noexcept { return std::move(_r); }
constexpr const Range & base() const & noexcept { return _r; }
};
}

#if OEL_STD_RANGES

template< typename R >
inline constexpr bool std::ranges::enable_borrowed_range< oel::_detail::OwningView<R> >
= std::ranges::enable_borrowed_range< std::remove_cv_t<R> >;

template< typename R >
inline constexpr bool std::ranges::enable_view< oel::_detail::OwningView<R> > = true;

#endif
4 changes: 2 additions & 2 deletions view/move.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ namespace _detail
OEL_ALWAYS_INLINE constexpr decltype(auto) operator[](difference_type index)
OEL_REQUIRES(iter_is_random_access< iterator_t<InputView> >) { return begin()[index]; }

constexpr InputView base() && { return std::move(_base); }
constexpr const InputView & base() const & { return _base; }
constexpr InputView base() && { return std::move(_base); }
constexpr const InputView & base() const & noexcept { return _base; }
};
}

Expand Down
4 changes: 2 additions & 2 deletions view/transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ namespace _detail
OEL_ALWAYS_INLINE constexpr auto size()
-> decltype( std::declval<V_>().size() ) { return _m.first.size(); }

constexpr View base() && { return std::move(_m.first); }
constexpr const View & base() const & { return _m.first; }
constexpr View base() && { return std::move(_m.first); }
constexpr const View & base() const & noexcept { return _m.first; }
};

template< typename F >
Expand Down

0 comments on commit efb0c09

Please sign in to comment.