GFluxDriverFactory.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file GFluxDriverFactory.cxx
3 /// \brief factory for generating GENIE GFluxI class objects
4 ///
5 /// \version
6 /// \author Robert Hatcher <rhatcher \at fnal.gov>
7 /// Fermi National Accelerator Laboratory
8 ///
9 /// \update 2012-08-03 initial version
10 ////////////////////////////////////////////////////////////////////////
11 
13 
15 #include <iostream>
16 #include <iomanip>
17 
18 namespace genie {
19 namespace flux {
20 
21 // Define static variable which holds the one-and-only instance
22 GFluxDriverFactory* GFluxDriverFactory::fgTheInstance;
23 
25 {
26  fgTheInstance = this; // record created self in static pointer
27 }
28 
30 {
31  fgTheInstance = 0;
32 }
33 
35 {
36  // Cleaner dtor calls GFluxDriverFactory dtor at job end
37  static Cleaner cleaner;
38 
39  if ( ! fgTheInstance ) {
40  // need to create one
41  cleaner.UseMe(); // dummy call to quiet compiler warnings
43  }
44 
45  return *fgTheInstance;
46 }
47 
48 GFluxI*
50 {
51  GFluxI* p = 0;
52 
53  // we don't want map creating an entry if it doesn't exist
54  // so use map::find() not map::operator[]
55  std::map<std::string, GFluxICtorFuncPtr_t>::iterator itr
56  = fFunctionMap.find(name);
57  if ( fFunctionMap.end() != itr ) {
58  // found an appropriate entry in the list
59  GFluxICtorFuncPtr_t foo = itr->second; // this is the function
60  p = (*foo)(); // use function to create the GFluxI
61  }
62  if ( ! p ) {
63  LOG("Flux",pWARN) << "### GFluxDriverFactory WARNING: "
64  << "GFluxI " << name << " is not known";
65  }
66  return p;
67 }
68 
70 {
71  // check if we know the name
72  bool res = false;
73  std::map<std::string, GFluxICtorFuncPtr_t>::iterator itr
74  = fFunctionMap.find(name);
75  if ( fFunctionMap.end() != itr ) res = true;
76  return res;
77 }
78 
79 const std::vector<std::string>&
81 {
82  // list of names might be out of date due to new registrations
83  // rescan the std::map on each call (which won't be frequent)
84  listnames.clear();
85 
86  // scan map for registered names
87  std::map<std::string, GFluxICtorFuncPtr_t>::const_iterator itr;
88  for ( itr = fFunctionMap.begin(); itr != fFunctionMap.end(); ++itr )
89  listnames.push_back(itr->first);
90 
91  return listnames;
92 }
93 
96  bool* boolptr)
97 {
98  // record new functions for creating processes
99  fFunctionMap[name] = foo;
100  fBoolPtrMap[name] = boolptr;
101  return true;
102 }
103 
105 {
106  const std::vector<std::string>& avail = AvailableFluxDrivers();
107  size_t n = avail.size();
108  std::cout << "GFluxDriverFactory has the following drivers registered:"
109  << std::endl;
110  for (size_t i=0; i<n; ++i) {
111  std::cout << " [" << std::setw(3) << i << "] "
112  << avail[i] << std::endl;
113  }
114 }
115 
116 } // namespace flux
117 } // namespace genie
const XML_Char * name
Definition: expat.h:151
A class for generating concrete GFluxI 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.
THE MAIN GENIE PROJECT NAMESPACE
Definition: GeneratorBase.h:8
const char * p
Definition: xmltok.h:285
static GFluxDriverFactory * fgTheInstance
const std::vector< std::string > & AvailableFluxDrivers() const
genie::GFluxI *(* GFluxICtorFuncPtr_t)()
Loaders::FluxType flux
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:97
std::vector< std::string > listnames
#define pWARN
Definition: Messenger.h:61
OStream cout
Definition: OStream.cxx:6
static GFluxDriverFactory & Instance()
std::map< std::string, GFluxICtorFuncPtr_t > fFunctionMap
bool RegisterCreator(std::string name, GFluxICtorFuncPtr_t ctorptr, bool *ptr)
std::map< std::string, bool * > fBoolPtrMap
bool IsKnownFluxDriver(const std::string &)
genie::GFluxI * GetFluxDriver(const std::string &)
GENIE Interface for user-defined flux classes.
Definition: GFluxI.h:37
enum BeamMode string