FEDEM Solver  R8.0
Source code of the dynamics solver
FFaGenericFactory.H
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2023 SAP SE
2 //
3 // SPDX-License-Identifier: Apache-2.0
4 //
5 // This file is part of FEDEM - https://openfedem.org
7 
8 #ifndef FFA_GENERIC_FACTORY_H
9 #define FFA_GENERIC_FACTORY_H
10 
11 #include <string>
12 #include <vector>
13 #include <map>
14 #if FFA_DEBUG
15 #include <iostream>
16 #endif
17 
20 
21 
22 template <class T, class Key = std::string, class ArgType = int>
23 class FFaGenericFactory : public FFaSingelton< FFaGenericFactory<T,Key,ArgType> >
24 {
25  typedef FFaDynCB2<ArgType,T*&> CreatorCB;
26  typedef std::map<Key,CreatorCB> CreatorType;
27 
28 public:
30  virtual ~FFaGenericFactory() {}
31 
32  bool registerCreator(const Key& key, const CreatorCB& creator)
33  {
34  return myCreatorMap.insert(std::make_pair(key,creator)).second;
35  }
36 
37  T* create(const Key& key, ArgType id)
38  {
39  T* retVal = 0;
40  typename CreatorType::iterator creator = myCreatorMap.find(key);
41  if (creator != myCreatorMap.end())
42  creator->second.invoke(id,retVal);
43 #if FFA_DEBUG
44  else
45  std::cerr <<"\n *** FFaGenericFactory: No creator defined for "
46  << key << std::endl;
47 #endif
48  return retVal;
49  }
50 
51  void getKeys(std::vector<Key>& keys) const
52  {
53  keys.clear();
54  keys.reserve(myCreatorMap.size());
55  for (const typename CreatorType::value_type& creator : myCreatorMap)
56  keys.push_back(creator.first);
57  }
58 
59  void clear() { myCreatorMap.clear(); this->removeInstance(); }
60 
61 private:
62  CreatorType myCreatorMap;
63 };
64 
65 #endif
Macros for definition of dynamic callback objects.
Definition: FFaGenericFactory.H:24
FFaDynCB2< ArgType, T *& > CreatorCB
Definition: FFaGenericFactory.H:25
bool registerCreator(const Key &key, const CreatorCB &creator)
Definition: FFaGenericFactory.H:32
void getKeys(std::vector< Key > &keys) const
Definition: FFaGenericFactory.H:51
T * create(const Key &key, ArgType id)
Definition: FFaGenericFactory.H:37
CreatorType myCreatorMap
Definition: FFaGenericFactory.H:62
virtual ~FFaGenericFactory()
Definition: FFaGenericFactory.H:30
FFaGenericFactory()
Definition: FFaGenericFactory.H:29
std::map< Key, CreatorCB > CreatorType
Definition: FFaGenericFactory.H:26
void clear()
Definition: FFaGenericFactory.H:59
Template class for singleton classes.
Definition: FFaSingelton.H:20
static void removeInstance()
Deletes the dynamically allocated instance.
Definition: FFaSingelton.H:41