8 #ifndef SERVLET_OPTIONAL_H
9 #define SERVLET_OPTIONAL_H
17 #include <type_traits>
63 constexpr
optional_ptr(T* ptr,
bool owner =
false) : _ptr{ptr}, _owner{owner} {}
75 { other._ptr =
nullptr; other._owner =
false; }
94 if (_owner)
delete _ptr;
95 _ptr =
nullptr; _owner =
false;
113 if (_owner)
delete _ptr;
114 _ptr = other._ptr; _owner = other._owner;
115 other._ptr =
nullptr; other._owner =
false;
131 if (_owner)
delete _ptr;
132 _ptr = ptr; _owner = owner;
142 constexpr
explicit operator bool() const noexcept {
return _ptr !=
nullptr; }
148 constexpr
bool has_value() const noexcept {
return _ptr !=
nullptr; }
154 constexpr
bool is_owner() const noexcept {
return _owner; }
193 other._ptr = tmp_ptr;
194 bool tmp_owner = _owner;
195 _owner = other._owner;
196 other._owner = tmp_owner;
319 constexpr
explicit operator bool() const noexcept {
return _ptr !=
nullptr; }
325 constexpr
bool has_value() const noexcept {
return _ptr !=
nullptr; }
345 template<
typename OT>
348 static_assert(std::is_copy_constructible<T>::value,
"result is not copy constructable");
349 static_assert(std::is_convertible<OT&&, T>::value,
"default is not convertible to result");
350 return _ptr ==
nullptr ?
static_cast<T
>(std::forward(dflt)) : *_ptr;
367 constexpr
void reset() noexcept { _ptr =
nullptr; }
379 template<
typename CharT,
typename Traits,
typename T>
380 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& out,
const optional_ptr<T>& opt)
382 if (opt) out << *opt;
395 template<
typename CharT,
typename Traits,
typename T>
396 std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& out,
const optional_ref<T>& opt)
398 if (opt) out << *opt;
412 constexpr
bool operator==(
const optional_ref<T> &l,
const optional_ref<T> &r)
414 return static_cast<bool>(l) == static_cast<bool>(r) && (!l || *l == *r);
425 constexpr
bool operator!=(
const optional_ref<T> &l,
const optional_ref<T> &r) {
return !(l == r); }
436 constexpr
bool operator<(const optional_ref<T> &l,
const optional_ref<T> &r)
438 return static_cast<bool>(r) && (!l || *l < *r);
450 constexpr
bool operator>(
const optional_ref<T> &l,
const optional_ref<T> &r) {
return r < l; }
461 constexpr
bool operator<=(const optional_ref<T> &l,
const optional_ref<T> &r) {
return !(r < l); }
472 constexpr
bool operator>=(
const optional_ref<T> &l,
const optional_ref<T> &r) {
return !(l < r); }
483 template<
typename T,
typename U>
484 constexpr
bool operator==(
const optional_ref<T> &l,
const U &r) {
return l && *l == r; }
495 template<
typename T,
typename U>
496 constexpr
bool operator==(
const T &l,
const optional_ref<T> &r) {
return r && l == *r; }
507 template<
typename T,
typename U>
508 constexpr
bool operator!=(
const optional_ref<T> &l, U
const &r) {
return !l || !(*l == r); }
519 template<
typename T,
typename U>
520 constexpr
bool operator!=(
const U &l,
const optional_ref<T> &r) {
return !r || !(l == *r); }
531 template<
typename T,
typename U>
532 constexpr
bool operator<(const optional_ref<T> &l,
const U &r) {
return !l || *l < r; }
543 template<
typename T,
typename U>
544 constexpr
bool operator<(const U &l, const optional_ref<T> &r) {
return r && l < *r; }
555 template<
typename T,
typename U>
556 constexpr
bool operator>(
const optional_ref<T> &l,
const U &r) {
return l && r < *l; }
567 template<
typename T,
typename U>
568 constexpr
bool operator>(
const U &l,
const optional_ref<T> &r) {
return !r || *r < l; }
579 template<
typename T,
typename U>
580 constexpr
bool operator<=(const optional_ref<T> &l,
const U &r) {
return !l || !(r < *l); }
591 template<
typename T,
typename U>
592 constexpr
bool operator<=(const U &l, const optional_ref<T> &r) {
return r && !(*r < l); }
603 template<
typename T,
typename U>
604 constexpr
bool operator>=(
const optional_ref<T> &l,
const U &r) {
return l && !(*l < r); }
615 template<
typename T,
typename U>
616 constexpr
bool operator>=(
const U &l,
const optional_ref<T> &r) {
return !r || !(l < *r); }
626 inline void swap(optional_ref<T> &l, optional_ref<T> &r) noexcept(noexcept(l.swap(r))) { l.swap(r); }
630 #endif // SERVLET_OPTIONAL_H
T & reference
Type definition for reference type.
Definition: optional.h:232
Exception thrown on attempt to access nullptr object if this is possible to catch this attempt...
Definition: exception.h:55
constexpr bool is_owner() const noexcept
Checks whether this object is the owner of the contained pointer.
Definition: optional.h:154
constexpr const T * operator->() const
Accesses the contained value as a constant pointer.
Definition: optional.h:298
const T & const_reference
Type definition for constant reference type.
Definition: optional.h:236
constexpr T value_or(OT &&dflt) const &noexcept
Returns the contained value if this has a value, otherwise returns given value.
Definition: optional.h:346
T value_type
Type definition for value type.
Definition: optional.h:228
constexpr void reset() noexcept
If this object contains a valid pointer, delete that.
Definition: optional.h:203
constexpr optional_ptr & operator=(optional_ptr &&other) noexcept
Move assignment.
Definition: optional.h:111
constexpr optional_ref & operator=(optional_ref &&other) noexcept
Move assignment.
Definition: optional.h:279
Optional reference implementation.
Definition: optional.h:221
constexpr optional_ref & operator=(const optional_ref &other) noexcept
Replaces contents of this object with the contents of other.
Definition: optional.h:273
~optional_ref() noexcept
Destructor.
Definition: optional.h:266
constexpr optional_ref & operator=(T &obj) noexcept
Replaces contents of this object with the reference to other.
Definition: optional.h:285
Exception used in the mod_servlet.
constexpr optional_ptr & operator=(const optional_ptr &other)=delete
Copy is prohibited.
constexpr T & operator*()&
Accesses the contained pointer.
Definition: optional.h:176
constexpr T * operator->()
Accesses the contained value as a pointer.
Definition: optional.h:292
constexpr T & value()&
Accesses the contained value as a reference.
Definition: optional.h:332
T value_type
Type definition for value type.
Definition: optional.h:39
T * pointer
Type definition for pointer type.
Definition: optional.h:43
constexpr optional_ref(const optional_ref &other) noexcept
Copy constructor.
Definition: optional.h:254
constexpr void swap(optional_ref &other) noexcept
Swaps the contents with those of other.
Definition: optional.h:357
const T * const_pointer
Type definition for const pointer type.
Definition: optional.h:47
constexpr const T & value() const &
Accesses the contained value as a constant reference.
Definition: optional.h:338
~optional_ptr() noexcept
Destructor.
Definition: optional.h:83
constexpr bool has_value() const noexcept
Checks whether this object contains valid pointer.
Definition: optional.h:148
constexpr optional_ptr()
Default constructor.
Definition: optional.h:55
constexpr const T & operator*() const &
Accesses the contained pointer.
Definition: optional.h:183
constexpr void reset() noexcept
If this object contains a reference to an object, reset the pointer to that to nullptr.
Definition: optional.h:367
constexpr const T * operator->() const
const version of pointer accessor
Definition: optional.h:167
constexpr void clear()
Clears the object.
Definition: optional.h:92
constexpr T * operator->()
Accesses the contained pointer.
Definition: optional.h:161
constexpr void swap(optional_ptr &other) noexcept
Swaps the contents with those of other.
Definition: optional.h:189
constexpr optional_ref(optional_ref &&other) noexcept
Move constructor.
Definition: optional.h:259
constexpr optional_ptr & assign(T *ptr, bool owner=false)
Assigns new pointer to this object.
Definition: optional.h:129
constexpr optional_ptr(T *ptr, bool owner=false)
Constructor which creates the object with a given pointer and specified ownership.
Definition: optional.h:63
Class implements smart pointer with optional ownership.
Definition: optional.h:33
constexpr const T & operator*() const
Accesses the contained value as a constant reference.
Definition: optional.h:311
constexpr optional_ref(T &obj) noexcept
Constructs object that contains a reference to a given object,.
Definition: optional.h:248
constexpr bool has_value() const noexcept
Checks whether this object contains valid pointer.
Definition: optional.h:325
constexpr T & operator*()
Accesses the contained value as a reference.
Definition: optional.h:305
constexpr optional_ref() noexcept
Default constructor.
Definition: optional.h:243
constexpr optional_ptr(optional_ptr &&other)
Move constructor.
Definition: optional.h:74