Skip to content

Commit

Permalink
Specialize _Non_propagating_cache and fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
miscco committed Jul 3, 2021
1 parent 92ce96e commit 49bf9a3
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 6 deletions.
74 changes: 71 additions & 3 deletions stl/inc/ranges
Original file line number Diff line number Diff line change
Expand Up @@ -621,10 +621,12 @@ namespace ranges {
};

// CLASS TEMPLATE ranges::_Non_propagating_cache
// clang-format off
template <class _Ty>
requires is_object_v<_Ty>
class _Non_propagating_cache { // a simplified optional that augments copy_constructible types with full copyability
concept _Objectable = is_object_v<_Ty>;

// clang-format off
template <_Objectable _Ty>
class _Non_propagating_cache { // a simplified optional that resets on copy / move
// clang-format on
public:
constexpr _Non_propagating_cache() noexcept : _Dummy{} {}
Expand Down Expand Up @@ -710,6 +712,72 @@ namespace ranges {
bool _Engaged = false;
};

// clang-format off
template <_Objectable _Ty>
requires is_trivially_destructible_v<_Ty>
class _Non_propagating_cache<_Ty> { // provide the same API more efficiently for
// clang-format on
public:
constexpr _Non_propagating_cache() noexcept : _Dummy{} {}

template <class... _Types>
constexpr _Non_propagating_cache(in_place_t, _Types&&... _Args) noexcept(
is_nothrow_constructible_v<_Ty, _Types...>) // strengthened
: _Val(_STD forward<_Types>(_Args)...), _Engaged{true} {}

constexpr _Non_propagating_cache(const _Non_propagating_cache&) noexcept {}

constexpr _Non_propagating_cache(_Non_propagating_cache&& _Other) noexcept {
_Other._Engaged = false;
}

constexpr _Non_propagating_cache& operator=(const _Non_propagating_cache& _Other) noexcept {
if (_STD addressof(_Other) == this) {
return *this;
}

_Engaged = false;

return *this;
}

_CONSTEXPR20_DYNALLOC _Non_propagating_cache& operator=(_Non_propagating_cache&& _Other) noexcept {
_Engaged = false;
_Other._Engaged = false;

return *this;
}

constexpr explicit operator bool() const noexcept {
return _Engaged;
}

_NODISCARD constexpr _Ty& operator*() noexcept {
_STL_INTERNAL_CHECK(_Engaged);
return _Val;
}
_NODISCARD constexpr const _Ty& operator*() const noexcept {
_STL_INTERNAL_CHECK(_Engaged);
return _Val;
}

template <class _It>
constexpr _Ty& _Emplace_deref(_It& _Iter) noexcept(noexcept(_Ty(*_Iter))) /* strengthened */ {
_Engaged = false;
_Construct_in_place(_Val, *_Iter);
_Engaged = true;

return _Val;
}

private:
union {
_Nontrivial_dummy_type _Dummy;
_Ty _Val;
};
bool _Engaged = false;
};

// CLASS TEMPLATE ranges::empty_view
// clang-format off
template <class _Ty>
Expand Down
11 changes: 8 additions & 3 deletions tests/std/tests/P0896R4_views_join/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,9 @@ int main() {
static_assert(test_one(sp, expected));
test_one(sp, expected);
}
{ // ...prvalue
static_assert(test_one(array<string, 5>{{{}, "Hello ", {}, "World!", {}}}, expected));
test_one(array<string, 5>{{{}, "Hello ", {}, "World!", {}}}, expected);
{ // ...copyable rvalue
static_assert(test_one(array<string_view, 5>{{{}, "Hello "sv, {}, "World!"sv, {}}}, expected));
test_one(array<string_view, 5>{{{}, "Hello "sv, {}, "World!"sv, {}}}, expected);
}
// ... move-only
test_move_only_views();
Expand Down Expand Up @@ -513,6 +513,11 @@ int main() {
assert(ranges::equal(joined, result));
}

{ // P2328 range of prvalue ranges
auto joined = views::iota(0, 5) | views::transform([](int i) { return std::to_string(i); }) | views::join;
assert(ranges::equal(joined, "01234"sv));
}

#if defined(__clang__) || defined(__EDG__) // TRANSITION, VSO-934264
STATIC_ASSERT(instantiation_test());
#endif // TRANSITION, VSO-934264
Expand Down

0 comments on commit 49bf9a3

Please sign in to comment.