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