mirror of
https://github.com/pocoproject/poco.git
synced 2025-02-20 22:31:23 +01:00
enh(Nullable, Optional): reimplement using std::optional (#4502)
This commit is contained in:
parent
03c35cff93
commit
e55bb7032d
@ -45,7 +45,7 @@ class Transcoder;
|
||||
namespace Keywords {
|
||||
|
||||
|
||||
static const NullData null = NULL_GENERIC;
|
||||
static const NullData null = std::nullopt;
|
||||
|
||||
|
||||
} // namespace Keywords
|
||||
|
@ -20,18 +20,13 @@
|
||||
|
||||
#include "Poco/Foundation.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include <algorithm>
|
||||
#include <optional>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
|
||||
|
||||
enum NullType
|
||||
{
|
||||
NULL_GENERIC = 0
|
||||
};
|
||||
|
||||
using NullType = std::nullopt_t;
|
||||
|
||||
template <typename C>
|
||||
class Nullable
|
||||
@ -58,88 +53,70 @@ class Nullable
|
||||
/// default construction.
|
||||
{
|
||||
public:
|
||||
Nullable():
|
||||
Nullable()
|
||||
/// Creates an empty Nullable.
|
||||
_value(),
|
||||
_isNull(true),
|
||||
_null()
|
||||
{
|
||||
}
|
||||
|
||||
Nullable(const NullType&):
|
||||
Nullable(const NullType&)
|
||||
/// Creates an empty Nullable.
|
||||
_value(),
|
||||
_isNull(true),
|
||||
_null()
|
||||
{
|
||||
}
|
||||
|
||||
Nullable(const C& value):
|
||||
/// Creates a Nullable with the given value.
|
||||
_value(value),
|
||||
_isNull(false),
|
||||
_null()
|
||||
_optional(value)
|
||||
{
|
||||
}
|
||||
|
||||
Nullable(C&& value):
|
||||
/// Creates a Nullable by moving the given value.
|
||||
_value(std::forward<C>(value)),
|
||||
_isNull(false),
|
||||
_null()
|
||||
_optional(std::forward<C>(value))
|
||||
{
|
||||
}
|
||||
|
||||
Nullable(const Nullable& other):
|
||||
/// Creates a Nullable by copying another one.
|
||||
_value(other._value),
|
||||
_isNull(other._isNull),
|
||||
_null()
|
||||
_optional(other._optional)
|
||||
{
|
||||
}
|
||||
|
||||
Nullable(Nullable&& other) noexcept:
|
||||
/// Creates a Nullable by moving another one.
|
||||
_value(std::move(other._value)),
|
||||
_isNull(other._isNull),
|
||||
_null()
|
||||
_optional(std::move(other._optional))
|
||||
{
|
||||
other._isNull = true;
|
||||
other._optional.reset();
|
||||
}
|
||||
|
||||
~Nullable()
|
||||
~Nullable() = default;
|
||||
/// Destroys the Nullable.
|
||||
{
|
||||
}
|
||||
|
||||
Nullable& assign(const C& value)
|
||||
/// Assigns a value to the Nullable.
|
||||
{
|
||||
_value = value;
|
||||
_isNull = false;
|
||||
_optional.emplace(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Nullable& assign(C&& value)
|
||||
/// Assigns a value to the Nullable.
|
||||
{
|
||||
_value = std::move(value);
|
||||
_isNull = false;
|
||||
_optional.emplace(std::move(value));
|
||||
return *this;
|
||||
}
|
||||
|
||||
Nullable& assign(const Nullable& other)
|
||||
/// Assigns another Nullable.
|
||||
{
|
||||
Nullable tmp(other);
|
||||
swap(tmp);
|
||||
if (&other != this)
|
||||
_optional = other._optional;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Nullable& assign(NullType)
|
||||
/// Sets value to null.
|
||||
{
|
||||
_isNull = true;
|
||||
_optional.reset();
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -164,42 +141,39 @@ public:
|
||||
Nullable& operator = (Nullable&& other) noexcept
|
||||
/// Moves another Nullable.
|
||||
{
|
||||
_isNull = other._isNull;
|
||||
_value = std::move(other._value);
|
||||
other._isNull = true;
|
||||
_optional = std::move(other._optional);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Nullable& operator = (NullType)
|
||||
/// Assigns another Nullable.
|
||||
/// Assigns NullType.
|
||||
{
|
||||
_isNull = true;
|
||||
_optional.reset();
|
||||
return *this;
|
||||
}
|
||||
|
||||
void swap(Nullable& other) noexcept
|
||||
/// Swaps this Nullable with other.
|
||||
{
|
||||
std::swap(_value, other._value);
|
||||
std::swap(_isNull, other._isNull);
|
||||
std::swap(_optional, other._optional);
|
||||
}
|
||||
|
||||
bool operator == (const Nullable<C>& other) const
|
||||
/// Compares two Nullables for equality
|
||||
{
|
||||
return (_isNull && other._isNull) || (_isNull == other._isNull && _value == other._value);
|
||||
return _optional == other._optional;
|
||||
}
|
||||
|
||||
bool operator == (const C& value) const
|
||||
/// Compares Nullable with value for equality
|
||||
{
|
||||
return (!_isNull && _value == value);
|
||||
return (_optional.has_value() && _optional.value() == value);
|
||||
}
|
||||
|
||||
bool operator == (const NullType&) const
|
||||
/// Compares Nullable with NullData for equality
|
||||
{
|
||||
return _isNull;
|
||||
return !_optional.has_value();
|
||||
}
|
||||
|
||||
bool operator != (const C& value) const
|
||||
@ -217,7 +191,7 @@ public:
|
||||
bool operator != (const NullType&) const
|
||||
/// Compares with NullData for non equality
|
||||
{
|
||||
return !_isNull;
|
||||
return _optional.has_value();
|
||||
}
|
||||
|
||||
bool operator < (const Nullable<C>& other) const
|
||||
@ -225,14 +199,7 @@ public:
|
||||
/// value is smaler than the other object's value.
|
||||
/// Null value is smaller than a non-null value.
|
||||
{
|
||||
if (_isNull && other._isNull) return false;
|
||||
|
||||
if (!_isNull && !other._isNull)
|
||||
return (_value < other._value);
|
||||
|
||||
if (_isNull && !other._isNull) return true;
|
||||
|
||||
return false;
|
||||
return _optional < other._optional;
|
||||
}
|
||||
|
||||
bool operator > (const Nullable<C>& other) const
|
||||
@ -248,8 +215,8 @@ public:
|
||||
///
|
||||
/// Throws a NullValueException if the Nullable is empty.
|
||||
{
|
||||
if (!_isNull)
|
||||
return _value;
|
||||
if (_optional.has_value())
|
||||
return _optional.value();
|
||||
else
|
||||
throw NullValueException();
|
||||
}
|
||||
@ -259,8 +226,8 @@ public:
|
||||
///
|
||||
/// Throws a NullValueException if the Nullable is empty.
|
||||
{
|
||||
if (!_isNull)
|
||||
return _value;
|
||||
if (_optional.has_value())
|
||||
return _optional.value();
|
||||
else
|
||||
throw NullValueException();
|
||||
}
|
||||
@ -269,7 +236,10 @@ public:
|
||||
/// Returns the Nullable's value, or the
|
||||
/// given default value if the Nullable is empty.
|
||||
{
|
||||
return _isNull ? deflt : _value;
|
||||
if (_optional.has_value())
|
||||
return _optional.value();
|
||||
|
||||
return deflt;
|
||||
}
|
||||
|
||||
operator C& ()
|
||||
@ -284,7 +254,7 @@ public:
|
||||
return value();
|
||||
}
|
||||
|
||||
operator NullType& ()
|
||||
operator const NullType& () const
|
||||
/// Get reference to the value
|
||||
{
|
||||
return _null;
|
||||
@ -293,19 +263,18 @@ public:
|
||||
bool isNull() const
|
||||
/// Returns true if the Nullable is empty.
|
||||
{
|
||||
return _isNull;
|
||||
return !_optional.has_value();
|
||||
}
|
||||
|
||||
void clear()
|
||||
/// Clears the Nullable.
|
||||
{
|
||||
_isNull = true;
|
||||
_optional.reset();
|
||||
}
|
||||
|
||||
private:
|
||||
C _value;
|
||||
bool _isNull;
|
||||
NullType _null;
|
||||
std::optional<C> _optional;
|
||||
static constexpr NullType _null {std::nullopt};
|
||||
};
|
||||
|
||||
|
||||
|
@ -20,7 +20,7 @@
|
||||
|
||||
#include "Poco/Foundation.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include <algorithm>
|
||||
#include <optional>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
@ -53,40 +53,33 @@ class Optional
|
||||
/// nillable == true.
|
||||
{
|
||||
public:
|
||||
Optional():
|
||||
Optional()
|
||||
/// Creates an empty Optional.
|
||||
_value(),
|
||||
_isSpecified(false)
|
||||
{
|
||||
}
|
||||
|
||||
Optional(const C& value):
|
||||
/// Creates a Optional with the given value.
|
||||
_value(value),
|
||||
_isSpecified(true)
|
||||
_optional(value)
|
||||
{
|
||||
}
|
||||
|
||||
Optional(C&& value):
|
||||
/// Creates a Optional by moving the given value.
|
||||
_value(std::forward<C>(value)),
|
||||
_isSpecified(true)
|
||||
_optional(std::forward<C>(value))
|
||||
{
|
||||
}
|
||||
|
||||
Optional(const Optional& other):
|
||||
/// Creates a Optional by copying another one.
|
||||
_value(other._value),
|
||||
_isSpecified(other._isSpecified)
|
||||
_optional(other._optional)
|
||||
{
|
||||
}
|
||||
|
||||
Optional(Optional&& other) noexcept:
|
||||
/// Creates a Optional by moving another one.
|
||||
_value(std::move(other._value)),
|
||||
_isSpecified(other._isSpecified)
|
||||
_optional(std::move(other._optional))
|
||||
{
|
||||
other._isSpecified = false;
|
||||
}
|
||||
|
||||
~Optional()
|
||||
@ -97,16 +90,14 @@ public:
|
||||
Optional& assign(const C& value)
|
||||
/// Assigns a value to the Optional.
|
||||
{
|
||||
_value = value;
|
||||
_isSpecified = true;
|
||||
_optional.emplace(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Optional& assign(C&& value)
|
||||
/// Moves a value into the Optional.
|
||||
{
|
||||
_value = std::move(value);
|
||||
_isSpecified = true;
|
||||
_optional.emplace(std::move(value));
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -135,17 +126,14 @@ public:
|
||||
|
||||
Optional& operator = (Optional&& other) noexcept
|
||||
{
|
||||
_value = std::move(other._value);
|
||||
_isSpecified = other._isSpecified;
|
||||
other._isSpecified = false;
|
||||
_optional = std::move(other._optional);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void swap(Optional& other) noexcept
|
||||
{
|
||||
using std::swap;
|
||||
swap(_value, other._value);
|
||||
swap(_isSpecified, other._isSpecified);
|
||||
swap(_optional, other._optional);
|
||||
}
|
||||
|
||||
const C& value() const
|
||||
@ -153,10 +141,10 @@ public:
|
||||
///
|
||||
/// Throws a Poco::NullValueException if the value has not been specified.
|
||||
{
|
||||
if (_isSpecified)
|
||||
return _value;
|
||||
else
|
||||
throw Poco::NullValueException();
|
||||
if (_optional.has_value())
|
||||
return _optional.value();
|
||||
|
||||
throw Poco::NullValueException();
|
||||
}
|
||||
|
||||
const C& value(const C& deflt) const
|
||||
@ -164,24 +152,27 @@ public:
|
||||
/// given default value if the Optional's
|
||||
/// value has not been specified.
|
||||
{
|
||||
return _isSpecified ? _value : deflt;
|
||||
if (_optional.has_value())
|
||||
return _optional.value();
|
||||
|
||||
return deflt;
|
||||
}
|
||||
|
||||
bool isSpecified() const
|
||||
/// Returns true iff the Optional's value has been specified.
|
||||
{
|
||||
return _isSpecified;
|
||||
return _optional.has_value();
|
||||
}
|
||||
|
||||
void clear()
|
||||
/// Clears the Optional.
|
||||
{
|
||||
_isSpecified = false;
|
||||
_optional.reset();
|
||||
}
|
||||
|
||||
private:
|
||||
C _value;
|
||||
bool _isSpecified;
|
||||
|
||||
std::optional<C> _optional;
|
||||
};
|
||||
|
||||
|
||||
|
@ -30,6 +30,7 @@
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
|
||||
using namespace std::string_literals;
|
||||
|
||||
using Poco::Bugcheck;
|
||||
using Poco::Exception;
|
||||
@ -966,7 +967,7 @@ void CoreTest::testNullable()
|
||||
|
||||
assertTrue (i == 1);
|
||||
assertTrue (f == 1.5);
|
||||
assertTrue (s == "abc");
|
||||
assertTrue (s == "abc"s);
|
||||
|
||||
i.clear();
|
||||
f.clear();
|
||||
@ -1035,7 +1036,7 @@ void CoreTest::testNullable()
|
||||
assertTrue (n2 != n1);
|
||||
assertTrue (n1 > n2);
|
||||
|
||||
NullType nd{};
|
||||
const auto nd {std::nullopt};
|
||||
assertTrue (n1 != nd);
|
||||
assertTrue (nd != n1);
|
||||
n1.clear();
|
||||
|
Loading…
x
Reference in New Issue
Block a user