dune-functions  2.5-dev
optional.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_FUNCTIONS_COMMON_OPTIONAL_HH
4 #define DUNE_FUNCTIONS_COMMON_OPTIONAL_HH
5 
6 #include <utility>
7 
8 #include <dune/common/typeutilities.hh>
9 
10 namespace Dune {
11 namespace Functions {
12 
13 
21 template<class T>
22 class Optional
23 {
24 public:
25 
28  p_(nullptr)
29  {}
30 
32  template<class TT, disableCopyMove<Optional, TT> = 0>
33  Optional(TT&& t) :
34  p_(nullptr)
35  {
36  emplace(std::forward<TT>(t));
37  }
38 
40  Optional(Optional&& other)
41  {
42  if (other)
43  p_ = new (buffer_) T(std::move(other.value()));
44  else
45  p_ = nullptr;
46  }
47 
49  Optional(const Optional& other)
50  {
51  if (other)
52  p_ = new (buffer_) T(other.value());
53  else
54  p_ = nullptr;
55  }
56 
59  {
60  if (operator bool())
61  p_->~T();
62  }
63 
70  template<class TT, disableCopyMove<Optional, TT> = 0 >
72  {
73  if (operator bool())
74  *p_ = std::forward<T>(t);
75  else
76  p_ = new (buffer_) T(std::forward<T>(t));
77  return *this;
78  }
79 
83  Optional& operator=(const Optional& other)
84  {
85  if (other)
86  *this = other.value();
87  else if (operator bool())
88  {
89  p_->~T();
90  p_ = nullptr;
91  }
92  return *this;
93  }
94 
99  {
100  if (other)
101  *this = std::move(other.value());
102  else if (operator bool())
103  {
104  p_->~T();
105  p_ = nullptr;
106  }
107  return *this;
108  }
109 
111  explicit operator bool() const
112  {
113  return p_;
114  }
115 
117  const T& value() const
118  {
119  return *p_;
120  }
121 
123  T& value()
124  {
125  return *p_;
126  }
127 
129  template< class... Args >
130  void emplace(Args&&... args)
131  {
132  if (operator bool())
133  p_->~T();
134  p_ = new (buffer_) T(std::forward<Args>(args)...);
135  }
136 
138  void release()
139  {
140  if (operator bool())
141  {
142  p_->~T();
143  p_ = nullptr;
144  }
145  }
146 
147 private:
148 
149  alignas(T) char buffer_[sizeof(T)];
150  T* p_;
151 };
152 
153 
154 } // namespace Functions
155 } // namespace Dune
156 
157 #endif // DUNE_FUNCTIONS_COMMON_OPTIONAL_HH
Optional & operator=(const Optional &other)
Copy assignment from optional.
Definition: optional.hh:83
Optional & operator=(TT &&t)
Assignment.
Definition: optional.hh:71
void release()
Destruct internal T leaving *this in empty state.
Definition: optional.hh:138
Optional(TT &&t)
Construct internal T from given argument.
Definition: optional.hh:33
Definition: polynomial.hh:7
Optional()
Default constructor.
Definition: optional.hh:27
Optional(const Optional &other)
Copy constructor.
Definition: optional.hh:49
Optional & operator=(Optional &&other)
Move assignment from optional.
Definition: optional.hh:98
Optional(Optional &&other)
Move constructor.
Definition: optional.hh:40
A wrapper that can either contain an object of T or be empty.
Definition: optional.hh:22
~Optional()
Destructor.
Definition: optional.hh:58
void emplace(Args &&... args)
Construct internal T from given arguments.
Definition: optional.hh:130
T & value()
Get mutable reference to internal T.
Definition: optional.hh:123
const T & value() const
Get reference to internal T.
Definition: optional.hh:117