UserActionFactory.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file UserActionFactory.cxx
3 /// \brief factory for generating G4Base/UserAction class objects
4 ///
5 /// \version
6 /// \author Robert Hatcher <rhatcher \at fnal.gov>
7 /// Fermi National Accelerator Laboratory
8 ///
9 /// update 2012-08-17 initial version
10 ////////////////////////////////////////////////////////////////////////
11 
12 #include "nutools/G4Base/UserActionFactory.h"
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*
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 
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 
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
static QCString name
Definition: declinfo.cpp:673
intermediate_table::iterator iterator
bool IsKnownUserAction(const std::string &)
std::vector< std::string > listnames
std::string string
Definition: nybbler.cc:12
intermediate_table::const_iterator const_iterator
static UserActionFactory & Instance()
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
p
Definition: test.py:223
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
bool RegisterCreator(std::string name, UserActionCtorFuncPtr_t ctorptr, bool *ptr)
g4b::UserAction * GetUserAction(const std::string &)