00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef G4CREATORFACTORYT_HH
00034 #define G4CREATORFACTORYT_HH
00035
00036 #include "globals.hh"
00037 #include <map>
00038
00039 template <typename T, typename Identifier, typename Creator>
00040 class G4CreatorFactoryT {
00041
00042 public:
00043
00044
00045 G4CreatorFactoryT();
00046
00047
00048 virtual ~G4CreatorFactoryT();
00049
00050
00051 G4bool Register(const Identifier& id, Creator creator);
00052
00053
00054 T* Create(const Identifier& id) const;
00055
00056 private:
00057
00058 typedef std::map<Identifier, Creator> Map;
00059
00060
00061 Map fMap;
00062
00063 };
00064
00065 template <typename T, typename Identifier, typename Creator>
00066 G4CreatorFactoryT<T, Identifier, Creator>::G4CreatorFactoryT() {}
00067
00068 template <typename T, typename Identifier, typename Creator>
00069 G4CreatorFactoryT<T, Identifier, Creator>::~G4CreatorFactoryT() {}
00070
00071 template <typename T, typename Identifier, typename Creator>
00072 G4bool
00073 G4CreatorFactoryT<T, Identifier, Creator>::Register(const Identifier& id,
00074 Creator creator)
00075 {
00076 if (fMap.find(id) != fMap.end()) {
00077 G4ExceptionDescription ed;
00078 ed << "Creator with identifier "<<id<<" already exists."<<G4endl;
00079 G4Exception
00080 ("G4CreatorFactoryT::Register(const Identifier& id, Creator creator)",
00081 "greps0102", JustWarning, ed,
00082 "Creator exists");
00083 return false;
00084 }
00085
00086
00087 std::pair<Identifier, Creator> myPair(id, creator);
00088 return fMap.insert(myPair).second;
00089 }
00090
00091 template <typename T, typename Identifier, typename Creator>
00092 T*
00093 G4CreatorFactoryT<T, Identifier, Creator>::Create(const Identifier& id) const
00094 {
00095 typename Map::const_iterator iter = fMap.find(id);
00096
00097 if (iter == fMap.end()) {
00098 G4ExceptionDescription ed;
00099 ed << "Identifier "<<id<<" does not exist."<<G4endl;
00100 G4Exception("G4CreatorFactoryT::Create(const Identifier& id)",
00101 "greps0103", JustWarning, ed,
00102 "Non-existent identifier");
00103 return 0;
00104 }
00105
00106 return iter->second();
00107 }
00108
00109 #endif