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,v 1.8 2006/06/29 21:29:04 gunter Exp $ |
---|
27 | // GEANT4 tag $Name: geant4-09-04-beta-01 $ |
---|
28 | // |
---|
29 | // Jane Tinslay, John Allison, Joseph Perl October 2005 |
---|
30 | // |
---|
31 | // Class Description: |
---|
32 | // Templated class to manage a set of objects, with one named |
---|
33 | // as current. Owns registered objects. |
---|
34 | // Class Description - End: |
---|
35 | |
---|
36 | #ifndef G4VISLISTMANAGER_HH |
---|
37 | #define G4VISLISTMANAGER_HH |
---|
38 | |
---|
39 | #include "G4String.hh" |
---|
40 | #include <map> |
---|
41 | #include <ostream> |
---|
42 | #include <sstream> |
---|
43 | |
---|
44 | template <typename T> |
---|
45 | class G4VisListManager { |
---|
46 | |
---|
47 | public: // With description |
---|
48 | |
---|
49 | G4VisListManager(); |
---|
50 | |
---|
51 | virtual ~G4VisListManager(); |
---|
52 | |
---|
53 | // Register ptr. Manager assumes ownership and |
---|
54 | // ptr becomes current |
---|
55 | void Register(T* ptr); |
---|
56 | |
---|
57 | void SetCurrent(const G4String& name); |
---|
58 | |
---|
59 | // Accessors |
---|
60 | const T* Current() const {return fpCurrent;} |
---|
61 | const std::map<G4String, T*>& Map() const; |
---|
62 | |
---|
63 | // Print configuration |
---|
64 | void Print(std::ostream& ostr, const G4String& name="") const; |
---|
65 | |
---|
66 | private: |
---|
67 | |
---|
68 | // Data members |
---|
69 | std::map<G4String, T*> fMap; |
---|
70 | T* fpCurrent; |
---|
71 | |
---|
72 | }; |
---|
73 | |
---|
74 | template <typename T> |
---|
75 | G4VisListManager<T>::G4VisListManager() |
---|
76 | :fpCurrent(0) |
---|
77 | {} |
---|
78 | |
---|
79 | template <typename T> |
---|
80 | G4VisListManager<T>::~G4VisListManager() |
---|
81 | { |
---|
82 | typename std::map<G4String, T*>::iterator iter = fMap.begin(); |
---|
83 | |
---|
84 | while (iter != fMap.end()) { |
---|
85 | delete iter->second; |
---|
86 | iter++; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | template <typename T> |
---|
91 | void |
---|
92 | G4VisListManager<T>::Register(T* ptr) |
---|
93 | { |
---|
94 | assert (0 != ptr); |
---|
95 | |
---|
96 | typename std::map<G4String, T*>::const_iterator iter = fMap.find(ptr->Name()); |
---|
97 | |
---|
98 | if (iter == fMap.end()) { |
---|
99 | fMap[ptr->Name()] = ptr; |
---|
100 | fpCurrent = ptr; |
---|
101 | } |
---|
102 | else { |
---|
103 | std::ostringstream o; |
---|
104 | o << "Key "<<ptr->Name()<<" already registered"; |
---|
105 | G4Exception |
---|
106 | ("G4VisListManager<T>::Register(T* ptr) ", |
---|
107 | "KeyExists", FatalErrorInArgument, o.str().c_str()); |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | template <typename T> |
---|
112 | void |
---|
113 | G4VisListManager<T>::SetCurrent(const G4String& name) |
---|
114 | { |
---|
115 | typename std::map<G4String, T*>::const_iterator iter = fMap.find(name); |
---|
116 | |
---|
117 | if (iter != fMap.end()) fpCurrent = fMap[name]; |
---|
118 | else { |
---|
119 | std::ostringstream o; |
---|
120 | o << "Key "<<name<<" has not been registered"; |
---|
121 | G4Exception |
---|
122 | ("G4VisListManager<T>::SetCurrent(T* ptr) ", |
---|
123 | "NonExistentName", FatalErrorInArgument, o.str().c_str()); |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | template <typename T> |
---|
128 | void |
---|
129 | G4VisListManager<T>::Print(std::ostream& ostr, const G4String& name) const |
---|
130 | { |
---|
131 | if (0 == fMap.size()) { |
---|
132 | G4cout<<" None"<<std::endl; |
---|
133 | return; |
---|
134 | } |
---|
135 | |
---|
136 | ostr<<" Current: "<<fpCurrent->Name()<<std::endl; |
---|
137 | |
---|
138 | if (!name.isNull()) { |
---|
139 | // Print out specified object |
---|
140 | typename std::map<G4String, T*>::const_iterator iter = fMap.find(name); |
---|
141 | |
---|
142 | if (iter != fMap.end()) { |
---|
143 | iter->second->Print(ostr); |
---|
144 | } |
---|
145 | else { |
---|
146 | ostr<<name<<" not found "<<std::endl; |
---|
147 | } |
---|
148 | } |
---|
149 | else { |
---|
150 | typename std::map<G4String, T*>::const_iterator iter = fMap.begin(); |
---|
151 | while (iter != fMap.end()) { |
---|
152 | iter->second->Print(ostr); |
---|
153 | ostr<<std::endl; |
---|
154 | iter++; |
---|
155 | } |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | template <typename T> |
---|
160 | const std::map<G4String, T*>& |
---|
161 | G4VisListManager<T>::Map() const |
---|
162 | { |
---|
163 | return fMap; |
---|
164 | } |
---|
165 | |
---|
166 | #endif |
---|