From b83e94a05efd4dc4d0836bbb59cb500d0c0b219a Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 3 May 2022 20:07:20 +0200 Subject: refactor: clean up factory & compressed pair --- include/yacppdic/detail/factory-impl.hpp | 78 +++++++++-- .../detail/internal/compressed_pair-impl.hpp | 98 ++++++++++---- .../yacppdic/detail/internal/compressed_pair.hpp | 148 ++++++++------------- include/yacppdic/detail/internal/factory_base.hpp | 24 ++++ include/yacppdic/detail/internal/tuple_indices.hpp | 39 ++++++ include/yacppdic/factory.hpp | 133 ++++++------------ 6 files changed, 297 insertions(+), 223 deletions(-) create mode 100644 include/yacppdic/detail/internal/factory_base.hpp create mode 100644 include/yacppdic/detail/internal/tuple_indices.hpp diff --git a/include/yacppdic/detail/factory-impl.hpp b/include/yacppdic/detail/factory-impl.hpp index 3deede2..c6da207 100644 --- a/include/yacppdic/detail/factory-impl.hpp +++ b/include/yacppdic/detail/factory-impl.hpp @@ -5,18 +5,19 @@ namespace yacppdic { -template -Factory::Factory(const Factory &factory) : _functor(factory._functor) +template +Factory::Factory(const Factory &factory) noexcept + : _functor(factory._functor) { } -template +template Factory::Factory(Factory &&factory) noexcept : _functor(std::move(factory._functor)) { } -template +template auto Factory::operator=(Factory &&factory) noexcept -> Factory & { @@ -24,7 +25,7 @@ auto Factory::operator=(Factory &&factory) noexcept return *this; } -template +template auto Factory::operator=(std::nullptr_t) noexcept -> Factory & { @@ -32,39 +33,88 @@ auto Factory::operator=(std::nullptr_t) noexcept return *this; } -template -Factory::~Factory() = default; - -template +template Factory::operator bool() const noexcept { return static_cast(_functor); } -template -auto Factory::operator()(Args... args) const -> Return +template +auto Factory::operator()(Args... args) const noexcept -> Return { return _functor(std::forward(args)...); } -template +template auto Factory::target_type() const noexcept -> const std::type_info & { return _functor.target_type(); } -template +template template auto Factory::target() noexcept -> Tp * { return static_cast(_functor.template target()); } -template +template template auto Factory::target() const noexcept -> const Tp * { return _functor.template target(); } +template +inline auto operator==(const Factory &factory, std::nullptr_t) noexcept + -> bool +{ + return !factory; +} + +template +inline auto operator==(std::nullptr_t, const Factory &factory) noexcept + -> bool +{ + return !factory; +} + +template +inline auto operator!=(const Factory &factory, std::nullptr_t) noexcept + -> bool +{ + return static_cast(factory); +} + +template +inline auto operator!=(std::nullptr_t, const Factory &factory) noexcept + -> bool +{ + return static_cast(factory); +} + +} // namespace yacppdic + +template +constexpr auto not_null(Function const & /*unused*/) -> bool +{ + return true; +} + +template +constexpr auto not_null(Function *function_ptr) -> bool +{ + return function_ptr; +} + +template +constexpr auto not_null(Return Class::*method_ptr) -> bool +{ + return method_ptr; +} + +template +constexpr auto not_null(yacppdic::Factory const &factory) -> bool +{ + return !!factory; } diff --git a/include/yacppdic/detail/internal/compressed_pair-impl.hpp b/include/yacppdic/detail/internal/compressed_pair-impl.hpp index 0e2a322..9a458e4 100644 --- a/include/yacppdic/detail/internal/compressed_pair-impl.hpp +++ b/include/yacppdic/detail/internal/compressed_pair-impl.hpp @@ -5,42 +5,94 @@ namespace yacppdic::internal { -template -constexpr CompressedPairElement::CompressedPairElement( - DefaultInitTag /*default_init_tag*/ -) +template +requires NotSameAs +template +requires std::default_initializable && std::default_initializable +constexpr CompressedPair::CompressedPair() noexcept + : BaseOne(ValueInitTag()), BaseTwo(ValueInitTag()) { } -template -constexpr CompressedPairElement::CompressedPairElement( - ValueInitTag /*value_init_tag*/ -) - : _value() +template +requires NotSameAs +template +constexpr CompressedPair::CompressedPair( + FirstValue &&first_value, + SecondValue &&second_value +) noexcept + : BaseOne(std::forward(first_value)), + BaseTwo(std::forward(second_value)) { } -template -template -constexpr CompressedPairElement::CompressedPairElement( - std::piecewise_construct_t /*tag_types*/, - std::tuple args, - TupleIndices /*indices*/ -) - : _value(std::forward(std::get(args))...) +template +requires NotSameAs +template +constexpr CompressedPair::CompressedPair( + std::piecewise_construct_t piecewise_construct, + std::tuple first_args, + std::tuple second_args +) noexcept + : BaseOne( + piecewise_construct, + std::move(first_args), + typename MakeTupleIndices::type() + ), + BaseTwo( + piecewise_construct, + std::move(second_args), + typename MakeTupleIndices::type() + ) { } -template -auto CompressedPairElement::get() noexcept -> ValueReference +template +requires NotSameAs +auto CompressedPair::first() noexcept -> + typename BaseOne::ValueReference { - return _value; + return static_cast(*this).get(); } -template -auto CompressedPairElement::get() const noexcept -> ConstValueReference +template +requires NotSameAs +auto CompressedPair::first() const noexcept -> + typename BaseOne::ConstValueReference { - return _value; + return static_cast(*this).get(); +} + +template +requires NotSameAs +auto CompressedPair::second() noexcept -> + typename BaseTwo::ValueReference +{ + return static_cast(*this).get(); +} + +template +requires NotSameAs +auto CompressedPair::second() const noexcept -> + typename BaseTwo::ConstValueReference +{ + return static_cast(*this).get(); +} + +template +requires NotSameAs +constexpr auto CompressedPair::get_first_base(CompressedPair *pair +) noexcept -> BaseOne * +{ + return static_cast(pair); +} + +template +requires NotSameAs +constexpr auto CompressedPair::get_second_base(CompressedPair *pair +) noexcept -> BaseTwo * +{ + return static_cast(pair); } } // namespace yacppdic::internal diff --git a/include/yacppdic/detail/internal/compressed_pair.hpp b/include/yacppdic/detail/internal/compressed_pair.hpp index 3c832c5..7d98bbd 100644 --- a/include/yacppdic/detail/internal/compressed_pair.hpp +++ b/include/yacppdic/detail/internal/compressed_pair.hpp @@ -1,6 +1,7 @@ #pragma once #include "yacppdic/detail/internal/misc_concepts.hpp" +#include "yacppdic/detail/internal/tuple_indices.hpp" #include #include @@ -11,104 +12,94 @@ namespace yacppdic::internal { // Tag used to default initialize one or both of the pair's elements. -struct DefaultInitTag +class DefaultInitTag { }; -struct ValueInitTag +class ValueInitTag { }; -template -struct TupleIndices -{ -}; - -template -struct IntegerSequence -{ - template