]>
Commit | Line | Data |
---|---|---|
2817d3e2 | 1 | // $Id$ |
2 | // Category: global | |
3 | // | |
4 | // See the class description in the header file. | |
5 | ||
6 | #include "TG4NameMap.h" | |
7 | #include "TG4Globals.h" | |
8 | ||
9 | #include "g4std/iomanip" | |
10 | #include "globals.hh" | |
11 | ||
12 | G4String TG4NameMap::fgUndefined = "Undefined"; | |
13 | ||
14 | typedef G4std::map<G4String, G4String, G4std::less<G4String> > | |
15 | :: iterator MapIterator; | |
16 | ||
17 | TG4NameMap::TG4NameMap() | |
18 | : fSecond(fgUndefined) { | |
19 | // | |
20 | } | |
21 | ||
22 | TG4NameMap::TG4NameMap(const TG4NameMap& right) { | |
23 | // | |
24 | TG4Globals::Exception("TG4NameMap is protected from copying."); | |
25 | } | |
26 | ||
27 | TG4NameMap::~TG4NameMap() { | |
28 | // | |
29 | } | |
30 | ||
31 | // operators | |
32 | ||
33 | TG4NameMap& TG4NameMap::operator=(const TG4NameMap& right) | |
34 | { | |
35 | // check assignement to self | |
36 | if (this == &right) return *this; | |
37 | ||
38 | TG4Globals::Exception("TG4NameMap is protected from assigning."); | |
39 | ||
40 | return *this; | |
41 | } | |
42 | ||
43 | // public methods | |
44 | ||
45 | G4bool TG4NameMap::Add(const G4String& first, const G4String& second) | |
46 | { | |
47 | // Adds names pair to the map. | |
48 | // fSecond is not used in this add method. | |
49 | // --- | |
50 | ||
51 | if (GetSecond(first) == fgUndefined) { | |
52 | // insert into map | |
53 | // only in case it is not yet here | |
54 | fMap[first] = second; | |
55 | return true; | |
56 | } | |
57 | return false; | |
58 | } | |
59 | ||
60 | ||
61 | G4bool TG4NameMap::AddName(const G4String& name) | |
62 | { | |
63 | // Adds name to the map. | |
64 | // --- | |
65 | ||
66 | if (GetSecond(name) == fgUndefined) { | |
67 | // insert into map | |
68 | // only in case it is not yet here | |
69 | fMap[name] = fSecond; | |
70 | return true; | |
71 | } | |
72 | return false; | |
73 | } | |
74 | ||
75 | ||
76 | const G4String& TG4NameMap::GetSecond(const G4String& name) | |
77 | { | |
78 | // Gets second name associated with given name. | |
79 | // --- | |
80 | ||
81 | MapIterator i = fMap.find(name); | |
82 | if (i == fMap.end()) | |
83 | return fgUndefined; | |
84 | else | |
85 | return (*i).second; | |
86 | } | |
87 | ||
88 | ||
89 | void TG4NameMap::PrintAll() | |
90 | { | |
91 | // Dumps all map. | |
92 | // --- | |
93 | ||
94 | if (fMap.size()) { | |
95 | G4cout << "Dump of TG4NameMap - " << fMap.size() << " entries:" << G4endl; | |
96 | G4int counter = 0; | |
97 | for (MapIterator i=fMap.begin(); i != fMap.end(); i++) { | |
98 | const G4String& first = (*i).first; | |
99 | const G4String& second = (*i).second; | |
100 | G4cout << "Map element " << G4std::setw(3) << counter++ << " " | |
101 | << first << " " << second << G4endl; | |
102 | } | |
103 | } | |
104 | } | |
105 | ||
106 | ||
107 | void TG4NameMap::Clear() | |
108 | { | |
109 | // Clears the map. | |
110 | // --- | |
111 | ||
ca6f2708 | 112 | fMap.clear(); |
2817d3e2 | 113 | fSecond = "Undefined"; |
114 | } |