sandbox
Loading...
Searching...
No Matches
primitive.hpp
1#ifndef LIBSBX_UTILITY_PRIMITIVE_HPP_
2#define LIBSBX_UTILITY_PRIMITIVE_HPP_
3
4#include <concepts>
5#include <type_traits>
6
7#include <fmt/format.h>
8
9#include <libsbx/utility/string_literal.hpp>
10
11namespace sbx::utility {
12
13template<typename Type, string_literal Unit>
14requires (std::is_arithmetic_v<Type>)
15class primitive {
16
17public:
18
19 using value_type = Type;
20
21 inline static constexpr auto unit = Unit;
22
23 constexpr explicit primitive(Type value = static_cast<value_type>(0)) noexcept
24 : _value{value} { }
25
26 constexpr ~primitive() = default;
27
28 constexpr operator Type() const noexcept {
29 return _value;
30 }
31
32private:
33
34 Type _value;
35
36}; // class primitive
37
38} // namespace sbx::utility
39
40template<typename Type, sbx::utility::string_literal Unit>
41struct fmt::formatter<sbx::utility::primitive<Type, Unit>> {
42
43 template<typename ParseContext>
44 constexpr auto parse(ParseContext& context) -> decltype(context.begin()) {
45 return context.begin();
46 }
47
48 template<typename FormatContext>
49 auto format(const sbx::utility::primitive<Type, Unit>& value, FormatContext& context) -> decltype(context.out()) {
50 return fmt::format_to(context.out(), "{}{}", static_cast<Type>(value), Unit);
51 }
52
53}; // struct fmt::formatter<sbx::utility::primitive<Type>>
54
55#endif // LIBSBX_UTILITY_PRIMITIVE_HPP_
Definition: primitive.hpp:15