Lodestone.Common
Loading...
Searching...
No Matches
Identifier.h
Go to the documentation of this file.
1//
2// Created by DexrnZacAttack on 10/17/25 using zPc-i2.
3//
4#ifndef LODESTONE_NAMESPACEDSTRING_H
5#define LODESTONE_NAMESPACEDSTRING_H
7#include <cstring>
8#include <format>
9
11
12// this class required me to upgrade to c++20
15 protected:
16 explicit constexpr Identifier(const char *name) noexcept
17 : m_namespace("lodestone"), m_path(name) {};
18
19 public:
20 constexpr Identifier(const char *_namespace, const char *path) noexcept
21 : m_namespace(_namespace), m_path(path) {};
22
23 constexpr const char *getPath() const noexcept { return m_path; }
24
25 constexpr std::string_view getPathView() const noexcept { return m_path; }
26
27 constexpr const char *getNamespace() const noexcept { return m_namespace; }
28
29 constexpr std::string_view getNamespaceView() const noexcept { return m_namespace; }
30
31 constexpr std::string getString() const noexcept {
32 return std::string(m_namespace) + ":" + m_path;
33 }
34
35 constexpr std::string toString() const override { return getString(); };
36
37 constexpr bool operator==(const Identifier &rhs) const noexcept {
38 return util::Util::strcmpConstexpr(this->m_path, rhs.m_path) &&
40 rhs.m_namespace);
41 }
42
43 constexpr operator std::string() const noexcept {
44 return getString();
45 }
46
47 constexpr friend std::ostream &
48 operator<<(std::ostream &os, const Identifier &s) noexcept {
49 os << s.getString();
50 return os;
51 }
52
53 private:
54 const char *m_namespace;
55 const char *m_path;
56 };
57} // namespace lodestone::common::registry
58
59template <>
60struct std::formatter<lodestone::common::registry::Identifier>
61 : formatter<string> {
62 std::format_context::iterator
64 format_context &c) const {
65 return formatter<string>::format(s.getString(), c);
66 }
67};
68
69template <>
70struct std::hash<lodestone::common::registry::Identifier> {
71 size_t operator()(
72 const lodestone::common::registry::Identifier &s) const noexcept {
73 return std::hash<std::string_view>{}(s.getPath()) ^
74 (std::hash<std::string_view>{}(s.getNamespace()) << 1);
75 }
76};
77
78#endif // LODESTONE_NAMESPACEDSTRING_H
Definition Identifier.h:14
constexpr std::string_view getPathView() const noexcept
Definition Identifier.h:25
constexpr std::string getString() const noexcept
Definition Identifier.h:31
constexpr Identifier(const char *name) noexcept
Definition Identifier.h:16
const char * m_namespace
Definition Identifier.h:54
constexpr friend std::ostream & operator<<(std::ostream &os, const Identifier &s) noexcept
Definition Identifier.h:48
constexpr const char * getNamespace() const noexcept
Definition Identifier.h:27
constexpr const char * getPath() const noexcept
Definition Identifier.h:23
constexpr std::string_view getNamespaceView() const noexcept
Definition Identifier.h:29
const char * m_path
Definition Identifier.h:55
constexpr bool operator==(const Identifier &rhs) const noexcept
Definition Identifier.h:37
constexpr std::string toString() const override
Definition Identifier.h:35
constexpr Identifier(const char *_namespace, const char *path) noexcept
Definition Identifier.h:20
Java toString clone.
Definition StringSerializable.h:13
static constexpr bool strcmpConstexpr(const char *lhs, const char *rhs)
Definition Util.h:15
Definition Identifiable.h:8
std::format_context::iterator format(const lodestone::common::registry::Identifier &s, format_context &c) const
Definition Identifier.h:63
size_t operator()(const lodestone::common::registry::Identifier &s) const noexcept
Definition Identifier.h:71