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