CXXIter 0.2
Loading...
Searching...
No Matches
Helpers.h
1#pragma once
2
3#include <tuple>
4#include <cstdlib>
5#include <optional>
6#include <unordered_set>
7
15namespace CXXIter::fn {
16
17 // ################################################################################################
18 // HELPERS
19 // ################################################################################################
20
38 template<size_t FIELD_IDX>
39 auto unzip() {
40 return [](const auto& item) { return std::get<FIELD_IDX>(item); };
41 }
42
75 template<typename TDynCastTarget>
76 auto tryDynCast() {
77 return [](auto itemPtr) -> std::optional<TDynCastTarget> {
78 TDynCastTarget targetPtr = dynamic_cast<TDynCastTarget>(itemPtr);
79 if(targetPtr == nullptr) { return {}; }
80 return targetPtr;
81 };
82 }
83
119 template<typename TItem, typename TFilterExtractFn>
120 auto filterIsOneOf(TFilterExtractFn filterExtractFn, const std::initializer_list<TItem>& acceptedValues) {
121 std::unordered_set<TItem> _acceptedValues = acceptedValues;
122 return [_acceptedValues = std::move(_acceptedValues), filterExtractFn](const auto& item) {
123 return _acceptedValues.contains(filterExtractFn(item));
124 };
125 }
126
156 template<typename TItem>
157 auto filterIsOneOf(const std::initializer_list<TItem>& acceptedValues) {
158 return filterIsOneOf<TItem>([](const auto& item) { return item; }, acceptedValues);
159 }
160
161}
Namespace that contains helper functions providing commonly required functionality when working with ...
Definition: Helpers.h:15
auto tryDynCast()
Helper to construct a filterMap() lambda that takes a pointer, dyncasts it to the requested type and ...
Definition: Helpers.h:76
auto unzip()
Helper to construct a lambda that extracts the FIELD_IDX-th element from tuples or pairs passing thro...
Definition: Helpers.h:39
auto filterIsOneOf(TFilterExtractFn filterExtractFn, const std::initializer_list< TItem > &acceptedValues)
Helper that constructs a filter() lambda that filters the iterator elements by calling the given filt...
Definition: Helpers.h:120