From 1e58222952250d2f033f9305eb07f5a760190fee Mon Sep 17 00:00:00 2001 From: Michael de Lang Date: Thu, 22 Feb 2024 22:13:37 +0100 Subject: [PATCH] Make Ichor::Dependency trivially default constructible, for optimal usage with StaticVector --- include/ichor/dependency_management/Dependency.h | 8 ++++++-- .../dependency_management/DependencyLifecycleManager.h | 2 +- .../DependencyManagerLifecycleManager.h | 2 +- include/ichor/dependency_management/DependencyRegister.h | 4 ++-- .../IServiceInterestedLifecycleManager.h | 2 +- include/ichor/dependency_management/LifecycleManager.h | 4 ++-- .../ichor/dependency_management/QueueLifecycleManager.h | 2 +- src/ichor/DependencyManager.cpp | 2 -- 8 files changed, 14 insertions(+), 12 deletions(-) diff --git a/include/ichor/dependency_management/Dependency.h b/include/ichor/dependency_management/Dependency.h index 8d3f6218..f2dfea55 100644 --- a/include/ichor/dependency_management/Dependency.h +++ b/include/ichor/dependency_management/Dependency.h @@ -19,7 +19,8 @@ namespace Ichor { } struct Dependency { - Dependency(uint64_t _interfaceNameHash, std::string_view _interfaceName, DependencyFlags _flags, uint64_t _satisfied) noexcept : interfaceNameHash(_interfaceNameHash), interfaceName(_interfaceName), flags(_flags), satisfied(_satisfied) {} + Dependency() noexcept = default; + Dependency(uint64_t _interfaceNameHash, char const *_interfaceName, DependencyFlags _flags, uint64_t _satisfied) noexcept : interfaceNameHash(_interfaceNameHash), interfaceName(_interfaceName), flags(_flags), satisfied(_satisfied) {} Dependency(const Dependency &other) noexcept = default; Dependency(Dependency &&other) noexcept = default; Dependency& operator=(const Dependency &other) noexcept = default; @@ -29,10 +30,13 @@ namespace Ichor { } uint64_t interfaceNameHash; - std::string_view interfaceName; + char const *interfaceName; DependencyFlags flags; uint64_t satisfied; }; + + static_assert(std::is_trivially_default_constructible_v, "Dependency is required to be trivially default constructible"); + static_assert(std::is_trivially_destructible_v, "Dependency is required to be trivially destructible"); } diff --git a/include/ichor/dependency_management/DependencyLifecycleManager.h b/include/ichor/dependency_management/DependencyLifecycleManager.h index 19ef1914..3c8c3252 100644 --- a/include/ichor/dependency_management/DependencyLifecycleManager.h +++ b/include/ichor/dependency_management/DependencyLifecycleManager.h @@ -11,7 +11,7 @@ namespace Ichor::Detail { class DependencyLifecycleManager final : public ILifecycleManager { public: explicit DependencyLifecycleManager(Properties&& properties) : _interfaces(), _registry(), _service(_registry, std::move(properties)) { - (_interfaces.emplace_back(typeNameHash(), typeName(), DependencyFlags::NONE, false),...); + (_interfaces.emplace_back(typeNameHash(), typeName().data(), DependencyFlags::NONE, false),...); } ~DependencyLifecycleManager() final { diff --git a/include/ichor/dependency_management/DependencyManagerLifecycleManager.h b/include/ichor/dependency_management/DependencyManagerLifecycleManager.h index ac77cbfc..708cb621 100644 --- a/include/ichor/dependency_management/DependencyManagerLifecycleManager.h +++ b/include/ichor/dependency_management/DependencyManagerLifecycleManager.h @@ -8,7 +8,7 @@ namespace Ichor::Detail { class DependencyManagerLifecycleManager final : public ILifecycleManager { public: explicit DependencyManagerLifecycleManager(DependencyManager *dm) : _dm(dm) { - _interfaces.emplace_back(typeNameHash(), typeName(), DependencyFlags::NONE, false); + _interfaces.emplace_back(typeNameHash(), typeName().data(), DependencyFlags::NONE, false); } ~DependencyManagerLifecycleManager() final = default; diff --git a/include/ichor/dependency_management/DependencyRegister.h b/include/ichor/dependency_management/DependencyRegister.h index c1c955b1..435bdaaf 100644 --- a/include/ichor/dependency_management/DependencyRegister.h +++ b/include/ichor/dependency_management/DependencyRegister.h @@ -22,7 +22,7 @@ namespace Ichor { #endif _registrations.emplace(typeNameHash(), std::make_tuple( - Dependency{typeNameHash(), typeName(), flags, 0}, + Dependency{typeNameHash(), typeName().data(), flags, 0}, std::function, IService&)>{[svc](NeverNull dep, IService& isvc){ svc->addDependencyInstance(*reinterpret_cast(dep.get()), isvc); }}, std::function, IService&)>{[svc](NeverNull dep, IService& isvc){ svc->removeDependencyInstance(*reinterpret_cast(dep.get()), isvc); }}, std::move(props))); @@ -34,7 +34,7 @@ namespace Ichor { static_assert(!DerivedTemplated, "Interface needs to be a non-service class."); _registrations.emplace(typeNameHash(), std::make_tuple( - Dependency{typeNameHash(), typeName(), DependencyFlags::REQUIRED, 0}, + Dependency{typeNameHash(), typeName().data(), DependencyFlags::REQUIRED, 0}, std::function, IService&)>{[svc](NeverNull dep, IService& isvc){ svc->template addDependencyInstance(reinterpret_cast(dep.get()), &isvc); }}, std::function, IService&)>{[svc](NeverNull dep, IService& isvc){ svc->template removeDependencyInstance(reinterpret_cast(dep.get()), &isvc); }}, tl::optional{})); diff --git a/include/ichor/dependency_management/IServiceInterestedLifecycleManager.h b/include/ichor/dependency_management/IServiceInterestedLifecycleManager.h index df9aff41..79f5e024 100644 --- a/include/ichor/dependency_management/IServiceInterestedLifecycleManager.h +++ b/include/ichor/dependency_management/IServiceInterestedLifecycleManager.h @@ -7,7 +7,7 @@ namespace Ichor::Detail { class IServiceInterestedLifecycleManager final : public ILifecycleManager { public: IServiceInterestedLifecycleManager(IService *self) : _self(self) { - _interfaces.emplace_back(typeNameHash(), typeName(), DependencyFlags::NONE, false); + _interfaces.emplace_back(typeNameHash(), typeName().data(), DependencyFlags::NONE, false); } ~IServiceInterestedLifecycleManager() final = default; diff --git a/include/ichor/dependency_management/LifecycleManager.h b/include/ichor/dependency_management/LifecycleManager.h index 97586f13..9fb4edb4 100644 --- a/include/ichor/dependency_management/LifecycleManager.h +++ b/include/ichor/dependency_management/LifecycleManager.h @@ -15,12 +15,12 @@ namespace Ichor::Detail { public: template requires RequestsProperties explicit LifecycleManager(Properties&& properties) : _interfaces(), _service(std::forward(properties)) { - (_interfaces.emplace_back(typeNameHash(), typeName(), DependencyFlags::NONE, false),...); + (_interfaces.emplace_back(typeNameHash(), typeName().data(), DependencyFlags::NONE, false),...); } template requires (!RequestsProperties) explicit LifecycleManager(Properties&& properties) : _interfaces(), _service() { - (_interfaces.emplace_back(typeNameHash(), typeName(), DependencyFlags::NONE, false),...); + (_interfaces.emplace_back(typeNameHash(), typeName().data(), DependencyFlags::NONE, false),...); _service.setProperties(std::forward(properties)); } diff --git a/include/ichor/dependency_management/QueueLifecycleManager.h b/include/ichor/dependency_management/QueueLifecycleManager.h index 417609d2..f905d059 100644 --- a/include/ichor/dependency_management/QueueLifecycleManager.h +++ b/include/ichor/dependency_management/QueueLifecycleManager.h @@ -9,7 +9,7 @@ namespace Ichor::Detail { class QueueLifecycleManager final : public ILifecycleManager { public: explicit QueueLifecycleManager(IEventQueue *q) : _q(q) { - _interfaces.emplace_back(typeNameHash(), typeName(), DependencyFlags::NONE, false); + _interfaces.emplace_back(typeNameHash(), typeName().data(), DependencyFlags::NONE, false); } ~QueueLifecycleManager() final = default; diff --git a/src/ichor/DependencyManager.cpp b/src/ichor/DependencyManager.cpp index b6912185..450a8061 100644 --- a/src/ichor/DependencyManager.cpp +++ b/src/ichor/DependencyManager.cpp @@ -32,8 +32,6 @@ namespace backward { } #endif -static_assert(!Ichor::Detail::is_sufficiently_trivial, "Ichor::Dependency not sufficiently non-trivial"); - Ichor::DependencyManager::DependencyManager(IEventQueue *eventQueue) : _eventQueue(eventQueue) { auto qlm = std::make_unique(_eventQueue); _services.emplace(qlm->serviceId(), std::move(qlm));