test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ParameterSwitch.h
Go to the documentation of this file.
1 #ifndef FWCore_ParameterSet_ParameterSwitch_h
2 #define FWCore_ParameterSet_ParameterSwitch_h
3 
12 
13 #include <map>
14 #include <memory>
15 #include <set>
16 #include <string>
17 #include <utility>
18 #include <sstream>
19 #include <ostream>
20 #include <iomanip>
21 
22 namespace edm {
23 
24  template<class T>
26 
27  public:
28  typedef std::map<T, edm::value_ptr<ParameterDescriptionNode> > CaseMap;
29  typedef typename std::map<T, edm::value_ptr<ParameterDescriptionNode> >::const_iterator CaseMapConstIter;
30 
31  ParameterSwitch(ParameterDescription<T> const& switchParameter,
32  std::auto_ptr<ParameterDescriptionCases<T> > cases) :
33  switch_(switchParameter),
34  cases_(*cases->caseMap())
35  {
36  if (cases->duplicateCaseValues()) {
37  throwDuplicateCaseValues(switchParameter.label());
38  }
39  }
40 
41  virtual ParameterDescriptionNode* clone() const {
42  return new ParameterSwitch(*this);
43  }
44 
45  private:
46 
47  virtual void checkAndGetLabelsAndTypes_(std::set<std::string> & usedLabels,
48  std::set<ParameterTypes> & parameterTypes,
49  std::set<ParameterTypes> & wildcardTypes) const {
50 
51  std::set<std::string> caseLabels;
52  std::set<ParameterTypes> caseParameterTypes;
53  std::set<ParameterTypes> caseWildcardTypes;
55  std::placeholders::_1,
56  std::ref(caseLabels),
57  std::ref(caseParameterTypes),
58  std::ref(caseWildcardTypes)));
59 
61  usedLabels,
62  caseLabels);
63 
65  caseParameterTypes,
66  caseWildcardTypes,
67  parameterTypes,
68  wildcardTypes);
69 
70  if (cases_.find(switch_.getDefaultValue()) == cases_.end()) {
72  }
73  }
74 
75  virtual void validate_(ParameterSet & pset,
76  std::set<std::string> & validatedLabels,
77  bool optional) const {
78 
79  switch_.validate(pset, validatedLabels, optional);
80  if (switch_.exists(pset)) {
81  T switchValue;
82  if (switch_.isTracked()) {
83  switchValue = pset.getParameter<T>(switch_.label());
84  }
85  else {
86  switchValue = pset.getUntrackedParameter<T>(switch_.label());
87  }
88  typename CaseMap::const_iterator selectedCase = cases_.find(switchValue);
89  if (selectedCase != cases_.end()) {
90  selectedCase->second->validate(pset, validatedLabels, false);
91  }
92  else {
93  std::stringstream ss;
94  ss << "The switch parameter with label \""
95  << switch_.label()
96  << "\" has been assigned an illegal value.\n"
97  << "The value from the configuration is \""
98  << switchValue
99  << "\".\n"
100  << "The allowed values are:\n";
101 
102  for (CaseMapConstIter iter = cases_.begin(), iEnd = cases_.end();
103  iter != iEnd;
104  ++iter) {
105  ss << " " << iter->first << "\n";
106  }
107  throwNoCaseForSwitchValue(ss.str());
108  }
109  }
110  }
111 
112  virtual void writeCfi_(std::ostream & os,
113  bool & startWithComma,
114  int indentation,
115  bool & wroteSomething) const {
116  switch_.writeCfi(os, startWithComma, indentation, wroteSomething);
117 
118  typename CaseMap::const_iterator selectedCase = cases_.find(switch_.getDefaultValue());
119  if (selectedCase != cases_.end()) {
120  selectedCase->second->writeCfi(os, startWithComma, indentation, wroteSomething);
121  }
122  }
123 
124  virtual void print_(std::ostream & os,
125  bool optional,
126  bool writeToCfi,
127  DocFormatHelper & dfh) {
128  printBase(os, optional, writeToCfi, dfh, switch_.label(), switch_.isTracked(), parameterTypeEnumToString(switch_.type()));
129  }
130 
131  virtual void printNestedContent_(std::ostream & os,
132  bool optional,
133  DocFormatHelper & dfh) {
134 
135  DocFormatHelper new_dfh(dfh);
136  printNestedContentBase(os, dfh, new_dfh, switch_.label());
137 
138  switch_.print(os, optional, true, new_dfh);
139  for_all(cases_, std::bind(&ParameterSwitchBase::printCaseT<T>,
140  std::placeholders::_1,
141  std::ref(os),
142  optional,
143  std::ref(new_dfh),
144  std::cref(switch_.label())));
145 
146  new_dfh.setPass(1);
147  new_dfh.setCounter(0);
148 
149  new_dfh.indent(os);
150  os << "switch:\n";
151  switch_.print(os, optional, true, new_dfh);
152  for_all(cases_, std::bind(&ParameterSwitchBase::printCaseT<T>,
153  std::placeholders::_1,
154  std::ref(os),
155  optional,
156  std::ref(new_dfh),
157  std::cref(switch_.label())));
158 
159  new_dfh.setPass(2);
160  new_dfh.setCounter(0);
161 
162  switch_.printNestedContent(os, optional, new_dfh);
163  for_all(cases_, std::bind(&ParameterSwitchBase::printCaseT<T>,
164  std::placeholders::_1,
165  std::ref(os),
166  optional,
167  std::ref(new_dfh),
168  std::cref(switch_.label())));
169  }
170 
171  virtual bool exists_(ParameterSet const& pset) const { return switch_.exists(pset); }
172 
173  static void checkCaseLabels(std::pair<T, edm::value_ptr<ParameterDescriptionNode> > const& thePair,
174  std::set<std::string> & labels,
175  std::set<ParameterTypes> & parameterTypes,
176  std::set<ParameterTypes> & wildcardTypes) {
177  thePair.second->checkAndGetLabelsAndTypes(labels, parameterTypes, wildcardTypes);
178  }
179 
182  };
183 }
184 #endif
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
virtual void checkAndGetLabelsAndTypes_(std::set< std::string > &usedLabels, std::set< ParameterTypes > &parameterTypes, std::set< ParameterTypes > &wildcardTypes) const
void insertAndCheckTypes(ParameterTypes switchType, std::set< ParameterTypes > const &caseParameterTypes, std::set< ParameterTypes > const &caseWildcardTypes, std::set< ParameterTypes > &parameterTypes, std::set< ParameterTypes > &wildcardTypes) const
ParameterSwitch(ParameterDescription< T > const &switchParameter, std::auto_ptr< ParameterDescriptionCases< T > > cases)
static void checkCaseLabels(std::pair< T, edm::value_ptr< ParameterDescriptionNode > > const &thePair, std::set< std::string > &labels, std::set< ParameterTypes > &parameterTypes, std::set< ParameterTypes > &wildcardTypes)
virtual void validate_(ParameterSet &pset, std::set< std::string > &validatedLabels, bool optional) const
void insertAndCheckLabels(std::string const &switchLabel, std::set< std::string > &usedLabels, std::set< std::string > &labels) const
std::string parameterTypeEnumToString(ParameterTypes iType)
void setCounter(int value)
Func for_all(ForwardSequence &s, Func f)
wrapper for std::for_each
Definition: Algorithms.h:16
void throwDuplicateCaseValues(std::string const &switchLabel) const
void throwNoCaseForDefault(std::string const &switchLabel) const
virtual void print_(std::ostream &os, bool optional, bool writeToCfi, DocFormatHelper &dfh)
virtual void printNestedContent_(std::ostream &os, bool optional, DocFormatHelper &dfh)
void setPass(int value)
virtual void writeCfi_(std::ostream &os, bool &startWithComma, int indentation, bool &wroteSomething) const
std::map< T, edm::value_ptr< ParameterDescriptionNode > >::const_iterator CaseMapConstIter
virtual ParameterDescriptionNode * clone() const
std::map< T, edm::value_ptr< ParameterDescriptionNode > > CaseMap
void printBase(std::ostream &os, bool optional, bool writeToCfi, DocFormatHelper &dfh, std::string const &switchLabel, bool isTracked, std::string const &typeString) const
void throwNoCaseForSwitchValue(std::string const &message) const
void printNestedContentBase(std::ostream &os, DocFormatHelper &dfh, DocFormatHelper &new_dfh, std::string const &switchLabel)
ParameterDescription< T > switch_
void indent(std::ostream &os) const
long double T
virtual bool exists_(ParameterSet const &pset) const
std::string const & label() const