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
54 signum(T const& t)
55 {
56  return t.signum();
57 }
58 
59 namespace detail {
60 namespace zero_helper {
61 
62 // For argument dependent lookup to function properly, calls to signum must
63 // be made from a namespace that does not include overloads of the function..
64 template <class T>
65 auto
66 call_signum(T const& t)
67 {
68  return signum(t);
69 }
70 
71 } // namespace zero_helper
72 } // namespace detail
73 
74 // Handle operators where T is on the left side using signum.
75 
76 template <typename T>
77 bool
78 operator==(T const& t, Zero)
79 {
80  return detail::zero_helper::call_signum(t) == 0;
81 }
82 
83 template <typename T>
84 bool
85 operator!=(T const& t, Zero)
86 {
87  return detail::zero_helper::call_signum(t) != 0;
88 }
89 
90 template <typename T>
91 bool
92 operator<(T const& t, Zero)
93 {
95 }
96 
97 template <typename T>
98 bool
99 operator>(T const& t, Zero)
100 {
101  return detail::zero_helper::call_signum(t) > 0;
102 }
103 
104 template <typename T>
105 bool
106 operator>=(T const& t, Zero)
107 {
108  return detail::zero_helper::call_signum(t) >= 0;
109 }
110 
111 template <typename T>
112 bool
113 operator<=(T const& t, Zero)
114 {
115  return detail::zero_helper::call_signum(t) <= 0;
116 }
117 
118 // Handle operators where T is on the right side by
119 // reversing the operation, so that T is on the left side.
120 
121 template <typename T>
122 bool
123 operator==(Zero, T const& t)
124 {
125  return t == zero;
126 }
127 
128 template <typename T>
129 bool
130 operator!=(Zero, T const& t)
131 {
132  return t != zero;
133 }
134 
135 template <typename T>
136 bool
137 operator<(Zero, T const& t)
138 {
139  return t > zero;
140 }
141 
142 template <typename T>
143 bool
144 operator>(Zero, T const& t)
145 {
146  return t < zero;
147 }
148 
149 template <typename T>
150 bool
151 operator>=(Zero, T const& t)
152 {
153  return t <= zero;
154 }
155 
156 template <typename T>
157 bool
158 operator<=(Zero, T const& t)
159 {
160  return t >= zero;
161 }
162 
163 } // namespace beast
164 
165 #endif
beast::signum
auto signum(T const &t)
Default implementation of signum calls the method on the class.
Definition: Zero.h:54
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:99
beast::operator>
bool operator>(SemanticVersion const &lhs, SemanticVersion const &rhs)
Definition: SemanticVersion.h:105
beast::operator>=
bool operator>=(SemanticVersion const &lhs, SemanticVersion const &rhs)
Definition: SemanticVersion.h:93
beast::Zero::Zero
Zero()=default
beast::operator!=
bool operator!=(LockFreeStackIterator< Container, LhsIsConst > const &lhs, LockFreeStackIterator< Container, RhsIsConst > const &rhs)
Definition: LockFreeStack.h:124
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:66
beast::operator<
bool operator<(SemanticVersion const &lhs, SemanticVersion const &rhs)
Definition: SemanticVersion.h:111
beast
Definition: base_uint.h:641