EvtTimeShiftFactory.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file EvtTimeShiftFactory.h
3 /// \brief A class for generating concrete EvtTimeShiftI derived classes
4 /// based on the factory pattern. This code supplies a CPP
5 /// macro which allows the classes to self-register and thus
6 /// no modification of this class is needed in order to expand
7 /// the list of classes it knows about.
8 ///
9 /// Implemented as a singleton holding a map between names and
10 /// pointers-to-functions (that call a class default constructor).
11 /// The functions pointers must return EvtTimeShiftI*.
12 ///
13 /// \version /// \author Robert Hatcher <rhatcher \at fnal.gov>
14 /// Fermi National Accelerator Laboratory
15 ///
16 ////////////////////////////////////////////////////////////////////////
17 #ifndef SIMB_EVTTIMESHIFTFACTORY_H
18 #define SIMB_EVTTIMESHIFTFACTORY_H
19 
20 #include <string>
21 #include <vector>
22 #include <map>
23 
24 #include "EvtTimeShiftI.h"
25 
26 namespace evgb {
27 
28 // define a type for the pointer to a function that returns a
29 // evgb::EvtTimeShiftI*
30 // i.e. calls the (typically default) ctor for the class.
31 typedef evgb::EvtTimeShiftI* (*EvtTimeShiftICtorFuncPtr_t)(const std::string& );
32 
34 {
35 public:
36  static EvtTimeShiftFactory& Instance();
37  // no public ctor for singleton, all user access is through Instance()
38 
40  const std::string& config="") const;
41  // instantiate a EvtTimeShift by name (1st arg), pass 2nd arg as config in ctor
42 
43  bool IsKnownEvtTimeShift(const std::string&);
44  // check if the name is in the list of names
45 
46  const std::vector<std::string>& AvailableEvtTimeShift() const;
47  // return a list of available names
48 
49  void Print() const;
50  // print what we know
51 
52  bool RegisterCreator(std::string name,
53  EvtTimeShiftICtorFuncPtr_t ctorptr, bool* ptr);
54  // register a new EvtTimeShiftI type by passing pointer to creator function
55 
56 private:
58  // the one and only instance
59 
60  std::map<std::string, EvtTimeShiftICtorFuncPtr_t> fFunctionMap;
61  // mapping between known class names and a registered ctor function
62 
63  std::map<std::string, bool*> fBoolPtrMap;
64 
65  mutable std::vector<std::string> listnames;
66  // copy of list of names, used solely due to AvailableFlavorMixers()
67  // method returning a const reference rather than a vector object.
68  // mutable because AvailableFlavorMixers() is const, but list might
69  // need recreation if new entries have been registered.
70 
71 private:
73  // private ctor, users access class via Instance()
74 
75  virtual ~EvtTimeShiftFactory();
76 
78  // method private and not implement, declared to prevent copying
79 
80  void operator=(const EvtTimeShiftFactory&);
81  // method private and not implement, declared to prevent assignment
82 
83  // sub-class Cleaner struct is used to clean up singleton at the end of job.
84  struct Cleaner {
85  void UseMe() { } // Dummy method to quiet compiler
90  } } };
91  friend struct Cleaner;
92 
93 };
94 
95 } // namespace evgb
96 
97 // Define macro to create a function to call the class' ctor
98 // and then registers this function with the factory instance for later use
99 // Users should have in their myPhyList.cc two lines that look like:
100 // #include "EvtTimeShiftFactory.h"
101 // TIMESHIFTREG(MyTimeShiftClass) // no semicolon
102 // where "MyTimeShiftClass" is the name of the class (assuming no special namespace)
103 // If the class is defined in a namespace (or two) use:
104 // #include "EvtTimeShiftFactory.h"
105 // TIMESHIFTREG3(myspace,myAltTimeShift,myspace::myAltTimeShift) // no semicolon
106 // TIMESHIFTREG4(myspace,evgb,YATimeShift,myspace::evgb::YATimeShift) // no semicolon
107 // and either can then be retrieved from the factory using:
108 // EvtTimeShiftFactory& factory =
109 // EvtTimeShiftFactory::Instance();
110 // evgb::EvtTimeShiftI* p = 0;
111 // std::string myConfig = "..."
112 // p = factory.GetEvtTimeShift("MyTimeShiftClass",myConfig);
113 // p = factory.GetEvtTimeShift("myspace::myAltTimeShift",myConfig);
114 // p = factory.GetEvtTimeShift("evgb::YATimeShift",myConfig);
115 //
116 // The expanded code looks like:
117 // evgb::EvtTimeShiftI* MyTimeShiftClass_ctor_function () { return new MyTimeShiftClass; }
118 // static bool MyTimeShiftClass_creator_registered =
119 // EvtTimeShiftFactory::Instance().RegisterCreator("MyTimeShiftClass",
120 // & MyTimeShiftClass_ctor_function );
121 // namespace myspace {
122 // evgb::EvtTimeShiftI* myAltAltTimeShift_ctor_function () { return new myspace::myAltAltTimeShift; }
123 // static bool myAltTimeShift_creator_registered =
124 // EvtTimeShiftFactory::Instance().RegisterCreator("myspace::myAltAltTimeShift",
125 // & myspace::myAltAltTimeShift_ctor_function ); }
126 
127 #define TIMESHIFTREG( _name ) \
128  evgb::EvtTimeShiftI* _name ## _ctor_function (const std::string& config) { return new _name(config); } \
129  static bool _name ## _creator_registered = \
130  evgb::EvtTimeShiftFactory::Instance().RegisterCreator(# _name, \
131  & _name ## _ctor_function, \
132  & _name ## _creator_registered );
133 
134 #define TIMESHIFTREG3( _ns, _name, _fqname ) \
135 namespace _ns { \
136  evgb::EvtTimeShiftI* _name ## _ctor_function (const std::string& config) { return new _fqname(config); } \
137  static bool _name ## _creator_registered = \
138  evgb::EvtTimeShiftFactory::Instance().RegisterCreator(# _fqname, \
139  & _fqname ## _ctor_function, \
140  & _fqname ## _creator_registered );}
141 
142 #define TIMESHIFTREG4( _nsa, _nsb, _name, _fqname ) \
143 namespace _nsa { \
144  namespace _nsb { \
145  evgb::EvtTimeShiftI* _name ## _ctor_function (const std::string& config) { return new _fqname(config); } \
146  static bool _name ## _creator_registered = \
147  evgb::EvtTimeShiftFactory::Instance().RegisterCreator(# _fqname, \
148  & _fqname ## _ctor_function, \
149  & _fqname ## _creator_registered );}}
150 #endif
static QCString name
Definition: declinfo.cpp:673
std::string string
Definition: nybbler.cc:12
static EvtTimeShiftFactory * fgTheInstance
bool IsKnownEvtTimeShift(const std::string &)
const std::vector< std::string > & AvailableEvtTimeShift() const
interface for event time distribution
Definition: EvtTimeShiftI.h:29
std::map< std::string, EvtTimeShiftICtorFuncPtr_t > fFunctionMap
static Config * config
Definition: config.cpp:1054
bool RegisterCreator(std::string name, EvtTimeShiftICtorFuncPtr_t ctorptr, bool *ptr)
std::map< std::string, bool * > fBoolPtrMap
std::vector< std::string > listnames
static EvtTimeShiftFactory & Instance()
evgb::EvtTimeShiftI *(* EvtTimeShiftICtorFuncPtr_t)(const std::string &)
Physics generators for neutrinos, cosmic rays, and others.
Definition: CRYHelper.cxx:33
void operator=(const EvtTimeShiftFactory &)
evgb::EvtTimeShiftI * GetEvtTimeShift(const std::string &name, const std::string &config="") const