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  friend std::hash<class_id>;
29 
30 public:
31  constexpr class_id() noexcept = default;
32  QX_COPYMOVABLE(class_id);
33 
34  /**
35  @brief Create a class id for a given type (the type doesn't require to use qx rtti system)
36  @tparam T - a type
37  @retval - T id
38  **/
39  template<class T>
40  constexpr static class_id create();
41 
42  constexpr bool operator==(const class_id& other) const noexcept;
43  constexpr bool operator!=(const class_id& other) const noexcept;
44 
45  // makes no sense except for use in ordered containers
46  constexpr bool operator<(const class_id& other) const noexcept;
47  // makes no sense except for use in switch
48  constexpr operator size_t() const noexcept;
49 
50  /**
51  @brief Get class name
52  @retval - class name
53  **/
54  constexpr string_view get_class_name() const;
55 
56 private:
57  string_view m_svClassName;
58  size_t m_nId = 0;
59 };
60 
61 constexpr bool class_id::operator==(const class_id& other) const noexcept
62 {
63  return m_nId == other.m_nId;
64 }
65 
66 constexpr bool class_id::operator!=(const class_id& other) const noexcept
67 {
68  return m_nId != other.m_nId;
69 }
70 
71 constexpr bool class_id::operator<(const class_id& other) const noexcept
72 {
73  return m_nId < other.m_nId;
74 }
75 
76 constexpr class_id::operator size_t() const noexcept
77 {
78  return m_nId;
79 }
80 
81 constexpr string_view class_id::get_class_name() const
82 {
83  return m_svClassName;
84 }
85 
86 template<class T>
87 constexpr class_id class_id::create()
88 {
89  class_id result;
90  result.m_svClassName = type_strings<T, char_type>::get_signature();
91  result.m_nId = string_hash(result.m_svClassName);
92  return result;
93 }
94 
95 } // namespace qx
96 
97 template<>
98 struct std::hash<qx::class_id>
99 {
100  constexpr size_t operator()(const qx::class_id& id) const noexcept
101  {
102  return id.m_nId;
103  }
104 };
105 
106 template<>
107 struct std::formatter<qx::class_id, qx::char_type> : qx::basic_formatter
108 {
109  template<class FormatContextType>
110  constexpr auto format(const qx::class_id& id, FormatContextType& ctx) const
111  {
112  return std::format_to(ctx.out(), QX_TEXT("{}"), id.get_class_name());
113  }
114 };
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:81
static constexpr string_view_type get_signature()
Get type signature (full name with template parameters)