G4CreatorFactoryT.hh

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 // $Id$
00027 //
00028 // Generic identifier-creator based factory. Based on 
00029 // factory presented in "Modern C++ Design, Andrei Alexandrescu"
00030 //
00031 // Jane Tinslay, September 2006
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   // Constructor
00045   G4CreatorFactoryT();
00046 
00047   // Destructor
00048   virtual ~G4CreatorFactoryT();
00049 
00050   // Register identifier<->creator pairs
00051   G4bool Register(const Identifier& id, Creator creator);
00052 
00053   // Create product with given identifier
00054   T* Create(const Identifier& id) const; 
00055 
00056 private:
00057 
00058   typedef std::map<Identifier, Creator> Map;
00059 
00060   // Data member
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   // Insert identifier<->creator pair into map
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

Generated on Mon May 27 17:47:58 2013 for Geant4 by  doxygen 1.4.7