GENIEGenerator
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GFlavorMixerFactory.h
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*!
3 
4 \class genie::flux::GFlavorMixerFactory
5 
6 \brief A class for generating concrete GFlavorMixerI derived classes
7  based on the factory pattern. This code supplies a CPP
8  macro which allows the classes to self-register and thus
9  no modification of this class is needed in order to expand
10  the list of classes it knows about.
11 
12  Implemented as a singleton holding a map between names and
13  pointers-to-functions (that call a class default constructor).
14  The functions pointers must return GFlavorMixerI*.
15 
16 \author Robert Hatcher <rhatcher \at fnal.gov>
17  Fermi National Accelerator Laboratory
18 
19 \created
20 
21 \cpright Copyright (c) 2003-2024, The GENIE Collaboration
22  for the full text of the license visit http://copyright.genie-mc.org
23 */
24 //____________________________________________________________________________
25 
26 #ifndef GENIE_FLUX_GFLAVORMIXERFACTORY_H
27 #define GENIE_FLUX_GFLAVORMIXERFACTORY_H
28 
29 #include <string>
30 #include <vector>
31 #include <map>
32 
34 
35 namespace genie {
36 namespace flux {
37 
38 // define a type for the pointer to a function that returns a
39 // genie::flux::GFlavorMixerI*
40 // i.e. calls the (typically default) ctor for the class.
41 typedef genie::flux::GFlavorMixerI* (*GFlavorMixerICtorFuncPtr_t)();
42 
44 {
45 public:
46  static GFlavorMixerFactory& Instance();
47  // no public ctor for singleton, all user access is through Instance()
48 
49  genie::flux::GFlavorMixerI* GetFlavorMixer(const std::string&);
50  // instantiate a PhysProc by name
51 
52  bool IsKnownFlavorMixer(const std::string&);
53  // check if the name is in the list of names
54 
55  const std::vector<std::string>& AvailableFlavorMixers() const;
56  // return a list of available names
57 
58  bool RegisterCreator(std::string name,
59  GFlavorMixerICtorFuncPtr_t ctorptr, bool* ptr);
60  // register a new GFlavorMixerI type by passing pointer to creator function
61 
62 private:
64  // the one and only instance
65 
66  std::map<std::string, GFlavorMixerICtorFuncPtr_t> fFunctionMap;
67  // mapping between known class names and a registered ctor function
68 
69  std::map<std::string, bool*> fBoolPtrMap;
70 
71  mutable std::vector<std::string> listnames;
72  // copy of list of names, used solely due to AvailableFlavorMixers()
73  // method returning a const reference rather than a vector object.
74  // mutable because AvailableFlavorMixers() is const, but list might
75  // need recreation if new entries have been registered.
76 
77 private:
79  // private ctor, users access class via Instance()
80 
81  virtual ~GFlavorMixerFactory();
82 
84  // method private and not implement, declared to prevent copying
85 
86  void operator=(const GFlavorMixerFactory&);
87  // method private and not implement, declared to prevent assignment
88 
89  // sub-class Cleaner struct is used to clean up singleton at the end of job.
90  struct Cleaner {
91  void UseMe() { } // Dummy method to quiet compiler
96  } } };
97  friend struct Cleaner;
98 
99 };
100 
101 } // namespace flux
102 } // namespcae genie
103 
104 // Define macro to create a function to call the class' ctor
105 // and then registers this function with the factory instance for later use
106 // Users should have in their myPhyList.cc two lines that look like:
107 // #include "GFlavorMixerFactory.h"
108 // FLAVORMIXREG(MyMixerClass) // no semicolon
109 // where "MyMixerClass" is the name of the class (assuming no special namespace)
110 // If the class is defined in a namespace (or two) use:
111 // #include "GFlavorMixerFactory.h"
112 // FLAVORMIXREG3(myspace,myAltMixer,myspace::myAltMixer) // no semicolon
113 // FLAVORMIXREG4(genie,flux,YAMixer,genie::flux::YAMixer) // no semicolon
114 // and either can then be retrieved from the factory using:
115 // GFlavorMixerFactory& factory =
116 // GFlavorMixerFactory::Instance();
117 // genie::flux::GFlavorMixerI* p = 0;
118 // p = factory.GetFlavorMixer("MyMixerClass");
119 // p = factory.GetFlavorMixer("myspace::myAltMixer");
120 // p = factory.GetFlavorMixer("genie::flux::YAMixer");
121 //
122 // The expanded code looks like:
123 // genie::flux::GFlavorMixerI* MyMixerClass_ctor_function () { return new MyMixerClass; }
124 // static bool MyMixerClass_creator_registered =
125 // GFlavorMixerFactory::Instance().RegisterCreator("MyMixerClass",
126 // & MyMixerClass_ctor_function );
127 // namespace myspace {
128 // genie::flux::GFlavorMixerI* myAltAltMixer_ctor_function () { return new myspace::myAltAltMixer; }
129 // static bool myAltMixer_creator_registered =
130 // GFlavorMixerFactory::Instance().RegisterCreator("myspace::myAltAltMixer",
131 // & myspace::myAltAltMixer_ctor_function ); }
132 
133 #define FLAVORMIXREG( _name ) \
134  genie::flux::GFlavorMixerI* _name ## _ctor_function () { return new _name; } \
135  static bool _name ## _creator_registered = \
136  genie::flux::GFlavorMixerFactory::Instance().RegisterCreator(# _name, \
137  & _name ## _ctor_function, \
138  & _name ## _creator_registered );
139 
140 #define FLAVORMIXREG3( _ns, _name, _fqname ) \
141 namespace _ns { \
142  genie::flux::GFlavorMixerI* _name ## _ctor_function () { return new _fqname; } \
143  static bool _name ## _creator_registered = \
144  genie::flux::GFlavorMixerFactory::Instance().RegisterCreator(# _fqname, \
145  & _fqname ## _ctor_function, \
146  & _fqname ## _creator_registered );}
147 
148 #define FLAVORMIXREG4( _nsa, _nsb, _name, _fqname ) \
149 namespace _nsa { \
150  namespace _nsb { \
151  genie::flux::GFlavorMixerI* _name ## _ctor_function () { return new _fqname; } \
152  static bool _name ## _creator_registered = \
153  genie::flux::GFlavorMixerFactory::Instance().RegisterCreator(# _fqname, \
154  & _fqname ## _ctor_function, \
155  & _fqname ## _creator_registered );}}
156 #endif
static GFlavorMixerFactory & Instance()
static GFlavorMixerFactory * fgTheInstance
genie::flux::GFlavorMixerI * GetFlavorMixer(const std::string &)
std::vector< std::string > listnames
std::map< std::string, bool * > fBoolPtrMap
GENIE interface for flavor modification.
Definition: GFlavorMixerI.h:42
void operator=(const GFlavorMixerFactory &)
std::map< std::string, GFlavorMixerICtorFuncPtr_t > fFunctionMap
const std::vector< std::string > & AvailableFlavorMixers() const
bool IsKnownFlavorMixer(const std::string &)
A class for generating concrete GFlavorMixerI derived classes based on the factory pattern...
bool RegisterCreator(std::string name, GFlavorMixerICtorFuncPtr_t ctorptr, bool *ptr)
genie::flux::GFlavorMixerI *(* GFlavorMixerICtorFuncPtr_t)()