Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:59:24

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 // Jane Tinslay, John Allison, Joseph Perl October 2005
0028 //
0029 // Class Description:
0030 // Templated class to manage a set of objects, with one named
0031 // as current. Owns registered objects.
0032 // Class Description - End:
0033 
0034 #ifndef G4VISLISTMANAGER_HH
0035 #define G4VISLISTMANAGER_HH
0036 
0037 #include "G4String.hh"
0038 #include <map>
0039 #include <ostream>
0040 
0041 template <typename T>
0042 class G4VisListManager {
0043 
0044 public: // With description
0045   
0046   G4VisListManager();
0047 
0048   virtual ~G4VisListManager();
0049 
0050   // Register ptr. Manager assumes ownership and
0051   // ptr becomes current
0052   void Register(T* ptr);
0053 
0054   void SetCurrent(const G4String& name);
0055 
0056   // Accessors
0057   const T* Current() const {return fpCurrent;}
0058   const std::map<G4String, T*>& Map() const;
0059 
0060   // Print configuration
0061   void Print(std::ostream& ostr, const G4String& name="") const;
0062 
0063 private:
0064 
0065   // Data members
0066   std::map<G4String, T*> fMap;
0067   T* fpCurrent;
0068 
0069 };
0070 
0071 template <typename T>
0072 G4VisListManager<T>::G4VisListManager()
0073   :fpCurrent(0) 
0074 {}
0075 
0076 template <typename T>
0077 G4VisListManager<T>::~G4VisListManager() 
0078 {
0079   typename std::map<G4String, T*>::iterator iter = fMap.begin();
0080 
0081   while (iter != fMap.end()) {
0082     delete iter->second;    
0083     iter++;
0084   }
0085 }
0086 
0087 template <typename T>
0088 void
0089 G4VisListManager<T>::Register(T* ptr) 
0090 {
0091   assert (0 != ptr);
0092 
0093   // Add to map.  Replace if name the same.
0094   fMap[ptr->Name()] = ptr;
0095   fpCurrent = ptr;    
0096 }
0097 
0098 template <typename T>
0099 void
0100 G4VisListManager<T>::SetCurrent(const G4String& name)
0101 {
0102   typename std::map<G4String, T*>::const_iterator iter = fMap.find(name);
0103 
0104   if (iter != fMap.end()) fpCurrent = fMap[name];
0105   else {
0106     G4ExceptionDescription ed;
0107     ed << "Key \"" << name << "\" has not been registered";
0108     G4Exception
0109       ("G4VisListManager<T>::SetCurrent(T* ptr) ",
0110        "visman0102", JustWarning, ed, "Non-existent name");
0111   }
0112 }
0113 
0114 template <typename T>
0115 void
0116 G4VisListManager<T>::Print(std::ostream& ostr, const G4String& name) const
0117 {
0118   if (0 == fMap.size()) {
0119     G4cout<<"  None"<<std::endl;
0120     return;
0121   }
0122     
0123   ostr<<"  Current: "<<fpCurrent->Name()<<std::endl;
0124 
0125   if (!name.empty()) {
0126     // Print out specified object
0127     typename std::map<G4String, T*>::const_iterator iter = fMap.find(name);
0128 
0129     if (iter != fMap.end()) {
0130       iter->second->Print(ostr);
0131     }
0132     else {
0133       ostr<<name<<" not found "<<std::endl;
0134     }
0135   }
0136   else {
0137     typename std::map<G4String, T*>::const_iterator iter = fMap.begin();
0138     while (iter != fMap.end()) {
0139       iter->second->Print(ostr);
0140       ostr<<std::endl;
0141       iter++;
0142     }
0143   }
0144 }
0145 
0146 template <typename T>
0147 const std::map<G4String, T*>&
0148 G4VisListManager<T>::Map() const
0149 {
0150   return fMap;
0151 }
0152 
0153 #endif