MySQL  8.0.19
Source Code Documentation
nullable.h
Go to the documentation of this file.
1 #ifndef NULLABLE_INCLUDED
2 #define NULLABLE_INCLUDED
3 
4 /*
5  Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License, version 2.0,
9  as published by the Free Software Foundation.
10 
11  This program is also distributed with certain software (including
12  but not limited to OpenSSL) that is licensed under separate terms,
13  as designated in a particular file or component or in included license
14  documentation. The authors of MySQL hereby grant you an additional
15  permission to link the program and your derivative works with the
16  separately licensed software that they have included with MySQL.
17 
18  This program is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  GNU General Public License, version 2.0, for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with this program; if not, write to the Free Software
25  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 */
27 
28 /**
29  @file include/nullable.h
30 */
31 
32 #include "my_dbug.h"
33 
34 namespace Mysql {
35 
36 /**
37  Class for storing value or NULL value.
38 */
39 template <typename T_value>
40 class Nullable {
41  public:
43 
45 
47  : m_has_value(other.m_has_value), m_value(other.m_value) {}
48 
49  /**
50  Assigns value from another Nullable<> object.
51  */
53  this->m_has_value = other.m_has_value;
54  if (this->m_has_value) {
55  this->m_value = other.m_value;
56  }
57  return *this;
58  }
59 
60  /**
61  Compares two Nullable<> objects for equality.
62  */
63  bool operator==(const Nullable<T_value> &other) const {
64  if (this->has_value() != other.has_value())
65  return false;
66  else if (this->has_value() && this->value() != other.value())
67  return false;
68  return true;
69  }
70 
71  /**
72  Compares two Nullable<> objects for in-equality.
73  */
74  bool operator!=(const Nullable<T_value> &other) const {
75  return !(*this == other);
76  }
77 
78  /**
79  Returns true if object has not-NULL value assigned. If this is false, one
80  should not try to get value by value().
81  */
82  bool has_value() const { return this->m_has_value; }
83 
84  /**
85  Returns actual value of object. Do not call this method if has_value()
86  returns false.
87  */
88  const T_value &value() const {
89  DBUG_ASSERT(this->m_has_value);
90  return this->m_value;
91  }
92 
93  private:
94  /**
95  Specifies if this object represents NULL value. If this is false, one
96  should not try to get value by value().
97  */
99  /**
100  Actual value, if m_has_value is true. Undefined if m_has_value is false.
101  */
102  T_value m_value{};
103 };
104 
105 } // namespace Mysql
106 
107 #endif // NULLABLE_INCLUDED
Mysql::Nullable::Nullable
Nullable(const Nullable< T_value > &other)
Definition: nullable.h:46
Mysql::Nullable::Nullable
Nullable(T_value value)
Definition: nullable.h:44
Mysql::Nullable::m_value
T_value m_value
Actual value, if m_has_value is true.
Definition: nullable.h:102
my_dbug.h
Mysql::Nullable::operator=
Nullable< T_value > & operator=(const Nullable< T_value > &other)
Assigns value from another Nullable<> object.
Definition: nullable.h:52
true
#define true
Definition: config_static.h:44
Mysql::Nullable::operator==
bool operator==(const Nullable< T_value > &other) const
Compares two Nullable<> objects for equality.
Definition: nullable.h:63
Mysql::Nullable
Class for storing value or NULL value.
Definition: nullable.h:40
Mysql::Nullable::value
const T_value & value() const
Returns actual value of object.
Definition: nullable.h:88
Mysql::Nullable::operator!=
bool operator!=(const Nullable< T_value > &other) const
Compares two Nullable<> objects for in-equality.
Definition: nullable.h:74
Mysql::Nullable::m_has_value
bool m_has_value
Specifies if this object represents NULL value.
Definition: nullable.h:98
Mysql::Nullable::has_value
bool has_value() const
Returns true if object has not-NULL value assigned.
Definition: nullable.h:82
Mysql::Nullable::Nullable
Nullable()
Definition: nullable.h:42
DBUG_ASSERT
#define DBUG_ASSERT(A)
Definition: my_dbug.h:197
Mysql
Definition: abstract_connection_program.h:37
false
#define false
Definition: config_static.h:43