CXXIter 0.2
Loading...
Searching...
No Matches
InplaceModifier.h
1#pragma once
2
3#include <cstdlib>
4#include <type_traits>
5
6#include "../Common.h"
7
8namespace CXXIter {
9
10 // ################################################################################################
11 // INPLACE MODIFIER
12 // ################################################################################################
13 namespace op {
15 template<typename TChainInput, typename TModifierFn>
16 requires std::is_object_v<typename trait::Iterator<TChainInput>::Item> || (!std::is_const_v<typename trait::Iterator<TChainInput>::Item>)
17 class [[nodiscard(CXXITER_CHAINER_NODISCARD_WARNING)]] InplaceModifier : public IterApi<InplaceModifier<TChainInput, TModifierFn>> {
18 friend struct trait::Iterator<InplaceModifier<TChainInput, TModifierFn>>;
19 friend struct trait::DoubleEndedIterator<InplaceModifier<TChainInput, TModifierFn>>;
20 friend struct trait::ExactSizeIterator<InplaceModifier<TChainInput, TModifierFn>>;
21 private:
22 using InputItem = typename TChainInput::Item;
23
24 TChainInput input;
25 TModifierFn modifierFn;
26 public:
27 constexpr InplaceModifier(TChainInput&& input, TModifierFn modifierFn) : input(std::move(input)), modifierFn(modifierFn) {}
28 };
29 }
30 // ------------------------------------------------------------------------------------------------
32 template<typename TChainInput, typename TModifierFn>
33 struct trait::Iterator<op::InplaceModifier<TChainInput, TModifierFn>> {
34 using ChainInputIterator = trait::Iterator<TChainInput>;
35 // CXXIter Interface
36 using Self = op::InplaceModifier<TChainInput, TModifierFn>;
37 using Item = typename ChainInputIterator::Item;
38
39 static constexpr inline IterValue<Item> next(Self& self) {
40 auto item = ChainInputIterator::next(self.input);
41 if(!item.has_value()) [[unlikely]] { return {}; }
42 self.modifierFn(item.value());
43 return item;
44 }
45 static constexpr inline SizeHint sizeHint(const Self& self) { return ChainInputIterator::sizeHint(self.input); }
46 static constexpr inline size_t advanceBy(Self& self, size_t n) { return ChainInputIterator::advanceBy(self.input, n); }
47 };
49 template<CXXIterDoubleEndedIterator TChainInput, typename TModifierFn>
50 struct trait::DoubleEndedIterator<op::InplaceModifier<TChainInput, TModifierFn>> {
51 using ChainInputIterator = trait::DoubleEndedIterator<TChainInput>;
52 // CXXIter Interface
53 using Self = op::InplaceModifier<TChainInput, TModifierFn>;
54 using Item = typename ChainInputIterator::Item;
55
56 static constexpr inline IterValue<Item> nextBack(Self& self) {
57 auto item = ChainInputIterator::nextBack(self.input);
58 if(!item.has_value()) [[unlikely]] { return {}; }
59 self.modifierFn(item.value());
60 return item;
61 }
62 };
64 template<CXXIterExactSizeIterator TChainInput, typename TItem>
65 struct trait::ExactSizeIterator<op::InplaceModifier<TChainInput, TItem>> {
66 static constexpr inline size_t size(const op::InplaceModifier<TChainInput, TItem>& self) { return trait::ExactSizeIterator<TChainInput>::size(self.input); }
67 };
68
69}
CXXIter.
Definition: CXXIter.h:48
static constexpr IterValue< typename Iterator< T >::Item > nextBack(Self &self)=delete
Pull the next last element from the iterator pipeline previous to this pipeline-element.
static constexpr size_t size(const typename trait::Iterator< T >::Self &self)=delete
Get the iterator's exact number of elements.
static constexpr IterValue< Item > next(Self &self)=delete
Pull one element from the iterator pipeline previous to this pipeline-element.
void Item
Item-Type. This is the type of elements that can be pulled from this pipeline-element.
Definition: Traits.h:32
trait::Iterator< T > Self
Self-Type. This is the type of the struct for which the trait::Iterator is being specialized.
Definition: Traits.h:28
static constexpr SizeHint sizeHint(const Self &self)=delete
Get the bounds on the remaining length of the iterator pipeline until this pipeline-element,...
static constexpr size_t advanceBy(Self &self, size_t n)=delete
Advance the iterator by n elements.