CXXIter 0.2
Loading...
Searching...
No Matches
Caster.h
1#pragma once
2
3#include <cstdlib>
4#include <utility>
5#include <type_traits>
6
7#include "../Common.h"
8
9namespace CXXIter {
10
11 // ################################################################################################
12 // CASTER
13 // ################################################################################################
14 namespace op {
16 template<typename TChainInput, typename TItem>
17 requires std::is_object_v<TItem>
18 class [[nodiscard(CXXITER_CHAINER_NODISCARD_WARNING)]] Caster : public IterApi<Caster<TChainInput, TItem>> {
19 friend struct trait::Iterator<Caster<TChainInput, TItem>>;
20 friend struct trait::DoubleEndedIterator<Caster<TChainInput, TItem>>;
21 friend struct trait::ExactSizeIterator<Caster<TChainInput, TItem>>;
22 private:
23 TChainInput input;
24 public:
25 constexpr Caster(TChainInput&& input) : input(std::move(input)) {}
26 };
27 }
28 // ------------------------------------------------------------------------------------------------
30 template<typename TChainInput, typename TItem>
31 requires std::is_object_v<TItem>
32 struct trait::Iterator<op::Caster<TChainInput, TItem>> {
33 using ChainInputIterator = trait::Iterator<TChainInput>;
34 // CXXIter Interface
35 using Self = op::Caster<TChainInput, TItem>;
36 using Item = TItem;
37
38 static constexpr inline IterValue<Item> next(Self& self) {
39 auto item = ChainInputIterator::next(self.input);
40 return item.template map<Item>([](auto&& item) { return static_cast<Item>(item); });
41 }
42 static constexpr inline SizeHint sizeHint(const Self& self) { return ChainInputIterator::sizeHint(self.input); }
43 static constexpr inline size_t advanceBy(Self& self, size_t n) { return ChainInputIterator::advanceBy(self.input, n); }
44 };
46 template<CXXIterDoubleEndedIterator TChainInput, typename TItem>
47 requires std::is_object_v<TItem>
48 struct trait::DoubleEndedIterator<op::Caster<TChainInput, TItem>> {
49 using ChainInputIterator = trait::DoubleEndedIterator<TChainInput>;
50 using Self = op::Caster<TChainInput, TItem>;
51 using Item = TItem;
52
53 static constexpr inline IterValue<TItem> nextBack(Self& self) {
54 auto item = ChainInputIterator::nextBack(self.input);
55 return item.template map<Item>([](auto&& item) { return static_cast<Item>(item); });
56 }
57 };
59 template<CXXIterExactSizeIterator TChainInput, typename TItem>
60 struct trait::ExactSizeIterator<op::Caster<TChainInput, TItem>> {
61 static constexpr inline size_t size(const op::Caster<TChainInput, TItem>& self) { return trait::ExactSizeIterator<TChainInput>::size(self.input); }
62 };
63
64}
CXXIter.
Definition: CXXIter.h:48