Geant4-11
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//
27// Jane Tinslay, John Allison, Joseph Perl October 2005
28//
29// Class Description:
30// Templated class to manage a set of objects, with one named
31// as current. Owns registered objects.
32// Class Description - End:
33
34#ifndef G4VISLISTMANAGER_HH
35#define G4VISLISTMANAGER_HH
36
37#include "G4String.hh"
38#include <map>
39#include <ostream>
40
41template <typename T>
43
44public: // With description
45
47
48 virtual ~G4VisListManager();
49
50 // Register ptr. Manager assumes ownership and
51 // ptr becomes current
52 void Register(T* ptr);
53
54 void SetCurrent(const G4String& name);
55
56 // Accessors
57 const T* Current() const {return fpCurrent;}
58 const std::map<G4String, T*>& Map() const;
59
60 // Print configuration
61 void Print(std::ostream& ostr, const G4String& name="") const;
62
63private:
64
65 // Data members
66 std::map<G4String, T*> fMap;
68
69};
70
71template <typename T>
73 :fpCurrent(0)
74{}
75
76template <typename T>
78{
79 typename std::map<G4String, T*>::iterator iter = fMap.begin();
80
81 while (iter != fMap.end()) {
82 delete iter->second;
83 iter++;
84 }
85}
86
87template <typename T>
88void
90{
91 assert (0 != ptr);
92
93 // Add to map. Replace if name the same.
94 fMap[ptr->Name()] = ptr;
95 fpCurrent = ptr;
96}
97
98template <typename T>
99void
101{
102 typename std::map<G4String, T*>::const_iterator iter = fMap.find(name);
103
104 if (iter != fMap.end()) fpCurrent = fMap[name];
105 else {
107 ed << "Key \"" << name << "\" has not been registered";
109 ("G4VisListManager<T>::SetCurrent(T* ptr) ",
110 "visman0102", JustWarning, ed, "Non-existent name");
111 }
112}
113
114template <typename T>
115void
116G4VisListManager<T>::Print(std::ostream& ostr, const G4String& name) const
117{
118 if (0 == fMap.size()) {
119 G4cout<<" None"<<std::endl;
120 return;
121 }
122
123 ostr<<" Current: "<<fpCurrent->Name()<<std::endl;
124
125 if (!name.empty()) {
126 // Print out specified object
127 typename std::map<G4String, T*>::const_iterator iter = fMap.find(name);
128
129 if (iter != fMap.end()) {
130 iter->second->Print(ostr);
131 }
132 else {
133 ostr<<name<<" not found "<<std::endl;
134 }
135 }
136 else {
137 typename std::map<G4String, T*>::const_iterator iter = fMap.begin();
138 while (iter != fMap.end()) {
139 iter->second->Print(ostr);
140 ostr<<std::endl;
141 iter++;
142 }
143 }
144}
145
146template <typename T>
147const std::map<G4String, T*>&
149{
150 return fMap;
151}
152
153#endif
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:40
G4GLOB_DLL std::ostream G4cout
void SetCurrent(const G4String &name)
void Print(std::ostream &ostr, const G4String &name="") const
virtual ~G4VisListManager()
const std::map< G4String, T * > & Map() const
const T * Current() const
void Register(T *ptr)
std::map< G4String, T * > fMap
const char * name(G4int ptype)