GENIEGenerator
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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>

Collaboration diagram for genie::flux::GFluxDriverFactory:
Collaboration graph
[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_t
fFunctionMap
 
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 fnal.gov> Fermi National Accelerator Laboratory
Created:
License:
Copyright (c) 2003-2024, 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.

References fgTheInstance.

Referenced by Instance().

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.

References fgTheInstance.

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.

References fFunctionMap, and listnames.

Referenced by main(), and PrintConfig().

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
GFluxI * genie::flux::GFluxDriverFactory::GetFluxDriver ( const std::string &  name)

Definition at line 48 of file GFluxDriverFactory.cxx.

References fFunctionMap, LOG, and pWARN.

Referenced by main().

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
const char * name
std::map< std::string, GFluxICtorFuncPtr_t > fFunctionMap
genie::GFluxI *(* GFluxICtorFuncPtr_t)()
GFluxDriverFactory & genie::flux::GFluxDriverFactory::Instance ( void  )
static

Definition at line 33 of file GFluxDriverFactory.cxx.

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

Referenced by main().

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.

References fFunctionMap.

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 }
const char * name
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.

References AvailableFluxDrivers().

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
bool genie::flux::GFluxDriverFactory::RegisterCreator ( std::string  name,
GFluxICtorFuncPtr_t  ctorptr,
bool *  ptr 
)

Definition at line 93 of file GFluxDriverFactory.cxx.

References fBoolPtrMap, and fFunctionMap.

96 {
97  // record new functions for creating processes
98  fFunctionMap[name] = foo;
99  fBoolPtrMap[name] = boolptr;
100  return true;
101 }
const char * name
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.

Referenced by RegisterCreator().

std::map<std::string, GFluxICtorFuncPtr_t> genie::flux::GFluxDriverFactory::fFunctionMap
private
GFluxDriverFactory * genie::flux::GFluxDriverFactory::fgTheInstance
staticprivate
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: