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 optional,
77  bool& startWithComma,
78  int indentation,
80  bool& wroteSomething) const {
81  node_left_->writeCfi(os, optional, startWithComma, indentation, options, wroteSomething);
82  node_right_->writeCfi(os, optional, startWithComma, indentation, options, wroteSomething);
84  }
85 
86  void IfExistsDescription::print_(std::ostream& os, bool optional, bool writeToCfi, DocFormatHelper& dfh) const {
87  if (dfh.pass() == 1) {
88  dfh.indent(os);
89  os << "IfExists pair:";
90 
91  if (dfh.brief()) {
92  if (optional)
93  os << " optional";
94 
95  if (!writeToCfi)
96  os << " (do not write to cfi)";
97 
98  os << " see Section " << dfh.section() << "." << dfh.counter() << "\n";
99  }
100  // not brief
101  else {
102  os << "\n";
103  dfh.indent2(os);
104 
105  if (optional)
106  os << "optional";
107  if (!writeToCfi)
108  os << " (do not write to cfi)";
109  if (optional || !writeToCfi) {
110  os << "\n";
111  dfh.indent2(os);
112  }
113 
114  os << "see Section " << dfh.section() << "." << dfh.counter() << "\n";
115 
116  if (!comment().empty()) {
118  }
119  os << "\n";
120  }
121  }
122  }
123 
124  void IfExistsDescription::printNestedContent_(std::ostream& os, bool optional, DocFormatHelper& dfh) const {
125  int indentation = dfh.indentation();
126  if (dfh.parent() != DocFormatHelper::TOP) {
128  }
129 
130  std::stringstream ss;
131  ss << dfh.section() << "." << dfh.counter();
132  std::string newSection = ss.str();
133 
135  os << "Section " << newSection;
136  if (optional)
137  os << " optional";
138  os << " IfExists pair description:\n";
140  if (optional) {
141  os << "If the first parameter exists, then the second is allowed to exist\n";
142  } else {
143  os << "If the first parameter exists, then the second is required to exist\n";
144  }
145  if (!dfh.brief())
146  os << "\n";
147 
148  DocFormatHelper new_dfh(dfh);
149  new_dfh.init();
150  new_dfh.setSection(newSection);
153 
154  node_left_->print(os, false, true, new_dfh);
155  node_right_->print(os, false, true, new_dfh);
156 
157  new_dfh.setPass(1);
158  new_dfh.setCounter(0);
159 
160  node_left_->print(os, false, true, new_dfh);
161  node_right_->print(os, false, true, new_dfh);
162 
163  new_dfh.setPass(2);
164  new_dfh.setCounter(0);
165 
166  node_left_->printNestedContent(os, false, new_dfh);
167  node_right_->printNestedContent(os, false, new_dfh);
168  }
169 
171  bool leftExists = node_left_->exists(pset);
172  bool rightExists = node_right_->exists(pset);
173 
174  if (leftExists && rightExists)
175  return true;
176  else if (!leftExists && !rightExists)
177  return true;
178  return false;
179  }
180 
182 
184 
185  void IfExistsDescription::throwIfDuplicateLabels(std::set<std::string> const& labelsLeft,
186  std::set<std::string> const& labelsRight) const {
187  std::set<std::string> duplicateLabels;
188  std::insert_iterator<std::set<std::string> > insertIter(duplicateLabels, duplicateLabels.begin());
189  std::set_intersection(labelsLeft.begin(), labelsLeft.end(), labelsRight.begin(), labelsRight.end(), insertIter);
190  if (!duplicateLabels.empty()) {
191  std::stringstream ss;
192  for (std::set<std::string>::const_iterator iter = duplicateLabels.begin(), iEnd = duplicateLabels.end();
193  iter != iEnd;
194  ++iter) {
195  ss << " \"" << *iter << "\"\n";
196  }
197  throw edm::Exception(errors::LogicError) << "Labels used in a node of a ParameterSetDescription\n"
198  << "\"ifExists\" expression must be not be the same as labels used\n"
199  << "in other nodes of the expression. The following duplicate\n"
200  << "labels were detected:\n"
201  << ss.str() << "\n";
202  }
203  }
204 
205  void IfExistsDescription::throwIfDuplicateTypes(std::set<ParameterTypes> const& types1,
206  std::set<ParameterTypes> const& types2) const {
207  if (!types1.empty()) {
208  std::set<ParameterTypes> duplicateTypes;
209  std::insert_iterator<std::set<ParameterTypes> > insertIter(duplicateTypes, duplicateTypes.begin());
210  std::set_intersection(types1.begin(), types1.end(), types2.begin(), types2.end(), insertIter);
211  if (!duplicateTypes.empty()) {
212  std::stringstream ss;
213  for (std::set<ParameterTypes>::const_iterator iter = duplicateTypes.begin(), iEnd = duplicateTypes.end();
214  iter != iEnd;
215  ++iter) {
216  ss << " \"" << parameterTypeEnumToString(*iter) << "\"\n";
217  }
219  << "Types used for wildcards in a node of a ParameterSetDescription\n"
220  << "\"ifExists\" expression must be different from types used for other parameters\n"
221  << "in other nodes. The following duplicate types were detected:\n"
222  << ss.str() << "\n";
223  }
224  }
225  }
226 } // namespace edm
void throwIfDuplicateTypes(std::set< ParameterTypes > const &types1, std::set< ParameterTypes > const &types2) const
int startColumn2() const
static void wrapAndPrintText(std::ostream &os, std::string const &text, size_t indent, size_t suggestedWidth)
void throwIfDuplicateLabels(std::set< std::string > const &labelsLeft, std::set< std::string > const &labelsRight) const
void checkAndGetLabelsAndTypes_(std::set< std::string > &usedLabels, std::set< ParameterTypes > &parameterTypes, std::set< ParameterTypes > &wildcardTypes) const override
std::string parameterTypeEnumToString(ParameterTypes iType)
size_t commentWidth() const
void setCounter(int value)
IfExistsDescription(ParameterDescriptionNode const &node_left, ParameterDescriptionNode const &node_right)
bool partiallyExists_(ParameterSet const &pset) const override
void indent2(std::ostream &os) const
void parameterMustBeTyped(CfiOptions &iOps) noexcept
bool exists_(ParameterSet const &pset) const override
edm::value_ptr< ParameterDescriptionNode > node_right_
edm::value_ptr< ParameterDescriptionNode > node_left_
void print_(std::ostream &os, bool optional, bool writeToCfi, DocFormatHelper &dfh) const override
static int offsetSectionContent()
optional
Definition: Types.py:245
void setPass(int value)
void printNestedContent_(std::ostream &os, bool optional, DocFormatHelper &dfh) const override
int howManyXORSubNodesExist_(ParameterSet const &pset) const override
std::string const & section() const
static void printSpaces(std::ostream &os, int n)
void validate_(ParameterSet &pset, std::set< std::string > &validatedLabels, bool optional) const override
void writeCfi_(std::ostream &os, bool optional, bool &startWithComma, int indentation, CfiOptions &, bool &wroteSomething) const override
std::string const & comment() const
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
bool exists(ParameterSet const &pset) const
HLT enums.
void setSection(std::string const &value)
void indent(std::ostream &os) const
def move(src, dest)
Definition: eostools.py:511
void setParent(DescriptionParent value)
cfi::CfiOptions CfiOptions
DescriptionParent parent() const
void setIndentation(int value)
std::vector< std::string > set_intersection(std::vector< std::string > const &v1, std::vector< std::string > const &v2)