rippled
Loading...
Searching...
No Matches
tagged_integer.h
1// Copyright (c) 2014, Nikolaos D. Bougalis <nikb@bougalis.net>
2
3#ifndef BEAST_UTILITY_TAGGED_INTEGER_H_INCLUDED
4#define BEAST_UTILITY_TAGGED_INTEGER_H_INCLUDED
5
6#include <xrpl/beast/hash/hash_append.h>
7
8#include <boost/operators.hpp>
9
10#include <iostream>
11#include <type_traits>
12
13namespace ripple {
14
26template <class Int, class Tag>
28 : boost::totally_ordered<
29 tagged_integer<Int, Tag>,
30 boost::integer_arithmetic<
31 tagged_integer<Int, Tag>,
32 boost::bitwise<
33 tagged_integer<Int, Tag>,
34 boost::unit_steppable<
35 tagged_integer<Int, Tag>,
36 boost::shiftable<tagged_integer<Int, Tag>>>>>>
37{
38private:
40
41public:
42 using value_type = Int;
43 using tag_type = Tag;
44
45 tagged_integer() = default;
46
47 template <
48 class OtherInt,
49 class = typename std::enable_if<
51 sizeof(OtherInt) <= sizeof(Int)>::type>
52 explicit constexpr tagged_integer(OtherInt value) noexcept : m_value(value)
53 {
54 static_assert(
55 sizeof(tagged_integer) == sizeof(Int),
56 "tagged_integer is adding padding");
57 }
58
59 bool
60 operator<(tagged_integer const& rhs) const noexcept
61 {
62 return m_value < rhs.m_value;
63 }
64
65 bool
66 operator==(tagged_integer const& rhs) const noexcept
67 {
68 return m_value == rhs.m_value;
69 }
70
72 operator+=(tagged_integer const& rhs) noexcept
73 {
74 m_value += rhs.m_value;
75 return *this;
76 }
77
79 operator-=(tagged_integer const& rhs) noexcept
80 {
81 m_value -= rhs.m_value;
82 return *this;
83 }
84
86 operator*=(tagged_integer const& rhs) noexcept
87 {
88 m_value *= rhs.m_value;
89 return *this;
90 }
91
93 operator/=(tagged_integer const& rhs) noexcept
94 {
95 m_value /= rhs.m_value;
96 return *this;
97 }
98
100 operator%=(tagged_integer const& rhs) noexcept
101 {
102 m_value %= rhs.m_value;
103 return *this;
104 }
105
107 operator|=(tagged_integer const& rhs) noexcept
108 {
109 m_value |= rhs.m_value;
110 return *this;
111 }
112
114 operator&=(tagged_integer const& rhs) noexcept
115 {
116 m_value &= rhs.m_value;
117 return *this;
118 }
119
121 operator^=(tagged_integer const& rhs) noexcept
122 {
123 m_value ^= rhs.m_value;
124 return *this;
125 }
126
128 operator<<=(tagged_integer const& rhs) noexcept
129 {
130 m_value <<= rhs.m_value;
131 return *this;
132 }
133
135 operator>>=(tagged_integer const& rhs) noexcept
136 {
137 m_value >>= rhs.m_value;
138 return *this;
139 }
140
142 operator~() const noexcept
143 {
144 return tagged_integer{~m_value};
145 }
146
148 operator+() const noexcept
149 {
150 return *this;
151 }
152
154 operator-() const noexcept
155 {
156 return tagged_integer{-m_value};
157 }
158
160 operator++() noexcept
161 {
162 ++m_value;
163 return *this;
164 }
165
167 operator--() noexcept
168 {
169 --m_value;
170 return *this;
171 }
172
173 explicit
174 operator Int() const noexcept
175 {
176 return m_value;
177 }
178
179 friend std::ostream&
181 {
182 s << t.m_value;
183 return s;
184 }
185
186 friend std::istream&
188 {
189 s >> t.m_value;
190 return s;
191 }
192
193 friend std::string
194 to_string(tagged_integer const& t)
195 {
196 return std::to_string(t.m_value);
197 }
198};
199
200} // namespace ripple
201
202namespace beast {
203template <class Int, class Tag, class HashAlgorithm>
204struct is_contiguously_hashable<ripple::tagged_integer<Int, Tag>, HashAlgorithm>
205 : public is_contiguously_hashable<Int, HashAlgorithm>
206{
207 explicit is_contiguously_hashable() = default;
208};
209
210} // namespace beast
211#endif
A type-safe wrap around standard integral types.
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:6
ApplyFlags operator|=(ApplyFlags &lhs, ApplyFlags const &rhs)
Definition ApplyView.h:71
ApplyFlags operator&=(ApplyFlags &lhs, ApplyFlags const &rhs)
Definition ApplyView.h:78
constexpr ApplyFlags operator~(ApplyFlags const &flags)
Definition ApplyView.h:60
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
Definition base_uint.h:628
bool operator<(Slice const &lhs, Slice const &rhs) noexcept
Definition Slice.h:204
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
Number operator-(Number const &x, Number const &y)
Definition Number.h:281
constexpr base_uint< Bits, Tag > operator+(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition base_uint.h:603
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition base_uint.h:566
Metafunction returning true if the type can be hashed in one call.
T to_string(T... args)