ThePEG  1.8.0
Switch.h
1 // -*- C++ -*-
2 //
3 // Switch.h is a part of ThePEG - Toolkit for HEP Event Generation
4 // Copyright (C) 1999-2011 Leif Lonnblad
5 //
6 // ThePEG is licenced under version 2 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  SwitchOption(SwitchBase & theSwitch, string newName,
55  string newDescription, long newValue);
56 
60  SwitchOption() : theValue(-999) {}
61 
65  const string & description() const { return theDescription; }
66 
70  long value() const { return theValue; }
71 
75  operator long () const;
76 
77 protected:
78 
79 private:
80 
85 
89  long theValue;
90 
91 };
92 
117 class SwitchBase: public InterfaceBase {
118 
119 public:
120 
122  typedef map<long, SwitchOption> OptionMap;
124  typedef map<string, SwitchOption> StringMap;
125 
127  friend class SwitchOption;
128 
129 public:
130 
151  SwitchBase(string newName, string newDescription,
152  string newClassName, const type_info & newTypeInfo,
153  bool depSafe, bool readonly)
154  : InterfaceBase(newName, newDescription, newClassName,
155  newTypeInfo, depSafe, readonly) {}
156 
164  virtual string exec(InterfacedBase & ib, string action,
165  string arguments) const;
166 
170  virtual string fullDescription(const InterfacedBase & ib) const;
171 
175  virtual string type() const;
176 
180  virtual void set(InterfacedBase & ib, long val)
181  const = 0;
182 
186  virtual long get(const InterfacedBase & ib)
187  const = 0;
188 
192  virtual long def(const InterfacedBase & ib)
193  const = 0;
194 
198  void setDef(InterfacedBase & i) const {
199  set(i, def(i));
200  }
201 
205  bool check(long newValue) const { return member(theOptions, newValue); }
206 
210  const OptionMap & options() const { return theOptions; }
211 
216  virtual string doxygenType() const;
217 
221  string opttag(long opt) const;
222 
223 protected:
224 
228  void registerOption(const SwitchOption & o) {
229  theOptions[o.value()] = o;
230  theOptionNames[o.name()] = o;
231  }
232 
233 private:
234 
238  OptionMap theOptions;
239 
243  StringMap theOptionNames;
244 
245 };
246 
271 template <typename T, typename Int>
272 class Switch: public SwitchBase {
273 
274 public:
275 
280  typedef void (T::*SetFn)(Int);
285  typedef Int (T::*GetFn)() const;
286 
290  typedef Int T::* Member;
291 
292 public:
293 
323  Switch(string newName, string newDescription,
324  Member newMember, Int newDef, bool depSafe = false,
325  bool readonly = false, SetFn newSetFn = 0, GetFn newGetFn = 0,
326  GetFn newDefFn = 0)
327  : SwitchBase(newName, newDescription, ClassTraits<T>::className(),
328  typeid(T), depSafe, readonly),
329  theMember(newMember), theDef(newDef), theSetFn(newSetFn),
330  theGetFn(newGetFn), theDefFn(newDefFn) {}
331 
335  virtual void set(InterfacedBase & ib, long val) const
336  ;
337 
341  virtual long get(const InterfacedBase & ib) const;
342 
346  virtual long def(const InterfacedBase & ib) const;
347 
351  void setSetFunction(SetFn sf) { theSetFn = sf; }
352 
356  void setGetFunction(GetFn gf) { theGetFn = gf; }
357 
361  void setDefaultFunction(GetFn df) { theDefFn = df; }
362 
367  virtual void doxygenDescription(ostream & stream) const;
368 
369 private:
370 
375 
380  Int theDef;
381 
385  SetFn theSetFn;
386 
390  GetFn theGetFn;
391 
395  GetFn theDefFn;
396 
397 };
398 
399 }
400 
401 #ifndef ThePEG_TEMPLATES_IN_CC_FILE
402 #include "Switch.tcc"
403 #endif
404 
405 #endif /* ThePEG_Switch_H */
long value() const
The value of this option.
Definition: Switch.h:70
map< string, SwitchOption > StringMap
A map with SwitchOptions indexed by their names.
Definition: Switch.h:124
const OptionMap & options() const
Return the map relating options to their values.
Definition: Switch.h:210
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:122
The Switch class and its base class SwitchBase defines an interface to a class derived from the Inter...
Definition: Switch.h:272
void setDefaultFunction(GetFn df)
Give a pointer to a member function to be used by &#39;def()&#39;.
Definition: Switch.h:361
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:380
bool member(const Container &c, const Key &k)
Check if a given object is a part of a container.
Definition: std.h:117
const string & description() const
The description of this option.
Definition: Switch.h:65
void registerOption(const SwitchOption &o)
Register a new option.
Definition: Switch.h:228
The Switch class and its base class SwitchBase defines an interface to a class derived from the Inter...
Definition: Switch.h:117
Member theMember
The pointer to the member variable.
Definition: Switch.h:374
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
SetFn theSetFn
A pointer to a member function to be used by &#39;set()&#39;.
Definition: Switch.h:385
SwitchOption()
Default constructor.
Definition: Switch.h:60
GetFn theGetFn
Pointer to member function to be used by get().
Definition: Switch.h:390
OptionMap theOptions
The map relating options to their values.
Definition: Switch.h:238
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:323
void setSetFunction(SetFn sf)
Give a pointer to a member function to be used by &#39;set()&#39;.
Definition: Switch.h:351
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:243
GetFn theDefFn
Pointer to member function to be used by def().
Definition: Switch.h:395
const string & name() const
Return name.
Definition: Named.h:37
SwitchBase(string newName, string newDescription, string newClassName, const type_info &newTypeInfo, bool depSafe, bool readonly)
Standard constructor.
Definition: Switch.h:151
string theDescription
The description of this option.
Definition: Switch.h:84
void setGetFunction(GetFn gf)
Give a pointer to a member function to be used by &#39;get()&#39;.
Definition: Switch.h:356
Int T::* Member
Declaration of a direct pointer to the member variable.
Definition: Switch.h:290
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:198
long theValue
The value of this option.
Definition: Switch.h:89
bool check(long newValue) const
Check if val is among the listed options.
Definition: Switch.h:205