CXXIter 0.2
Loading...
Searching...
No Matches
IterValue.h
1#pragma once
2
3#include <type_traits>
4#include <optional>
5
6namespace CXXIter {
7
8 // ################################################################################################
9 // ITERATOR OPTIONAL (supports references)
10 // ################################################################################################
11
18 template<typename TValue>
19 class IterValue {
20 private:
22 using TValueDeref = std::remove_reference_t<TValue>;
23 using TValueStore = std::conditional_t<
24 std::is_reference_v<TValue>,
25 std::reference_wrapper<TValueDeref>,
26 TValue
27 >;
28
29 std::optional<TValueStore> inner;
30
31 public:
33 constexpr IterValue() noexcept {}
35 constexpr IterValue(TValue value) noexcept requires std::is_reference_v<TValue> : inner(value) {}
37 constexpr IterValue(const TValueDeref& value) noexcept(std::is_nothrow_copy_constructible_v<TValueDeref>)
38 requires (!std::is_reference_v<TValue>) : inner(value) {}
40 constexpr IterValue(TValueDeref&& value) noexcept(std::is_nothrow_move_constructible_v<TValueDeref>)
41 : inner(std::forward<TValueDeref>(value)) {}
42
44 constexpr IterValue& operator=(IterValue&& o) = default;
46 constexpr IterValue(IterValue&& o) noexcept(std::is_nothrow_move_constructible_v<TValueDeref>)
47 : inner(std::move(o.inner)) {
48 o.inner.reset();
49 };
50
52 constexpr auto& operator=(TValueDeref&& o) {
53 this->inner = std::forward<decltype(o)>(o);
54 return *this;
55 }
56
62 constexpr inline const TValueDeref& value() const { return inner.value(); }
68 constexpr inline TValueDeref& value() { return inner.value(); }
74 constexpr inline const TValueDeref& value_or(TValueDeref&& def) const noexcept { return inner.value_or(def); }
80 constexpr inline TValueDeref& value_or(TValueDeref&& def) noexcept { return inner.value_or(def); }
81
86 constexpr bool has_value() const noexcept { return inner.has_value(); }
87
92 constexpr void swap(IterValue<TValue>& o) noexcept { inner.swap(o.inner); }
93
99 constexpr std::optional<TValueStore> toStdOptional() noexcept {
100 return std::optional<TValueStore>(std::move(this->inner));
101 }
105 constexpr operator std::optional<TValueStore>() noexcept { return toStdOptional(); }
106
113 template<typename TOutValue, std::invocable<TValueDeref&&> TMapFn>
114 requires (!std::is_reference_v<TValue>)
115 constexpr IterValue<TOutValue> map(TMapFn mapFn) {
116 if(!has_value()) { return {}; }
117 return mapFn(std::forward<TValueDeref>(value()));
118 }
119
126 template<typename TOutValue, std::invocable<TValue> TMapFn>
127 requires std::is_reference_v<TValue>
128 constexpr IterValue<TOutValue> map(TMapFn mapFn) {
129 if(!this->has_value()) { return {}; }
130 return mapFn(this->value());
131 }
132 };
133
134}
Container that is used to pass elements through CXXIter's iterator pipelines.
Definition: IterValue.h:19
constexpr bool has_value() const noexcept
Get whether this optional IteratorValue contains a value.
Definition: IterValue.h:86
constexpr std::optional< TValueStore > toStdOptional() noexcept
Convert this IterValue to a std::optional<> on the owned (no-reference) type.
Definition: IterValue.h:99
constexpr void swap(IterValue< TValue > &o) noexcept
Swap the values within this and another IterValue container.
Definition: IterValue.h:92
constexpr const TValueDeref & value_or(TValueDeref &&def) const noexcept
Get the contained value, or alternatively the given def if none is present.
Definition: IterValue.h:74
constexpr IterValue(TValue value) noexcept
Definition: IterValue.h:35
constexpr IterValue(IterValue &&o) noexcept(std::is_nothrow_move_constructible_v< TValueDeref >)
Definition: IterValue.h:46
constexpr IterValue< TOutValue > map(TMapFn mapFn)
Map this IterValue's contained value (if any) to the requested new TOutValue type,...
Definition: IterValue.h:128
constexpr IterValue< TOutValue > map(TMapFn mapFn)
Map this IterValue's contained value (if any) to the requested new TOutValue type,...
Definition: IterValue.h:115
constexpr TValueDeref & value_or(TValueDeref &&def) noexcept
Get the contained value, or alternatively the given def if none is present.
Definition: IterValue.h:80
constexpr IterValue(TValueDeref &&value) noexcept(std::is_nothrow_move_constructible_v< TValueDeref >)
Definition: IterValue.h:40
constexpr TValueDeref & value()
Get the contained value (if any).
Definition: IterValue.h:68
constexpr IterValue() noexcept
Definition: IterValue.h:33
constexpr IterValue & operator=(IterValue &&o)=default
constexpr auto & operator=(TValueDeref &&o)
Definition: IterValue.h:52
constexpr const TValueDeref & value() const
Get the contained value (if any).
Definition: IterValue.h:62
constexpr IterValue(const TValueDeref &value) noexcept(std::is_nothrow_copy_constructible_v< TValueDeref >)
Definition: IterValue.h:37
CXXIter.
Definition: CXXIter.h:48