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