File: generator.imp

package info (click to toggle)
c%2B%2B-annotations 13.02.02-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,576 kB
  • sloc: cpp: 25,297; makefile: 1,523; ansic: 165; sh: 126; perl: 90; fortran: 27
file content (71 lines) | stat: -rw-r--r-- 1,332 bytes parent folder | download | duplicates (3)
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
template <typename T>
Generator<T>::Generator(Generator &&tmp) noexcept
: 
    d_handle(tmp.d_handle)
{
    tmp.d_handle = nullptr;
}

template <typename T>
Generator<T>::~Generator()
{
    if (d_handle)
        d_handle.destroy();
}

template <typename T>
Generator<T> &Generator<T>::operator=(Generator<T> &&tmp) noexcept
{
    swap(tmp);
    return *this;
}

template <typename T>
Iterator<T> Generator<T>::begin()
{
    if (d_handle)
    {
        d_handle.resume();
        if (d_handle.done())
            d_handle.promise().rethrow_if_exception();
    }

    return Iterator<T>{ d_handle };
}

template <typename T>
inline Sentinel Generator<T>::end() noexcept
{
    return Sentinel{};
}

template <typename T>
inline void Generator<T>::swap(Generator &other) noexcept
{
    std::swap(d_handle, other.d_handle);
}

template <typename T>
inline
Generator<T>::Generator(Handle handle) noexcept
: 
    d_handle(handle)
{}

//------------------------------------------------

template<typename FUNC, typename T>
Generator<
    std::invoke_result_t<
        FUNC &, 
        typename Generator<T>::iterator::reference>
    >
fmap(FUNC func, Generator<T> source)
{
    for (auto &&value: source)
        co_yield std::invoke(
                    func, 
                    static_cast<decltype(value)>(value)
                );
}