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>
27 : boost::totally_ordered<
28 tagged_integer<Int, Tag>,
29 boost::integer_arithmetic<
30 tagged_integer<Int, Tag>,
31 boost::bitwise<
32 tagged_integer<Int, Tag>,
33 boost::unit_steppable<tagged_integer<Int, Tag>, boost::shiftable<tagged_integer<Int, Tag>>>>>>
34{
35private:
37
38public:
39 using value_type = Int;
40 using tag_type = Tag;
41
42 tagged_integer() = default;
43
44 template <
45 class OtherInt,
46 class = typename std::enable_if<std::is_integral<OtherInt>::value && sizeof(OtherInt) <= sizeof(Int)>::type>
47 explicit constexpr tagged_integer(OtherInt value) noexcept : m_value(value)
48 {
49 static_assert(sizeof(tagged_integer) == sizeof(Int), "tagged_integer is adding padding");
50 }
51
52 bool
53 operator<(tagged_integer const& rhs) const noexcept
54 {
55 return m_value < rhs.m_value;
56 }
57
58 bool
59 operator==(tagged_integer const& rhs) const noexcept
60 {
61 return m_value == rhs.m_value;
62 }
63
65 operator+=(tagged_integer const& rhs) noexcept
66 {
67 m_value += rhs.m_value;
68 return *this;
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~() const noexcept
136 {
137 return tagged_integer{~m_value};
138 }
139
141 operator+() const noexcept
142 {
143 return *this;
144 }
145
147 operator-() const noexcept
148 {
149 return tagged_integer{-m_value};
150 }
151
153 operator++() noexcept
154 {
155 ++m_value;
156 return *this;
157 }
158
160 operator--() noexcept
161 {
162 --m_value;
163 return *this;
164 }
165
166 explicit
167 operator Int() const noexcept
168 {
169 return m_value;
170 }
171
172 friend std::ostream&
174 {
175 s << t.m_value;
176 return s;
177 }
178
179 friend std::istream&
181 {
182 s >> t.m_value;
183 return s;
184 }
185
186 friend std::string
187 to_string(tagged_integer const& t)
188 {
189 return std::to_string(t.m_value);
190 }
191};
192
193} // namespace xrpl
194
195namespace beast {
196template <class Int, class Tag, class HashAlgorithm>
197struct is_contiguously_hashable<xrpl::tagged_integer<Int, Tag>, HashAlgorithm>
198 : public is_contiguously_hashable<Int, HashAlgorithm>
199{
200 explicit is_contiguously_hashable() = default;
201};
202
203} // 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:198
constexpr ApplyFlags operator~(ApplyFlags const &flags)
Definition ApplyView.h:53
constexpr base_uint< Bits, Tag > operator+(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition base_uint.h:589
constexpr HashRouterFlags & operator|=(HashRouterFlags &lhs, HashRouterFlags rhs)
Definition HashRouter.h:41
Number operator-(Number const &x, Number const &y)
Definition Number.h:638
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:597
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
Definition base_uint.h:612
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:552
Metafunction returning true if the type can be hashed in one call.
T to_string(T... args)