forked from revng/revng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformadapter.h
99 lines (76 loc) · 2.88 KB
/
transformadapter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#ifndef _TRANSFORMADAPTER_H
#define _TRANSFORMADAPTER_H
//
// This file is distributed under the MIT License. See LICENSE.md for details.
//
// Standard includes
#include <functional>
// Local includes
#include "range.h"
#include "iteratorwrapper.h"
template<typename NewType, typename Wrapped>
class TransformIterator : public IteratorWrapper<Wrapped> {
private:
using base = IteratorWrapper<Wrapped>;
public:
using value_type = NewType;
using reference = NewType;
using pointer = typename base::pointer;
using difference_type = typename base::difference_type;
using transformer = std::function<NewType(typename base::reference)>;
public:
TransformIterator(Wrapped Iterator, transformer F) :
IteratorWrapper<Wrapped>(Iterator),
F(F) { }
reference operator*() const {
return F(base::operator*());
}
pointer operator->() const = delete;
reference operator[](const difference_type& n) const {
return F(base::operator[](n));
}
private:
transformer F;
};
namespace adaptors {
template<typename R, typename Iterator>
class Transform {
public:
using transformer = typename TransformIterator<R, Iterator>::transformer;
Transform(transformer Transformer) : Transformer(Transformer) { }
template<typename I>
Range<TransformIterator<R, I>> transform(Range<I> Input) {
return Range<TransformIterator<R, I>>(TransformIterator<R, I>(Input.begin(),
Transformer),
TransformIterator<R, I>(Input.end(),
Transformer));
}
private:
transformer Transformer;
};
}
template <typename T>
struct function_traits
: public function_traits<decltype(&T::operator())>
{ };
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...) const> {
using return_type = ReturnType;
};
template<typename R, typename I>
using TransformFunction = std::function<R(typename I::reference)>;
template<typename R, typename I>
auto operator|(Range<I> Input, R Transformer) -> Range<TransformIterator<typename function_traits<R>::return_type, I>> {
return adaptors::Transform<typename function_traits<R>::return_type, I>(Transformer).transform(Input);
}
template<typename R, typename C>
auto operator|(C Input, R Transformer) -> Range<TransformIterator<typename function_traits<R>::return_type, typename C::iterator>> {
return adaptors::Transform<typename function_traits<R>::return_type, typename C::iterator>(Transformer).transform(make_range(Input));
}
// template<typename C>
// Range<TransformIterator<typename C::iterator>>
// operator|(C Input, TransformFunction<typename C::iterator> Function) {
// adaptors::Transform<typename C::iterator> Transformer(Function);
// return Transformer.transform(make_range(Input));
// }
#endif // _TRANSFORMADAPTER_H