forked from PacktPublishing/Software-Architecture-with-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10_factory_class.cpp
84 lines (71 loc) · 2.64 KB
/
10_factory_class.cpp
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
#include <functional>
#include <iostream>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
struct IDocument {
virtual ~IDocument() = default;
virtual std::vector<std::string> extract_text() = 0;
};
class PdfDocument : public IDocument {
public:
explicit PdfDocument(std::string_view path) {}
std::vector<std::string> extract_text() override { return {"Text from PDF"}; }
};
class HtmlDocument : public IDocument {
public:
explicit HtmlDocument(std::string_view path) {}
std::vector<std::string> extract_text() override {
return {"Text from HTML"};
}
};
class OdtDocument : public IDocument {
public:
explicit OdtDocument(std::string_view path) {}
std::vector<std::string> extract_text() override { return {"Text from ODT"}; }
};
std::unique_ptr<IDocument> open(std::string_view path) {
if (path.ends_with(".pdf"))
return std::make_unique<PdfDocument>(path); // C++20 addition to sv
if (path.ends_with(".html")) return std::make_unique<HtmlDocument>(path);
return nullptr;
}
class DocumentOpener {
public:
using DocumentType = std::unique_ptr<IDocument>;
using ConcreteOpener = DocumentType (*)(std::string_view);
// using ConcreteOpener = std::function<DocumentType(std::string_view)>;
// using ConcreteOpener = tl::function_ref<DocumentType(std::string_view)>;
void Register(std::string_view extension, ConcreteOpener opener) {
openerByExtension.emplace(extension, opener);
}
DocumentType open(std::string_view path) {
if (auto last_dot = path.find_last_of('.');
last_dot != std::string_view::npos) {
auto extension = path.substr(last_dot + 1);
return openerByExtension.at(extension)(path);
} else {
throw std::invalid_argument{"Trying to open a file with no extension"};
}
}
private:
std::unordered_map<std::string_view, ConcreteOpener> openerByExtension;
};
int main() {
auto document_opener = DocumentOpener{};
document_opener.Register("pdf",
[](auto path) -> DocumentOpener::DocumentType {
return std::make_unique<PdfDocument>(path);
});
document_opener.Register("html",
[](auto path) -> DocumentOpener::DocumentType {
return std::make_unique<HtmlDocument>(path);
});
document_opener.Register("odt",
[](auto path) -> DocumentOpener::DocumentType {
return std::make_unique<OdtDocument>(path);
});
auto document = document_opener.open("file.odt");
std::cout << document->extract_text().front();
}