E-MailRelay

goptional.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 Graeme Walker <graeme_walker@users.sourceforge.net>
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 as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file goptional.h
19///
20
21#ifndef G_OPTIONAL_H
22#define G_OPTIONAL_H
23
24#include "gdef.h"
25
26#if GCONFIG_HAVE_CXX_OPTIONAL
27
28#include <optional>
29
30#else
31
32#include <utility>
33#include <stdexcept>
34
35 namespace G
36{
37 template <typename T> class optional ;
38}
39
40 namespace std /// NOLINT
41{
42 using G::optional ;
43}
44
45//| \class G::optional
46/// A class template like a simplified c++17 std::optional.
47///
48 template <typename T>
49 class G::optional
50{
51 public:
52 optional() noexcept(noexcept(T())) ;
53 ///< Default constructor for no value.
54
55 explicit optional( const T & ) ;
56 ///< Constructor for a defined value.
57
58 void reset() ;
59 ///< Clears the value.
60
61 bool has_value() const noexcept ;
62 ///< Returns true if a defined value.
63
64 explicit operator bool() const noexcept ;
65 ///< Returns true if a defined value.
66
67 const T & value() const ;
68 ///< Returns the value.
69
70 T value_or( const T & ) const ;
71 ///< Returns the value or a default.
72
73 optional<T> & operator=( const T & ) ;
74 ///< Assignment for a defined value.
75
76public:
77 ~optional() = default ;
78 optional( const optional & ) = default ;
79 optional( optional && ) noexcept = default ;
80 optional & operator=( const optional & ) = default ;
81 optional & operator=( optional && ) noexcept = default ;
82
83private:
84 void doThrow() const ;
85
86private:
87 T m_value {} ;
88 bool m_has_value {false} ;
89} ;
90
91 template <typename T>
92 G::optional<T>::optional() noexcept(noexcept(T()))
93= default ;
94
95template <typename T>
96 G::optional<T>::optional( const T & t ) :
97 m_value(t) ,
98 m_has_value(true)
99{
100}
101
102 template <typename T>
103 void G::optional<T>::reset()
104{
105 m_has_value = false ;
106}
107
108 template <typename T>
109 bool G::optional<T>::has_value() const noexcept
110{
111 return m_has_value ;
112}
113
114 template <typename T>
115 G::optional<T>::operator bool() const noexcept
116{
117 return m_has_value ;
118}
119
120 template <typename T>
121 const T & G::optional<T>::value() const
122{
123 if( !m_has_value ) doThrow() ;
124 return m_value ;
125}
126
127 template <typename T>
128 void G::optional<T>::doThrow() const
129{
130 throw std::runtime_error( "bad optional access" ) ;
131}
132
133 template <typename T>
134T G::optional<T>::value_or( const T & default_ ) const
135{
136 return m_has_value ? m_value : default_ ;
137}
138
139 template <typename T>
140 G::optional<T> & G::optional<T>::operator=( const T & t )
141{
142 m_value = t ;
143 m_has_value = true ;
144 return *this ;
145}
146
147#endif
148
149#endif
G::optional
A class template like a simplified c++17 std::optional.
Definition: goptional.h:50
G::optional::optional
optional() noexcept(noexcept(T()))
Default constructor for no value.
G::optional::operator=
optional< T > & operator=(const T &)
Assignment for a defined value.
Definition: goptional.h:140
G::optional::value
const T & value() const
Returns the value.
Definition: goptional.h:121
G::optional::has_value
bool has_value() const noexcept
Returns true if a defined value.
Definition: goptional.h:109
G::optional::reset
void reset()
Clears the value.
Definition: goptional.h:103
G::optional::value_or
T value_or(const T &) const
Returns the value or a default.
Definition: goptional.h:134
G
Low-level classes.
Definition: garg.h:36
std
STL namespace.