rippled
Zero.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of Beast: https://github.com/vinniefalco/Beast
4  Copyright 2014, Tom Ritchford <tom@swirly.com>
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_ZERO_H_INCLUDED
21 #define BEAST_UTILITY_ZERO_H_INCLUDED
22 
23 namespace beast {
24 
42 struct Zero
43 {
44  explicit Zero() = default;
45 };
46 
47 namespace {
48 static constexpr Zero zero{};
49 }
50 
52 template <typename T>
53 auto signum(T const& t)
54 {
55  return t.signum();
56 }
57 
58 namespace detail {
59 namespace zero_helper {
60 
61 // For argument dependent lookup to function properly, calls to signum must
62 // be made from a namespace that does not include overloads of the function..
63 template <class T>
64 auto call_signum(T const& t)
65 {
66  return signum(t);
67 }
68 
69 } // zero_helper
70 } // detail
71 
72 // Handle operators where T is on the left side using signum.
73 
74 template <typename T>
75 bool operator==(T const& t, Zero)
76 {
77  return detail::zero_helper::call_signum(t) == 0;
78 }
79 
80 template <typename T>
81 bool operator!=(T const& t, Zero)
82 {
83  return detail::zero_helper::call_signum(t) != 0;
84 }
85 
86 template <typename T>
87 bool operator<(T const& t, Zero)
88 {
90 }
91 
92 template <typename T>
93 bool operator>(T const& t, Zero)
94 {
96 }
97 
98 template <typename T>
99 bool operator>=(T const& t, Zero)
100 {
101  return detail::zero_helper::call_signum(t) >= 0;
102 }
103 
104 template <typename T>
105 bool operator<=(T const& t, Zero)
106 {
107  return detail::zero_helper::call_signum(t) <= 0;
108 }
109 
110 // Handle operators where T is on the right side by
111 // reversing the operation, so that T is on the left side.
112 
113 template <typename T>
114 bool operator==(Zero, T const& t)
115 {
116  return t == zero;
117 }
118 
119 template <typename T>
120 bool operator!=(Zero, T const& t)
121 {
122  return t != zero;
123 }
124 
125 template <typename T>
126 bool operator<(Zero, T const& t)
127 {
128  return t > zero;
129 }
130 
131 template <typename T>
132 bool operator>(Zero, T const& t)
133 {
134  return t < zero;
135 }
136 
137 template <typename T>
138 bool operator>=(Zero, T const& t)
139 {
140  return t <= zero;
141 }
142 
143 template <typename T>
144 bool operator<=(Zero, T const& t)
145 {
146  return t >= zero;
147 }
148 
149 } // beast
150 
151 #endif
beast::signum
auto signum(T const &t)
Default implementation of signum calls the method on the class.
Definition: Zero.h:53
beast::operator==
bool operator==(LockFreeStackIterator< Container, LhsIsConst > const &lhs, LockFreeStackIterator< Container, RhsIsConst > const &rhs)
Definition: LockFreeStack.h:115
beast::operator<=
bool operator<=(SemanticVersion const &lhs, SemanticVersion const &rhs)
Definition: SemanticVersion.h:94
beast::operator>
bool operator>(SemanticVersion const &lhs, SemanticVersion const &rhs)
Definition: SemanticVersion.h:100
beast::operator>=
bool operator>=(SemanticVersion const &lhs, SemanticVersion const &rhs)
Definition: SemanticVersion.h:88
beast::Zero::Zero
Zero()=default
beast::operator!=
bool operator!=(LockFreeStackIterator< Container, LhsIsConst > const &lhs, LockFreeStackIterator< Container, RhsIsConst > const &rhs)
Definition: LockFreeStack.h:122
beast::Zero
Zero allows classes to offer efficient comparisons to zero.
Definition: Zero.h:42
beast::detail::zero_helper::call_signum
auto call_signum(T const &t)
Definition: Zero.h:64
beast::operator<
bool operator<(SemanticVersion const &lhs, SemanticVersion const &rhs)
Definition: SemanticVersion.h:106
beast
Definition: base_uint.h:582