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) Royal Holloway,
3University of London 2001 - 2024.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#ifndef BDSACCELERATORCOMPONENTREGISTRY_H
20#define BDSACCELERATORCOMPONENTREGISTRY_H
21
22#include "globals.hh" // geant4 globals / types
23
24#include <functional> // for all the std::hash business
25#include <iterator>
26#include <map>
27#include <set>
28#include <string>
29#include <unordered_map>
30
32
46{
47public:
49 ACRegistryKey(const std::string& componentNameIn, G4double rigidityIn);
50 ~ACRegistryKey() = default;
51 ACRegistryKey(const ACRegistryKey& other) = default;
52 bool operator==(const ACRegistryKey& other) const;
53 friend std::ostream& operator<<(std::ostream &out, ACRegistryKey const &k);
54
55 std::string componentName;
56 G4double rigidity;
57 static G4double tolerance;
58};
59
61namespace std
62{
63 template <>
64 struct hash<ACRegistryKey>
65 {
66 std::size_t operator()(const ACRegistryKey& k) const
67 {
68 std::size_t h1 = std::hash<std::string>()(k.componentName);
69 std::size_t h2 = std::hash<double>()(k.rigidity);
70 std::size_t ht = h1 ^ (h2 + 0x9e3779b9 + (h1<<6) + (h1>>2));
71 return ht;
72 }
73 };
74}
75
93{
94private:
95 // Typedefs up first so we can declare public iterators.
99 typedef std::unordered_map<ACRegistryKey, BDSAcceleratorComponent*> RegistryMap;
100
103
106
107public:
110
114
123 G4double rigidtyAtConstructionTime,
124 bool isModified = false);
125
127 G4bool IsRegistered(BDSAcceleratorComponent* component, G4double rigidtyAtConstructionTime);
128
130 G4bool IsRegistered(const G4String& componentName, G4double rigidtyAtConstructionTime);
131
133 G4bool IsRegisteredAllocated(const BDSAcceleratorComponent* componentName) const;
134
138 BDSAcceleratorComponent* GetComponent(const G4String& name, G4double rigidtyAtConstructionTime);
139
143
147
150 std::unordered_map<ACRegistryKey, BDSAcceleratorComponent*> AllComponentsIncludingUnique() const;
151
152 G4int AlreadyRegisteredNameCount(const G4String& name) const;
153
157 typedef RegistryMap::iterator iterator;
158 typedef RegistryMap::const_iterator const_iterator;
159 iterator begin() {return registry.begin();}
160 iterator end() {return registry.end();}
161 const_iterator begin() const {return registry.begin();}
162 const_iterator end() const {return registry.end();}
163 G4bool empty() const {return registry.empty();}
165
167 size_t size() const {return registry.size();}
168
170 friend std::ostream& operator<<(std::ostream &out, BDSAcceleratorComponentRegistry const &r);
171
173 void PrintNumberOfEachType() const;
174
175private:
178
181
185
187 std::set<BDSAcceleratorComponent*> allocatedComponents;
188
190 std::set<BDSAcceleratorComponent*> curvilinearComponents;
191
192 std::set<BDSAcceleratorComponent*> tunnelComponents;
193
198 std::unordered_map<std::string, int> typeCounter;
199
202 std::unordered_map<std::string, int> nameCounter;
203};
204
205#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