MySQL 9.3.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
optional.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2025, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_SRC_REST_MRS_SRC_HELPER_OPTIONAL_H_
27#define ROUTER_SRC_REST_MRS_SRC_HELPER_OPTIONAL_H_
28
29#include <cassert>
30
31namespace helper {
32
33/**
34 * Lightweight implementation of optional.
35 *
36 * This class was especially created for POD types.
37 */
38template <typename ValueType>
39class Optional {
40 public:
41 Optional() = default;
42 Optional(const ValueType value) : is_set_{true}, v_{value} {}
43 Optional(const Optional &other) { *this = other; }
44
45 ValueType &operator*() {
46 assert(is_set_);
47 return v_;
48 }
49
50 ValueType operator*() const {
51 assert(is_set_);
52 return v_;
53 }
54
55 ValueType *operator->() {
56 assert(is_set_);
57 return &v_;
58 }
59
60 const ValueType *operator->() const {
61 assert(is_set_);
62 return &v_;
63 }
64
66 v_ = value.v_;
67 is_set_ = value.is_set_;
68
69 return *this;
70 }
71
72 Optional &operator=(const ValueType value) {
73 v_ = value;
74 is_set_ = true;
75
76 return *this;
77 }
78
79 void reset() { is_set_ = false; }
80
81 bool has_value() const { return is_set_; }
82 ValueType value() const {
83 assert(is_set_);
84 return v_;
85 }
86
87 operator bool() const { return is_set_; }
88
89 private:
90 bool is_set_{false};
91 ValueType v_{};
92};
93
94template <class T>
95bool operator==(const Optional<T> &lhs, const Optional<T> &rhs) {
96 if (lhs.has_value() != rhs.has_value()) return false;
97
98 return *lhs == *rhs;
99}
100
101template <class T>
102bool operator==(const T &lhs, const Optional<T> &rhs) {
103 if (!rhs.has_value()) return false;
104
105 return lhs == *rhs;
106}
107
108template <class T>
109bool operator==(const Optional<T> &lhs, const T rhs) {
110 if (!lhs.has_value()) return false;
111
112 return *lhs == rhs;
113}
114
115} // namespace helper
116
117#endif // ROUTER_SRC_REST_MRS_SRC_HELPER_OPTIONAL_H_
Lightweight implementation of optional.
Definition: optional.h:39
ValueType v_
Definition: optional.h:91
Optional(const Optional &other)
Definition: optional.h:43
bool is_set_
Definition: optional.h:90
Optional & operator=(const Optional &value)
Definition: optional.h:65
const ValueType * operator->() const
Definition: optional.h:60
Optional(const ValueType value)
Definition: optional.h:42
ValueType value() const
Definition: optional.h:82
Optional()=default
void reset()
Definition: optional.h:79
Optional & operator=(const ValueType value)
Definition: optional.h:72
ValueType & operator*()
Definition: optional.h:45
ValueType operator*() const
Definition: optional.h:50
ValueType * operator->()
Definition: optional.h:55
bool has_value() const
Definition: optional.h:81
#define T
Definition: jit_executor_value.cc:373
Definition: cache.h:33
bool operator==(const Optional< T > &lhs, const Optional< T > &rhs)
Definition: optional.h:95