Health-GPS 3.0.0.0
Global Health Policy Simulation model (Health-GPS)
Loading...
Searching...
No Matches
identifier.h
Go to the documentation of this file.
1#pragma once
2#include <functional>
3#include <map>
4#include <ostream>
5#include <string>
6#include <unordered_map>
7
8#include "nlohmann/json.hpp"
9
10namespace hgps {
11namespace core {
12
20struct Identifier final {
22 constexpr Identifier() = default;
23
28 Identifier(const char *const value);
29
34 Identifier(const std::string &value);
35
38 bool is_empty() const noexcept;
39
42 std::size_t size() const noexcept;
43
46 const std::string &to_string() const noexcept;
47
50 std::size_t hash() const noexcept;
51
55 bool equal(const std::string &other) const noexcept;
56
60 bool equal(const Identifier &other) const noexcept;
61
65 bool operator==(const Identifier &rhs) const noexcept;
66
70 std::strong_ordering operator<=>(const Identifier &rhs) const noexcept = default;
71
74 static Identifier empty();
75
80 friend std::ostream &operator<<(std::ostream &stream, const Identifier &identifier);
81
82 private:
83 std::string value_{};
84 std::size_t hash_code_{std::hash<std::string>{}("")};
85
86 void validate_identifier() const;
87};
88
89void from_json(const nlohmann::json &j, Identifier &id);
90
91namespace detail {
92template <template <typename...> class Map, class Value>
93void map_from_json(const nlohmann::json &j, Map<hgps::core::Identifier, Value> &map) {
94 map.clear();
95 for (const auto &item : j.items()) {
96 map.emplace(item.key(), item.value().get<Value>());
97 }
98}
99} // namespace detail
100
101} // namespace core
102
104core::Identifier operator""_id(const char *id, size_t);
105} // namespace hgps
106
107namespace std {
108template <class T>
109void from_json(const nlohmann::json &j, std::map<hgps::core::Identifier, T> &map) {
110 hgps::core::detail::map_from_json(j, map);
111}
112
113template <class T>
114void from_json(const nlohmann::json &j, std::unordered_map<hgps::core::Identifier, T> &map) {
115 hgps::core::detail::map_from_json(j, map);
116}
117
119template <> struct hash<hgps::core::Identifier> {
120 size_t operator()(const hgps::core::Identifier &id) const noexcept { return id.hash(); }
121};
122
123} // namespace std
void from_json(const nlohmann::json &j, Identifier &id)
Definition identifier.cpp:60
@ other
Common diseases excluding cancers.
Top-level namespace for Health-GPS Console host application.
Definition command_options.cpp:8
Global namespace.
Definition column_iterator.h:123
Entity unique identifier data type.
Definition identifier.h:20
bool is_empty() const noexcept
Checks whether this is an empty identifier.
Definition identifier.cpp:24
bool equal(const std::string &other) const noexcept
Determines whether a string representation and this instance have same value.
Definition identifier.cpp:36
constexpr Identifier()=default
Initialises a new instance of the hgps::core::Identifier class.
std::size_t size() const noexcept
Gets the side of the identifier.
Definition identifier.cpp:26
std::size_t hash() const noexcept
Gets the identifier hash code value.
Definition identifier.cpp:30
static Identifier empty()
Represents the empty Identifier, read-only.
Definition identifier.cpp:9
const std::string & to_string() const noexcept
Convert the identifier to a string representation.
Definition identifier.cpp:28