3.2-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
optional.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3/*****************************************************************************
4 * See the file COPYING for full copying permissions. *
5 * *
6 * This program is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 *****************************************************************************/
26#ifndef DUMUX_COMMON_OPTIONAL_HH
27#define DUMUX_COMMON_OPTIONAL_HH
28
29#warning "This header is deprecated and will be removed after release 3.2"
30#include <utility>
31
32#include <dune/common/typeutilities.hh>
33
34namespace Dumux {
35
41template<class T>
42class [[deprecated("Optional is deprecated (removed after 3.2); use std::optional")]] Optional
43{
44public:
45
46 Optional() :
47 p_(nullptr)
48 {}
49
50 template<class TT, Dune::disableCopyMove<Optional, TT> = 0>
51 Optional(TT&& t) :
52 p_(nullptr)
53 {
54 emplace(std::forward<TT>(t));
55 }
56
57 Optional(Optional&& other)
58 {
59 if (other)
60 p_ = new (buffer_) T(std::move(other.value()));
61 else
62 p_ = nullptr;
63 }
64
65 Optional(const Optional& other)
66 {
67 if (other)
68 p_ = new (buffer_) T(other.value());
69 else
70 p_ = nullptr;
71 }
72
73 ~Optional()
74 {
75 if (operator bool())
76 p_->~T();
77 }
78
79 template<class TT, Dune::disableCopyMove<Optional, TT> = 0 >
80 Optional& operator=(TT&& t)
81 {
82 if (operator bool())
83 *p_ = std::forward<T>(t);
84 else
85 p_ = new (buffer_) T(std::forward<T>(t));
86 return *this;
87 }
88
89 Optional& operator=(const Optional& other)
90 {
91 if (other)
92 *this = other.value();
93 else if (operator bool())
94 {
95 p_->~T();
96 p_ = nullptr;
97 }
98 return *this;
99 }
100
101 Optional& operator=(Optional&& other)
102 {
103 if (other)
104 *this = std::move(other.value());
105 else if (operator bool())
106 {
107 p_->~T();
108 p_ = nullptr;
109 }
110 return *this;
111 }
112
113 explicit operator bool() const
114 {
115 return p_;
116 }
117
118 const T& value() const
119 {
120 return *p_;
121 }
122
123 T& value()
124 {
125 return *p_;
126 }
127
128 template< class... Args >
129 void emplace(Args&&... args)
130 {
131 if (operator bool())
132 p_->~T();
133 p_ = new (buffer_) T(std::forward<Args>(args)...);
134 }
135
136 void release()
137 {
138 if (operator bool())
139 {
140 p_->~T();
141 p_ = nullptr;
142 }
143 }
144
145private:
146
147 alignas(T) char buffer_[sizeof(T)];
148 T* p_;
149};
150
151
152} // namespace Dumux
153
154#endif // DUMUX_COMMON_OPTIONAL_HH
Definition: adapt.hh:29
A wrapper that can either contain an object of T or be empty.
Definition: optional.hh:43