-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path10_factory_class.cpp
81 lines (68 loc) · 2.39 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
#include <filesystem>
#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) {
const auto extension = std::filesystem::path(path).extension();
if (extension == ".pdf") return std::make_unique<PdfDocument>(path);
if (extension == ".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)>;
void register_plugin(std::string_view extension, ConcreteOpener opener) {
openerByExtension.emplace(extension, opener);
}
DocumentType open(std::string_view path) {
if (const auto p = std::filesystem::path(path); p.has_extension()) {
return openerByExtension.at(p.extension().string())(path);
}
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_plugin(
".pdf", [](auto path) -> DocumentOpener::DocumentType {
return std::make_unique<PdfDocument>(path);
});
document_opener.register_plugin(
".html", [](auto path) -> DocumentOpener::DocumentType {
return std::make_unique<HtmlDocument>(path);
});
document_opener.register_plugin(
".odt", [](auto path) -> DocumentOpener::DocumentType {
return std::make_unique<OdtDocument>(path);
});
auto document = document_opener.open("file.odt");
std::cout << document->extract_text().front();
}