18#include "BDSAcceleratorComponentRegistry.hh"
21#include "BDSUtilities.hh"
28G4double ACRegistryKey::tolerance = 1e-7;
39ACRegistryKey::ACRegistryKey():
44ACRegistryKey::ACRegistryKey(
const std::string& componentNameIn,
46 componentName(componentNameIn),
50bool ACRegistryKey::operator==(
const ACRegistryKey& other)
const
53 G4bool v2 = std::abs(other.rigidity - rigidity) < tolerance;
57std::ostream& operator<<(std::ostream& out,
ACRegistryKey const& k)
59 out <<
"\"" << k.
componentName <<
"\" at Brho = " << k.rigidity;
70 G4cout << __METHOD_NAME__ <<
"size of registry " <<
registry.size() << G4endl;
78 for (
auto ac : tunnelComponents)
85 G4double rigidtyAtConstructionTime,
102 for (
const auto element : *line)
112 registry[{component->
GetName(),rigidtyAtConstructionTime}] = component;
118 for (
const auto element : *line)
122 G4cout << __METHOD_NAME__ <<
"size of registry " <<
registry.size() << G4endl;
127 G4double rigidtyAtConstructionTime)
138 G4double rigidtyAtConstructionTime)
140 auto search =
registry.find({name,rigidtyAtConstructionTime});
145 G4double rigidtyAtConstructionTime)
148 {
return registry.at({name,rigidtyAtConstructionTime});}
149 catch (
const std::out_of_range& )
151 G4cerr << __METHOD_NAME__ <<
"unknown component named: \"" << name <<
"\"" << G4endl;
163 tunnelComponents.insert(component);
168 std::unordered_map<ACRegistryKey, BDSAcceleratorComponent*> result;
174G4int BDSAcceleratorComponentRegistry::AlreadyRegisteredNameCount(
const G4String& name)
const
183 std::ios_base::fmtflags ff = out.flags();
184 out <<
"Accelerator Component Registry:" << G4endl;
186 {out << std::left << std::setw(15) << it.second->GetType() << it.first << G4endl;}
193 G4cout << __METHOD_NAME__ << G4endl;
195 {G4cout << std::setw(20) << kv.first <<
" : " << kv.second << G4endl;}
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.
static BDSAcceleratorComponentRegistry * instance
The singleton instance.
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.
~BDSAcceleratorComponentRegistry()
std::unordered_map< std::string, int > nameCounter
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.
virtual G4String GetName() const
The name of the component without modification.
G4String GetType() const
Get a string describing the type of the component.
A class that hold multiple accelerator components.
V MapGetWithDefault(const std::map< K, V > &m, const K &key, const V &defaultValue)