ResourceLocator.hpp
1.1 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
#pragma once
#include <exception>
#include <memory>
#include <string>
class PathProvider {
public:
PathProvider() = default;
PathProvider(const PathProvider &) = default;
PathProvider(PathProvider &&) noexcept = default;
PathProvider &operator=(PathProvider &&) noexcept = default;
PathProvider &operator=(const PathProvider &) = default;
virtual ~PathProvider() = default;
virtual std::string path(const std::string &file) const = 0;
};
class AbsolutePathProvider : public PathProvider {
public:
explicit AbsolutePathProvider(const std::string &relativePath)
: relativePath(relativePath) {}
std::string path(const std::string &file) const override {
return relativePath + file;
}
private:
const std::string relativePath;
};
class ResourceLocator {
public:
static const PathProvider &getPathProvider() {
if (!pathProvider) {
throw std::runtime_error("Path provider not provided");
}
return *pathProvider;
}
static void setPathProvider(std::unique_ptr<PathProvider> provider) {
pathProvider = std::move(provider);
}
private:
static std::unique_ptr<PathProvider> pathProvider;
};