mirror of
https://github.com/momo5502/emulator.git
synced 2026-01-22 13:13:57 +00:00
Introduce path_key util
It represents a canonical path that can be used as key for unordered containers
This commit is contained in:
61
src/common/utils/path_key.hpp
Normal file
61
src/common/utils/path_key.hpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include "string.hpp"
|
||||
#include <filesystem>
|
||||
|
||||
namespace utils
|
||||
{
|
||||
class path_key
|
||||
{
|
||||
public:
|
||||
path_key() = default;
|
||||
path_key(const std::filesystem::path& p)
|
||||
: path_(canonicalize_path(p))
|
||||
{
|
||||
}
|
||||
|
||||
path_key(const path_key&) = default;
|
||||
path_key(path_key&&) noexcept = default;
|
||||
|
||||
path_key& operator=(const path_key&) = default;
|
||||
path_key& operator=(path_key&&) noexcept = default;
|
||||
|
||||
~path_key() = default;
|
||||
|
||||
const std::filesystem::path& get() const
|
||||
{
|
||||
return this->path_;
|
||||
}
|
||||
|
||||
bool operator==(const path_key& other) const
|
||||
{
|
||||
return this->get() == other.get();
|
||||
}
|
||||
|
||||
bool operator!=(const path_key& other) const
|
||||
{
|
||||
return !this->operator==(other);
|
||||
}
|
||||
|
||||
static std::filesystem::path canonicalize_path(const std::filesystem::path& key)
|
||||
{
|
||||
auto path = key.lexically_normal().wstring();
|
||||
return utils::string::to_lower_consume(path);
|
||||
}
|
||||
|
||||
private:
|
||||
std::filesystem::path path_{};
|
||||
};
|
||||
}
|
||||
|
||||
namespace std
|
||||
{
|
||||
template <>
|
||||
struct hash<utils::path_key>
|
||||
{
|
||||
size_t operator()(const utils::path_key& p) const noexcept
|
||||
{
|
||||
return hash<std::filesystem::path::string_type>()(p.get().native());
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user