ResourceLocator.hpp 1.1 KB
#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;
};