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