Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Concepts
optional_ref.hpp
Go to the documentation of this file.
1
6/*
7 * This file is part of Algorithms library toolkit.
8 * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz)
9
10 * Algorithms library toolkit is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14
15 * Algorithms library toolkit 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 for more details.
19
20 * You should have received a copy of the GNU General Public License
21 * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#pragma once
25
26#include <optional>
27#include <memory>
28#include <compare>
29
30namespace ext {
31
32template < class T >
34 T * m_value;
35
36public:
37 optional_ref ( ) : optional_ref ( std::nullopt ) { }
38 optional_ref ( std::nullopt_t ) : m_value ( nullptr ) { }
39
40 optional_ref ( T & val ) : m_value ( std::addressof ( val ) ) {
41 }
42
43 template < class U >
44 optional_ref ( const optional_ref < U > & other ) : optional_ref ( other.value ( ) ) {
45 }
46
47 template < class U >
48 optional_ref ( optional_ref < U > && other ) : optional_ref ( std::move ( other ).value ( ) ) {
49 }
50
51 optional_ref & operator = ( const optional_ref & ) = delete;
53
54 optional_ref ( const optional_ref & other ) = default;
55 optional_ref ( optional_ref && other ) noexcept = default;
56
57 ~ optional_ref ( ) noexcept = default;
58
59 const T * operator-> ( ) const {
60 return m_value;
61 }
62
63 T * operator-> ( ) {
64 return m_value;
65 }
66
67 const T & operator * ( ) const & {
68 return value ( );
69 }
70
71 T & operator * ( ) & {
72 return value ( );
73 }
74
75 const T && operator * ( ) const && {
76 return std::move ( value ( ) );
77 }
78
79 T && operator * ( ) && {
80 return std::move ( value ( ) );
81 }
82
83 explicit operator bool ( ) const noexcept {
84 return has_value ( );
85 }
86
87 bool has_value ( ) const noexcept {
88 return m_value != nullptr;
89 }
90
91 T & value ( ) & {
92 return * m_value;
93 }
94
95 const T & value ( ) const & {
96 return * m_value;
97 }
98
99 T && value ( ) && {
100 return std::move ( * m_value );
101 }
102
103 const T && value ( ) const && {
104 return std::move ( * m_value );
105 }
106
107 const T & value_or ( const T & default_value ) const & {
108 if ( has_value ( ) )
109 return value ( );
110 else
111 return default_value;
112 }
113
114 T & value_or ( T & default_value ) & {
115 if ( has_value ( ) )
116 return value ( );
117 else
118 return default_value;
119 }
120
121 T && value_or ( T && default_value ) && {
122 if ( has_value ( ) )
123 return std::move ( value ( ) );
124 else
125 return std::move ( default_value );
126 }
127
137 bool operator == ( const optional_ref < T >& rhs ) const {
138 const optional_ref < T > lhs = * this;
139 return lhs.has_value ( ) && rhs.has_value ( ) ? lhs.value ( ) == rhs.value ( ) : lhs.has_value ( ) == rhs.has_value ( );
140 }
141
151 auto operator <=> ( const optional_ref < T >& rhs ) const {
152 const optional_ref < T > lhs = * this;
153 return lhs.has_value ( ) && rhs.has_value ( ) ? lhs.value ( ) <=> rhs.value ( ) : lhs.has_value ( ) <=> rhs.has_value ( );
154 }
155
156};
157
169template< class T >
170std::ostream & operator << ( std::ostream & out, const ext::optional_ref < T > & optional ) {
171 if ( ! optional )
172 return out << "void";
173 else
174 return out << optional.value ( );
175}
176
177
178} /* namespace ext */
Definition: optional_ref.hpp:33
optional_ref(T &val)
Definition: optional_ref.hpp:40
bool operator==(const optional_ref< T > &rhs) const
Compares two optional_ref instances for equvalence.
Definition: optional_ref.hpp:137
const T * operator->() const
Definition: optional_ref.hpp:59
optional_ref(optional_ref &&other) noexcept=default
bool has_value() const noexcept
Definition: optional_ref.hpp:87
optional_ref(const optional_ref< U > &other)
Definition: optional_ref.hpp:44
T & value() &
Definition: optional_ref.hpp:91
optional_ref & operator=(const optional_ref &)=delete
optional_ref(optional_ref< U > &&other)
Definition: optional_ref.hpp:48
optional_ref(std::nullopt_t)
Definition: optional_ref.hpp:38
const T && value() const &&
Definition: optional_ref.hpp:103
const T & value_or(const T &default_value) const &
Definition: optional_ref.hpp:107
const T & value() const &
Definition: optional_ref.hpp:95
T && value() &&
Definition: optional_ref.hpp:99
T & value_or(T &default_value) &
Definition: optional_ref.hpp:114
auto operator<=>(const optional_ref< T > &rhs) const
Compares two optional_ref instances by less relation.
Definition: optional_ref.hpp:151
T && value_or(T &&default_value) &&
Definition: optional_ref.hpp:121
optional_ref(const optional_ref &other)=default
optional_ref()
Definition: optional_ref.hpp:37
const T & operator*() const &
Definition: optional_ref.hpp:67
Definition: sigHandler.cpp:20
std::ostream & operator<<(ext::reference_wrapper< std::ostream > &os, std::ostream &(*const func)(std::ostream &))
Overloaded function allowing same operations on wrapped output stream as on the actual output stream,...
Definition: GlobalData.cpp:33
Definition: FordFulkerson.hpp:16