Commit d856ec97 authored by VAN TOLL Wouter's avatar VAN TOLL Wouter

CostFunctionFactory:

- Now ensuring that RegisterAllCostFunctions() works only once.
- Added documentation.
parent f6b3878c
......@@ -19,27 +19,31 @@
** See the file AUTHORS.md for a list of all contributors.
*/
#include "core/costFunctionFactory.h"
#include <core/costFunctionFactory.h>
#include "CostFunctions/FOEAvoidance.h"
#include "CostFunctions/GenericCost.h"
#include "CostFunctions/GoalReachingForce.h"
#include "CostFunctions/Karamouzas.h"
#include "CostFunctions/Moussaid.h"
#include "CostFunctions/ORCA.h"
#include "CostFunctions/Paris.h"
#include "CostFunctions/PLEdestrians.h"
#include "CostFunctions/PowerLaw.h"
#include "CostFunctions/RandomFunction.h"
#include "CostFunctions/RVO.h"
#include "CostFunctions/SocialForcesAvoidance.h"
#include "CostFunctions/TtcaDca.h"
#include "CostFunctions/VanToll.h"
#include <CostFunctions/FOEAvoidance.h>
#include <CostFunctions/GenericCost.h>
#include <CostFunctions/GoalReachingForce.h>
#include <CostFunctions/Karamouzas.h>
#include <CostFunctions/Moussaid.h>
#include <CostFunctions/ORCA.h>
#include <CostFunctions/Paris.h>
#include <CostFunctions/PLEdestrians.h>
#include <CostFunctions/PowerLaw.h>
#include <CostFunctions/RandomFunction.h>
#include <CostFunctions/RVO.h>
#include <CostFunctions/SocialForcesAvoidance.h>
#include <CostFunctions/TtcaDca.h>
#include <CostFunctions/VanToll.h>
CostFunctionFactory::Registry CostFunctionFactory::registry = CostFunctionFactory::Registry();
void CostFunctionFactory::RegisterAllCostFunctions()
{
// do the registration only once
if (!registry.empty())
return;
registerCostFunction<FOEAvoidance>();
registerCostFunction<GenericCost>();
registerCostFunction<GoalReachingForce>();
......
......@@ -28,15 +28,19 @@
#include <string>
#include <iostream>
/// <summary>A static class that can create new instances of cost functions upon request.</summary>
class CostFunctionFactory
{
public:
typedef std::map<std::string, std::function<CostFunction*()>> Registry;
private:
/// <summary>A mapping from names to CostFunction creators.</summary>
static Registry registry;
private:
/// <summary>Adds the given CostFunction class type to the registry, and stores it under the name specified in that class's GetName() function.
/// After completion of this method, CreateCostFunction() will be able to create instances of this class type.</summary>
template<typename CostFunctionType> static void registerCostFunction()
{
const std::string& name = CostFunctionType::GetName();
......@@ -49,8 +53,14 @@ private:
}
public:
/// <summary>Registers all possible types of cost functions that can exist.
/// Call this method once before you start using CreateCostFunction().</summary>
static void RegisterAllCostFunctions();
/// <summary>Creates and returns a new CostFunction instance whose name matches the given value.</summary>
/// <details>This method looks through the registry created in RegisterAllCostFunctions().</details>
/// <param name="name">The name of the cost function to create.</param>
/// <returns>A pointer to a new instance of the CostFunction subclass whose GetName() matches the "name" parameter.</returns>
static CostFunction* CreateCostFunction(const std::string& name)
{
if (registry.count(name) == 0)
......
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