G4VisListManager.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 // Jane Tinslay, John Allison, Joseph Perl October 2005
00029 //
00030 // Class Description:
00031 // Templated class to manage a set of objects, with one named
00032 // as current. Owns registered objects.
00033 // Class Description - End:
00034 
00035 #ifndef G4VISLISTMANAGER_HH
00036 #define G4VISLISTMANAGER_HH
00037 
00038 #include "G4String.hh"
00039 #include <map>
00040 #include <ostream>
00041 
00042 template <typename T>
00043 class G4VisListManager {
00044 
00045 public: // With description
00046   
00047   G4VisListManager();
00048 
00049   virtual ~G4VisListManager();
00050 
00051   // Register ptr. Manager assumes ownership and
00052   // ptr becomes current
00053   void Register(T* ptr);
00054 
00055   void SetCurrent(const G4String& name);
00056 
00057   // Accessors
00058   const T* Current() const {return fpCurrent;}
00059   const std::map<G4String, T*>& Map() const;
00060 
00061   // Print configuration
00062   void Print(std::ostream& ostr, const G4String& name="") const;
00063 
00064 private:
00065 
00066   // Data members
00067   std::map<G4String, T*> fMap;
00068   T* fpCurrent;
00069 
00070 };
00071 
00072 template <typename T>
00073 G4VisListManager<T>::G4VisListManager()
00074   :fpCurrent(0) 
00075 {}
00076 
00077 template <typename T>
00078 G4VisListManager<T>::~G4VisListManager() 
00079 {
00080   typename std::map<G4String, T*>::iterator iter = fMap.begin();
00081 
00082   while (iter != fMap.end()) {
00083     delete iter->second;    
00084     iter++;
00085   }
00086 }
00087 
00088 template <typename T>
00089 void
00090 G4VisListManager<T>::Register(T* ptr) 
00091 {
00092   assert (0 != ptr);
00093 
00094   // Add to map.  Replace if name the same.
00095   fMap[ptr->Name()] = ptr;
00096   fpCurrent = ptr;    
00097 }
00098 
00099 template <typename T>
00100 void
00101 G4VisListManager<T>::SetCurrent(const G4String& name)
00102 {
00103   typename std::map<G4String, T*>::const_iterator iter = fMap.find(name);
00104 
00105   if (iter != fMap.end()) fpCurrent = fMap[name];
00106   else {
00107     G4ExceptionDescription ed;
00108     ed << "Key \"" << name << "\" has not been registered";
00109     G4Exception
00110       ("G4VisListManager<T>::SetCurrent(T* ptr) ",
00111        "visman0102", JustWarning, ed, "Non-existent name");
00112   }
00113 }
00114 
00115 template <typename T>
00116 void
00117 G4VisListManager<T>::Print(std::ostream& ostr, const G4String& name) const
00118 {
00119   if (0 == fMap.size()) {
00120     G4cout<<"  None"<<std::endl;
00121     return;
00122   }
00123     
00124   ostr<<"  Current: "<<fpCurrent->Name()<<std::endl;
00125 
00126   if (!name.isNull()) {
00127     // Print out specified object
00128     typename std::map<G4String, T*>::const_iterator iter = fMap.find(name);
00129 
00130     if (iter != fMap.end()) {
00131       iter->second->Print(ostr);
00132     }
00133     else {
00134       ostr<<name<<" not found "<<std::endl;
00135     }
00136   }
00137   else {
00138     typename std::map<G4String, T*>::const_iterator iter = fMap.begin();
00139     while (iter != fMap.end()) {
00140       iter->second->Print(ostr);
00141       ostr<<std::endl;
00142       iter++;
00143     }
00144   }
00145 }
00146 
00147 template <typename T>
00148 const std::map<G4String, T*>&
00149 G4VisListManager<T>::Map() const
00150 {
00151   return fMap;
00152 }
00153 
00154 #endif

Generated on Mon May 27 17:50:16 2013 for Geant4 by  doxygen 1.4.7