MySQL  8.0.27
Source Code Documentation
val_int_compare.h
Go to the documentation of this file.
1 /* Copyright (c) 2016, 2021, Oracle and/or its affiliates.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License, version 2.0,
5  as published by the Free Software Foundation.
6 
7  This program is also distributed with certain software (including
8  but not limited to OpenSSL) that is licensed under separate terms,
9  as designated in a particular file or component or in included license
10  documentation. The authors of MySQL hereby grant you an additional
11  permission to link the program and your derivative works with the
12  separately licensed software that they have included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #ifndef VAL_INT_COMPARE_INCLUDED
24 #define VAL_INT_COMPARE_INCLUDED
25 
26 #include <assert.h>
27 #include <functional>
28 
29 #include "my_inttypes.h"
30 
31 /**
32  Holds value/unsigned_flag for the result of val_int(),
33  so that we can compare with operator<(), operator==() and operator<=()
34  */
36  public:
37  constexpr Integer_value(longlong val, bool unsigned_flag)
38  : m_val(val), m_unsigned_flag(unsigned_flag) {}
39 
40  constexpr longlong val() const { return m_val; }
41  constexpr bool is_unsigned() const { return m_unsigned_flag; }
42 
44  assert(!is_negative());
45  return static_cast<ulonglong>(m_val);
46  }
47 
48  constexpr bool is_negative() const { return !is_unsigned() && val() < 0; }
49 
50  private:
51  const longlong m_val;
52  const bool m_unsigned_flag;
53 };
54 
55 inline bool operator<(const Integer_value &lhs, const Integer_value &rhs) {
56  const bool lhs_is_neg = lhs.is_negative();
57  const bool rhs_is_neg = rhs.is_negative();
58  if (lhs_is_neg != rhs_is_neg)
59  // Different signs, lhs is smaller if it is negative.
60  return lhs_is_neg;
61  if (lhs_is_neg)
62  // Both are negative, compare as signed.
63  return lhs.val() < rhs.val();
64 
65  // Both are non-negative, compare as unsigned
66  return std::less<ulonglong>()(lhs.val(), rhs.val());
67 }
68 
69 inline bool operator==(const Integer_value &lhs, const Integer_value &rhs) {
70  const bool lhs_is_neg = lhs.is_negative();
71  const bool rhs_is_neg = rhs.is_negative();
72  if (lhs_is_neg != rhs_is_neg)
73  // Different signs, cannot be equal
74  return false;
75 
76  // Same sign, compare values.
77  return lhs.val() == rhs.val();
78 }
79 
80 inline bool operator<=(const Integer_value &lhs, const Integer_value &rhs) {
81  return lhs < rhs || lhs == rhs;
82 }
83 
84 #endif // VAL_INT_COMPARE_INCLUDED
Holds value/unsigned_flag for the result of val_int(), so that we can compare with operator<(),...
Definition: val_int_compare.h:35
const bool m_unsigned_flag
Definition: val_int_compare.h:52
constexpr longlong val() const
Definition: val_int_compare.h:40
constexpr bool is_negative() const
Definition: val_int_compare.h:48
ulonglong val_unsigned() const
Definition: val_int_compare.h:43
constexpr Integer_value(longlong val, bool unsigned_flag)
Definition: val_int_compare.h:37
constexpr bool is_unsigned() const
Definition: val_int_compare.h:41
const longlong m_val
Definition: val_int_compare.h:51
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:55
long long int longlong
Definition: my_inttypes.h:54
bool operator<=(const Integer_value &lhs, const Integer_value &rhs)
Definition: val_int_compare.h:80
bool operator==(const Integer_value &lhs, const Integer_value &rhs)
Definition: val_int_compare.h:69
bool operator<(const Integer_value &lhs, const Integer_value &rhs)
Definition: val_int_compare.h:55