di 0.1.0
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>
20 using Mixin = meta::Type<MixinHelper<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>
33 using SSizeType = meta::Type<SSizeTypeHelper<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 IsAtom = void;
47
48 using Type = Tag::Type;
49 using SSizeType = detail::SSizeType<Tag>;
50
51 StrongInt() = default;
52
53 constexpr explicit StrongInt(Type value) : m_value(value) {}
54
55 constexpr auto raw_value() const -> Type { return m_value; }
56
57 constexpr auto operator+=(Type x) -> StrongInt& {
58 m_value += x;
59 return *this;
60 }
61 constexpr auto operator+=(SSizeType x) -> StrongInt&
63 {
64 m_value += x;
65 return *this;
66 }
67
68 constexpr auto operator-=(Type x) -> StrongInt& {
69 m_value -= x;
70 return *this;
71 }
72 constexpr auto operator-=(SSizeType x) -> StrongInt&
74 {
75 m_value -= x;
76 return *this;
77 }
78
79 constexpr auto operator++() -> StrongInt& {
80 ++m_value;
81 return *this;
82 }
83 constexpr auto operator++(int) -> StrongInt {
84 auto copy = *this;
85 ++m_value;
86 return copy;
87 }
88
89 constexpr auto operator--() -> StrongInt& {
90 --m_value;
91 return *this;
92 }
93 constexpr auto operator--(int) -> StrongInt {
94 auto copy = *this;
95 --m_value;
96 return copy;
97 }
98
99private:
100 constexpr friend auto operator+(StrongInt a, Type b) -> StrongInt { return StrongInt(a.raw_value() + b); }
101 constexpr friend auto operator+(StrongInt a, SSizeType b) -> StrongInt
103 {
104 return StrongInt(a.raw_value() + b);
105 }
106 constexpr friend auto operator+(Type a, StrongInt b) -> StrongInt { return StrongInt(a + b.raw_value()); }
107 constexpr friend auto operator+(SSizeType a, StrongInt b) -> StrongInt
109 {
110 return StrongInt(a + b.raw_value());
111 }
112
113 constexpr friend auto operator-(StrongInt a, Type b) -> StrongInt { return StrongInt(a.raw_value() - b); }
114 constexpr friend auto operator-(StrongInt a, SSizeType b) -> StrongInt
116 {
117 return StrongInt(a.raw_value() - b);
118 }
119 constexpr friend auto operator-(Type a, StrongInt b) -> StrongInt { return StrongInt(a - b.raw_value()); }
120 constexpr friend auto operator-(SSizeType a, StrongInt b) -> StrongInt
122 {
123 return StrongInt(a - b.raw_value());
124 }
125
126 constexpr friend auto operator-(StrongInt a, StrongInt b) -> SSizeType { return a.raw_value() - b.raw_value(); }
127
128 constexpr friend auto operator==(StrongInt a, StrongInt b) -> bool { return a.raw_value() == b.raw_value(); }
129 constexpr friend auto operator<=>(StrongInt a, StrongInt b) -> di::strong_ordering {
130 return a.raw_value() <=> b.raw_value();
131 }
132
133 template<concepts::Encoding Enc>
135 FormatParseContext<Enc>& parse_context, bool debug) {
137 return format::formatter<Base, Enc>(parse_context, debug) % [](concepts::CopyConstructible auto formatter) {
138 return [=](concepts::FormatContext auto& context, StrongInt self) {
139 if constexpr (concepts::Pointer<Base>) {
140 auto* pointer = reinterpret_cast<void*>(self.raw_value());
141 return formatter(context, pointer);
142 } else {
143 return formatter(context, self.raw_value());
144 }
145 };
146 };
147 }
148
149 Type m_value { 0 };
150};
151}
152
153namespace di {
154using util::StrongInt;
155}
StrongInt()=default
Definition format_parse_context.h:14
Definition strong_int.h:44
constexpr auto operator++() -> StrongInt &
Definition strong_int.h:79
constexpr auto operator--() -> StrongInt &
Definition strong_int.h:89
constexpr friend auto operator+(StrongInt a, SSizeType b) -> StrongInt requires(!concepts::SameAs< SSizeType, Type >)
Definition strong_int.h:101
constexpr friend auto operator==(StrongInt a, StrongInt b) -> bool
Definition strong_int.h:128
constexpr auto operator-=(Type x) -> StrongInt &
Definition strong_int.h:68
constexpr friend auto operator-(StrongInt a, Type b) -> StrongInt
Definition strong_int.h:113
Tag::Type Type
Definition strong_int.h:48
constexpr friend auto operator-(StrongInt a, SSizeType b) -> StrongInt requires(!concepts::SameAs< SSizeType, Type >)
Definition strong_int.h:114
constexpr auto operator+=(Type x) -> StrongInt &
Definition strong_int.h:57
constexpr friend auto tag_invoke(types::Tag< format::formatter_in_place >, InPlaceType< StrongInt >, FormatParseContext< Enc > &parse_context, bool debug)
Definition strong_int.h:134
detail::SSizeType< Tag > SSizeType
Definition strong_int.h:49
constexpr auto raw_value() const -> Type
Definition strong_int.h:55
void IsAtom
Definition strong_int.h:46
constexpr auto operator-=(SSizeType x) -> StrongInt &requires(!concepts::SameAs< SSizeType, Type >)
Definition strong_int.h:72
constexpr auto operator+=(SSizeType x) -> StrongInt &requires(!concepts::SameAs< SSizeType, Type >)
Definition strong_int.h:61
constexpr auto operator--(int) -> StrongInt
Definition strong_int.h:93
constexpr StrongInt(Type value)
Definition strong_int.h:53
constexpr friend auto operator<=>(StrongInt a, StrongInt b) -> di::strong_ordering
Definition strong_int.h:129
constexpr friend auto operator+(SSizeType a, StrongInt b) -> StrongInt requires(!concepts::SameAs< SSizeType, Type >)
Definition strong_int.h:107
constexpr auto operator++(int) -> StrongInt
Definition strong_int.h:83
constexpr friend auto operator+(Type a, StrongInt b) -> StrongInt
Definition strong_int.h:106
constexpr friend auto operator-(Type a, StrongInt b) -> StrongInt
Definition strong_int.h:119
constexpr friend auto operator-(StrongInt a, StrongInt b) -> SSizeType
Definition strong_int.h:126
constexpr friend auto operator+(StrongInt a, Type b) -> StrongInt
Definition strong_int.h:100
constexpr friend auto operator-(SSizeType a, StrongInt b) -> StrongInt requires(!concepts::SameAs< SSizeType, Type >)
Definition strong_int.h:120
Definition operations.h:34
Definition format_context.h:9
Definition language.h:61
Definition core.h:114
constexpr auto formatter(FormatParseContext< Enc > &parse_context, bool debug=false)
Definition formatter.h:27
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 vocab.h:96
Definition any_storable.h:9
constexpr auto copy
Definition copy.h:30
Definition in_place_type.h:5