refactor(handle): remove non-pointer handle
Previous commit rendered it obsolete. Refs: #1
This commit is contained in:
@@ -15,97 +15,6 @@ namespace furc {
|
|||||||
template <typename T, typename Error = std::string, typename = void>
|
template <typename T, typename Error = std::string, typename = void>
|
||||||
class handle;
|
class handle;
|
||||||
|
|
||||||
template <typename T, typename Error>
|
|
||||||
class handle<T, Error> {
|
|
||||||
template <typename, typename, typename>
|
|
||||||
friend class handle;
|
|
||||||
|
|
||||||
friend std::ostream& operator<<(std::ostream& os, const handle& result);
|
|
||||||
public:
|
|
||||||
using value_type = std::remove_reference_t<T>;
|
|
||||||
using pointer = value_type*;
|
|
||||||
using const_pointer = const value_type*;
|
|
||||||
using reference = T&;
|
|
||||||
using const_reference = const T&;
|
|
||||||
public:
|
|
||||||
handle(location location, value_type&& value)
|
|
||||||
: m_location(location), m_value(std::move(value)) {}
|
|
||||||
|
|
||||||
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<value_type, Args...>>>
|
|
||||||
handle(location location, Args&&... args)
|
|
||||||
: m_location(location), m_value(value_type(std::forward<Args>(args)...)) {}
|
|
||||||
|
|
||||||
handle(location location, Error&& error)
|
|
||||||
: m_location(location), m_error(std::move(error)) {}
|
|
||||||
|
|
||||||
template <typename U>
|
|
||||||
handle(const handle<U, Error>& error)
|
|
||||||
: m_location(error.location()), m_error(error.error()) {}
|
|
||||||
|
|
||||||
~handle() = default;
|
|
||||||
|
|
||||||
handle(handle&& other) noexcept
|
|
||||||
: m_location(other.m_location), m_value(std::move(other.m_value)), m_error(std::move(other.m_error)) {
|
|
||||||
other.m_value.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
handle(const handle&) = delete;
|
|
||||||
|
|
||||||
handle& operator=(handle&& other) noexcept {
|
|
||||||
if (this == &other) return *this;
|
|
||||||
m_location = other.m_location;
|
|
||||||
m_value = other.m_value;
|
|
||||||
m_error = std::move(other.m_error);
|
|
||||||
other.m_value.reset();
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
handle& operator=(const handle&) = delete;
|
|
||||||
public:
|
|
||||||
location location() const { return m_location; }
|
|
||||||
|
|
||||||
bool present() const { return m_value.has_value(); }
|
|
||||||
bool has_error() const { return !m_value.has_value(); }
|
|
||||||
|
|
||||||
value_type move() {
|
|
||||||
value_type value = std::move(*m_value);
|
|
||||||
m_value.reset();
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
Error error() const { return m_error; }
|
|
||||||
|
|
||||||
reference operator*() { return m_value.value(); } // NOLINT(bugprone-unchecked-optional-access)
|
|
||||||
const_reference operator*() const { return m_value.value(); } // NOLINT(bugprone-unchecked-optional-access)
|
|
||||||
pointer operator->() { return &m_value.value(); } // NOLINT(bugprone-unchecked-optional-access)
|
|
||||||
const_pointer operator->() const { return &m_value.value(); } // NOLINT(bugprone-unchecked-optional-access)
|
|
||||||
public:
|
|
||||||
bool operator==(const handle<T, Error>& rhs) const {
|
|
||||||
if (present() != rhs.present() || error() != rhs.error()) return false;
|
|
||||||
if (m_value.has_value() && m_value.value() != rhs.m_value.value()) // NOLINT(bugprone-unchecked-optional-access)
|
|
||||||
return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator==(const T& rhs) const {
|
|
||||||
return m_value.has_value() && *m_value == rhs; // NOLINT(bugprone-unchecked-optional-access)
|
|
||||||
}
|
|
||||||
public:
|
|
||||||
friend std::ostream& operator<<(std::ostream& os, const handle& result) {
|
|
||||||
os << result.m_location << ": ";
|
|
||||||
if (result.m_value.has_value()) {
|
|
||||||
os << *result.m_value;
|
|
||||||
} else {
|
|
||||||
os << "ERROR: " << result.m_error;
|
|
||||||
}
|
|
||||||
return os;
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
struct location m_location;
|
|
||||||
std::optional<value_type> m_value = {};
|
|
||||||
Error m_error;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T, typename Error>
|
template <typename T, typename Error>
|
||||||
class handle<T*, Error> {
|
class handle<T*, Error> {
|
||||||
template <typename, typename, typename>
|
template <typename, typename, typename>
|
||||||
|
|||||||
Reference in New Issue
Block a user