From 7a191f97ce0c222566973f9e18b43036844ecf3c Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 11 Jun 2022 22:38:10 +0200 Subject: refactor!: simplify tagging --- examples/basic/src/bootstrap.cpp | 4 +-- examples/basic/src/interfaces/enemy.hpp | 8 ++--- include/yacppdic/container.hpp | 12 ++++--- include/yacppdic/detail/auto_wirable-impl.hpp | 2 +- include/yacppdic/detail/container-impl.hpp | 17 +++++----- .../yacppdic/detail/internal/object_identifier.hpp | 8 +++-- include/yacppdic/tagging.hpp | 38 ++++++++++++++++++++-- 7 files changed, 62 insertions(+), 27 deletions(-) diff --git a/examples/basic/src/bootstrap.cpp b/examples/basic/src/bootstrap.cpp index ac81bd9..78c5a37 100644 --- a/examples/basic/src/bootstrap.cpp +++ b/examples/basic/src/bootstrap.cpp @@ -27,7 +27,7 @@ auto bootstrap() -> yacppdic::Container return std::make_unique(65); } ) - .when_tagged(SMALL_ENEMY_TAG); + .when_tagged(); container.bind() .to_factory( @@ -36,7 +36,7 @@ auto bootstrap() -> yacppdic::Container return std::make_unique(130); } ) - .when_tagged(BIG_ENEMY_TAG); + .when_tagged(); return container; } diff --git a/examples/basic/src/interfaces/enemy.hpp b/examples/basic/src/interfaces/enemy.hpp index 5ad7d90..d0b6337 100644 --- a/examples/basic/src/interfaces/enemy.hpp +++ b/examples/basic/src/interfaces/enemy.hpp @@ -1,14 +1,12 @@ #pragma once #include +#include #include -// NOLINTNEXTLINE(modernize-avoid-c-arrays, cppcoreguidelines-avoid-c-arrays) -constexpr char SMALL_ENEMY_TAG[] = "small"; - -// NOLINTNEXTLINE(modernize-avoid-c-arrays, cppcoreguidelines-avoid-c-arrays) -constexpr char BIG_ENEMY_TAG[] = "big"; +constexpr yacppdic::Tag SMALL_ENEMY_TAG = "small"; +constexpr yacppdic::Tag BIG_ENEMY_TAG = "big"; // NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) class IEnemy diff --git a/include/yacppdic/container.hpp b/include/yacppdic/container.hpp index 0003520..6191fed 100644 --- a/include/yacppdic/container.hpp +++ b/include/yacppdic/container.hpp @@ -5,6 +5,7 @@ #include "yacppdic/concepts.hpp" #include "yacppdic/detail/internal/object_identifier.hpp" #include "yacppdic/factory.hpp" +#include "yacppdic/tagging.hpp" #include #include @@ -34,7 +35,8 @@ template class BindingWhen { public: - void when_tagged(std::string_view tag) noexcept; + template + void when_tagged() noexcept; private: internal::Bindings &_bindings; @@ -85,13 +87,13 @@ public: requires IsFactory auto get() const noexcept -> AFactory; - template + template requires Abstract - auto get_tagged(const char *tag) const noexcept -> std::unique_ptr; + auto get_tagged() const noexcept -> std::unique_ptr; - template + template requires IsFactory - auto get_tagged(const char *tag) const noexcept -> AFactory; + auto get_tagged() const noexcept -> AFactory; private: internal::Bindings _bindings; diff --git a/include/yacppdic/detail/auto_wirable-impl.hpp b/include/yacppdic/detail/auto_wirable-impl.hpp index e81f34c..a6b1506 100644 --- a/include/yacppdic/detail/auto_wirable-impl.hpp +++ b/include/yacppdic/detail/auto_wirable-impl.hpp @@ -20,7 +20,7 @@ auto AutoWirable::_get_dependency( const Container &container ) noexcept { - return container.get_tagged(Dependency::get_tag()); + return container.get_tagged(); } template diff --git a/include/yacppdic/detail/container-impl.hpp b/include/yacppdic/detail/container-impl.hpp index 7480dee..5d74616 100644 --- a/include/yacppdic/detail/container-impl.hpp +++ b/include/yacppdic/detail/container-impl.hpp @@ -20,13 +20,14 @@ BindingWhen::BindingWhen( } template -void BindingWhen::when_tagged(std::string_view tag) noexcept +template +void BindingWhen::when_tagged() noexcept { auto wrapped = _bindings.at(_object_identifier); _bindings.erase(_object_identifier); - auto tagged_object_identifier = internal::ObjectIdentifier::create(tag); + auto tagged_object_identifier = internal::ObjectIdentifier::create(); _bindings[tagged_object_identifier] = wrapped; } @@ -120,11 +121,11 @@ auto Container::get() const noexcept -> AFactory return wrapper->get(); } -template +template requires Abstract -auto Container::get_tagged(const char *tag) const noexcept -> std::unique_ptr +auto Container::get_tagged() const noexcept -> std::unique_ptr { - auto object_identifier = internal::ObjectIdentifier::create(tag); + auto object_identifier = internal::ObjectIdentifier::create(); if (!_bindings.contains(object_identifier)) { @@ -142,11 +143,11 @@ auto Container::get_tagged(const char *tag) const noexcept -> std::unique_ptrget(); } -template +template requires IsFactory -auto Container::get_tagged(const char *tag) const noexcept -> AFactory +auto Container::get_tagged() const noexcept -> AFactory { - auto object_identifier = internal::ObjectIdentifier::create(tag); + auto object_identifier = internal::ObjectIdentifier::create(); if (!_bindings.contains(object_identifier)) { diff --git a/include/yacppdic/detail/internal/object_identifier.hpp b/include/yacppdic/detail/internal/object_identifier.hpp index c82b3e8..bf7aeaf 100644 --- a/include/yacppdic/detail/internal/object_identifier.hpp +++ b/include/yacppdic/detail/internal/object_identifier.hpp @@ -1,5 +1,7 @@ #pragma once +#include "yacppdic/tagging.hpp" + #include #include @@ -30,10 +32,10 @@ public: return static_cast(typeid(Type)); } - template - static auto create(std::string_view tag) noexcept -> ObjectIdentifier + template + static auto create() noexcept -> ObjectIdentifier { - return ObjectIdentifier(typeid(Type), tag); + return ObjectIdentifier(typeid(Type), tag.str()); } private: diff --git a/include/yacppdic/tagging.hpp b/include/yacppdic/tagging.hpp index 012a46a..498ff5d 100644 --- a/include/yacppdic/tagging.hpp +++ b/include/yacppdic/tagging.hpp @@ -1,11 +1,43 @@ #pragma once +#include +#include +#include +#include #include namespace yacppdic { -template +template +// NOLINTNEXTLINE(modernize-avoid-c-arrays, cppcoreguidelines-avoid-c-arrays) +using CStyleConstRefArray = const Value (&)[length]; + +template +class Tag +{ +public: + using Storage = std::array; + + // NOLINTNEXTLINE(google-explicit-constructor) + constexpr Tag(CStyleConstRefArray in_data) noexcept + { + std::copy(std::begin(in_data), std::end(in_data), data.begin()); + } + + [[nodiscard]] constexpr auto str() const noexcept -> std::string_view + { + return data.data(); + } + + // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes) + Storage data{}; +}; + +template +Tag(CStyleConstRefArray) -> Tag; + +template // NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) class Tagged { @@ -16,7 +48,7 @@ public: Tagged(const Tagged &tagged) noexcept = delete; Tagged(Tagged &&tagged) noexcept = delete; - static constexpr auto get_tag() noexcept -> char const * + static constexpr auto get_tag() noexcept -> decltype(tag) { return tag; } @@ -30,7 +62,7 @@ struct is_tagged : public std::false_type // NOLINT(readability-identifier-namin { }; -template +template struct is_tagged> : public std::true_type { }; -- cgit v1.2.3-18-g5258