Iros
 
Loading...
Searching...
No Matches
strong_int.h
Go to the documentation of this file.
1#pragma once
2
3#include "di/format/prelude.h"
4#include "di/meta/algorithm.h"
5#include "di/meta/core.h"
6#include "di/meta/language.h"
7
8namespace di::util {
9namespace detail {
10 struct NoopMixin {};
11
12 template<typename T>
13 struct MixinHelper : meta::TypeConstant<NoopMixin> {};
14
15 template<typename T>
16 requires(requires { typename T::Mixin; })
17 struct MixinHelper<T> : meta::TypeConstant<typename T::Mixin> {};
18
19 template<typename T>
21
22 template<typename T>
23 concept Tag = requires { typename T::Type; };
24
25 template<typename T>
26 struct SSizeTypeHelper : meta::TypeConstant<meta::MakeSigned<meta::Type<T>>> {};
27
28 template<typename T>
29 requires(requires { typename T::SSizeType; })
30 struct SSizeTypeHelper<T> : meta::TypeConstant<typename T::SSizeType> {};
31
32 template<Tag T>
34
35 template<typename T>
36 constexpr inline bool format_as_pointer = false;
37
38 template<typename T>
39 requires(T::format_as_pointer)
40 constexpr inline bool format_as_pointer<T> = true;
41}
42
43template<detail::Tag Tag>
44class StrongInt : public detail::Mixin<Tag> {
45public:
46 using Type = Tag::Type;
48
49 StrongInt() = default;
50
51 constexpr explicit StrongInt(Type value) : m_value(value) {}
52
53 constexpr auto raw_value() const -> Type { return m_value; }
54
55 constexpr auto operator+=(Type x) -> StrongInt& {
56 m_value += x;
57 return *this;
58 }
60 m_value += x;
61 return *this;
62 }
63
64 constexpr auto operator-=(Type x) -> StrongInt& {
65 m_value -= x;
66 return *this;
67 }
69 m_value -= x;
70 return *this;
71 }
72
73 constexpr auto operator++() -> StrongInt& {
74 ++m_value;
75 return *this;
76 }
77 constexpr auto operator++(int) -> StrongInt {
78 auto copy = *this;
79 ++m_value;
80 return copy;
81 }
82
83 constexpr auto operator--() -> StrongInt& {
84 --m_value;
85 return *this;
86 }
87 constexpr auto operator--(int) -> StrongInt {
88 auto copy = *this;
89 --m_value;
90 return copy;
91 }
92
93private:
94 constexpr friend auto operator+(StrongInt a, Type b) -> StrongInt { return StrongInt(a.raw_value() + b); }
95 constexpr friend auto operator+(StrongInt a, SSizeType b) -> StrongInt
97 {
98 return StrongInt(a.raw_value() + b);
99 }
100 constexpr friend auto operator+(Type a, StrongInt b) -> StrongInt { return StrongInt(a + b.raw_value()); }
101 constexpr friend auto operator+(SSizeType a, StrongInt b) -> StrongInt
103 {
104 return StrongInt(a + b.raw_value());
105 }
106
107 constexpr friend auto operator-(StrongInt a, Type b) -> StrongInt { return StrongInt(a.raw_value() - b); }
108 constexpr friend auto operator-(StrongInt a, SSizeType b) -> StrongInt
110 {
111 return StrongInt(a.raw_value() - b);
112 }
113 constexpr friend auto operator-(Type a, StrongInt b) -> StrongInt { return StrongInt(a - b.raw_value()); }
114 constexpr friend auto operator-(SSizeType a, StrongInt b) -> StrongInt
116 {
117 return StrongInt(a - b.raw_value());
118 }
119
120 constexpr friend auto operator-(StrongInt a, StrongInt b) -> SSizeType { return a.raw_value() - b.raw_value(); }
121
122 constexpr friend auto operator==(StrongInt a, StrongInt b) -> bool { return a.raw_value() == b.raw_value(); }
123 constexpr friend auto operator<=>(StrongInt a, StrongInt b) -> di::strong_ordering {
124 return a.raw_value() <=> b.raw_value();
125 }
126
127 template<concepts::Encoding Enc>
129 FormatParseContext<Enc>& parse_context, bool debug) {
131 return format::formatter<Base, Enc>(parse_context, debug) % [](concepts::CopyConstructible auto formatter) {
132 return [=](concepts::FormatContext auto& context, StrongInt self) {
133 if constexpr (concepts::Pointer<Base>) {
134 auto* pointer = reinterpret_cast<void*>(self.raw_value());
135 return formatter(context, pointer);
136 } else {
137 return formatter(context, self.raw_value());
138 }
139 };
140 };
141 }
142
143 Type m_value { 0 };
144};
145}
146
147namespace di {
148using util::StrongInt;
149}
Definition format_parse_context.h:14
Definition strong_int.h:44
constexpr auto operator++() -> StrongInt &
Definition strong_int.h:73
constexpr auto operator--() -> StrongInt &
Definition strong_int.h:83
constexpr friend auto operator+(StrongInt a, SSizeType b) -> StrongInt requires(!concepts::SameAs< SSizeType, Type >)
Definition strong_int.h:95
constexpr friend auto operator==(StrongInt a, StrongInt b) -> bool
Definition strong_int.h:122
constexpr auto operator-=(Type x) -> StrongInt &
Definition strong_int.h:64
constexpr friend auto operator-(StrongInt a, Type b) -> StrongInt
Definition strong_int.h:107
Tag::Type Type
Definition strong_int.h:46
constexpr friend auto operator-(StrongInt a, SSizeType b) -> StrongInt requires(!concepts::SameAs< SSizeType, Type >)
Definition strong_int.h:108
constexpr auto operator+=(Type x) -> StrongInt &
Definition strong_int.h:55
constexpr friend auto tag_invoke(types::Tag< format::formatter_in_place >, InPlaceType< StrongInt >, FormatParseContext< Enc > &parse_context, bool debug)
Definition strong_int.h:128
detail::SSizeType< Tag > SSizeType
Definition strong_int.h:47
constexpr auto raw_value() const -> Type
Definition strong_int.h:53
constexpr auto operator-=(SSizeType x) -> StrongInt &requires(!concepts::SameAs< SSizeType, Type >)
Definition strong_int.h:68
constexpr auto operator+=(SSizeType x) -> StrongInt &requires(!concepts::SameAs< SSizeType, Type >)
Definition strong_int.h:59
constexpr auto operator--(int) -> StrongInt
Definition strong_int.h:87
constexpr StrongInt(Type value)
Definition strong_int.h:51
constexpr friend auto operator<=>(StrongInt a, StrongInt b) -> di::strong_ordering
Definition strong_int.h:123
constexpr friend auto operator+(SSizeType a, StrongInt b) -> StrongInt requires(!concepts::SameAs< SSizeType, Type >)
Definition strong_int.h:101
constexpr auto operator++(int) -> StrongInt
Definition strong_int.h:77
constexpr friend auto operator+(Type a, StrongInt b) -> StrongInt
Definition strong_int.h:100
constexpr friend auto operator-(Type a, StrongInt b) -> StrongInt
Definition strong_int.h:113
constexpr friend auto operator-(StrongInt a, StrongInt b) -> SSizeType
Definition strong_int.h:120
constexpr friend auto operator+(StrongInt a, Type b) -> StrongInt
Definition strong_int.h:94
constexpr friend auto operator-(SSizeType a, StrongInt b) -> StrongInt requires(!concepts::SameAs< SSizeType, Type >)
Definition strong_int.h:114
Definition operations.h:34
Definition format_context.h:9
Definition language.h:61
Definition core.h:114
Definition strong_int.h:23
constexpr auto formatter(FormatParseContext< Enc > &parse_context, bool debug=false)
Definition formatter.h:27
Definition merge_interfaces.h:6
detail::ConditionalHelper< value, T, U >::Type Conditional
Definition core.h:88
T::Type Type
Definition core.h:26
di::meta::Decay< decltype(T)> Tag
Definition tag_invoke.h:28
Definition clamp.h:9
constexpr bool format_as_pointer
Definition strong_int.h:36
meta::Type< MixinHelper< T > > Mixin
Definition strong_int.h:20
meta::Type< SSizeTypeHelper< T > > SSizeType
Definition strong_int.h:33
Definition vocab.h:96
Definition zstring_parser.h:9
constexpr auto copy
Definition copy.h:30
Definition core.h:18
Definition in_place_type.h:5
Definition strong_int.h:13
Definition strong_int.h:10
Definition strong_int.h:26