-
Notifications
You must be signed in to change notification settings - Fork 0
/
interfaces.h
108 lines (81 loc) · 2.9 KB
/
interfaces.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
100
101
102
103
104
105
106
////////////////////////////////////////////////////////////////////////////////
template <typename output_type_>
class SampleSource {
public:
typedef output_type_ output_type;
typedef sample_type_traits<output_type> traits;
virtual ~SampleSource() {};
virtual output_type read() = 0;
};
////////////////////////////////////////////////////////////////////////////////
template <typename output_type_>
class CustomSampleSource : public SampleSource<output_type_> {
public:
typedef output_type_ (*func_type)();
func_type func;
CustomSampleSource(func_type f) : func(f) {};
virtual inline ~CustomSampleSource() {};
virtual inline output_type_ read() {
return (*func)();
}
};
////////////////////////////////////////////////////////////////////////////////
template <typename input_type_>
class SampleSink {
public:
typedef input_type_ input_type;
SampleSource<input_type> * source;
virtual ~SampleSink() {};
virtual inline void connect(SampleSource<input_type> * source_ = NULL) {
source = source_;
}
virtual inline bool sink() = 0;
protected:
inline input_type_ read() {
return source->read();
}
};
////////////////////////////////////////////////////////////////////////////////
template <typename input_type_>
class CustomSampleSink : public SampleSink<input_type_> {
public:
inline ~CustomSampleSink() {};
typedef bool (*func_type)(input_type_);
func_type func;
inline CustomSampleSink(func_type f, SampleSource<input_type_> * source = NULL) : func(f) {
connect(source);
}
virtual inline bool sink() {
return (*func)(SampleSink<input_type_>::read());
}
};
////////////////////////////////////////////////////////////////////////////////
template <typename input_type_, typename output_type_ = input_type_ >
class SampleProcessor : public SampleSource<output_type_> {
public:
typedef input_type_ input_type;
typedef output_type_ output_type;
SampleSource<input_type> * source;
virtual inline ~SampleProcessor() {};
virtual inline void connect(SampleSource<input_type> * source_ = NULL) {
source = source_;
}
virtual inline output_type read() {
return process(source->read());
}
virtual inline output_type process(input_type in) { return in; }
};
////////////////////////////////////////////////////////////////////////////////
template <typename input_type_, typename output_type_ = input_type_ >
class CustomSampleProcessor : public SampleProcessor<input_type_, output_type_> {
public:
typedef output_type_ (*func_type)(input_type_);
func_type func;
inline CustomSampleProcessor(func_type f, SampleSource<input_type_> * source = NULL) : func(f) {
connect(source);
};
virtual inline ~CustomSampleProcessor() {};
virtual inline output_type_ process(input_type_ v) {
return (*func)(v);
}
};