qxLib
category.h
Go to the documentation of this file.
1 /**
2 
3  @file category.h
4  @author Khrapov
5  @date 5.12.2022
6  @copyright (c) Nick Khrapov, 2022. All right reserved.
7 
8 **/
9 #pragma once
10 
13 #include <qx/macros/common.h>
14 #include <qx/render/color.h>
15 #include <qx/verbosity.h>
16 
17 #include <optional>
18 
19 /**
20  @brief Define a category
21  @param name - category name
22  @param ... - optional category color
23 **/
24 #define QX_DEFINE_CATEGORY(name, ...) constexpr qx::category name = qx::category(QXT(#name), ##__VA_ARGS__)
25 
26 /**
27  @brief Set the file category
28  You can access this value via QX_GET_FILE_CATEGORY()
29  This category will not be exported via #include
30  @param _category - category to use in this file
31 **/
32 #define QX_SET_FILE_CATEGORY(_category) \
33  template<> \
34  struct qx::details::file_category<qx::djb2a_hash(QX_SHORT_FILE, 0)> \
35  { \
36  static constexpr const qx::category& get() noexcept \
37  { \
38  return _category; \
39  } \
40  }
41 
42 /**
43  @brief Get category defined in QX_SET_FILE_CATEGORY
44  If there is none, CatDefault will be used
45 **/
46 #define QX_GET_FILE_CATEGORY() qx::details::file_category<qx::djb2a_hash(QX_SHORT_FILE)>::get()
47 
48 namespace qx
49 {
50 
51 /**
52 
53  @class category
54  @brief A category is a class that identifies a particular piece of code.
55  This code can be located in different files, but united by one functionality.
56  Objects of this class can be used in logging, asserts and profiling.
57  @author Khrapov
58  @date 5.12.2022
59 
60 **/
61 class category
62 {
63 public:
64  constexpr category() = default;
65  constexpr category(const category&) = default;
66  constexpr category(category&&) = default;
67  constexpr category& operator=(const category&) = default;
68  constexpr category& operator=(category&&) = default;
69 
70  constexpr bool operator==(const category&) const = default;
71 
72  /**
73  @brief category object constructor
74  @param svName - category name. For ex. CatRendering or CatWidgets
75  @param optCategoryColor - color to be used if supported.
76  If it isn't provided, a color generated from a name hash is used.
77  **/
78  constexpr explicit category(string_view svName, std::optional<color> optCategoryColor = std::nullopt) noexcept;
79 
80  /**
81  @brief Create new category from this one with custom verbosity
82  @param eVerbosity - category verbosity.
83  User code will use this category with top priority and perform compile time checks if possible
84  @retval - new category
85  **/
86  constexpr category set_verbosity(verbosity eVerbosity) const noexcept;
87 
88  /**
89  @brief Get category name
90  @retval - category name
91  **/
92  constexpr string_view get_name() const noexcept;
93 
94  /**
95  @brief Get category color
96  @retval - category color
97  **/
98  constexpr const color& get_color() const noexcept;
99 
100  /**
101  @brief Get category verbosity
102  @retval - category verbosity
103  User code will use this category with top priority and perform compile time checks if possible
104  **/
105  constexpr verbosity get_verbosity() const noexcept;
106 
107 private:
108  color m_Color;
109  string_view m_svName;
110  verbosity m_Verbosity = QX_CONF_COMPILE_TIME_VERBOSITY;
111 };
112 
113 } // namespace qx
114 
115 #include <qx/category.inl>
116 
117 QX_DEFINE_CATEGORY(CatDefault, qx::color::white());
118 
119 namespace qx::details
120 {
121 
122 template<size_t nHash>
124 {
125  static constexpr const category& get() noexcept
126  {
127  return CatDefault;
128  }
129 };
130 
131 } // namespace qx::details
#define QX_DEFINE_CATEGORY(name,...)
Define a category.
Definition: category.h:24
A category is a class that identifies a particular piece of code. This code can be located in differe...
Definition: category.h:62
constexpr string_view get_name() const noexcept
Get category name.
Definition: category.inl:26
constexpr category set_verbosity(verbosity eVerbosity) const noexcept
Create new category from this one with custom verbosity.
Definition: category.inl:19
constexpr const color & get_color() const noexcept
Get category color.
Definition: category.inl:31
constexpr verbosity get_verbosity() const noexcept
Get category verbosity.
Definition: category.inl:36
RGBA color.
Definition: color.h:195