MAIA bb96820c
Multiphysics at AIA
Loading...
Searching...
No Matches
factory.h
Go to the documentation of this file.
1// Copyright (C) 2024 The m-AIA AUTHORS
2//
3// This file is part of m-AIA (https://git.rwth-aachen.de/aia/m-AIA/m-AIA)
4//
5// SPDX-License-Identifier: LGPL-3.0-only
6
7#ifndef FACTOR_H_
8#define FACTOR_H_
9
10#include <functional>
11#include <map>
12#include <memory>
13#include "INCLUDE/maiatypes.h"
14#include "UTIL/functions.h"
15
16
34template <class AbstractProduct, typename IdentifierType, typename ReturnType = std::unique_ptr<AbstractProduct>,
35 class ProductCreator = std::function<ReturnType()>, class... Args>
36class MFactory {
37 public:
44 MBool add(const IdentifierType& id, ProductCreator creator) {
45 return m_assoc.insert(typename AssocMap::value_type(id, creator)).second;
46 }
47
53 MBool remove(const IdentifierType& id) { return m_assoc.erase(id) == true; }
54
61 ReturnType create(const IdentifierType& id, Args... args) const {
62 auto i = m_assoc.find(id);
63 if(i != m_assoc.end()) {
64 return (i->second)(args...);
65 } else {
66 TERMM(1, "Identifier not found.");
67 }
68 }
69
70 private:
71 typedef std::map<IdentifierType, ProductCreator> AssocMap;
73};
74
75#endif // FACTOR_H_
Class implementing the object factory pattern.
Definition: factory.h:36
std::map< IdentifierType, ProductCreator > AssocMap
Definition: factory.h:71
AssocMap m_assoc
Definition: factory.h:72
MBool remove(const IdentifierType &id)
Function to remove existing types from the factory.
Definition: factory.h:53
ReturnType create(const IdentifierType &id, Args... args) const
Function to create a new instance of a type.
Definition: factory.h:61
MBool add(const IdentifierType &id, ProductCreator creator)
Function to add new types to the factory.
Definition: factory.h:44
bool MBool
Definition: maiatypes.h:58