Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:03:26

0001 
0002 #include "FWCore/ParameterSet/interface/IfExistsDescription.h"
0003 #include "FWCore/Utilities/interface/EDMException.h"
0004 #include "FWCore/ParameterSet/interface/DocFormatHelper.h"
0005 
0006 #include <algorithm>
0007 #include <sstream>
0008 #include <ostream>
0009 #include <iomanip>
0010 
0011 namespace edm {
0012 
0013   IfExistsDescription::IfExistsDescription(ParameterDescriptionNode const& node_left,
0014                                            ParameterDescriptionNode const& node_right)
0015       : node_left_(node_left.clone()), node_right_(node_right.clone()) {}
0016 
0017   IfExistsDescription::IfExistsDescription(std::unique_ptr<ParameterDescriptionNode> node_left,
0018                                            ParameterDescriptionNode const& node_right)
0019       : node_left_(std::move(node_left)), node_right_(node_right.clone()) {}
0020 
0021   IfExistsDescription::IfExistsDescription(ParameterDescriptionNode const& node_left,
0022                                            std::unique_ptr<ParameterDescriptionNode> node_right)
0023       : node_left_(node_left.clone()), node_right_(std::move(node_right)) {}
0024 
0025   IfExistsDescription::IfExistsDescription(std::unique_ptr<ParameterDescriptionNode> node_left,
0026                                            std::unique_ptr<ParameterDescriptionNode> node_right)
0027       : node_left_(std::move(node_left)), node_right_(std::move(node_right)) {}
0028 
0029   void IfExistsDescription::checkAndGetLabelsAndTypes_(std::set<std::string>& usedLabels,
0030                                                        std::set<ParameterTypes>& parameterTypes,
0031                                                        std::set<ParameterTypes>& wildcardTypes) const {
0032     std::set<std::string> labelsLeft;
0033     std::set<ParameterTypes> parameterTypesLeft;
0034     std::set<ParameterTypes> wildcardTypesLeft;
0035     node_left_->checkAndGetLabelsAndTypes(labelsLeft, parameterTypesLeft, wildcardTypesLeft);
0036 
0037     std::set<std::string> labelsRight;
0038     std::set<ParameterTypes> parameterTypesRight;
0039     std::set<ParameterTypes> wildcardTypesRight;
0040     node_right_->checkAndGetLabelsAndTypes(labelsRight, parameterTypesRight, wildcardTypesRight);
0041 
0042     throwIfDuplicateLabels(labelsLeft, labelsRight);
0043     throwIfDuplicateTypes(wildcardTypesLeft, parameterTypesRight);
0044     throwIfDuplicateTypes(wildcardTypesRight, parameterTypesLeft);
0045 
0046     usedLabels.insert(labelsLeft.begin(), labelsLeft.end());
0047     usedLabels.insert(labelsRight.begin(), labelsRight.end());
0048 
0049     parameterTypes.insert(parameterTypesRight.begin(), parameterTypesRight.end());
0050     parameterTypes.insert(parameterTypesLeft.begin(), parameterTypesLeft.end());
0051 
0052     wildcardTypes.insert(wildcardTypesRight.begin(), wildcardTypesRight.end());
0053     wildcardTypes.insert(wildcardTypesLeft.begin(), wildcardTypesLeft.end());
0054   }
0055 
0056   void IfExistsDescription::validate_(ParameterSet& pset, std::set<std::string>& validatedLabels, bool optional) const {
0057     bool leftExists = node_left_->exists(pset);
0058     bool rightExists = node_right_->exists(pset);
0059 
0060     if (!leftExists && !rightExists) {
0061       return;
0062     } else if (leftExists && rightExists) {
0063       node_left_->validate(pset, validatedLabels, false);
0064       node_right_->validate(pset, validatedLabels, false);
0065     } else if (leftExists && !rightExists) {
0066       node_left_->validate(pset, validatedLabels, false);
0067       if (!optional)
0068         node_right_->validate(pset, validatedLabels, false);
0069     } else if (!leftExists && rightExists) {
0070       node_left_->validate(pset, validatedLabels, false);
0071       node_right_->validate(pset, validatedLabels, false);
0072     }
0073   }
0074 
0075   void IfExistsDescription::writeCfi_(
0076       std::ostream& os, bool optional, bool& startWithComma, int indentation, bool& wroteSomething) const {
0077     node_left_->writeCfi(os, optional, startWithComma, indentation, wroteSomething);
0078     node_right_->writeCfi(os, optional, startWithComma, indentation, wroteSomething);
0079   }
0080 
0081   void IfExistsDescription::print_(std::ostream& os, bool optional, bool writeToCfi, DocFormatHelper& dfh) const {
0082     if (dfh.pass() == 1) {
0083       dfh.indent(os);
0084       os << "IfExists pair:";
0085 
0086       if (dfh.brief()) {
0087         if (optional)
0088           os << " optional";
0089 
0090         if (!writeToCfi)
0091           os << " (do not write to cfi)";
0092 
0093         os << " see Section " << dfh.section() << "." << dfh.counter() << "\n";
0094       }
0095       // not brief
0096       else {
0097         os << "\n";
0098         dfh.indent2(os);
0099 
0100         if (optional)
0101           os << "optional";
0102         if (!writeToCfi)
0103           os << " (do not write to cfi)";
0104         if (optional || !writeToCfi) {
0105           os << "\n";
0106           dfh.indent2(os);
0107         }
0108 
0109         os << "see Section " << dfh.section() << "." << dfh.counter() << "\n";
0110 
0111         if (!comment().empty()) {
0112           DocFormatHelper::wrapAndPrintText(os, comment(), dfh.startColumn2(), dfh.commentWidth());
0113         }
0114         os << "\n";
0115       }
0116     }
0117   }
0118 
0119   void IfExistsDescription::printNestedContent_(std::ostream& os, bool optional, DocFormatHelper& dfh) const {
0120     int indentation = dfh.indentation();
0121     if (dfh.parent() != DocFormatHelper::TOP) {
0122       indentation -= DocFormatHelper::offsetSectionContent();
0123     }
0124 
0125     std::stringstream ss;
0126     ss << dfh.section() << "." << dfh.counter();
0127     std::string newSection = ss.str();
0128 
0129     printSpaces(os, indentation);
0130     os << "Section " << newSection;
0131     if (optional)
0132       os << " optional";
0133     os << " IfExists pair description:\n";
0134     printSpaces(os, indentation);
0135     if (optional) {
0136       os << "If the first parameter exists, then the second is allowed to exist\n";
0137     } else {
0138       os << "If the first parameter exists, then the second is required to exist\n";
0139     }
0140     if (!dfh.brief())
0141       os << "\n";
0142 
0143     DocFormatHelper new_dfh(dfh);
0144     new_dfh.init();
0145     new_dfh.setSection(newSection);
0146     new_dfh.setIndentation(indentation + DocFormatHelper::offsetSectionContent());
0147     new_dfh.setParent(DocFormatHelper::OTHER);
0148 
0149     node_left_->print(os, false, true, new_dfh);
0150     node_right_->print(os, false, true, new_dfh);
0151 
0152     new_dfh.setPass(1);
0153     new_dfh.setCounter(0);
0154 
0155     node_left_->print(os, false, true, new_dfh);
0156     node_right_->print(os, false, true, new_dfh);
0157 
0158     new_dfh.setPass(2);
0159     new_dfh.setCounter(0);
0160 
0161     node_left_->printNestedContent(os, false, new_dfh);
0162     node_right_->printNestedContent(os, false, new_dfh);
0163   }
0164 
0165   bool IfExistsDescription::exists_(ParameterSet const& pset) const {
0166     bool leftExists = node_left_->exists(pset);
0167     bool rightExists = node_right_->exists(pset);
0168 
0169     if (leftExists && rightExists)
0170       return true;
0171     else if (!leftExists && !rightExists)
0172       return true;
0173     return false;
0174   }
0175 
0176   bool IfExistsDescription::partiallyExists_(ParameterSet const& pset) const { return exists(pset); }
0177 
0178   int IfExistsDescription::howManyXORSubNodesExist_(ParameterSet const& pset) const { return exists(pset) ? 1 : 0; }
0179 
0180   void IfExistsDescription::throwIfDuplicateLabels(std::set<std::string> const& labelsLeft,
0181                                                    std::set<std::string> const& labelsRight) const {
0182     std::set<std::string> duplicateLabels;
0183     std::insert_iterator<std::set<std::string> > insertIter(duplicateLabels, duplicateLabels.begin());
0184     std::set_intersection(labelsLeft.begin(), labelsLeft.end(), labelsRight.begin(), labelsRight.end(), insertIter);
0185     if (!duplicateLabels.empty()) {
0186       std::stringstream ss;
0187       for (std::set<std::string>::const_iterator iter = duplicateLabels.begin(), iEnd = duplicateLabels.end();
0188            iter != iEnd;
0189            ++iter) {
0190         ss << " \"" << *iter << "\"\n";
0191       }
0192       throw edm::Exception(errors::LogicError) << "Labels used in a node of a ParameterSetDescription\n"
0193                                                << "\"ifExists\" expression must be not be the same as labels used\n"
0194                                                << "in other nodes of the expression.  The following duplicate\n"
0195                                                << "labels were detected:\n"
0196                                                << ss.str() << "\n";
0197     }
0198   }
0199 
0200   void IfExistsDescription::throwIfDuplicateTypes(std::set<ParameterTypes> const& types1,
0201                                                   std::set<ParameterTypes> const& types2) const {
0202     if (!types1.empty()) {
0203       std::set<ParameterTypes> duplicateTypes;
0204       std::insert_iterator<std::set<ParameterTypes> > insertIter(duplicateTypes, duplicateTypes.begin());
0205       std::set_intersection(types1.begin(), types1.end(), types2.begin(), types2.end(), insertIter);
0206       if (!duplicateTypes.empty()) {
0207         std::stringstream ss;
0208         for (std::set<ParameterTypes>::const_iterator iter = duplicateTypes.begin(), iEnd = duplicateTypes.end();
0209              iter != iEnd;
0210              ++iter) {
0211           ss << " \"" << parameterTypeEnumToString(*iter) << "\"\n";
0212         }
0213         throw edm::Exception(errors::LogicError)
0214             << "Types used for wildcards in a node of a ParameterSetDescription\n"
0215             << "\"ifExists\" expression must be different from types used for other parameters\n"
0216             << "in other nodes.  The following duplicate types were detected:\n"
0217             << ss.str() << "\n";
0218       }
0219     }
0220   }
0221 }  // namespace edm