rippled
Loading...
Searching...
No Matches
tagged_integer.h
1// Copyright (c) 2014, Nikolaos D. Bougalis <nikb@bougalis.net>
2
3#pragma once
4
5#include <xrpl/beast/hash/hash_append.h>
6
7#include <boost/operators.hpp>
8
9#include <iostream>
10#include <type_traits>
11
12namespace xrpl {
13
25template <class Int, class Tag>
26class tagged_integer : boost::totally_ordered<
27 tagged_integer<Int, Tag>,
28 boost::integer_arithmetic<
29 tagged_integer<Int, Tag>,
30 boost::bitwise<
31 tagged_integer<Int, Tag>,
32 boost::unit_steppable<
33 tagged_integer<Int, Tag>,
34 boost::shiftable<tagged_integer<Int, Tag>>>>>>
35{
36private:
38
39public:
40 using value_type = Int;
41 using tag_type = Tag;
42
43 tagged_integer() = default;
44
45 template <
46 class OtherInt,
47 class = typename std::enable_if<
48 std::is_integral<OtherInt>::value && sizeof(OtherInt) <= sizeof(Int)>::type>
49 explicit constexpr tagged_integer(OtherInt value) noexcept : m_value(value)
50 {
51 static_assert(sizeof(tagged_integer) == sizeof(Int), "tagged_integer is adding padding");
52 }
53
54 bool
55 operator<(tagged_integer const& rhs) const noexcept
56 {
57 return m_value < rhs.m_value;
58 }
59
60 bool
61 operator==(tagged_integer const& rhs) const noexcept
62 {
63 return m_value == rhs.m_value;
64 }
65
67 operator+=(tagged_integer const& rhs) noexcept
68 {
69 m_value += rhs.m_value;
70 return *this;
71 }
72
74 operator-=(tagged_integer const& rhs) noexcept
75 {
76 m_value -= rhs.m_value;
77 return *this;
78 }
79
81 operator*=(tagged_integer const& rhs) noexcept
82 {
83 m_value *= rhs.m_value;
84 return *this;
85 }
86
88 operator/=(tagged_integer const& rhs) noexcept
89 {
90 m_value /= rhs.m_value;
91 return *this;
92 }
93
95 operator%=(tagged_integer const& rhs) noexcept
96 {
97 m_value %= rhs.m_value;
98 return *this;
99 }
100
102 operator|=(tagged_integer const& rhs) noexcept
103 {
104 m_value |= rhs.m_value;
105 return *this;
106 }
107
109 operator&=(tagged_integer const& rhs) noexcept
110 {
111 m_value &= rhs.m_value;
112 return *this;
113 }
114
116 operator^=(tagged_integer const& rhs) noexcept
117 {
118 m_value ^= rhs.m_value;
119 return *this;
120 }
121
123 operator<<=(tagged_integer const& rhs) noexcept
124 {
125 m_value <<= rhs.m_value;
126 return *this;
127 }
128
130 operator>>=(tagged_integer const& rhs) noexcept
131 {
132 m_value >>= rhs.m_value;
133 return *this;
134 }
135
137 operator~() const noexcept
138 {
139 return tagged_integer{~m_value};
140 }
141
143 operator+() const noexcept
144 {
145 return *this;
146 }
147
149 operator-() const noexcept
150 {
151 return tagged_integer{-m_value};
152 }
153
155 operator++() noexcept
156 {
157 ++m_value;
158 return *this;
159 }
160
162 operator--() noexcept
163 {
164 --m_value;
165 return *this;
166 }
167
168 explicit
169 operator Int() const noexcept
170 {
171 return m_value;
172 }
173
174 friend std::ostream&
176 {
177 s << t.m_value;
178 return s;
179 }
180
181 friend std::istream&
183 {
184 s >> t.m_value;
185 return s;
186 }
187
188 friend std::string
189 to_string(tagged_integer const& t)
190 {
191 return std::to_string(t.m_value);
192 }
193};
194
195} // namespace xrpl
196
197namespace beast {
198template <class Int, class Tag, class HashAlgorithm>
199struct is_contiguously_hashable<xrpl::tagged_integer<Int, Tag>, HashAlgorithm>
200 : public is_contiguously_hashable<Int, HashAlgorithm>
201{
202 explicit is_contiguously_hashable() = default;
203};
204
205} // namespace beast
A type-safe wrap around standard integral types.
tagged_integer()=default
int Int
std::istream & operator>>(std::istream &is, Endpoint &endpoint)
Input stream conversion.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool operator<(Slice const &lhs, Slice const &rhs) noexcept
Definition Slice.h:199
constexpr ApplyFlags operator~(ApplyFlags const &flags)
Definition ApplyView.h:55
constexpr base_uint< Bits, Tag > operator+(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition base_uint.h:594
constexpr HashRouterFlags & operator|=(HashRouterFlags &lhs, HashRouterFlags rhs)
Definition HashRouter.h:41
Number operator-(Number const &x, Number const &y)
Definition Number.h:648
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:602
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
Definition base_uint.h:617
constexpr HashRouterFlags & operator&=(HashRouterFlags &lhs, HashRouterFlags rhs)
Definition HashRouter.h:56
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition base_uint.h:557
Metafunction returning true if the type can be hashed in one call.
T to_string(T... args)