/usr/include/dune/functions/common/optional.hh is in libdune-functions-dev 2.5.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_FUNCTIONS_COMMON_OPTIONAL_HH
#define DUNE_FUNCTIONS_COMMON_OPTIONAL_HH
#include <utility>
#include <dune/common/typeutilities.hh>
namespace Dune {
namespace Functions {
/**
* \brief A wrapper that can either contain an object of T or be empty
*
* \ingroup Utility
*
* \tparam T Type of wrapped objects
*/
template<class T>
class Optional
{
public:
//! Default constructor
Optional() :
p_(nullptr)
{}
//! Construct internal T from given argument
template<class TT, disableCopyMove<Optional, TT> = 0>
Optional(TT&& t) :
p_(nullptr)
{
emplace(std::forward<TT>(t));
}
//! Move constructor
Optional(Optional&& other)
{
if (other)
p_ = new (buffer_) T(std::move(other.value()));
else
p_ = nullptr;
}
//! Copy constructor
Optional(const Optional& other)
{
if (other)
p_ = new (buffer_) T(other.value());
else
p_ = nullptr;
}
//! Destructor
~Optional()
{
if (operator bool())
p_->~T();
}
/**
* \brief Assignment
*
* If internal T exists, this does an assignement
* from argument, otherwise a construction.
*/
template<class TT, disableCopyMove<Optional, TT> = 0 >
Optional& operator=(TT&& t)
{
if (operator bool())
*p_ = std::forward<T>(t);
else
p_ = new (buffer_) T(std::forward<T>(t));
return *this;
}
/**
* \brief Copy assignment from optional
*/
Optional& operator=(const Optional& other)
{
if (other)
*this = other.value();
else if (operator bool())
{
p_->~T();
p_ = nullptr;
}
return *this;
}
/**
* \brief Move assignment from optional
*/
Optional& operator=(Optional&& other)
{
if (other)
*this = std::move(other.value());
else if (operator bool())
{
p_->~T();
p_ = nullptr;
}
return *this;
}
//! Check if *this is not emtpy
explicit operator bool() const
{
return p_;
}
//! Get reference to internal T
const T& value() const
{
return *p_;
}
//! Get mutable reference to internal T
T& value()
{
return *p_;
}
//! Construct internal T from given arguments
template< class... Args >
void emplace(Args&&... args)
{
if (operator bool())
p_->~T();
p_ = new (buffer_) T(std::forward<Args>(args)...);
}
//! Destruct internal T leaving *this in empty state
void release()
{
if (operator bool())
{
p_->~T();
p_ = nullptr;
}
}
private:
alignas(T) char buffer_[sizeof(T)];
T* p_;
};
} // namespace Functions
} // namespace Dune
#endif // DUNE_FUNCTIONS_COMMON_OPTIONAL_HH
|