BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSAcceleratorComponentRegistry.hh
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
18#ifndef BDSACCELERATORCOMPONENTREGISTRY_H
19#define BDSACCELERATORCOMPONENTREGISTRY_H
20
21#include "globals.hh" // geant4 globals / types
22
23#include <functional> // for all the std::hash business
24#include <iterator>
25#include <map>
26#include <set>
27#include <string>
28#include <unordered_map>
29
31
45{
46public:
48 ACRegistryKey(const std::string& componentNameIn, G4double rigidityIn);
49 ~ACRegistryKey() = default;
50 ACRegistryKey(const ACRegistryKey& other) = default;
51 bool operator==(const ACRegistryKey& other) const;
52 friend std::ostream& operator<<(std::ostream &out, ACRegistryKey const &k);
53
54 std::string componentName;
55 G4double rigidity;
56 static G4double tolerance;
57};
58
60namespace std
61{
62 template <>
63 struct hash<ACRegistryKey>
64 {
65 std::size_t operator()(const ACRegistryKey& k) const
66 {
67 std::size_t h1 = std::hash<std::string>()(k.componentName);
68 std::size_t h2 = std::hash<double>()(k.rigidity);
69 std::size_t ht = h1 ^ (h2 + 0x9e3779b9 + (h1<<6) + (h1>>2));
70 return ht;
71 }
72 };
73}
74
92{
93private:
94 // Typedefs up first so we can declare public iterators.
98 typedef std::unordered_map<ACRegistryKey, BDSAcceleratorComponent*> RegistryMap;
99
102
105
106public:
109
113
122 G4double rigidtyAtConstructionTime,
123 bool isModified = false);
124
126 G4bool IsRegistered(BDSAcceleratorComponent* component, G4double rigidtyAtConstructionTime);
127
129 G4bool IsRegistered(const G4String& componentName, G4double rigidtyAtConstructionTime);
130
132 G4bool IsRegisteredAllocated(const BDSAcceleratorComponent* componentName) const;
133
137 BDSAcceleratorComponent* GetComponent(const G4String& name, G4double rigidtyAtConstructionTime);
138
142
146
149 std::unordered_map<ACRegistryKey, BDSAcceleratorComponent*> AllComponentsIncludingUnique() const;
150
151 G4int AlreadyRegisteredNameCount(const G4String& name) const;
152
156 typedef RegistryMap::iterator iterator;
157 typedef RegistryMap::const_iterator const_iterator;
158 iterator begin() {return registry.begin();}
159 iterator end() {return registry.end();}
160 const_iterator begin() const {return registry.begin();}
161 const_iterator end() const {return registry.end();}
162 G4bool empty() const {return registry.empty();}
164
166 size_t size() const {return registry.size();}
167
169 friend std::ostream& operator<<(std::ostream &out, BDSAcceleratorComponentRegistry const &r);
170
172 void PrintNumberOfEachType() const;
173
174private:
177
180
184
186 std::set<BDSAcceleratorComponent*> allocatedComponents;
187
189 std::set<BDSAcceleratorComponent*> curvilinearComponents;
190
191 std::set<BDSAcceleratorComponent*> tunnelComponents;
192
197 std::unordered_map<std::string, int> typeCounter;
198
201 std::unordered_map<std::string, int> nameCounter;
202};
203
204#endif
custom key class for pair of <name,rigidity>.
std::string componentName
Has to be std::string for hashing as G4String doesn't provide one.
A registry of constructed BDSAcceleratorComponent instances that can be searched.
void RegisterCurvilinearComponent(BDSAcceleratorComponent *component)
static BDSAcceleratorComponentRegistry * Instance()
Singleton accessor.
void RegisterTunnelComponent(BDSAcceleratorComponent *component)
std::set< BDSAcceleratorComponent * > allocatedComponents
Set of created components not in registry, for memory management.
std::set< BDSAcceleratorComponent * > curvilinearComponents
Set of curvilinear components - purely for memory management.
friend std::ostream & operator<<(std::ostream &out, BDSAcceleratorComponentRegistry const &r)
Output stream.
static BDSAcceleratorComponentRegistry * instance
The singleton instance.
std::unordered_map< ACRegistryKey, BDSAcceleratorComponent * > RegistryMap
G4bool IsRegisteredAllocated(const BDSAcceleratorComponent *componentName) const
Check if a unique component is registered in the allocatedComponents.
std::unordered_map< ACRegistryKey, BDSAcceleratorComponent * > AllComponentsIncludingUnique() const
void PrintNumberOfEachType() const
Print out the number of each type of component registered.
std::unordered_map< std::string, int > typeCounter
BDSAcceleratorComponent * GetComponent(const G4String &name, G4double rigidtyAtConstructionTime)
void RegisterComponent(BDSAcceleratorComponent *component, G4double rigidtyAtConstructionTime, bool isModified=false)
G4bool IsRegistered(BDSAcceleratorComponent *component, G4double rigidtyAtConstructionTime)
Check whether an accelerator component is already registered.
BDSAcceleratorComponentRegistry()
Default constructor is private as singleton.
std::unordered_map< std::string, int > nameCounter
BDSAcceleratorComponentRegistry & operator=(const BDSAcceleratorComponentRegistry &)
assignment and copy constructor not implemented nor used
RegistryMap registry
Registry is a map - note 'register' is a protected keyword.
RegistryMap registryForAllocated
A map for absolutely everything including components that are unique.
Abstract class that represents a component of an accelerator.
STL namespace.
std::size_t operator()(const ACRegistryKey &k) const