forked from cpp-testing/GUnit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic_polymorphism.cpp
More file actions
48 lines (38 loc) · 1.03 KB
/
Copy pathstatic_polymorphism.cpp
File metadata and controls
48 lines (38 loc) · 1.03 KB
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
#include <cassert>
#include <fstream>
#include <iostream>
template <class T>
struct Reader {
int read() { return static_cast<T*>(this)->read_impl(); }
int read_impl() { return 0; }
};
struct FileReader : Reader<FileReader> {
explicit FileReader(const std::string& str) : file(str) { assert(file.good()); }
int read_impl() {
auto value = 0;
file >> value;
return value;
}
std::fstream file;
};
template <class T>
struct Viewer {
void show(int value) { static_cast<T*>(this)->show_impl(value); }
void show_impl(int) {}
};
struct ConsoleViewer : Viewer<ConsoleViewer> {
void show_impl(int value) { std::cout << value << std::endl; }
};
template <class TReader, class TViewer>
struct App {
TReader& reader;
TViewer& viewer;
App(TReader& reader, TViewer& viewer) : reader(reader), viewer(viewer) {}
void run() { viewer.show(reader.read()); }
};
int main() {
FileReader reader{"input.txt"};
ConsoleViewer viewer{};
App<FileReader, ConsoleViewer>{reader, viewer}.run();
// App{reader, viewer}.run();
}