diff --git a/tstl/include/expected.hpp b/tstl/include/expected.hpp index 87740c5e..a0443c58 100644 --- a/tstl/include/expected.hpp +++ b/tstl/include/expected.hpp @@ -17,190 +17,214 @@ namespace std { -constexpr struct only_set_valid_t {} only_set_valid; +constexpr struct only_set_valid_t { +} only_set_valid; template struct exceptional { - typedef E error_type; + using error_type = E; ///< The error type - error_type error; + error_type error; ///< The error value - exceptional(): error() { + exceptional() + : error() { //Nothing else to init } - explicit exceptional(error_type e) : error(e) { + explicit exceptional(error_type e) + : error(e) { //Nothing else to init } }; -template +template union trivial_expected_storage { - typedef T value_type; - typedef E error_type; + using value_type = T; ///< The value type + using error_type = E; ///< The error type - error_type error; - value_type value; + error_type error; ///< The error value + value_type value; ///< The value - constexpr trivial_expected_storage() : value(){ + constexpr trivial_expected_storage() + : value() { //Nothing else to init } - constexpr trivial_expected_storage(const exceptional& e) : error(e.error){ + constexpr trivial_expected_storage(const exceptional& e) + : error(e.error) { //Nothing else to init } - template - constexpr trivial_expected_storage(Args&&... args): value(std::forward(args)...){ + template + constexpr trivial_expected_storage(Args&&... args) + : value(std::forward(args)...) { //Nothing else to init } ~trivial_expected_storage() = default; }; -template -union trivial_expected_storage { - typedef void value_type; - typedef E error_type; +template +union trivial_expected_storage { + using value_type = Type; ///< The value type + using error_type = E; ///< The error type - error_type error; + error_type error; ///< The error value constexpr trivial_expected_storage() { //Nothing else to init } - constexpr trivial_expected_storage(const exceptional& e) : error(e.error){ + constexpr trivial_expected_storage(const exceptional& e) + : error(e.error) { //Nothing else to init } ~trivial_expected_storage() = default; }; -template +template struct non_trivial_expected_storage { - typedef T value_type; - typedef E error_type; + using value_type = T; ///< The value type + using error_type = E; ///< The error type - error_type error; - value_type value; + error_type error; ///< The error value + value_type value; ///< The value - constexpr non_trivial_expected_storage() : value(){ + constexpr non_trivial_expected_storage() + : value() { //Nothing else to init } - constexpr non_trivial_expected_storage(const exceptional& e) : error(e.error){ + constexpr non_trivial_expected_storage(const exceptional& e) + : error(e.error) { //Nothing else to init } - template - constexpr non_trivial_expected_storage(Args&&... args): value(std::forward(args)...){ + template + constexpr non_trivial_expected_storage(Args&&... args) + : value(std::forward(args)...) { //Nothing else to init } - ~non_trivial_expected_storage() {}; + ~non_trivial_expected_storage(){}; }; -template -struct non_trivial_expected_storage { - typedef void value_type; - typedef E error_type; +template +struct non_trivial_expected_storage { + using value_type = Type; ///< The value type + using error_type = E; ///< The error type - error_type error; + error_type error; ///< The error value - constexpr non_trivial_expected_storage(){ + constexpr non_trivial_expected_storage() { //Nothing else to init } - constexpr non_trivial_expected_storage(exceptional& e) : error(e.error){ + constexpr non_trivial_expected_storage(exceptional& e) + : error(e.error) { //Nothing else to init } - ~non_trivial_expected_storage() {}; + ~non_trivial_expected_storage(){}; }; -template +template struct trivial_expected_base { - typedef T value_type; - typedef E error_type; + using value_type = T; ///< The value type + using error_type = E; ///< The error type - bool has_value; + bool has_value; ///< Indicates if the base has a value trivial_expected_storage storage; - trivial_expected_base() : has_value(true), storage(){ + trivial_expected_base() + : has_value(true), storage() { //Nothing else to init } - trivial_expected_base(only_set_valid_t, bool hv) : has_value(hv){ + trivial_expected_base(only_set_valid_t, bool hv) + : has_value(hv) { //Nothing else to init } - trivial_expected_base(const value_type& v) : has_value(true), storage(v){ + trivial_expected_base(const value_type& v) + : has_value(true), storage(v) { //Nothing else to init } - trivial_expected_base(value_type&& v) : has_value(true), storage(std::forward(v)){ + trivial_expected_base(value_type&& v) + : has_value(true), storage(std::forward(v)) { //Nothing else to init } - trivial_expected_base(const exceptional& e) : has_value(false), storage(e) { + trivial_expected_base(const exceptional& e) + : has_value(false), storage(e) { //Nothing else to init } ~trivial_expected_base() = default; }; -template -struct trivial_expected_base { - typedef E error_type; +template +struct trivial_expected_base { + using error_type = E; ///< The error type - bool has_value; + bool has_value; ///< Indicates if the base has a value trivial_expected_storage storage; - trivial_expected_base() : has_value(true), storage(){ + trivial_expected_base() + : has_value(true), storage() { //Nothing else to init } - trivial_expected_base(only_set_valid_t, bool hv) : has_value(hv){ + trivial_expected_base(only_set_valid_t, bool hv) + : has_value(hv) { //Nothing else to init } - trivial_expected_base(const exceptional& e) : has_value(false), storage(e) { + trivial_expected_base(const exceptional& e) + : has_value(false), storage(e) { //Nothing else to init } ~trivial_expected_base() = default; }; -template +template struct non_trivial_expected_base { - typedef T value_type; - typedef E error_type; + using value_type = T; ///< The value type + using error_type = E; ///< The error type bool has_value; non_trivial_expected_storage storage; - non_trivial_expected_base() : has_value(true), storage(){ + non_trivial_expected_base() + : has_value(true), storage() { //Nothing else to init } - non_trivial_expected_base(only_set_valid_t, bool hv) : has_value(hv){ + non_trivial_expected_base(only_set_valid_t, bool hv) + : has_value(hv) { //Nothing else to init } - non_trivial_expected_base(const value_type& v) : has_value(true), storage(v){ + non_trivial_expected_base(const value_type& v) + : has_value(true), storage(v) { //Nothing else to init } - non_trivial_expected_base(value_type&& v) : has_value(true), storage(std::forward(v)){ + non_trivial_expected_base(value_type&& v) + : has_value(true), storage(std::forward(v)) { //Nothing else to init } - non_trivial_expected_base(const exceptional& e) : has_value(false), storage(e) { + non_trivial_expected_base(const exceptional& e) + : has_value(false), storage(e) { //Nothing else to init } - ~non_trivial_expected_base(){ - if(has_value){ + ~non_trivial_expected_base() { + if (has_value) { storage.value.~value_type(); } else { storage.error.~error_type(); @@ -208,27 +232,30 @@ struct non_trivial_expected_base { } }; -template -struct non_trivial_expected_base { - typedef E error_type; +template +struct non_trivial_expected_base { + using error_type = E; ///< The error type - bool has_value; + bool has_value; ///< Indicates if the base has a value non_trivial_expected_storage storage; - non_trivial_expected_base() : has_value(true), storage(){ + non_trivial_expected_base() + : has_value(true), storage() { //Nothing else to init } - non_trivial_expected_base(only_set_valid_t, bool hv) : has_value(hv){ + non_trivial_expected_base(only_set_valid_t, bool hv) + : has_value(hv) { //Nothing else to init } - non_trivial_expected_base(const exceptional& e) : has_value(false), storage(e) { + non_trivial_expected_base(const exceptional& e) + : has_value(false), storage(e) { //Nothing else to init } - ~non_trivial_expected_base(){ - if(!has_value){ + ~non_trivial_expected_base() { + if (!has_value) { storage.error.~error_type(); } } @@ -236,15 +263,14 @@ struct non_trivial_expected_base { template using expected_base = std::conditional_t< - std::is_trivially_destructible::value && std::is_trivially_destructible::value, - trivial_expected_base, - non_trivial_expected_base - >; + std::is_trivially_destructible::value && std::is_trivially_destructible::value, + trivial_expected_base, + non_trivial_expected_base>; -template +template struct expected : expected_base { - typedef T value_type; - typedef E error_type; + using value_type = T; ///< The value type + using error_type = E; ///< The error type typedef expected this_type; typedef expected_base base_type; @@ -266,7 +292,7 @@ private: return static_addressof(base_type::storage.error); } - constexpr const bool& contained_has_value() const& { + constexpr const bool& contained_has_value() const & { return base_type::has_value; } @@ -278,7 +304,7 @@ private: return std::move(base_type::has_value); } - constexpr const value_type& contained_value() const& { + constexpr const value_type& contained_value() const & { return base_type::storage.value; } @@ -290,7 +316,7 @@ private: return std::move(base_type::storage.value); } - constexpr const error_type& contained_error() const& { + constexpr const error_type& contained_error() const & { return base_type::storage.error; } @@ -305,64 +331,70 @@ private: public: /* Constructors */ - constexpr expected(const value_type& v) : base_type(v){ + constexpr expected(const value_type& v) + : base_type(v) { //Nothing else to init } - constexpr expected(value_type&& v) : base_type(std::forward(v)){ + constexpr expected(value_type&& v) + : base_type(std::forward(v)) { //Nothing else to init } - expected(const expected& rhs) : base_type(only_set_valid, rhs.valid()) { - if(rhs.valid()){ + expected(const expected& rhs) + : base_type(only_set_valid, rhs.valid()) { + if (rhs.valid()) { ::new (value_ptr()) value_type(rhs.contained_value()); } else { ::new (error_ptr()) error_type(rhs.contained_error()); } } - expected(expected&& rhs) : base_type(only_set_valid, rhs.valid()) { - if(rhs.valid()){ + expected(expected&& rhs) + : base_type(only_set_valid, rhs.valid()) { + if (rhs.valid()) { new (value_ptr()) value_type(std::move(rhs.contained_value())); } else { new (error_ptr()) error_type(std::move(rhs.contained_error())); } } - expected(const exceptional& e) : base_type(e) { + expected(const exceptional& e) + : base_type(e) { //Nothing else to init } - expected() : base_type() {} + expected() + : base_type() {} ~expected() = default; /* Operators */ - expected& operator=(const expected& rhs){ + expected& operator=(const expected& rhs) { this_type(rhs).swap(*this); return *this; } - expected& operator=(expected&& rhs){ + expected& operator=(expected&& rhs) { this_type(std::move(rhs)).swap(*this); return *this; } - expected& operator=(const value_type& v){ + expected& operator=(const value_type& v) { this_type(v).swap(*this); return *this; } - expected& operator=(value_type&& v){ + expected& operator=(value_type&& v) { this_type(std::move(v)).swap(*this); return *this; } /* Swap */ - void swap(expected& rhs){ - if (valid()){ - if (rhs.valid()){ + void swap(expected& rhs) { + if (valid()) { + if (rhs.valid()) { std::swap(contained_value(), rhs.contained_value()); } else { error_type t = std::move(rhs.contained_error()); @@ -371,7 +403,7 @@ public: std::swap(contained_has_value(), rhs.contained_has_value()); } } else { - if (rhs.valid()){ + if (rhs.valid()) { rhs.swap(*this); } else { std::swap(contained_error(), rhs.contained_error()); @@ -393,11 +425,11 @@ public: return contained_value(); } - constexpr const value_type& operator*() const { + constexpr const value_type& operator*() const { return contained_value(); } - value_type& operator*(){ + value_type& operator*() { return contained_value(); } @@ -405,7 +437,7 @@ public: return value_ptr(); } - value_type* operator->(){ + value_type* operator->() { return value_ptr(); } @@ -422,10 +454,10 @@ public: } }; -template -struct expected : expected_base { - typedef void value_type; - typedef E error_type; +template +struct expected : expected_base { + using value_type = void; ///< The value type + using error_type = E; ///< The error type typedef expected this_type; typedef expected_base base_type; @@ -439,7 +471,7 @@ private: return static_addressof(base_type::storage.error); } - constexpr const bool& contained_has_value() const& { + constexpr const bool& contained_has_value() const & { return base_type::has_value; } @@ -451,7 +483,7 @@ private: return std::move(base_type::has_value); } - constexpr const error_type& contained_error() const& { + constexpr const error_type& contained_error() const & { return base_type::storage.error; } @@ -466,47 +498,51 @@ private: public: /* Constructors */ - expected(const expected& rhs) : base_type(only_set_valid, rhs.valid()) { - if(!rhs.valid()){ + expected(const expected& rhs) + : base_type(only_set_valid, rhs.valid()) { + if (!rhs.valid()) { ::new (error_ptr()) error_type(rhs.contained_error()); } } - expected(expected&& rhs) : base_type(only_set_valid, rhs.valid()) { - if(!rhs.valid()){ + expected(expected&& rhs) + : base_type(only_set_valid, rhs.valid()) { + if (!rhs.valid()) { new (error_ptr()) error_type(std::move(rhs.contained_error())); } } - expected(const exceptional& e) : base_type(e) { + expected(const exceptional& e) + : base_type(e) { //Nothing else to init } - expected() : base_type() {} + expected() + : base_type() {} ~expected() = default; /* Operators */ - expected& operator=(const expected& rhs){ + expected& operator=(const expected& rhs) { this_type(rhs).swap(*this); return *this; } - expected& operator=(expected&& rhs){ + expected& operator=(expected&& rhs) { this_type(std::move(rhs)).swap(*this); return *this; } /* Swap */ - void swap(expected& rhs){ - if (valid()){ - if (!rhs.valid()){ + void swap(expected& rhs) { + if (valid()) { + if (!rhs.valid()) { error_type t = std::move(rhs.contained_error()); new (error_ptr()) error_type(t); } } else { - if (!rhs.valid()){ + if (!rhs.valid()) { std::swap(contained_error(), rhs.contained_error()); } } @@ -535,41 +571,41 @@ public: } }; -template -inline expected make_expected(T&& v){ +template +inline expected make_expected(T&& v) { return expected(std::forward(v)); } -template -inline expected make_expected(const T& v){ +template +inline expected make_expected(const T& v) { return expected(v); } -template -inline expected make_expected_from_error(E v){ +template +inline expected make_expected_from_error(E v) { return expected(exceptional(v)); } -template -inline expected make_expected_from_error(E v){ +template +inline expected make_expected_from_error(E v) { return expected(exceptional(v)); } -template -inline expected make_unexpected(E v){ +template +inline expected make_unexpected(E v) { return expected(exceptional(v)); } -template -inline expected make_expected_zero(E v){ - if(v){ +template +inline expected make_expected_zero(E v) { + if (v) { return expected(exceptional(v)); } else { return expected(); } } -inline expected make_expected(){ +inline expected make_expected() { return expected(); }