Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4VisListManager.hh
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 // $Id: G4VisListManager.hh 66373 2012-12-18 09:41:34Z gcosmo $
27 //
28 // Jane Tinslay, John Allison, Joseph Perl October 2005
29 //
30 // Class Description:
31 // Templated class to manage a set of objects, with one named
32 // as current. Owns registered objects.
33 // Class Description - End:
34 
35 #ifndef G4VISLISTMANAGER_HH
36 #define G4VISLISTMANAGER_HH
37 
38 #include "G4String.hh"
39 #include <map>
40 #include <ostream>
41 
42 template <typename T>
44 
45 public: // With description
46 
48 
49  virtual ~G4VisListManager();
50 
51  // Register ptr. Manager assumes ownership and
52  // ptr becomes current
53  void Register(T* ptr);
54 
55  void SetCurrent(const G4String& name);
56 
57  // Accessors
58  const T* Current() const {return fpCurrent;}
59  const std::map<G4String, T*>& Map() const;
60 
61  // Print configuration
62  void Print(std::ostream& ostr, const G4String& name="") const;
63 
64 private:
65 
66  // Data members
67  std::map<G4String, T*> fMap;
68  T* fpCurrent;
69 
70 };
71 
72 template <typename T>
74  :fpCurrent(0)
75 {}
76 
77 template <typename T>
79 {
80  typename std::map<G4String, T*>::iterator iter = fMap.begin();
81 
82  while (iter != fMap.end()) {
83  delete iter->second;
84  iter++;
85  }
86 }
87 
88 template <typename T>
89 void
91 {
92  assert (0 != ptr);
93 
94  // Add to map. Replace if name the same.
95  fMap[ptr->Name()] = ptr;
96  fpCurrent = ptr;
97 }
98 
99 template <typename T>
100 void
102 {
103  typename std::map<G4String, T*>::const_iterator iter = fMap.find(name);
104 
105  if (iter != fMap.end()) fpCurrent = fMap[name];
106  else {
108  ed << "Key \"" << name << "\" has not been registered";
110  ("G4VisListManager<T>::SetCurrent(T* ptr) ",
111  "visman0102", JustWarning, ed, "Non-existent name");
112  }
113 }
114 
115 template <typename T>
116 void
117 G4VisListManager<T>::Print(std::ostream& ostr, const G4String& name) const
118 {
119  if (0 == fMap.size()) {
120  G4cout<<" None"<<std::endl;
121  return;
122  }
123 
124  ostr<<" Current: "<<fpCurrent->Name()<<std::endl;
125 
126  if (!name.isNull()) {
127  // Print out specified object
128  typename std::map<G4String, T*>::const_iterator iter = fMap.find(name);
129 
130  if (iter != fMap.end()) {
131  iter->second->Print(ostr);
132  }
133  else {
134  ostr<<name<<" not found "<<std::endl;
135  }
136  }
137  else {
138  typename std::map<G4String, T*>::const_iterator iter = fMap.begin();
139  while (iter != fMap.end()) {
140  iter->second->Print(ostr);
141  ostr<<std::endl;
142  iter++;
143  }
144  }
145 }
146 
147 template <typename T>
148 const std::map<G4String, T*>&
150 {
151  return fMap;
152 }
153 
154 #endif
const XML_Char * name
Definition: expat.h:151
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4GLOB_DLL std::ostream G4cout
void SetCurrent(const G4String &name)
const std::map< G4String, T * > & Map() const
const T * Current() const
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
virtual ~G4VisListManager()
void Register(T *ptr)
void Print(std::ostream &ostr, const G4String &name="") const
G4bool isNull() const