3.1-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#include <utility>
30
31#include <dune/common/typeutilities.hh>
32
33namespace Dumux {
34
40template<class T>
42{
43public:
44
46 p_(nullptr)
47 {}
48
49 template<class TT, Dune::disableCopyMove<Optional, TT> = 0>
50 Optional(TT&& t) :
51 p_(nullptr)
52 {
53 emplace(std::forward<TT>(t));
54 }
55
57 {
58 if (other)
59 p_ = new (buffer_) T(std::move(other.value()));
60 else
61 p_ = nullptr;
62 }
63
64 Optional(const Optional& other)
65 {
66 if (other)
67 p_ = new (buffer_) T(other.value());
68 else
69 p_ = nullptr;
70 }
71
73 {
74 if (operator bool())
75 p_->~T();
76 }
77
78 template<class TT, Dune::disableCopyMove<Optional, TT> = 0 >
80 {
81 if (operator bool())
82 *p_ = std::forward<T>(t);
83 else
84 p_ = new (buffer_) T(std::forward<T>(t));
85 return *this;
86 }
87
89 {
90 if (other)
91 *this = other.value();
92 else if (operator bool())
93 {
94 p_->~T();
95 p_ = nullptr;
96 }
97 return *this;
98 }
99
101 {
102 if (other)
103 *this = std::move(other.value());
104 else if (operator bool())
105 {
106 p_->~T();
107 p_ = nullptr;
108 }
109 return *this;
110 }
111
112 explicit operator bool() const
113 {
114 return p_;
115 }
116
117 const T& value() const
118 {
119 return *p_;
120 }
121
122 T& value()
123 {
124 return *p_;
125 }
126
127 template< class... Args >
128 void emplace(Args&&... args)
129 {
130 if (operator bool())
131 p_->~T();
132 p_ = new (buffer_) T(std::forward<Args>(args)...);
133 }
134
135 void release()
136 {
137 if (operator bool())
138 {
139 p_->~T();
140 p_ = nullptr;
141 }
142 }
143
144private:
145
146 alignas(T) char buffer_[sizeof(T)];
147 T* p_;
148};
149
150
151} // namespace Dumux
152
153#endif // DUMUX_COMMON_OPTIONAL_HH
make the local view function available whenever we use the grid geometry
Definition: adapt.hh:29
A wrapper that can either contain an object of T or be empty.
Definition: optional.hh:42
Optional(TT &&t)
Definition: optional.hh:50
~Optional()
Definition: optional.hh:72
Optional & operator=(const Optional &other)
Definition: optional.hh:88
T & value()
Definition: optional.hh:122
Optional & operator=(TT &&t)
Definition: optional.hh:79
Optional & operator=(Optional &&other)
Definition: optional.hh:100
const T & value() const
Definition: optional.hh:117
Optional(const Optional &other)
Definition: optional.hh:64
Optional(Optional &&other)
Definition: optional.hh:56
void emplace(Args &&... args)
Definition: optional.hh:128
Optional()
Definition: optional.hh:45
void release()
Definition: optional.hh:135