CMS 3D CMS Logo

IfExistsDescription.cc
Go to the documentation of this file.
1 
5 
6 #include <algorithm>
7 #include <sstream>
8 #include <ostream>
9 #include <iomanip>
10 
11 namespace edm {
12 
14  ParameterDescriptionNode const& node_right)
15  : node_left_(node_left.clone()), node_right_(node_right.clone()) {}
16 
17  IfExistsDescription::IfExistsDescription(std::unique_ptr<ParameterDescriptionNode> node_left,
18  ParameterDescriptionNode const& node_right)
19  : node_left_(std::move(node_left)), node_right_(node_right.clone()) {}
20 
22  std::unique_ptr<ParameterDescriptionNode> node_right)
23  : node_left_(node_left.clone()), node_right_(std::move(node_right)) {}
24 
25  IfExistsDescription::IfExistsDescription(std::unique_ptr<ParameterDescriptionNode> node_left,
26  std::unique_ptr<ParameterDescriptionNode> node_right)
27  : node_left_(std::move(node_left)), node_right_(std::move(node_right)) {}
28 
29  void IfExistsDescription::checkAndGetLabelsAndTypes_(std::set<std::string>& usedLabels,
30  std::set<ParameterTypes>& parameterTypes,
31  std::set<ParameterTypes>& wildcardTypes) const {
32  std::set<std::string> labelsLeft;
33  std::set<ParameterTypes> parameterTypesLeft;
34  std::set<ParameterTypes> wildcardTypesLeft;
35  node_left_->checkAndGetLabelsAndTypes(labelsLeft, parameterTypesLeft, wildcardTypesLeft);
36 
37  std::set<std::string> labelsRight;
38  std::set<ParameterTypes> parameterTypesRight;
39  std::set<ParameterTypes> wildcardTypesRight;
40  node_right_->checkAndGetLabelsAndTypes(labelsRight, parameterTypesRight, wildcardTypesRight);
41 
42  throwIfDuplicateLabels(labelsLeft, labelsRight);
43  throwIfDuplicateTypes(wildcardTypesLeft, parameterTypesRight);
44  throwIfDuplicateTypes(wildcardTypesRight, parameterTypesLeft);
45 
46  usedLabels.insert(labelsLeft.begin(), labelsLeft.end());
47  usedLabels.insert(labelsRight.begin(), labelsRight.end());
48 
49  parameterTypes.insert(parameterTypesRight.begin(), parameterTypesRight.end());
50  parameterTypes.insert(parameterTypesLeft.begin(), parameterTypesLeft.end());
51 
52  wildcardTypes.insert(wildcardTypesRight.begin(), wildcardTypesRight.end());
53  wildcardTypes.insert(wildcardTypesLeft.begin(), wildcardTypesLeft.end());
54  }
55 
56  void IfExistsDescription::validate_(ParameterSet& pset, std::set<std::string>& validatedLabels, bool optional) const {
57  bool leftExists = node_left_->exists(pset);
58  bool rightExists = node_right_->exists(pset);
59 
60  if (!leftExists && !rightExists) {
61  return;
62  } else if (leftExists && rightExists) {
63  node_left_->validate(pset, validatedLabels, false);
64  node_right_->validate(pset, validatedLabels, false);
65  } else if (leftExists && !rightExists) {
66  node_left_->validate(pset, validatedLabels, false);
67  if (!optional)
68  node_right_->validate(pset, validatedLabels, false);
69  } else if (!leftExists && rightExists) {
70  node_left_->validate(pset, validatedLabels, false);
71  node_right_->validate(pset, validatedLabels, false);
72  }
73  }
74 
75  void IfExistsDescription::writeCfi_(std::ostream& os,
76  bool& startWithComma,
77  int indentation,
78  bool& wroteSomething) const {
79  node_left_->writeCfi(os, startWithComma, indentation, wroteSomething);
80  node_right_->writeCfi(os, startWithComma, indentation, wroteSomething);
81  }
82 
83  void IfExistsDescription::print_(std::ostream& os, bool optional, bool writeToCfi, DocFormatHelper& dfh) const {
84  if (dfh.pass() == 1) {
85  dfh.indent(os);
86  os << "IfExists pair:";
87 
88  if (dfh.brief()) {
89  if (optional)
90  os << " optional";
91 
92  if (!writeToCfi)
93  os << " (do not write to cfi)";
94 
95  os << " see Section " << dfh.section() << "." << dfh.counter() << "\n";
96  }
97  // not brief
98  else {
99  os << "\n";
100  dfh.indent2(os);
101 
102  if (optional)
103  os << "optional";
104  if (!writeToCfi)
105  os << " (do not write to cfi)";
106  if (optional || !writeToCfi) {
107  os << "\n";
108  dfh.indent2(os);
109  }
110 
111  os << "see Section " << dfh.section() << "." << dfh.counter() << "\n";
112 
113  if (!comment().empty()) {
115  }
116  os << "\n";
117  }
118  }
119  }
120 
121  void IfExistsDescription::printNestedContent_(std::ostream& os, bool optional, DocFormatHelper& dfh) const {
122  int indentation = dfh.indentation();
123  if (dfh.parent() != DocFormatHelper::TOP) {
124  indentation -= DocFormatHelper::offsetSectionContent();
125  }
126 
127  std::stringstream ss;
128  ss << dfh.section() << "." << dfh.counter();
129  std::string newSection = ss.str();
130 
131  printSpaces(os, indentation);
132  os << "Section " << newSection;
133  if (optional)
134  os << " optional";
135  os << " IfExists pair description:\n";
136  printSpaces(os, indentation);
137  if (optional) {
138  os << "If the first parameter exists, then the second is allowed to exist\n";
139  } else {
140  os << "If the first parameter exists, then the second is required to exist\n";
141  }
142  if (!dfh.brief())
143  os << "\n";
144 
145  DocFormatHelper new_dfh(dfh);
146  new_dfh.init();
147  new_dfh.setSection(newSection);
148  new_dfh.setIndentation(indentation + DocFormatHelper::offsetSectionContent());
150 
151  node_left_->print(os, false, true, new_dfh);
152  node_right_->print(os, false, true, new_dfh);
153 
154  new_dfh.setPass(1);
155  new_dfh.setCounter(0);
156 
157  node_left_->print(os, false, true, new_dfh);
158  node_right_->print(os, false, true, new_dfh);
159 
160  new_dfh.setPass(2);
161  new_dfh.setCounter(0);
162 
163  node_left_->printNestedContent(os, false, new_dfh);
164  node_right_->printNestedContent(os, false, new_dfh);
165  }
166 
168  bool leftExists = node_left_->exists(pset);
169  bool rightExists = node_right_->exists(pset);
170 
171  if (leftExists && rightExists)
172  return true;
173  else if (!leftExists && !rightExists)
174  return true;
175  return false;
176  }
177 
178  bool IfExistsDescription::partiallyExists_(ParameterSet const& pset) const { return exists(pset); }
179 
180  int IfExistsDescription::howManyXORSubNodesExist_(ParameterSet const& pset) const { return exists(pset) ? 1 : 0; }
181 
182  void IfExistsDescription::throwIfDuplicateLabels(std::set<std::string> const& labelsLeft,
183  std::set<std::string> const& labelsRight) const {
184  std::set<std::string> duplicateLabels;
185  std::insert_iterator<std::set<std::string> > insertIter(duplicateLabels, duplicateLabels.begin());
186  std::set_intersection(labelsLeft.begin(), labelsLeft.end(), labelsRight.begin(), labelsRight.end(), insertIter);
187  if (!duplicateLabels.empty()) {
188  std::stringstream ss;
189  for (std::set<std::string>::const_iterator iter = duplicateLabels.begin(), iEnd = duplicateLabels.end();
190  iter != iEnd;
191  ++iter) {
192  ss << " \"" << *iter << "\"\n";
193  }
194  throw edm::Exception(errors::LogicError) << "Labels used in a node of a ParameterSetDescription\n"
195  << "\"ifExists\" expression must be not be the same as labels used\n"
196  << "in other nodes of the expression. The following duplicate\n"
197  << "labels were detected:\n"
198  << ss.str() << "\n";
199  }
200  }
201 
202  void IfExistsDescription::throwIfDuplicateTypes(std::set<ParameterTypes> const& types1,
203  std::set<ParameterTypes> const& types2) const {
204  if (!types1.empty()) {
205  std::set<ParameterTypes> duplicateTypes;
206  std::insert_iterator<std::set<ParameterTypes> > insertIter(duplicateTypes, duplicateTypes.begin());
207  std::set_intersection(types1.begin(), types1.end(), types2.begin(), types2.end(), insertIter);
208  if (!duplicateTypes.empty()) {
209  std::stringstream ss;
210  for (std::set<ParameterTypes>::const_iterator iter = duplicateTypes.begin(), iEnd = duplicateTypes.end();
211  iter != iEnd;
212  ++iter) {
213  ss << " \"" << parameterTypeEnumToString(*iter) << "\"\n";
214  }
216  << "Types used for wildcards in a node of a ParameterSetDescription\n"
217  << "\"ifExists\" expression must be different from types used for other parameters\n"
218  << "in other nodes. The following duplicate types were detected:\n"
219  << ss.str() << "\n";
220  }
221  }
222  }
223 } // namespace edm
bool exists_(ParameterSet const &pset) const override
bool partiallyExists_(ParameterSet const &pset) const override
static void wrapAndPrintText(std::ostream &os, std::string const &text, size_t indent, size_t suggestedWidth)
void print_(std::ostream &os, bool optional, bool writeToCfi, DocFormatHelper &dfh) const override
int startColumn2() const
void printNestedContent_(std::ostream &os, bool optional, DocFormatHelper &dfh) const override
ParameterDescriptionNode * clone() const override
std::string parameterTypeEnumToString(ParameterTypes iType)
DescriptionParent parent() const
void setCounter(int value)
IfExistsDescription(ParameterDescriptionNode const &node_left, ParameterDescriptionNode const &node_right)
void throwIfDuplicateLabels(std::set< std::string > const &labelsLeft, std::set< std::string > const &labelsRight) const
void validate_(ParameterSet &pset, std::set< std::string > &validatedLabels, bool optional) const override
void throwIfDuplicateTypes(std::set< ParameterTypes > const &types1, std::set< ParameterTypes > const &types2) const
int indentation() const
std::string const & comment() const
edm::value_ptr< ParameterDescriptionNode > node_right_
edm::value_ptr< ParameterDescriptionNode > node_left_
static int offsetSectionContent()
void checkAndGetLabelsAndTypes_(std::set< std::string > &usedLabels, std::set< ParameterTypes > &parameterTypes, std::set< ParameterTypes > &wildcardTypes) const override
void indent2(std::ostream &os) const
void setPass(int value)
static void printSpaces(std::ostream &os, int n)
void writeCfi_(std::ostream &os, bool &startWithComma, int indentation, bool &wroteSomething) const override
bool exists(ParameterSet const &pset) const
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
HLT enums.
int howManyXORSubNodesExist_(ParameterSet const &pset) const override
void indent(std::ostream &os) const
void setSection(std::string const &value)
size_t commentWidth() const
def move(src, dest)
Definition: eostools.py:511
void setParent(DescriptionParent value)
void setIndentation(int value)
std::string const & section() const