-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerator.hpp
101 lines (83 loc) · 2.76 KB
/
generator.hpp
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
100
101
//
// Generator.h
// buffermanager
//
// Created by Simon Wagner on 10.06.13.
// Copyright (c) 2013 DBTUM. All rights reserved.
//
#ifndef __buffermanager__Generator__
#define __buffermanager__Generator__
#include <ucontext.h>
#include <functional>
#include <cassert>
#include <memory>
//typedef int GT;
enum GeneratorState {
UNKNOWN, HAS_NEXT, END, INVALID
};
template<typename GT>
class Generator;
template<typename GT>
class GeneratorIterator {
Generator<GT>& generator;
int64_t index;
GT* value;
public:
static constexpr int64_t END_INDEX = -1;
private:
bool next();
public:
GeneratorIterator(Generator<GT>& generator, int64_t index);
GeneratorIterator& operator++(); //++GeneratorIterator
GeneratorIterator operator++(int); //GeneratorIterator++
int64_t getIndex() { return index; }
bool operator==(const GeneratorIterator<GT>& other) { return this->index == other.index; }
bool operator!=(const GeneratorIterator<GT>& other) { return this->index != other.index; }
GT& operator*() { assert(value); return *value; }
GT* operator->() { assert(value); return value; }
};
template<typename GT>
class Generator {
public:
typedef std::function<void(Generator<GT>&)> GeneratorRunCallback;
private:
friend GeneratorIterator<GT>;
GT* m_Next;
GeneratorState m_state;
ucontext_t m_mainContext;
ucontext_t m_generatorContext;
GeneratorRunCallback m_Run;
void* m_stackData;
private:
void take();
static void generatorCallback(uint32_t upperHalf, uint32_t lowerHalf);
public:
Generator(GeneratorRunCallback run);
~Generator();
Generator(const Generator& g) = delete;
Generator& operator=(const Generator& g) = delete;
void yield(GT&& value);
void exit();
GT next();
bool hasNext();
GeneratorIterator<GT> begin() { return GeneratorIterator<GT>(*this, 0); };
GeneratorIterator<GT> end() { return GeneratorIterator<GT>(*this, GeneratorIterator<GT>::END_INDEX); };
};
template<typename GT>
class GeneratorIterable {
std::unique_ptr<Generator<GT>> generator;
public:
GeneratorIterable(std::unique_ptr<Generator<GT>> generator) : generator(std::move(generator)) {}
GeneratorIterable(GeneratorIterable&& i) : generator(std::move(i.generator)) {};
GeneratorIterable(const GeneratorIterable& i) = delete;
GeneratorIterable& operator=(const GeneratorIterable& i) = delete;
GeneratorIterator<GT> begin() { return generator->begin(); };
GeneratorIterator<GT> end() { return generator->end(); };
};
template<typename GT>
inline GeneratorIterable<GT> make_generator(typename Generator<GT>::GeneratorRunCallback run)
{
return GeneratorIterable<GT>(std::unique_ptr<Generator<GT>>(new Generator<GT>(run)));
}
#include "generator.inl"
#endif /* defined(__buffermanager__Generator__) */