qxLib
class_id.h
Go to the documentation of this file.
1 /**
2 
3  @file class_id.h
4  @author Khrapov
5  @date 11.09.2021
6  @copyright © Nick Khrapov, 2021. All right reserved.
7 
8 **/
9 #pragma once
10 
13 #include <qx/meta/type_strings.h>
14 
15 namespace qx
16 {
17 
18 /**
19 
20  @class class_id
21  @brief Class id, unique for each class using qx rtti system
22  @author Khrapov
23  @date 5.07.2024
24 
25 **/
26 class class_id
27 {
28  QX_COPYMOVABLE(class_id);
29 
30  friend std::hash<class_id>;
31 
32 public:
33  constexpr class_id() noexcept = default;
34 
35  /**
36  @brief Create a class id for a given type (the type doesn't require to use qx rtti system)
37  @tparam T - a type
38  @retval - T id
39  **/
40  template<class T>
41  constexpr static class_id create();
42 
43  constexpr bool operator==(const class_id& other) const noexcept;
44  constexpr bool operator!=(const class_id& other) const noexcept;
45 
46  // makes no sense except for use in ordered containers
47  constexpr bool operator<(const class_id& other) const noexcept;
48  // makes no sense except for use in switch
49  constexpr operator size_t() const noexcept;
50 
51  /**
52  @brief Get class name
53  @retval - class name
54  **/
55  constexpr string_view get_class_name() const;
56 
57 private:
58  string_view m_svClassName;
59  size_t m_nId = 0;
60 };
61 
62 constexpr bool class_id::operator==(const class_id& other) const noexcept
63 {
64  return m_nId == other.m_nId;
65 }
66 
67 constexpr bool class_id::operator!=(const class_id& other) const noexcept
68 {
69  return m_nId != other.m_nId;
70 }
71 
72 constexpr bool class_id::operator<(const class_id& other) const noexcept
73 {
74  return m_nId < other.m_nId;
75 }
76 
77 constexpr class_id::operator size_t() const noexcept
78 {
79  return m_nId;
80 }
81 
82 constexpr string_view class_id::get_class_name() const
83 {
84  return m_svClassName;
85 }
86 
87 template<class T>
88 constexpr class_id class_id::create()
89 {
90  class_id result;
91  result.m_svClassName = type_strings<T, char_type>::get_signature();
92  result.m_nId = string_hash(result.m_svClassName);
93  return result;
94 }
95 
96 } // namespace qx
97 
98 template<>
99 struct std::hash<qx::class_id>
100 {
101  constexpr size_t operator()(const qx::class_id& id) const noexcept
102  {
103  return id.m_nId;
104  }
105 };
106 
107 template<>
108 struct std::formatter<qx::class_id, qx::char_type> : qx::basic_formatter
109 {
110  template<class FormatContextType>
111  constexpr auto format(const qx::class_id& id, FormatContextType& ctx) const
112  {
113  return std::format_to(ctx.out(), QX_TEXT("{}"), id.get_class_name());
114  }
115 };
String hash object.
Definition: string_hash.h:38
Class id, unique for each class using qx rtti system.
Definition: class_id.h:27
constexpr static class_id create()
Create a class id for a given type (the type doesn't require to use qx rtti system)
constexpr string_view get_class_name() const
Get class name.
Definition: class_id.h:82
static constexpr string_view_type get_signature()
Get type signature (full name with template parameters)