Classes | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
genie::flux::GFluxDriverFactory Class Reference

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. More...

#include <GFluxDriverFactory.h>

Classes

struct  Cleaner
 

Public Member Functions

genie::GFluxIGetFluxDriver (const std::string &)
 
bool IsKnownFluxDriver (const std::string &)
 
const std::vector< std::string > & AvailableFluxDrivers () const
 
bool RegisterCreator (std::string name, GFluxICtorFuncPtr_t ctorptr, bool *ptr)
 
void PrintConfig () const
 

Static Public Member Functions

static GFluxDriverFactoryInstance ()
 

Private Member Functions

 GFluxDriverFactory ()
 
virtual ~GFluxDriverFactory ()
 
 GFluxDriverFactory (const GFluxDriverFactory &)
 
void operator= (const GFluxDriverFactory &)
 

Private Attributes

std::map< std::string, GFluxICtorFuncPtr_tfFunctionMap
 
std::map< std::string, bool * > fBoolPtrMap
 
std::vector< std::stringlistnames
 

Static Private Attributes

static GFluxDriverFactoryfgTheInstance
 

Friends

struct Cleaner
 

Detailed Description

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.

Implemented as a singleton holding a map between names and pointers-to-functions (that call a class default constructor). The functions pointers must return GFluxI*.

Author
Robert Hatcher <rhatcher fnal.gov> Fermi National Accelerator Laboratory

Copyright (c) 2003-2020, The GENIE Collaboration for the full text of the license visit http://copyright.genie-mc.org

Definition at line 46 of file GFluxDriverFactory.h.

Constructor & Destructor Documentation

genie::flux::GFluxDriverFactory::GFluxDriverFactory ( )
private

Definition at line 23 of file GFluxDriverFactory.cxx.

24 {
25  fgTheInstance = this; // record created self in static pointer
26 }
static GFluxDriverFactory * fgTheInstance
genie::flux::GFluxDriverFactory::~GFluxDriverFactory ( )
privatevirtual

Definition at line 28 of file GFluxDriverFactory.cxx.

29 {
30  fgTheInstance = 0;
31 }
static GFluxDriverFactory * fgTheInstance
genie::flux::GFluxDriverFactory::GFluxDriverFactory ( const GFluxDriverFactory )
private

Member Function Documentation

const std::vector< std::string > & genie::flux::GFluxDriverFactory::AvailableFluxDrivers ( ) const

Definition at line 79 of file GFluxDriverFactory.cxx.

80 {
81  // list of names might be out of date due to new registrations
82  // rescan the std::map on each call (which won't be frequent)
83  listnames.clear();
84 
85  // scan map for registered names
87  for ( itr = fFunctionMap.begin(); itr != fFunctionMap.end(); ++itr )
88  listnames.push_back(itr->first);
89 
90  return listnames;
91 }
intermediate_table::const_iterator const_iterator
std::vector< std::string > listnames
std::map< std::string, GFluxICtorFuncPtr_t > fFunctionMap
GFluxI * genie::flux::GFluxDriverFactory::GetFluxDriver ( const std::string name)

Definition at line 48 of file GFluxDriverFactory.cxx.

49 {
50  GFluxI* p = 0;
51 
52  // we don't want map creating an entry if it doesn't exist
53  // so use map::find() not map::operator[]
55  = fFunctionMap.find(name);
56  if ( fFunctionMap.end() != itr ) {
57  // found an appropriate entry in the list
58  GFluxICtorFuncPtr_t foo = itr->second; // this is the function
59  p = (*foo)(); // use function to create the GFluxI
60  }
61  if ( ! p ) {
62  LOG("Flux",pWARN) << "### GFluxDriverFactory WARNING: "
63  << "GFluxI " << name << " is not known";
64  }
65  return p;
66 }
static QCString name
Definition: declinfo.cpp:673
intermediate_table::iterator iterator
genie::GFluxI *(* GFluxICtorFuncPtr_t)()
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:96
p
Definition: test.py:223
#define pWARN
Definition: Messenger.h:60
std::map< std::string, GFluxICtorFuncPtr_t > fFunctionMap
GFluxDriverFactory & genie::flux::GFluxDriverFactory::Instance ( void  )
static

Definition at line 33 of file GFluxDriverFactory.cxx.

34 {
35  // Cleaner dtor calls GFluxDriverFactory dtor at job end
36  static Cleaner cleaner;
37 
38  if ( ! fgTheInstance ) {
39  // need to create one
40  cleaner.UseMe(); // dummy call to quiet compiler warnings
42  }
43 
44  return *fgTheInstance;
45 }
static GFluxDriverFactory * fgTheInstance
bool genie::flux::GFluxDriverFactory::IsKnownFluxDriver ( const std::string name)

Definition at line 68 of file GFluxDriverFactory.cxx.

69 {
70  // check if we know the name
71  bool res = false;
73  = fFunctionMap.find(name);
74  if ( fFunctionMap.end() != itr ) res = true;
75  return res;
76 }
static QCString name
Definition: declinfo.cpp:673
intermediate_table::iterator iterator
std::map< std::string, GFluxICtorFuncPtr_t > fFunctionMap
void genie::flux::GFluxDriverFactory::operator= ( const GFluxDriverFactory )
private
void genie::flux::GFluxDriverFactory::PrintConfig ( void  ) const

Definition at line 103 of file GFluxDriverFactory.cxx.

104 {
105  const std::vector<std::string>& avail = AvailableFluxDrivers();
106  size_t n = avail.size();
107  std::cout << "GFluxDriverFactory has the following drivers registered:"
108  << std::endl;
109  for (size_t i=0; i<n; ++i) {
110  std::cout << " [" << std::setw(3) << i << "] "
111  << avail[i] << std::endl;
112  }
113 }
const std::vector< std::string > & AvailableFluxDrivers() const
std::void_t< T > n
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
QTextStream & endl(QTextStream &s)
bool genie::flux::GFluxDriverFactory::RegisterCreator ( std::string  name,
GFluxICtorFuncPtr_t  ctorptr,
bool ptr 
)

Definition at line 93 of file GFluxDriverFactory.cxx.

96 {
97  // record new functions for creating processes
98  fFunctionMap[name] = foo;
99  fBoolPtrMap[name] = boolptr;
100  return true;
101 }
static QCString name
Definition: declinfo.cpp:673
std::map< std::string, GFluxICtorFuncPtr_t > fFunctionMap
std::map< std::string, bool * > fBoolPtrMap

Friends And Related Function Documentation

friend struct Cleaner
friend

Definition at line 102 of file GFluxDriverFactory.h.

Member Data Documentation

std::map<std::string, bool*> genie::flux::GFluxDriverFactory::fBoolPtrMap
private

Definition at line 74 of file GFluxDriverFactory.h.

std::map<std::string, GFluxICtorFuncPtr_t> genie::flux::GFluxDriverFactory::fFunctionMap
private

Definition at line 71 of file GFluxDriverFactory.h.

GFluxDriverFactory * genie::flux::GFluxDriverFactory::fgTheInstance
staticprivate

Definition at line 68 of file GFluxDriverFactory.h.

std::vector<std::string> genie::flux::GFluxDriverFactory::listnames
mutableprivate

Definition at line 76 of file GFluxDriverFactory.h.


The documentation for this class was generated from the following files: