thepeg is hosted by Hepforge, IPPP Durham
ThePEG  2.1.5
Switch.h
1 // -*- C++ -*-
2 //
3 // Switch.h is a part of ThePEG - Toolkit for HEP Event Generation
4 // Copyright (C) 1999-2017 Leif Lonnblad
5 //
6 // ThePEG is licenced under version 3 of the GPL, see COPYING for details.
7 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
8 //
9 #ifndef ThePEG_Switch_H
10 #define ThePEG_Switch_H
11 // This is the declaration of the Switch, SwitchBase and SwitchOption classes.
12 
13 #include "ThePEG/Config/ThePEG.h"
14 #include "Switch.fh"
15 #include "Switch.xh"
16 #include "InterfaceBase.h"
17 
18 namespace ThePEG {
19 
36 class SwitchOption: public Named {
37 
38 public:
39 
54  template<typename EnumT>
55  SwitchOption(SwitchBase & theSwitch, string newName,
56  string newDescription, EnumT newValue);
57 
61  SwitchOption() : theValue(-999) {}
62 
66  const string & description() const { return theDescription; }
67 
71  long value() const { return theValue; }
72 
76  operator long () const;
77 
78 protected:
79 
80 private:
81 
86 
90  long theValue;
91 
92 };
93 
118 class SwitchBase: public InterfaceBase {
119 
120 public:
121 
123  typedef map<long, SwitchOption> OptionMap;
125  typedef map<string, SwitchOption> StringMap;
126 
128  friend class SwitchOption;
129 
130 public:
131 
152  SwitchBase(string newName, string newDescription,
153  string newClassName, const type_info & newTypeInfo,
154  bool depSafe, bool readonly)
155  : InterfaceBase(newName, newDescription, newClassName,
156  newTypeInfo, depSafe, readonly) {}
157 
165  virtual string exec(InterfacedBase & ib, string action,
166  string arguments) const;
167 
171  virtual string fullDescription(const InterfacedBase & ib) const;
172 
176  virtual string type() const;
177 
181  virtual void set(InterfacedBase & ib, long val)
182  const = 0;
183 
187  virtual long get(const InterfacedBase & ib)
188  const = 0;
189 
193  virtual long def(const InterfacedBase & ib)
194  const = 0;
195 
199  void setDef(InterfacedBase & i) const {
200  set(i, def(i));
201  }
202 
206  bool check(long newValue) const { return member(theOptions, newValue); }
207 
211  const OptionMap & options() const { return theOptions; }
212 
217  virtual string doxygenType() const;
218 
222  string opttag(long opt) const;
223 
224 protected:
225 
229  void registerOption(const SwitchOption & o) {
230  theOptions[o.value()] = o;
231  theOptionNames[o.name()] = o;
232  }
233 
234 private:
235 
239  OptionMap theOptions;
240 
244  StringMap theOptionNames;
245 
246 };
247 
272 template <typename T, typename Int>
273 class Switch: public SwitchBase {
274 
275 public:
276 
281  typedef void (T::*SetFn)(Int);
286  typedef Int (T::*GetFn)() const;
287 
291  typedef Int T::* Member;
292 
293 public:
294 
324  Switch(string newName, string newDescription,
325  Member newMember, Int newDef, bool depSafe = false,
326  bool readonly = false, SetFn newSetFn = 0, GetFn newGetFn = 0,
327  GetFn newDefFn = 0)
328  : SwitchBase(newName, newDescription, ClassTraits<T>::className(),
329  typeid(T), depSafe, readonly),
330  theMember(newMember), theDef(newDef), theSetFn(newSetFn),
331  theGetFn(newGetFn), theDefFn(newDefFn) {}
332 
336  virtual void set(InterfacedBase & ib, long val) const
337  ;
338 
342  virtual long get(const InterfacedBase & ib) const;
343 
347  virtual long def(const InterfacedBase & ib) const;
348 
352  void setSetFunction(SetFn sf) { theSetFn = sf; }
353 
357  void setGetFunction(GetFn gf) { theGetFn = gf; }
358 
362  void setDefaultFunction(GetFn df) { theDefFn = df; }
363 
368  virtual void doxygenDescription(ostream & stream) const;
369 
370 private:
371 
376 
381  Int theDef;
382 
386  SetFn theSetFn;
387 
391  GetFn theGetFn;
392 
396  GetFn theDefFn;
397 
398 };
399 
400 }
401 
402 #ifndef ThePEG_TEMPLATES_IN_CC_FILE
403 #include "Switch.tcc"
404 #endif
405 
406 #endif /* ThePEG_Switch_H */
long value() const
The value of this option.
Definition: Switch.h:71
map< string, SwitchOption > StringMap
A map with SwitchOptions indexed by their names.
Definition: Switch.h:125
const OptionMap & options() const
Return the map relating options to their values.
Definition: Switch.h:211
SwitchOption is used by the Switch class and its base class SwitchBase to define valid options in a s...
Definition: Switch.h:36
map< long, SwitchOption > OptionMap
A map with SwitchOptions indexed by their values.
Definition: Switch.h:123
The Switch class and its base class SwitchBase defines an interface to a class derived from the Inter...
Definition: Switch.h:273
void setDefaultFunction(GetFn df)
Give a pointer to a member function to be used by &#39;def()&#39;.
Definition: Switch.h:362
The InterfaceBase class defines a generic interface to any class derived from the InterfacedBase clas...
Definition: InterfaceBase.h:59
Int theDef
Default value to be used if no corresponding member function pointers are given.
Definition: Switch.h:381
bool member(const Container &c, const Key &k)
Check if a given object is a part of a container.
Definition: std.h:124
const string & description() const
The description of this option.
Definition: Switch.h:66
void registerOption(const SwitchOption &o)
Register a new option.
Definition: Switch.h:229
The Switch class and its base class SwitchBase defines an interface to a class derived from the Inter...
Definition: Switch.h:118
Member theMember
The pointer to the member variable.
Definition: Switch.h:375
This is the main namespace within which all identifiers in ThePEG are declared.
Definition: FactoryBase.h:28
The Named class is a simple concrete base class to used by classes of objects with a name...
Definition: Named.h:24
This is the main config header file for ThePEG.
SetFn theSetFn
A pointer to a member function to be used by &#39;set()&#39;.
Definition: Switch.h:386
SwitchOption()
Default constructor.
Definition: Switch.h:61
GetFn theGetFn
Pointer to member function to be used by get().
Definition: Switch.h:391
OptionMap theOptions
The map relating options to their values.
Definition: Switch.h:239
Switch(string newName, string newDescription, Member newMember, Int newDef, bool depSafe=false, bool readonly=false, SetFn newSetFn=0, GetFn newGetFn=0, GetFn newDefFn=0)
Standard constructor.
Definition: Switch.h:324
void setSetFunction(SetFn sf)
Give a pointer to a member function to be used by &#39;set()&#39;.
Definition: Switch.h:352
InterfacedBase is the base class of all Interfaced objects to be handled by the BaseRepository class...
StringMap theOptionNames
The map relating options to their names.
Definition: Switch.h:244
GetFn theDefFn
Pointer to member function to be used by def().
Definition: Switch.h:396
const string & name() const
Return name.
Definition: Named.h:42
SwitchBase(string newName, string newDescription, string newClassName, const type_info &newTypeInfo, bool depSafe, bool readonly)
Standard constructor.
Definition: Switch.h:152
string theDescription
The description of this option.
Definition: Switch.h:85
void setGetFunction(GetFn gf)
Give a pointer to a member function to be used by &#39;get()&#39;.
Definition: Switch.h:357
Int T::* Member
Declaration of a direct pointer to the member variable.
Definition: Switch.h:291
The default concrete implementation of ClassTraitsBase.
Definition: ClassTraits.h:134
void setDef(InterfacedBase &i) const
Set the member variable of ib to its default value.
Definition: Switch.h:199
long theValue
The value of this option.
Definition: Switch.h:90
bool check(long newValue) const
Check if val is among the listed options.
Definition: Switch.h:206