LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
UserActionFactory.cxx
Go to the documentation of this file.
1 
13 
15 
16 namespace g4b {
17 
18 // Define static variable which holds the one-and-only instance
19 UserActionFactory* UserActionFactory::fgTheInstance;
20 
22 {
23  fgTheInstance = this; // record created self in static pointer
24 }
25 
27 {
28  fgTheInstance = 0;
29 }
30 
32 {
33  // Cleaner dtor calls UserActionFactory dtor at job end
34  static Cleaner cleaner;
35 
36  if ( ! fgTheInstance ) {
37  // need to create one
38  cleaner.UseMe(); // dummy call to quiet compiler warnings
40  }
41 
42  return *fgTheInstance;
43 }
44 
45 UserAction*
46 UserActionFactory::GetUserAction(const std::string& name)
47 {
48  UserAction* p = 0;
49 
50  // we don't want map creating an entry if it doesn't exist
51  // so use map::find() not map::operator[]
53  = fFunctionMap.find(name);
54  if ( fFunctionMap.end() != itr ) {
55  // found an appropriate entry in the list
56  UserActionCtorFuncPtr_t foo = itr->second; // this is the function
57  p = (*foo)(); // use function to create the UserAction
58  p->SetName(name); // let object know its name
59  }
60  if ( ! p ) {
61  mf::LogWarning("UserAction") << "### UserActionFactory WARNING: "
62  << "UserAction " << name << " is not known";
63  }
64  return p;
65 }
66 
67 bool UserActionFactory::IsKnownUserAction(const std::string& name)
68 {
69  // check if we know the name
70  bool res = false;
72  = fFunctionMap.find(name);
73  if ( fFunctionMap.end() != itr ) res = true;
74  return res;
75 }
76 
77 const std::vector<std::string>&
79 {
80  // list of names might be out of date due to new registrations
81  // rescan the std::map on each call (which won't be frequent)
82  listnames.clear();
83 
84  // scan map for registered names
86  for ( itr = fFunctionMap.begin(); itr != fFunctionMap.end(); ++itr )
87  listnames.push_back(itr->first);
88 
89  return listnames;
90 }
91 
92 bool UserActionFactory::RegisterCreator(std::string name,
94  bool* boolptr)
95 {
96  // record new functions for creating processes
97  fFunctionMap[name] = foo;
98  fBoolPtrMap[name] = boolptr;
99  return true;
100 }
101 
102 } // namespace g4b
intermediate_table::iterator iterator
bool IsKnownUserAction(const std::string &)
std::vector< std::string > listnames
intermediate_table::const_iterator const_iterator
static UserActionFactory & Instance()
A class for generating concrete UserAction derived classes based on the factory pattern. This code supplies a CPP macro which allows the classes to self-register and thus no modification of this class is needed in order to expand the list of classes it knows about.
std::map< std::string, UserActionCtorFuncPtr_t > fFunctionMap
void SetName(std::string const &name)
Definition: UserAction.h:99
const std::vector< std::string > & AvailableUserActions() const
std::map< std::string, bool * > fBoolPtrMap
g4b::UserAction *(* UserActionCtorFuncPtr_t)()
static UserActionFactory * fgTheInstance
basic interface to Geant4 for ART-based software
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
bool RegisterCreator(std::string name, UserActionCtorFuncPtr_t ctorptr, bool *ptr)
g4b::UserAction * GetUserAction(const std::string &)