CMS 3D CMS Logo

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  public:
27  typedef std::map<T, edm::value_ptr<ParameterDescriptionNode> > CaseMap;
28  typedef typename std::map<T, edm::value_ptr<ParameterDescriptionNode> >::const_iterator CaseMapConstIter;
29 
30  ParameterSwitch(ParameterDescription<T> const& switchParameter,
31  std::unique_ptr<ParameterDescriptionCases<T> > cases)
32  : switch_(switchParameter), cases_(*cases->caseMap()) {
33  if (cases->duplicateCaseValues()) {
34  throwDuplicateCaseValues(switchParameter.label());
35  }
36  }
37 
38  ParameterDescriptionNode* clone() const override { return new ParameterSwitch(*this); }
39 
40  private:
41  void checkAndGetLabelsAndTypes_(std::set<std::string>& usedLabels,
42  std::set<ParameterTypes>& parameterTypes,
43  std::set<ParameterTypes>& wildcardTypes) const override {
44  std::set<std::string> caseLabels;
45  std::set<ParameterTypes> caseParameterTypes;
46  std::set<ParameterTypes> caseWildcardTypes;
49  std::placeholders::_1,
50  std::ref(caseLabels),
51  std::ref(caseParameterTypes),
52  std::ref(caseWildcardTypes)));
53 
54  insertAndCheckLabels(switch_.label(), usedLabels, caseLabels);
55 
56  insertAndCheckTypes(switch_.type(), caseParameterTypes, caseWildcardTypes, parameterTypes, wildcardTypes);
57 
58  if (cases_.find(switch_.getDefaultValue()) == cases_.end()) {
60  }
61  }
62 
63  void validate_(ParameterSet& pset, std::set<std::string>& validatedLabels, bool optional) const override {
64  switch_.validate(pset, validatedLabels, optional);
65  if (switch_.exists(pset)) {
66  T switchValue;
67  if (switch_.isTracked()) {
68  switchValue = pset.getParameter<T>(switch_.label());
69  } else {
70  switchValue = pset.getUntrackedParameter<T>(switch_.label());
71  }
72  typename CaseMap::const_iterator selectedCase = cases_.find(switchValue);
73  if (selectedCase != cases_.end()) {
74  selectedCase->second->validate(pset, validatedLabels, false);
75  } else {
76  std::stringstream ss;
77  ss << "The switch parameter with label \"" << switch_.label() << "\" has been assigned an illegal value.\n"
78  << "The value from the configuration is \"" << switchValue << "\".\n"
79  << "The allowed values are:\n";
80 
81  for (CaseMapConstIter iter = cases_.begin(), iEnd = cases_.end(); iter != iEnd; ++iter) {
82  ss << " " << iter->first << "\n";
83  }
84  throwNoCaseForSwitchValue(ss.str());
85  }
86  }
87  }
88 
89  void writeCfi_(std::ostream& os, bool& startWithComma, int indentation, bool& wroteSomething) const override {
90  switch_.writeCfi(os, startWithComma, indentation, wroteSomething);
91 
92  typename CaseMap::const_iterator selectedCase = cases_.find(switch_.getDefaultValue());
93  if (selectedCase != cases_.end()) {
94  selectedCase->second->writeCfi(os, startWithComma, indentation, wroteSomething);
95  }
96  }
97 
98  void print_(std::ostream& os, bool optional, bool writeToCfi, DocFormatHelper& dfh) const override {
99  printBase(os,
100  optional,
101  writeToCfi,
102  dfh,
103  switch_.label(),
104  switch_.isTracked(),
106  }
107 
108  void printNestedContent_(std::ostream& os, bool optional, DocFormatHelper& dfh) const override {
109  DocFormatHelper new_dfh(dfh);
110  printNestedContentBase(os, dfh, new_dfh, switch_.label());
111 
112  switch_.print(os, optional, true, new_dfh);
113  for_all(cases_,
114  std::bind(&ParameterSwitchBase::printCaseT<T>,
115  std::placeholders::_1,
116  std::ref(os),
117  optional,
118  std::ref(new_dfh),
119  std::cref(switch_.label())));
120 
121  new_dfh.setPass(1);
122  new_dfh.setCounter(0);
123 
124  new_dfh.indent(os);
125  os << "switch:\n";
126  switch_.print(os, optional, true, new_dfh);
127  for_all(cases_,
128  std::bind(&ParameterSwitchBase::printCaseT<T>,
129  std::placeholders::_1,
130  std::ref(os),
131  optional,
132  std::ref(new_dfh),
133  std::cref(switch_.label())));
134 
135  new_dfh.setPass(2);
136  new_dfh.setCounter(0);
137 
138  switch_.printNestedContent(os, optional, new_dfh);
139  for_all(cases_,
140  std::bind(&ParameterSwitchBase::printCaseT<T>,
141  std::placeholders::_1,
142  std::ref(os),
143  optional,
144  std::ref(new_dfh),
145  std::cref(switch_.label())));
146  }
147 
148  bool exists_(ParameterSet const& pset) const override { return switch_.exists(pset); }
149 
150  static void checkCaseLabels(std::pair<T, edm::value_ptr<ParameterDescriptionNode> > const& thePair,
151  std::set<std::string>& labels,
152  std::set<ParameterTypes>& parameterTypes,
153  std::set<ParameterTypes>& wildcardTypes) {
154  thePair.second->checkAndGetLabelsAndTypes(labels, parameterTypes, wildcardTypes);
155  }
156 
158  CaseMap cases_;
159  };
160 } // namespace edm
161 #endif
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
void writeCfi_(std::ostream &os, bool &startWithComma, int indentation, bool &wroteSomething) const override
void insertAndCheckTypes(ParameterTypes switchType, std::set< ParameterTypes > const &caseParameterTypes, std::set< ParameterTypes > const &caseWildcardTypes, std::set< ParameterTypes > &parameterTypes, std::set< ParameterTypes > &wildcardTypes) const
void print_(std::ostream &os, bool optional, bool writeToCfi, DocFormatHelper &dfh) const override
void printNestedContentBase(std::ostream &os, DocFormatHelper &dfh, DocFormatHelper &new_dfh, std::string const &switchLabel) const
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)
ParameterDescriptionNode * clone() const override
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:14
void throwDuplicateCaseValues(std::string const &switchLabel) const
void checkAndGetLabelsAndTypes_(std::set< std::string > &usedLabels, std::set< ParameterTypes > &parameterTypes, std::set< ParameterTypes > &wildcardTypes) const override
void throwNoCaseForDefault(std::string const &switchLabel) const
void validate_(ParameterSet &pset, std::set< std::string > &validatedLabels, bool optional) const override
void setPass(int value)
void printNestedContent_(std::ostream &os, bool optional, DocFormatHelper &dfh) const override
std::map< T, edm::value_ptr< ParameterDescriptionNode > >::const_iterator CaseMapConstIter
bool exists_(ParameterSet const &pset) const override
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
HLT enums.
ParameterDescription< T > switch_
ParameterSwitch(ParameterDescription< T > const &switchParameter, std::unique_ptr< ParameterDescriptionCases< T > > cases)
void indent(std::ostream &os) const
long double T
std::string const & label() const