G4PhysicsConstructorRegistry.cc

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 //
00027 // $Id: 
00028 //
00029 // -------------------------------------------------------------------
00030 //
00031 // GEANT4 Class file
00032 //
00033 //
00034 // File name:    G4PhysicsConstructorRegistry
00035 //
00036 // Author  W. Pokorski  21.09.2012
00037 //
00038 // Modifications:
00039 //
00040 
00041 #include "G4ios.hh"
00042 
00043 #include "G4PhysicsConstructorRegistry.hh"
00044 #include "G4VPhysicsConstructor.hh"
00045 #include "G4PhysicsConstructorFactory.hh"
00046 
00047 G4PhysicsConstructorRegistry* G4PhysicsConstructorRegistry::theInstance = 0;
00048 
00049 G4PhysicsConstructorRegistry* G4PhysicsConstructorRegistry::Instance()
00050 {
00051   if(0 == theInstance) {
00052     static G4PhysicsConstructorRegistry manager;
00053     theInstance = &manager;
00054   }
00055   return theInstance;
00056 }
00057 
00058 G4PhysicsConstructorRegistry::G4PhysicsConstructorRegistry()
00059 {}
00060 
00061 G4PhysicsConstructorRegistry::~G4PhysicsConstructorRegistry()
00062 {
00063   Clean();
00064 }
00065 
00066 void G4PhysicsConstructorRegistry::Clean()
00067 {
00068   size_t n = physConstr.size(); 
00069   if(n > 0) {
00070     for (size_t i=0; i<n; ++i) {
00071       if(physConstr[i]) {
00072         G4VPhysicsConstructor* p = physConstr[i];
00073         physConstr[i] = 0;
00074         delete p;
00075       }
00076     }
00077     physConstr.clear();
00078   }
00079 }
00080 
00081 void G4PhysicsConstructorRegistry::Register(G4VPhysicsConstructor* p)
00082 {
00083   if(!p) return;
00084   size_t n = physConstr.size(); 
00085   if(n > 0) {
00086     for (size_t i=0; i<n; ++i) {
00087       if(physConstr[i] == p) { return; }
00088     }
00089   }
00090   physConstr.push_back(p);
00091 }
00092 
00093 void G4PhysicsConstructorRegistry::DeRegister(G4VPhysicsConstructor* p)
00094 {
00095   if(!p) return;
00096   size_t n = physConstr.size(); 
00097   if(n > 0) {
00098     for (size_t i=0; i<n; ++i) {
00099       if(physConstr[i] == p) {
00100         physConstr[i] = 0;
00101         return;
00102       }
00103     }
00104   }
00105 }
00106 
00107 void G4PhysicsConstructorRegistry::AddFactory(G4String name, G4VBasePhysConstrFactory* factory)
00108 {
00109   factories[name] = factory;
00110 }
00111 
00112 G4VPhysicsConstructor* G4PhysicsConstructorRegistry::GetPhysicsConstructor(const G4String& name)
00113 {
00114   // check if factory exists...
00115   //
00116   if (factories.find(name)!=factories.end())
00117     {
00118         // we could store the list of called factories in some vector and
00119         // before returning we can could first check if this physics constructor was already instantiated
00120         // if yes, we can throw an exception saying that this physics can been already registered
00121         
00122       return factories[name]->Instantiate();
00123     }
00124   else
00125     {
00126       G4ExceptionDescription ED;
00127       ED << "The factory for the physics constructor ["<< name << "] does not exist!" << G4endl;
00128       G4Exception("G4PhysicsConstructorRegistry::GetPhysicsConstructor", "PhysicsList001", FatalException, ED);
00129       return 0;
00130     }
00131 }

Generated on Mon May 27 17:49:19 2013 for Geant4 by  doxygen 1.4.7