Commit f0e2c819 authored by Pascal Palenda's avatar Pascal Palenda
Browse files

Add config to factories

parent fc512d0a
......@@ -115,12 +115,18 @@ namespace ITA
///
typedef std::function<std::unique_ptr<IAudibilityFilter> ( const std::shared_ptr<IAudibilityFilter::AudibilityFilterConfig>& pConfig )> CreateCallback;
///
/// \brief The function signature for a config create function.
///
typedef std::function<std::shared_ptr<IAudibilityFilter::AudibilityFilterConfig> ()> ConfigCreateCallback;
///
/// \brief Register a type of filter to the factory.
/// \param type name of the filter type to be registered.
/// \param createFunction the corresponding create function.
/// \param configCreateFunction the corresponding config create function.
///
static void RegisterFilter ( const std::string& type, CreateCallback createFunction );
static void RegisterFilter ( const std::string& type, CreateCallback createFunction, ConfigCreateCallback configCreateFunction );
///
/// \brief Unregister a type of filter from the factory.
......@@ -135,11 +141,18 @@ namespace ITA
///
static std::unique_ptr<IAudibilityFilter> CreateFilter ( const std::shared_ptr<IAudibilityFilter::AudibilityFilterConfig>& pConfig );
///
/// \brief Factory method for filter configs.
/// \param sType the configuration for the filter.
/// \return a pointer to the created filter config.
///
static std::shared_ptr<IAudibilityFilter::AudibilityFilterConfig> CreateConfig ( const std::string& sType );
private:
///
/// \brief Map containing all creation functions.
///
static std::map<std::string, CreateCallback> m_mFilters;
static std::map<std::string, std::pair<CreateCallback, ConfigCreateCallback>> m_mFilters;
};
} // namespace AudibilityFilter
} // namespace SimulationScheduler
......
......@@ -124,12 +124,18 @@ namespace ITA
///
typedef std::function<std::unique_ptr<IWorkerInterface> ( const std::shared_ptr<IWorkerInterface::WorkerConfig>&, ISchedulerInterface* )> CreateCallback;
///
/// \brief The function signature for a config create function.
///
typedef std::function<std::shared_ptr<IWorkerInterface::WorkerConfig> ( )> ConfigCreateCallback;
///
/// \brief Register a type of worker to the factory.
/// \param type name of the worker type to be registered.
/// \param createFunction the corresponding create function.
/// \param configCreateFunction the corresponding config create function.
///
static void RegisterWorker ( const std::string& type, CreateCallback createFunction );
static void RegisterWorker ( const std::string& type, CreateCallback createFunction, ConfigCreateCallback configCreateFunction );
///
/// \brief Unregister a type of worker from the factory.
......@@ -145,11 +151,18 @@ namespace ITA
///
static std::unique_ptr<IWorkerInterface> CreateWorker ( const std::shared_ptr<IWorkerInterface::WorkerConfig>& pConfig,
ISchedulerInterface* pParent );
///
/// \brief Factory method for filter configs.
/// \param sType the configuration for the filter.
/// \return a pointer to the created filter config.
///
static std::shared_ptr<IWorkerInterface::WorkerConfig> CreateConfig ( const std::string& sType );
private:
///
/// \brief Map containing all creation functions.
///
static std::map<std::string, CreateCallback> m_mWorkers;
static std::map < std::string, std::pair<CreateCallback, ConfigCreateCallback> > m_mWorkers;
};
} // namespace SimulationScheduler
} // namespace ITA
......
......@@ -22,13 +22,21 @@ namespace ITA
{
namespace AudibilityFilter
{
std::map<std::string, CAudibilityFilterFactory::CreateCallback> CAudibilityFilterFactory::m_mFilters = {
{CRateFilter::GetType ( ), CRateFilter::CreateFilter},
{CReverberationRadiusFilter::GetType ( ), CReverberationRadiusFilter::CreateFilter},
{CRotationFilter::GetType ( ), CRotationFilter::CreateFilter},
{CTranslationFilter::GetType ( ), CTranslationFilter::CreateFilter},
{CZoneFilter::GetType ( ), CZoneFilter::CreateFilter},
{CPerceptiveRotationFilter::GetType ( ), CPerceptiveRotationFilter::CreateFilter} };
std::map<std::string, std::pair<CAudibilityFilterFactory::CreateCallback, CAudibilityFilterFactory::ConfigCreateCallback>>
CAudibilityFilterFactory::m_mFilters = {
{CRateFilter::GetType ( ),
{ CRateFilter::CreateFilter, std::make_shared<CRateFilter::RateFilterConfig> } },
{CReverberationRadiusFilter::GetType ( ),
{ CReverberationRadiusFilter::CreateFilter, std::make_shared<CReverberationRadiusFilter::ReverberationRadiusFilterConfig> } },
{CRotationFilter::GetType ( ),
{ CRotationFilter::CreateFilter, std::make_shared<CRotationFilter::RotationFilterConfig> } },
{CTranslationFilter::GetType ( ),
{ CTranslationFilter::CreateFilter, std::make_shared<CTranslationFilter::TranslationFilterConfig> } },
{CZoneFilter::GetType ( ),
{ CZoneFilter::CreateFilter, std::make_shared<CZoneFilter::ZoneFilterConfig> } },
{CPerceptiveRotationFilter::GetType ( ),
{ CPerceptiveRotationFilter::CreateFilter, std::make_shared<CPerceptiveRotationFilter::PerceptiveRotationFilterConfig> } }
};
IAudibilityFilter::AudibilityFilterConfig::AudibilityFilterConfig ( ) : sFilterType ( m_sFilterType )
......@@ -91,9 +99,9 @@ namespace ITA
ITA_EXCEPT_INVALID_PARAMETER ( "The previous state happend before the new update" );
}
void CAudibilityFilterFactory::RegisterFilter ( const std::string& type, CreateCallback createFunction )
void CAudibilityFilterFactory::RegisterFilter ( const std::string& type, CreateCallback createFunction, ConfigCreateCallback configCreateFunction )
{
m_mFilters [type] = createFunction;
m_mFilters [type] = { createFunction, configCreateFunction };
}
void CAudibilityFilterFactory::UnregisterFilter ( const std::string& type )
......@@ -107,7 +115,19 @@ namespace ITA
if ( it != m_mFilters.end ( ) )
{
// call the creation callback to construct this derived type
return it->second ( pConfig );
return it->second.first ( pConfig );
}
return nullptr;
}
std::shared_ptr<IAudibilityFilter::AudibilityFilterConfig> CAudibilityFilterFactory::CreateConfig (
const std::string& sType )
{
auto it = m_mFilters.find ( sType );
if ( it != m_mFilters.end ( ) )
{
// call the creation callback to construct this derived type
return it->second.second ( );
}
return nullptr;
}
......
......@@ -49,26 +49,7 @@ namespace ITA
auto configProperties = oProperty.second.GetPropertyListConstRef ( );
auto type = configProperties.GetValue<std::string> ( audibilityFilterTypeKey );
std::shared_ptr<IAudibilityFilter::AudibilityFilterConfig> filterConfig { };
// todo, this is not nice ... this should be fixed in the future, maybe also a factory. Or with the current Factory.
if ( type == CRateFilter::GetType ( ) )
filterConfig = std::make_shared<CRateFilter::RateFilterConfig> ( );
if ( type == CReverberationRadiusFilter::GetType ( ) )
filterConfig = std::make_shared<CReverberationRadiusFilter::ReverberationRadiusFilterConfig> ( );
if ( type == CRotationFilter::GetType ( ) )
filterConfig = std::make_shared<CRotationFilter::RotationFilterConfig> ( );
if ( type == CTranslationFilter::GetType ( ) )
filterConfig = std::make_shared<CTranslationFilter::TranslationFilterConfig> ( );
if ( type == CZoneFilter::GetType ( ) )
filterConfig = std::make_shared<CZoneFilter::ZoneFilterConfig> ( );
if ( type == CPerceptiveRotationFilter::GetType ( ) )
filterConfig = std::make_shared<CPerceptiveRotationFilter::PerceptiveRotationFilterConfig> ( );
auto filterConfig = CAudibilityFilterFactory::CreateConfig ( type );
filterConfig->Load ( configProperties );
vpFilterConfigs.push_back ( filterConfig );
......
......@@ -51,13 +51,7 @@ namespace ITA
auto configProperties = oProperty.second.GetPropertyListConstRef ( );
auto type = configProperties.GetValue<std::string> ( workerTypeKey );
std::shared_ptr<IWorkerInterface::WorkerConfig> workerConfig { };
// todo, this is not nice ... this should be fixed in the future, maybe also a factory. Or with the current Factory.
#ifdef WITH_RAVEN
if ( type == RoomAcoustics::Raven::CWorkerThread::GetType ( ) )
workerConfig = std::make_shared<RoomAcoustics::Raven::CWorkerThread::WorkerThreadConfig> ( );
#endif
auto workerConfig = CWorkerFactory::CreateConfig ( type );
workerConfig->Load ( configProperties );
vpWorkerConfigs.push_back ( workerConfig );
......
......@@ -11,10 +11,10 @@ namespace ITA
namespace SimulationScheduler
{
// todo This can fail if the name changes .. Fix this/make it bullet proof
std::map<std::string, CWorkerFactory::CreateCallback> CWorkerFactory::m_mWorkers =
std::map<std::string, std::pair<CWorkerFactory::CreateCallback, CWorkerFactory::ConfigCreateCallback>> CWorkerFactory::m_mWorkers =
{
#ifdef WITH_RAVEN
{RoomAcoustics::Raven::CWorkerThread::GetType(), RoomAcoustics::Raven::CWorkerThread::CreateWorker}
{RoomAcoustics::Raven::CWorkerThread::GetType ( ), {RoomAcoustics::Raven::CWorkerThread::CreateWorker, std::make_shared<RoomAcoustics::Raven::CWorkerThread::WorkerThreadConfig> } }
#endif
};
......@@ -59,9 +59,9 @@ namespace ITA
m_pParentScheduler ( pParent )
{ }
void CWorkerFactory::RegisterWorker ( const std::string& type, CreateCallback createFunction )
void CWorkerFactory::RegisterWorker ( const std::string& type, CreateCallback createFunction, ConfigCreateCallback configCreateFunction )
{
m_mWorkers [type] = createFunction;
m_mWorkers [type] = { createFunction, configCreateFunction };
}
void CWorkerFactory::UnregisterWorker ( const std::string& type )
......@@ -75,7 +75,18 @@ namespace ITA
if ( it != m_mWorkers.end ( ) )
{
// call the creation callback to construct this derived type
return it->second ( pConfig, pParent );
return it->second.first ( pConfig, pParent );
}
return nullptr;
}
std::shared_ptr<IWorkerInterface::WorkerConfig> CWorkerFactory::CreateConfig(const std::string& sType)
{
auto it = m_mWorkers.find ( sType );
if ( it != m_mWorkers.end ( ) )
{
// call the creation callback to construct this derived type
return it->second.second ( );
}
return nullptr;
}
......
......@@ -4,6 +4,7 @@
// GTest
#include "gtest/gtest.h"
#include "ITA/simulation_scheduler/audibility_filter/rate_filter.h"
using namespace ITA::SimulationScheduler;
......@@ -11,7 +12,9 @@ using namespace AudibilityFilter;
TEST (FilterFactoryTest, addNewFilterType)
{
CAudibilityFilterFactory::RegisterFilter ( "MockFilter", MockAudibilityFilter::createFilter );
CAudibilityFilterFactory::RegisterFilter ( "MockFilter",
MockAudibilityFilter::createFilter,
std::make_shared<MockAudibilityFilter::MockFilterConfig> );
auto filterConfig = std::make_shared<MockAudibilityFilter::MockFilterConfig> ( );
filterConfig->behaviour = MockAudibilityFilter::Behaviour::nothingAudible;
......@@ -19,4 +22,17 @@ TEST (FilterFactoryTest, addNewFilterType)
const auto filter = CAudibilityFilterFactory::CreateFilter ( filterConfig );
ASSERT_NE ( filter, nullptr );
}
TEST ( FilterFactoryTest, configFactoryTest )
{
CAudibilityFilterFactory::RegisterFilter ( "MockFilter",
MockAudibilityFilter::createFilter,
std::make_shared<MockAudibilityFilter::MockFilterConfig> );
const auto config = dynamic_pointer_cast< MockAudibilityFilter::MockFilterConfig >(CAudibilityFilterFactory::CreateConfig ( "MockFilter" ));
EXPECT_NE ( config, nullptr );
const auto config2 = dynamic_pointer_cast< CRateFilter::RateFilterConfig >( CAudibilityFilterFactory::CreateConfig ( CRateFilter::GetType() ) );
EXPECT_NE ( config2, nullptr );
}
\ No newline at end of file
......@@ -52,7 +52,9 @@ struct CompleteTest : public testing::Test, public ITATimerEventHandler
schedulerConfig->vpWorkerConfigs.push_back ( workerConfig );
CAudibilityFilterFactory::RegisterFilter ( "MockFilter", MockAudibilityFilter::createFilter );
CAudibilityFilterFactory::RegisterFilter ( "MockFilter",
MockAudibilityFilter::createFilter,
std::make_shared<MockAudibilityFilter::MockFilterConfig> );
auto filterConfig = std::make_shared<MockAudibilityFilter::MockFilterConfig>();
filterConfig->behaviour = MockAudibilityFilter::Behaviour::everythingAudible;
......
......@@ -51,7 +51,9 @@ struct FilterNetworkTest : public testing::TestWithParam<FilterNetworkTestParam>
{
const auto param = GetParam ( );
CAudibilityFilterFactory::RegisterFilter ( "MockFilter", MockAudibilityFilter::createFilter );
CAudibilityFilterFactory::RegisterFilter ( "MockFilter",
MockAudibilityFilter::createFilter,
std::make_shared<MockAudibilityFilter::MockFilterConfig> );
const auto filter1 = std::make_shared<MockAudibilityFilter::MockFilterConfig> ( );
filter1->behaviour = param.filter1audible ? MockAudibilityFilter::Behaviour::everythingAudible : MockAudibilityFilter::Behaviour::nothingAudible;
......
......@@ -41,8 +41,10 @@ struct LocalSchedulerTest : public testing::Test
LocalSchedulerTest ( )
{
AudibilityFilter::CAudibilityFilterFactory::RegisterFilter ( "MockFilter", MockAudibilityFilter::createFilter );
CWorkerFactory::RegisterWorker ( "MockWorker", MockWorker::createWorker );
AudibilityFilter::CAudibilityFilterFactory::RegisterFilter ( "MockFilter",
MockAudibilityFilter::createFilter,
std::make_shared<MockAudibilityFilter::MockFilterConfig> );
CWorkerFactory::RegisterWorker ( "MockWorker", MockWorker::createWorker, std::make_shared<MockWorker::MockWorkerConfig> );
auto schedulerConfig = CScheduler::LocalSchedulerConfig ( );
......@@ -72,7 +74,7 @@ struct LocalSchedulerTest : public testing::Test
TEST ( LocalSchedulerCreationTest, WithOneWorker )
{
CWorkerFactory::RegisterWorker ( "MockWorker", MockWorker::createWorker );
CWorkerFactory::RegisterWorker ( "MockWorker", MockWorker::createWorker, std::make_shared<MockWorker::MockWorkerConfig> );
auto schedulerConfig = CScheduler::LocalSchedulerConfig ( );
......@@ -86,7 +88,9 @@ TEST ( LocalSchedulerCreationTest, WithOneWorker )
TEST ( LocalSchedulerCreationTest, WithOneFilter )
{
AudibilityFilter::CAudibilityFilterFactory::RegisterFilter ( "MockFilter", MockAudibilityFilter::createFilter );
AudibilityFilter::CAudibilityFilterFactory::RegisterFilter ( "MockFilter",
MockAudibilityFilter::createFilter,
std::make_shared<MockAudibilityFilter::MockFilterConfig> );
auto schedulerConfig = CScheduler::LocalSchedulerConfig ( );
......
......@@ -22,7 +22,7 @@ struct RavenWorkerThreadTest : testing::Test
RavenWorkerThreadTest ( )
{
// overwrite the correct create function.
CWorkerFactory::RegisterWorker ( CWorkerThread::GetType(), TestRavenWorkerThread::createWorker );
CWorkerFactory::RegisterWorker ( CWorkerThread::GetType(), TestRavenWorkerThread::createWorker, std::make_shared<TestRavenWorkerThread::WorkerThreadConfig> );
scheduler = std::make_unique<MockScheduler> ( );
......@@ -35,7 +35,7 @@ struct RavenWorkerThreadTest : testing::Test
~RavenWorkerThreadTest()
{
// Reset the create function
CWorkerFactory::RegisterWorker ( Raven::CWorkerThread::GetType(), CWorkerThread::CreateWorker );
CWorkerFactory::RegisterWorker ( Raven::CWorkerThread::GetType(), CWorkerThread::CreateWorker, std::make_shared<CWorkerThread::WorkerThreadConfig> );
}
};
......
......@@ -11,7 +11,7 @@ using namespace RoomAcoustics;
TEST (WorkerFactoryTest, addNewWorkerType)
{
CWorkerFactory::RegisterWorker ( "MockWorker", MockWorker::createWorker );
CWorkerFactory::RegisterWorker ( "MockWorker", MockWorker::createWorker, std::make_shared<MockWorker::MockWorkerConfig> );
const auto config = std::make_shared<MockWorker::MockWorkerConfig>();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment