rippled
tagged_integer.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright 2014, Nikolaos D. Bougalis <nikb@bougalis.net>
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef BEAST_UTILITY_TAGGED_INTEGER_H_INCLUDED
21 #define BEAST_UTILITY_TAGGED_INTEGER_H_INCLUDED
22 
23 #include <ripple/beast/hash/hash_append.h>
24 #include <boost/operators.hpp>
25 #include <functional>
26 #include <iostream>
27 #include <type_traits>
28 #include <utility>
29 
30 namespace ripple {
31 
43 template <class Int, class Tag>
45  : boost::totally_ordered<
46  tagged_integer<Int, Tag>,
47  boost::integer_arithmetic<
48  tagged_integer<Int, Tag>,
49  boost::bitwise<
50  tagged_integer<Int, Tag>,
51  boost::unit_steppable<
52  tagged_integer<Int, Tag>,
53  boost::shiftable<tagged_integer<Int, Tag>>>>>>
54 {
55 private:
56  Int m_value;
57 
58 public:
59  using value_type = Int;
60  using tag_type = Tag;
61 
62  tagged_integer() = default;
63 
64  template <
65  class OtherInt,
66  class = typename std::enable_if<
68  sizeof(OtherInt) <= sizeof(Int)>::type>
69  explicit constexpr tagged_integer(OtherInt value) noexcept : m_value(value)
70  {
71  static_assert(
72  sizeof(tagged_integer) == sizeof(Int),
73  "tagged_integer is adding padding");
74  }
75 
76  bool
77  operator<(const tagged_integer& rhs) const noexcept
78  {
79  return m_value < rhs.m_value;
80  }
81 
82  bool
83  operator==(const tagged_integer& rhs) const noexcept
84  {
85  return m_value == rhs.m_value;
86  }
87 
89  operator+=(tagged_integer const& rhs) noexcept
90  {
91  m_value += rhs.m_value;
92  return *this;
93  }
94 
96  operator-=(tagged_integer const& rhs) noexcept
97  {
98  m_value -= rhs.m_value;
99  return *this;
100  }
101 
103  operator*=(tagged_integer const& rhs) noexcept
104  {
105  m_value *= rhs.m_value;
106  return *this;
107  }
108 
110  operator/=(tagged_integer const& rhs) noexcept
111  {
112  m_value /= rhs.m_value;
113  return *this;
114  }
115 
117  operator%=(tagged_integer const& rhs) noexcept
118  {
119  m_value %= rhs.m_value;
120  return *this;
121  }
122 
124  operator|=(tagged_integer const& rhs) noexcept
125  {
126  m_value |= rhs.m_value;
127  return *this;
128  }
129 
131  operator&=(tagged_integer const& rhs) noexcept
132  {
133  m_value &= rhs.m_value;
134  return *this;
135  }
136 
138  operator^=(tagged_integer const& rhs) noexcept
139  {
140  m_value ^= rhs.m_value;
141  return *this;
142  }
143 
145  operator<<=(const tagged_integer& rhs) noexcept
146  {
147  m_value <<= rhs.m_value;
148  return *this;
149  }
150 
152  operator>>=(const tagged_integer& rhs) noexcept
153  {
154  m_value >>= rhs.m_value;
155  return *this;
156  }
157 
159  operator~() const noexcept
160  {
161  return tagged_integer{~m_value};
162  }
163 
165  operator+() const noexcept
166  {
167  return *this;
168  }
169 
171  operator-() const noexcept
172  {
173  return tagged_integer{-m_value};
174  }
175 
177  operator++() noexcept
178  {
179  ++m_value;
180  return *this;
181  }
182 
184  operator--() noexcept
185  {
186  --m_value;
187  return *this;
188  }
189 
190  explicit operator Int() const noexcept
191  {
192  return m_value;
193  }
194 
195  friend std::ostream&
197  {
198  s << t.m_value;
199  return s;
200  }
201 
202  friend std::istream&
204  {
205  s >> t.m_value;
206  return s;
207  }
208 
209  friend std::string
210  to_string(tagged_integer const& t)
211  {
212  return std::to_string(t.m_value);
213  }
214 };
215 
216 } // namespace ripple
217 
218 namespace beast {
219 template <class Int, class Tag, class HashAlgorithm>
220 struct is_contiguously_hashable<ripple::tagged_integer<Int, Tag>, HashAlgorithm>
221  : public is_contiguously_hashable<Int, HashAlgorithm>
222 {
223  explicit is_contiguously_hashable() = default;
224 };
225 
226 } // namespace beast
227 #endif
ripple::tagged_integer::m_value
Int m_value
Definition: tagged_integer.h:56
ripple::operator&=
ApplyFlags operator&=(ApplyFlags &lhs, ApplyFlags const &rhs)
Definition: ApplyView.h:89
std::string
STL class.
utility
functional
beast::IP::operator>>
std::istream & operator>>(std::istream &is, Endpoint &endpoint)
Input stream conversion.
Definition: IPEndpoint.cpp:96
ripple::operator-
Number operator-(Number const &x, Number const &y)
Definition: Number.h:277
ripple::tagged_integer< std::uint32_t, IdTag >::tag_type
IdTag tag_type
Definition: tagged_integer.h:60
beast::is_contiguously_hashable::is_contiguously_hashable
is_contiguously_hashable()=default
iostream
ripple::operator<<
std::ostream & operator<<(std::ostream &os, TOffer< TIn, TOut > const &offer)
Definition: Offer.h:242
ripple::operator==
bool operator==(Manifest const &lhs, Manifest const &rhs)
Definition: Manifest.h:165
ripple::operator|=
ApplyFlags operator|=(ApplyFlags &lhs, ApplyFlags const &rhs)
Definition: ApplyView.h:82
ripple::operator+
constexpr base_uint< Bits, Tag > operator+(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition: base_uint.h:613
std::enable_if
ripple::operator<
bool operator<(CanonicalTXSet::Key const &lhs, CanonicalTXSet::Key const &rhs)
Definition: CanonicalTXSet.cpp:25
std::ostream
STL class.
std::to_string
T to_string(T... args)
beast::is_contiguously_hashable
Metafunction returning true if the type can be hashed in one call.
Definition: hash_append.h:183
std::uint32_t
std::is_integral
Json::Int
int Int
Definition: json_forwards.h:26
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
ripple::tagged_integer
A type-safe wrap around standard integral types.
Definition: tagged_integer.h:44
std::istream
STL class.
type_traits
ripple::operator~
constexpr ApplyFlags operator~(ApplyFlags const &flags)
Definition: ApplyView.h:71
ripple::tagged_integer::tagged_integer
tagged_integer()=default
beast
Definition: base_uint.h:641