Skip to content

Commit

Permalink
Make Ichor::Dependency trivially default constructible, for optimal u…
Browse files Browse the repository at this point in the history
…sage with StaticVector
  • Loading branch information
Oipo committed Feb 22, 2024
1 parent aa7966f commit 1e58222
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 12 deletions.
8 changes: 6 additions & 2 deletions include/ichor/dependency_management/Dependency.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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>, "Dependency is required to be trivially default constructible");
static_assert(std::is_trivially_destructible_v<Dependency>, "Dependency is required to be trivially destructible");
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IFaces>(), typeName<IFaces>(), DependencyFlags::NONE, false),...);
(_interfaces.emplace_back(typeNameHash<IFaces>(), typeName<IFaces>().data(), DependencyFlags::NONE, false),...);
}

~DependencyLifecycleManager() final {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Ichor::Detail {
class DependencyManagerLifecycleManager final : public ILifecycleManager {
public:
explicit DependencyManagerLifecycleManager(DependencyManager *dm) : _dm(dm) {
_interfaces.emplace_back(typeNameHash<DependencyManager>(), typeName<DependencyManager>(), DependencyFlags::NONE, false);
_interfaces.emplace_back(typeNameHash<DependencyManager>(), typeName<DependencyManager>().data(), DependencyFlags::NONE, false);
}

~DependencyManagerLifecycleManager() final = default;
Expand Down
4 changes: 2 additions & 2 deletions include/ichor/dependency_management/DependencyRegister.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Ichor {
#endif

_registrations.emplace(typeNameHash<Interface>(), std::make_tuple(
Dependency{typeNameHash<Interface>(), typeName<Interface>(), flags, 0},
Dependency{typeNameHash<Interface>(), typeName<Interface>().data(), flags, 0},
std::function<void(NeverNull<void*>, IService&)>{[svc](NeverNull<void*> dep, IService& isvc){ svc->addDependencyInstance(*reinterpret_cast<Interface*>(dep.get()), isvc); }},
std::function<void(NeverNull<void*>, IService&)>{[svc](NeverNull<void*> dep, IService& isvc){ svc->removeDependencyInstance(*reinterpret_cast<Interface*>(dep.get()), isvc); }},
std::move(props)));
Expand All @@ -34,7 +34,7 @@ namespace Ichor {
static_assert(!DerivedTemplated<Interface, AdvancedService>, "Interface needs to be a non-service class.");

_registrations.emplace(typeNameHash<Interface>(), std::make_tuple(
Dependency{typeNameHash<Interface>(), typeName<Interface>(), DependencyFlags::REQUIRED, 0},
Dependency{typeNameHash<Interface>(), typeName<Interface>().data(), DependencyFlags::REQUIRED, 0},
std::function<void(NeverNull<void*>, IService&)>{[svc](NeverNull<void*> dep, IService& isvc){ svc->template addDependencyInstance<Interface>(reinterpret_cast<Interface*>(dep.get()), &isvc); }},
std::function<void(NeverNull<void*>, IService&)>{[svc](NeverNull<void*> dep, IService& isvc){ svc->template removeDependencyInstance<Interface>(reinterpret_cast<Interface*>(dep.get()), &isvc); }},
tl::optional<Properties>{}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Ichor::Detail {
class IServiceInterestedLifecycleManager final : public ILifecycleManager {
public:
IServiceInterestedLifecycleManager(IService *self) : _self(self) {
_interfaces.emplace_back(typeNameHash<IService>(), typeName<IService>(), DependencyFlags::NONE, false);
_interfaces.emplace_back(typeNameHash<IService>(), typeName<IService>().data(), DependencyFlags::NONE, false);
}
~IServiceInterestedLifecycleManager() final = default;

Expand Down
4 changes: 2 additions & 2 deletions include/ichor/dependency_management/LifecycleManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ namespace Ichor::Detail {
public:
template <typename U = ServiceType> requires RequestsProperties<U>
explicit LifecycleManager(Properties&& properties) : _interfaces(), _service(std::forward<Properties>(properties)) {
(_interfaces.emplace_back(typeNameHash<IFaces>(), typeName<IFaces>(), DependencyFlags::NONE, false),...);
(_interfaces.emplace_back(typeNameHash<IFaces>(), typeName<IFaces>().data(), DependencyFlags::NONE, false),...);
}

template <typename U = ServiceType> requires (!RequestsProperties<U>)
explicit LifecycleManager(Properties&& properties) : _interfaces(), _service() {
(_interfaces.emplace_back(typeNameHash<IFaces>(), typeName<IFaces>(), DependencyFlags::NONE, false),...);
(_interfaces.emplace_back(typeNameHash<IFaces>(), typeName<IFaces>().data(), DependencyFlags::NONE, false),...);
_service.setProperties(std::forward<Properties>(properties));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Ichor::Detail {
class QueueLifecycleManager final : public ILifecycleManager {
public:
explicit QueueLifecycleManager(IEventQueue *q) : _q(q) {
_interfaces.emplace_back(typeNameHash<IEventQueue>(), typeName<IEventQueue>(), DependencyFlags::NONE, false);
_interfaces.emplace_back(typeNameHash<IEventQueue>(), typeName<IEventQueue>().data(), DependencyFlags::NONE, false);
}

~QueueLifecycleManager() final = default;
Expand Down
2 changes: 0 additions & 2 deletions src/ichor/DependencyManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ namespace backward {
}
#endif

static_assert(!Ichor::Detail::is_sufficiently_trivial<Ichor::Dependency>, "Ichor::Dependency not sufficiently non-trivial");

Ichor::DependencyManager::DependencyManager(IEventQueue *eventQueue) : _eventQueue(eventQueue) {
auto qlm = std::make_unique<Detail::QueueLifecycleManager>(_eventQueue);
_services.emplace(qlm->serviceId(), std::move(qlm));
Expand Down

0 comments on commit 1e58222

Please sign in to comment.