/usr/include/libfilezilla/optional.hpp is in libfilezilla-dev 0.4.0.1-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 | #ifndef LIBFILEZILLA_OPTIONAL_HEADER
#define LIBFILEZILLA_OPTIONAL_HEADER
/** \file
* \brief Header for the \ref fz::sparse_optional "sparse_optional" template class
*/
namespace fz {
/** \brief Similar to C++17's std::optional, but stores the data in
* dynamic memory.
*
* sparse_optional is useful to save memory if it is expected
* that the object is rarely set and the object's size is bigger than
* a simple pointer.
*/
template<typename T>
class sparse_optional final
{
public:
sparse_optional();
explicit sparse_optional(T const& v);
sparse_optional(sparse_optional<T> const& v);
sparse_optional(sparse_optional<T> && v) noexcept;
~sparse_optional();
void clear();
explicit operator bool() const { return v_ != 0; };
T& operator*() { return *v_; }
T const& operator*() const { return *v_; }
T* operator->() { return v_; }
T const* operator->() const { return v_; }
bool operator==(sparse_optional<T> const& cmp) const;
inline bool operator!=(sparse_optional<T> const& cmp) const { return !(*this == cmp); }
bool operator<(sparse_optional<T> const& cmp) const;
sparse_optional<T>& operator=(sparse_optional<T> const& v);
sparse_optional<T>& operator=(sparse_optional<T> && v) noexcept;
private:
T* v_;
};
template<typename T>
sparse_optional<T>::sparse_optional()
: v_()
{
}
template<typename T>
sparse_optional<T>::sparse_optional(T const& v)
: v_(new T(v))
{
}
template<typename T>
sparse_optional<T>::sparse_optional(sparse_optional<T> const& v)
{
if (v) {
v_ = new T(*v);
}
else {
v_ = 0;
}
}
template<typename T>
sparse_optional<T>::sparse_optional(sparse_optional<T> && v) noexcept
{
v_ = v.v_;
v.v_ = 0;
}
template<typename T>
sparse_optional<T>::~sparse_optional()
{
delete v_;
}
template<typename T>
void sparse_optional<T>::clear()
{
delete v_;
v_ = 0;
}
template<typename T>
sparse_optional<T>& sparse_optional<T>::operator=(sparse_optional<T> const& v)
{
if (this != &v) {
delete v_;
if (v.v_) {
v_ = new T(*v.v_);
}
else {
v_ = 0;
}
}
return *this;
}
template<typename T>
sparse_optional<T>& sparse_optional<T>::operator=(sparse_optional<T> && v) noexcept
{
if (this != &v) {
delete v_;
v_ = v.v_;
v.v_ = 0;
}
return *this;
}
template<typename T>
bool sparse_optional<T>::operator==(sparse_optional<T> const& cmp) const
{
if (!v_ && !cmp.v_) {
return true;
}
if (!v_ || !cmp.v_) {
return false;
}
return *v_ == *cmp.v_;
}
template<typename T>
bool sparse_optional<T>::operator<(sparse_optional<T> const& cmp) const
{
if (!v_ || !cmp.v_) {
return cmp.v_ != 0;
}
return *v_ < *cmp.v_;
}
}
#endif
|