File indexing completed on 2025-01-18 09:58:04
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 #ifndef G4CREATORFACTORYT_HH
0033 #define G4CREATORFACTORYT_HH
0034
0035 #include "globals.hh"
0036 #include <map>
0037
0038 template <typename T, typename Identifier, typename Creator>
0039 class G4CreatorFactoryT {
0040
0041 public:
0042
0043
0044 G4CreatorFactoryT();
0045
0046
0047 virtual ~G4CreatorFactoryT();
0048
0049
0050 G4bool Register(const Identifier& id, Creator creator);
0051
0052
0053 T* Create(const Identifier& id) const;
0054
0055 private:
0056
0057 typedef std::map<Identifier, Creator> Map;
0058
0059
0060 Map fMap;
0061
0062 };
0063
0064 template <typename T, typename Identifier, typename Creator>
0065 G4CreatorFactoryT<T, Identifier, Creator>::G4CreatorFactoryT() {}
0066
0067 template <typename T, typename Identifier, typename Creator>
0068 G4CreatorFactoryT<T, Identifier, Creator>::~G4CreatorFactoryT() {}
0069
0070 template <typename T, typename Identifier, typename Creator>
0071 G4bool
0072 G4CreatorFactoryT<T, Identifier, Creator>::Register(const Identifier& id,
0073 Creator creator)
0074 {
0075 if (fMap.find(id) != fMap.end()) {
0076 G4ExceptionDescription ed;
0077 ed << "Creator with identifier "<<id<<" already exists."<<G4endl;
0078 G4Exception
0079 ("G4CreatorFactoryT::Register(const Identifier& id, Creator creator)",
0080 "greps0102", JustWarning, ed,
0081 "Creator exists");
0082 return false;
0083 }
0084
0085
0086 std::pair<Identifier, Creator> myPair(id, creator);
0087 return fMap.insert(myPair).second;
0088 }
0089
0090 template <typename T, typename Identifier, typename Creator>
0091 T*
0092 G4CreatorFactoryT<T, Identifier, Creator>::Create(const Identifier& id) const
0093 {
0094 typename Map::const_iterator iter = fMap.find(id);
0095
0096 if (iter == fMap.end()) {
0097 G4ExceptionDescription ed;
0098 ed << "Identifier "<<id<<" does not exist."<<G4endl;
0099 G4Exception("G4CreatorFactoryT::Create(const Identifier& id)",
0100 "greps0103", JustWarning, ed,
0101 "Non-existent identifier");
0102 return 0;
0103 }
0104
0105 return iter->second();
0106 }
0107
0108 #endif