GENIEGenerator
Loading...
Searching...
No Matches
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>

Collaboration diagram for genie::flux::GFluxDriverFactory:
[legend]

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::string > listnames

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 \at fnal.gov> Fermi National Accelerator Laboratory
Created:\n
License:\n Copyright (c) 2003-2025, 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

◆ GFluxDriverFactory() [1/2]

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

References fgTheInstance.

Referenced by GFluxDriverFactory(), Instance(), and operator=().

◆ ~GFluxDriverFactory()

genie::flux::GFluxDriverFactory::~GFluxDriverFactory ( )
privatevirtual

Definition at line 28 of file GFluxDriverFactory.cxx.

29{
30 fgTheInstance = 0;
31}

References fgTheInstance.

◆ GFluxDriverFactory() [2/2]

genie::flux::GFluxDriverFactory::GFluxDriverFactory ( const GFluxDriverFactory & )
private

References GFluxDriverFactory().

Member Function Documentation

◆ AvailableFluxDrivers()

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
86 std::map<std::string, GFluxICtorFuncPtr_t>::const_iterator itr;
87 for ( itr = fFunctionMap.begin(); itr != fFunctionMap.end(); ++itr )
88 listnames.push_back(itr->first);
89
90 return listnames;
91}
std::vector< std::string > listnames
std::map< std::string, GFluxICtorFuncPtr_t > fFunctionMap

References fFunctionMap, and listnames.

Referenced by main(), and PrintConfig().

◆ GetFluxDriver()

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[]
54 std::map<std::string, GFluxICtorFuncPtr_t>::iterator itr
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}
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE,...
Definition Messenger.h:96
#define pWARN
Definition Messenger.h:60
genie::GFluxI *(* GFluxICtorFuncPtr_t)()

References fFunctionMap, LOG, and pWARN.

Referenced by main().

◆ Instance()

GFluxDriverFactory & genie::flux::GFluxDriverFactory::Instance ( )
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}

References fgTheInstance, GFluxDriverFactory(), and genie::flux::GFluxDriverFactory::Cleaner::UseMe().

Referenced by main().

◆ IsKnownFluxDriver()

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;
72 std::map<std::string, GFluxICtorFuncPtr_t>::iterator itr
73 = fFunctionMap.find(name);
74 if ( fFunctionMap.end() != itr ) res = true;
75 return res;
76}

References fFunctionMap.

◆ operator=()

void genie::flux::GFluxDriverFactory::operator= ( const GFluxDriverFactory & )
private

References GFluxDriverFactory().

◆ PrintConfig()

void genie::flux::GFluxDriverFactory::PrintConfig ( ) 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

References AvailableFluxDrivers().

◆ RegisterCreator()

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}
std::map< std::string, bool * > fBoolPtrMap

References fBoolPtrMap, and fFunctionMap.

◆ Cleaner

friend struct Cleaner
friend

Definition at line 102 of file GFluxDriverFactory.h.

Member Data Documentation

◆ fBoolPtrMap

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

Definition at line 74 of file GFluxDriverFactory.h.

Referenced by RegisterCreator().

◆ fFunctionMap

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

◆ fgTheInstance

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

◆ listnames

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

Definition at line 76 of file GFluxDriverFactory.h.

Referenced by AvailableFluxDrivers().


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