19#include "BDSAcceleratorComponentRegistry.hh"
22#include "BDSUtilities.hh"
29G4double ACRegistryKey::tolerance = 1e-7;
40ACRegistryKey::ACRegistryKey():
45ACRegistryKey::ACRegistryKey(
const std::string& componentNameIn,
47 componentName(componentNameIn),
51bool ACRegistryKey::operator==(
const ACRegistryKey& other)
const
54 G4bool v2 = std::abs(other.rigidity - rigidity) < tolerance;
58std::ostream& operator<<(std::ostream& out,
ACRegistryKey const& k)
60 out <<
"\"" << k.
componentName <<
"\" at Brho = " << k.rigidity;
71 G4cout << __METHOD_NAME__ <<
"size of registry " <<
registry.size() << G4endl;
79 for (
auto ac : tunnelComponents)
86 G4double rigidtyAtConstructionTime,
103 for (
const auto element : *line)
113 registry[{component->
GetName(),rigidtyAtConstructionTime}] = component;
119 for (
const auto element : *line)
123 G4cout << __METHOD_NAME__ <<
"size of registry " <<
registry.size() << G4endl;
128 G4double rigidtyAtConstructionTime)
139 G4double rigidtyAtConstructionTime)
141 auto search =
registry.find({name,rigidtyAtConstructionTime});
146 G4double rigidtyAtConstructionTime)
149 {
return registry.at({name,rigidtyAtConstructionTime});}
150 catch (
const std::out_of_range& )
152 G4cerr << __METHOD_NAME__ <<
"unknown component named: \"" << name <<
"\"" << G4endl;
164 tunnelComponents.insert(component);
169 std::unordered_map<ACRegistryKey, BDSAcceleratorComponent*> result;
175G4int BDSAcceleratorComponentRegistry::AlreadyRegisteredNameCount(
const G4String& name)
const
184 std::ios_base::fmtflags ff = out.flags();
185 out <<
"Accelerator Component Registry:" << G4endl;
187 {out << std::left << std::setw(15) << it.second->GetType() << it.first << G4endl;}
194 G4cout << __METHOD_NAME__ << G4endl;
196 {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)