E-MailRelay
goptional.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2023 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#include <utility>
26#include <stdexcept>
27
28namespace G
29{
30 template <typename T> class optional ;
31}
32
33//| \class G::optional
34/// A class template like a simplified c++17 std::optional.
35///
36template <typename T>
38{
39public:
40 optional() noexcept(noexcept(T())) ;
41 ///< Default constructor for no value.
42
43 explicit optional( const T & ) ;
44 ///< Constructor for a defined value.
45
46 optional( bool has_value , const T & value ) ;
47 ///< Constructor. Not in std::optional.
48
49 void clear() ;
50 ///< Clears the value. Not in std::optional.
51
52 bool has_value() const noexcept ;
53 ///< Returns true if a defined value.
54
55 explicit operator bool() const noexcept ;
56 ///< Returns true if a defined value.
57
58 const T & value() const ;
59 ///< Returns the value.
60
61 T value_or( const T & ) const ;
62 ///< Returns the value or a default.
63
64 optional<T> & operator=( const T & ) ;
65 ///< Assignment for a defined value.
66
67public:
68 ~optional() = default ;
69 optional( const optional & ) = default ;
70 optional( optional && ) noexcept = default ;
71 optional & operator=( const optional & ) = default ;
72 optional & operator=( optional && ) noexcept = default ;
73
74private:
75 void doThrow() const ;
76
77private:
78 T m_value {} ;
79 bool m_has_value {false} ;
80} ;
81
82template <typename T>
83G::optional<T>::optional() noexcept(noexcept(T()))
84= default ;
85
86template <typename T>
87G::optional<T>::optional( const T & t ) :
88 m_value(t) ,
89 m_has_value(true)
90{
91}
92
93template <typename T>
94G::optional<T>::optional( bool has_value , const T & value ) :
95 m_value(value) ,
96 m_has_value(has_value)
97{
98}
99
100template <typename T>
102{
103 m_has_value = false ;
104}
105
106template <typename T>
107bool G::optional<T>::has_value() const noexcept
108{
109 return m_has_value ;
110}
111
112template <typename T>
113G::optional<T>::operator bool() const noexcept
114{
115 return m_has_value ;
116}
117
118template <typename T>
119const T & G::optional<T>::value() const
120{
121 if( !m_has_value ) doThrow() ;
122 return m_value ;
123}
124
125template <typename T>
126void G::optional<T>::doThrow() const
127{
128 throw std::runtime_error( "bad optional access" ) ;
129}
130
131template <typename T>
132T G::optional<T>::value_or( const T & default_ ) const
133{
134 return m_has_value ? m_value : default_ ;
135}
136
137template <typename T>
139{
140 m_value = t ;
141 m_has_value = true ;
142 return *this ;
143}
144
145#endif
A class template like a simplified c++17 std::optional.
Definition: goptional.h:38
void clear()
Clears the value. Not in std::optional.
Definition: goptional.h:101
optional() noexcept(noexcept(T()))
Default constructor for no value.
optional< T > & operator=(const T &)
Assignment for a defined value.
Definition: goptional.h:138
const T & value() const
Returns the value.
Definition: goptional.h:119
bool has_value() const noexcept
Returns true if a defined value.
Definition: goptional.h:107
T value_or(const T &) const
Returns the value or a default.
Definition: goptional.h:132
Low-level classes.
Definition: garg.h:30