forked from cpp-testing/GUnit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype_erasure.cpp
More file actions
47 lines (37 loc) · 1.18 KB
/
Copy pathtype_erasure.cpp
File metadata and controls
47 lines (37 loc) · 1.18 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
#include <boost/type_erasure/any.hpp>
#include <boost/type_erasure/builtin.hpp>
#include <boost/type_erasure/free.hpp>
#include <boost/type_erasure/member.hpp>
#include <cassert>
#include <fstream>
#include <iostream>
BOOST_TYPE_ERASURE_MEMBER((has_read), read, 0)
BOOST_TYPE_ERASURE_MEMBER((has_show), show, 1)
using Reader = boost::mpl::vector<has_read<int()>, boost::type_erasure::copy_constructible<>>;
using Viewer = boost::mpl::vector<has_show<void(int)>, boost::type_erasure::copy_constructible<>>;
class FileReader {
std::fstream file;
public:
explicit FileReader(const std::string& str) : file(path) { assert(file.good()); }
int read() {
auto value = 12;
file >> value;
return value;
}
};
class ConsoleViewer {
public:
void show(int value) { std::cout << value << std::endl; }
};
class App {
boost::type_erasure::any<Reader> reader;
boost::type_erasure::any<Viewer> viewer;
public:
App(boost::type_erasure::any<Reader> reader, boost::type_erasure::any<Viewer> viewer) : reader(reader), viewer(viewer) {}
void run() { viewer.show(reader.read()); }
};
int main() {
FileReader reader{"input.txt"};
ConsoleViewer viewer{};
App{reader, viewer}.run();
}