Add factory
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: toolmode) (push) Failing after 22s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: game) (push) Failing after 21s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: toolmode) (push) Failing after 22s
Bigfoot / Sonarqube (game) (push) Skipped
Bigfoot / Sonarqube (toolmode) (push) Skipped
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: game) (push) Failing after 21s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: toolmode) (push) Failing after 22s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang (Unity Build: ON, Variant: game) (push) Failing after 22s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang (Unity Build: ON, Variant: toolmode) (push) Failing after 20s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: game) (push) Failing after 21s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: game) (push) Failing after 22s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: toolmode) (push) Failing after 21s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang (Unity Build: ON, Variant: toolmode) (push) Failing after 20s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: game) (push) Failing after 22s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: toolmode) (push) Failing after 23s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: game) (push) Failing after 23s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: toolmode) (push) Failing after 22s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: game) (push) Failing after 20s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang (Unity Build: ON, Variant: game) (push) Failing after 21s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: toolmode) (push) Failing after 22s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang (Unity Build: ON, Variant: game) (push) Failing after 20s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: toolmode) (push) Failing after 22s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang (Unity Build: ON, Variant: toolmode) (push) Failing after 22s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: game) (push) Failing after 21s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: game) (push) Failing after 19s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: toolmode) (push) Failing after 20s
Bigfoot / Clang Format Checks (push) Failing after 12s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: toolmode) (push) Failing after 22s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: game) (push) Failing after 21s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: toolmode) (push) Failing after 22s
Bigfoot / Sonarqube (game) (push) Skipped
Bigfoot / Sonarqube (toolmode) (push) Skipped
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: game) (push) Failing after 21s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: toolmode) (push) Failing after 22s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang (Unity Build: ON, Variant: game) (push) Failing after 22s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang (Unity Build: ON, Variant: toolmode) (push) Failing after 20s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: game) (push) Failing after 21s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: game) (push) Failing after 22s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: toolmode) (push) Failing after 21s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang (Unity Build: ON, Variant: toolmode) (push) Failing after 20s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: game) (push) Failing after 22s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: toolmode) (push) Failing after 23s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: game) (push) Failing after 23s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: toolmode) (push) Failing after 22s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: game) (push) Failing after 20s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang (Unity Build: ON, Variant: game) (push) Failing after 21s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: toolmode) (push) Failing after 22s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang (Unity Build: ON, Variant: game) (push) Failing after 20s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: toolmode) (push) Failing after 22s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang (Unity Build: ON, Variant: toolmode) (push) Failing after 22s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: game) (push) Failing after 21s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: game) (push) Failing after 19s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: toolmode) (push) Failing after 20s
Bigfoot / Clang Format Checks (push) Failing after 12s
This commit is contained in:
@@ -0,0 +1,75 @@
|
|||||||
|
/*********************************************************************
|
||||||
|
* \file Factory.hpp
|
||||||
|
*
|
||||||
|
* \author Romain BOULLARD
|
||||||
|
* \date September 2026
|
||||||
|
*********************************************************************/
|
||||||
|
#ifndef BIGFOOT_UTILS_FACTORY_HPP
|
||||||
|
#define BIGFOOT_UTILS_FACTORY_HPP
|
||||||
|
#include <EASTL/unique_ptr.h>
|
||||||
|
#include <EASTL/utility.h>
|
||||||
|
#include <ankerl/unordered_dense.h>
|
||||||
|
|
||||||
|
namespace Bigfoot
|
||||||
|
{
|
||||||
|
template<class TYPE, class KEY, class CREATEFUNCTION>
|
||||||
|
class Factory
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Register a key inside the factory
|
||||||
|
*
|
||||||
|
* \param p_key The key
|
||||||
|
* \param p_function The create function
|
||||||
|
* \return True if the key has been registered, false if it was already registered
|
||||||
|
*/
|
||||||
|
[[nodiscard]]
|
||||||
|
bool Register(const KEY p_key, CREATEFUNCTION&& p_function)
|
||||||
|
{
|
||||||
|
if (m_createFunctions.find(p_key) == m_createFunctions.end())
|
||||||
|
{
|
||||||
|
m_createFunctions.emplace(p_key, eastl::move(p_function));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an instance from the factory
|
||||||
|
*
|
||||||
|
* \param p_key The key to create. Needs to have been registered with Register()
|
||||||
|
* \param p_args Arguments for the creation
|
||||||
|
* \return The created instance. nullptr is returned if the key has not been registered
|
||||||
|
*/
|
||||||
|
template<typename... ARGS>
|
||||||
|
[[nodiscard]]
|
||||||
|
eastl::unique_ptr<TYPE> Create(const KEY p_key, ARGS&&... p_args) const
|
||||||
|
{
|
||||||
|
if (const auto createFunction = m_createFunctions.find(p_key); createFunction != m_createFunctions.end())
|
||||||
|
{
|
||||||
|
return createFunction->second(eastl::forward<ARGS>(p_args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove a key and its associated create function from the factory
|
||||||
|
*
|
||||||
|
* \param p_key The key to remove
|
||||||
|
*/
|
||||||
|
void Remove(const KEY p_key)
|
||||||
|
{
|
||||||
|
m_createFunctions.erase(p_key);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
/**
|
||||||
|
* The mapping between keys and create functions
|
||||||
|
*/
|
||||||
|
ankerl::unordered_dense::map<KEY, CREATEFUNCTION> m_createFunctions;
|
||||||
|
};
|
||||||
|
} // namespace Bigfoot
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
/*********************************************************************
|
||||||
|
* \file Factory.cpp
|
||||||
|
*
|
||||||
|
* \author Romain BOULLARD
|
||||||
|
* \date September 2026
|
||||||
|
*********************************************************************/
|
||||||
|
#include <Utils/Factory.hpp>
|
||||||
|
|
||||||
|
#include <EASTL/functional.h>
|
||||||
|
#include <EASTL/unique_ptr.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
namespace Bigfoot
|
||||||
|
{
|
||||||
|
class Widget
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~Widget() = default;
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
virtual std::uint32_t GetValue() const = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
class WidgetA final: public Widget
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit WidgetA(const std::uint32_t p_value):
|
||||||
|
m_value(p_value)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
std::uint32_t GetValue() const override
|
||||||
|
{
|
||||||
|
return m_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::uint32_t m_value;
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class WidgetKeys
|
||||||
|
{
|
||||||
|
WidgetA
|
||||||
|
};
|
||||||
|
|
||||||
|
using WidgetFactory = Factory<Widget, WidgetKeys, eastl::function<eastl::unique_ptr<Widget>(std::uint32_t)>>;
|
||||||
|
|
||||||
|
/****************************************************************************************/
|
||||||
|
|
||||||
|
class FactoryFixture: public ::testing::Test
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
static eastl::unique_ptr<Widget> CreateWidgetA(const std::uint32_t p_value)
|
||||||
|
{
|
||||||
|
return eastl::make_unique<WidgetA>(p_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
WidgetFactory m_factory;
|
||||||
|
};
|
||||||
|
|
||||||
|
/****************************************************************************************/
|
||||||
|
|
||||||
|
TEST_F(FactoryFixture, Register_ShouldReturnTrueOnSuccess)
|
||||||
|
{
|
||||||
|
EXPECT_TRUE(m_factory.Register(WidgetKeys::WidgetA, &CreateWidgetA));
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************************/
|
||||||
|
|
||||||
|
TEST_F(FactoryFixture, Register_ShouldReturnFalseOnKeyDuplication)
|
||||||
|
{
|
||||||
|
std::ignore = m_factory.Register(WidgetKeys::WidgetA, &CreateWidgetA);
|
||||||
|
|
||||||
|
EXPECT_FALSE(m_factory.Register(WidgetKeys::WidgetA, &CreateWidgetA));
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************************/
|
||||||
|
|
||||||
|
TEST_F(FactoryFixture, Create_ShouldReturnAValidInstanceIfKeyIsRegistered)
|
||||||
|
{
|
||||||
|
std::ignore = m_factory.Register(WidgetKeys::WidgetA, &CreateWidgetA);
|
||||||
|
|
||||||
|
const eastl::unique_ptr<Widget> widget = m_factory.Create(WidgetKeys::WidgetA, 42);
|
||||||
|
|
||||||
|
EXPECT_EQ(widget->GetValue(), 42);
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************************/
|
||||||
|
|
||||||
|
TEST_F(FactoryFixture, Create_ShouldReturnNullptrIfKeyIsNotRegistered)
|
||||||
|
{
|
||||||
|
EXPECT_EQ(m_factory.Create(WidgetKeys::WidgetA, 42), nullptr);
|
||||||
|
}
|
||||||
|
} // namespace Bigfoot
|
||||||
Reference in New Issue
Block a user