Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:58:04

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 //
0027 // Generic identifier-creator based factory. Based on 
0028 // factory presented in "Modern C++ Design, Andrei Alexandrescu"
0029 //
0030 // Jane Tinslay, September 2006
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   // Constructor
0044   G4CreatorFactoryT();
0045 
0046   // Destructor
0047   virtual ~G4CreatorFactoryT();
0048 
0049   // Register identifier<->creator pairs
0050   G4bool Register(const Identifier& id, Creator creator);
0051 
0052   // Create product with given identifier
0053   T* Create(const Identifier& id) const; 
0054 
0055 private:
0056 
0057   typedef std::map<Identifier, Creator> Map;
0058 
0059   // Data member
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   // Insert identifier<->creator pair into map
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