Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-05-23 03:13:14

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_(std::ostream& os,
0076                                       bool optional,
0077                                       bool& startWithComma,
0078                                       int indentation,
0079                                       CfiOptions& options,
0080                                       bool& wroteSomething) const {
0081     node_left_->writeCfi(os, optional, startWithComma, indentation, options, wroteSomething);
0082     node_right_->writeCfi(os, optional, startWithComma, indentation, options, wroteSomething);
0083     parameterMustBeTyped(options);
0084   }
0085 
0086   void IfExistsDescription::print_(std::ostream& os, bool optional, bool writeToCfi, DocFormatHelper& dfh) const {
0087     if (dfh.pass() == 1) {
0088       dfh.indent(os);
0089       os << "IfExists pair:";
0090 
0091       if (dfh.brief()) {
0092         if (optional)
0093           os << " optional";
0094 
0095         if (!writeToCfi)
0096           os << " (do not write to cfi)";
0097 
0098         os << " see Section " << dfh.section() << "." << dfh.counter() << "\n";
0099       }
0100       // not brief
0101       else {
0102         os << "\n";
0103         dfh.indent2(os);
0104 
0105         if (optional)
0106           os << "optional";
0107         if (!writeToCfi)
0108           os << " (do not write to cfi)";
0109         if (optional || !writeToCfi) {
0110           os << "\n";
0111           dfh.indent2(os);
0112         }
0113 
0114         os << "see Section " << dfh.section() << "." << dfh.counter() << "\n";
0115 
0116         if (!comment().empty()) {
0117           DocFormatHelper::wrapAndPrintText(os, comment(), dfh.startColumn2(), dfh.commentWidth());
0118         }
0119         os << "\n";
0120       }
0121     }
0122   }
0123 
0124   void IfExistsDescription::printNestedContent_(std::ostream& os, bool optional, DocFormatHelper& dfh) const {
0125     int indentation = dfh.indentation();
0126     if (dfh.parent() != DocFormatHelper::TOP) {
0127       indentation -= DocFormatHelper::offsetSectionContent();
0128     }
0129 
0130     std::stringstream ss;
0131     ss << dfh.section() << "." << dfh.counter();
0132     std::string newSection = ss.str();
0133 
0134     printSpaces(os, indentation);
0135     os << "Section " << newSection;
0136     if (optional)
0137       os << " optional";
0138     os << " IfExists pair description:\n";
0139     printSpaces(os, indentation);
0140     if (optional) {
0141       os << "If the first parameter exists, then the second is allowed to exist\n";
0142     } else {
0143       os << "If the first parameter exists, then the second is required to exist\n";
0144     }
0145     if (!dfh.brief())
0146       os << "\n";
0147 
0148     DocFormatHelper new_dfh(dfh);
0149     new_dfh.init();
0150     new_dfh.setSection(newSection);
0151     new_dfh.setIndentation(indentation + DocFormatHelper::offsetSectionContent());
0152     new_dfh.setParent(DocFormatHelper::OTHER);
0153 
0154     node_left_->print(os, false, true, new_dfh);
0155     node_right_->print(os, false, true, new_dfh);
0156 
0157     new_dfh.setPass(1);
0158     new_dfh.setCounter(0);
0159 
0160     node_left_->print(os, false, true, new_dfh);
0161     node_right_->print(os, false, true, new_dfh);
0162 
0163     new_dfh.setPass(2);
0164     new_dfh.setCounter(0);
0165 
0166     node_left_->printNestedContent(os, false, new_dfh);
0167     node_right_->printNestedContent(os, false, new_dfh);
0168   }
0169 
0170   bool IfExistsDescription::exists_(ParameterSet const& pset) const {
0171     bool leftExists = node_left_->exists(pset);
0172     bool rightExists = node_right_->exists(pset);
0173 
0174     if (leftExists && rightExists)
0175       return true;
0176     else if (!leftExists && !rightExists)
0177       return true;
0178     return false;
0179   }
0180 
0181   bool IfExistsDescription::partiallyExists_(ParameterSet const& pset) const { return exists(pset); }
0182 
0183   int IfExistsDescription::howManyXORSubNodesExist_(ParameterSet const& pset) const { return exists(pset) ? 1 : 0; }
0184 
0185   void IfExistsDescription::throwIfDuplicateLabels(std::set<std::string> const& labelsLeft,
0186                                                    std::set<std::string> const& labelsRight) const {
0187     std::set<std::string> duplicateLabels;
0188     std::insert_iterator<std::set<std::string> > insertIter(duplicateLabels, duplicateLabels.begin());
0189     std::set_intersection(labelsLeft.begin(), labelsLeft.end(), labelsRight.begin(), labelsRight.end(), insertIter);
0190     if (!duplicateLabels.empty()) {
0191       std::stringstream ss;
0192       for (std::set<std::string>::const_iterator iter = duplicateLabels.begin(), iEnd = duplicateLabels.end();
0193            iter != iEnd;
0194            ++iter) {
0195         ss << " \"" << *iter << "\"\n";
0196       }
0197       throw edm::Exception(errors::LogicError) << "Labels used in a node of a ParameterSetDescription\n"
0198                                                << "\"ifExists\" expression must be not be the same as labels used\n"
0199                                                << "in other nodes of the expression.  The following duplicate\n"
0200                                                << "labels were detected:\n"
0201                                                << ss.str() << "\n";
0202     }
0203   }
0204 
0205   void IfExistsDescription::throwIfDuplicateTypes(std::set<ParameterTypes> const& types1,
0206                                                   std::set<ParameterTypes> const& types2) const {
0207     if (!types1.empty()) {
0208       std::set<ParameterTypes> duplicateTypes;
0209       std::insert_iterator<std::set<ParameterTypes> > insertIter(duplicateTypes, duplicateTypes.begin());
0210       std::set_intersection(types1.begin(), types1.end(), types2.begin(), types2.end(), insertIter);
0211       if (!duplicateTypes.empty()) {
0212         std::stringstream ss;
0213         for (std::set<ParameterTypes>::const_iterator iter = duplicateTypes.begin(), iEnd = duplicateTypes.end();
0214              iter != iEnd;
0215              ++iter) {
0216           ss << " \"" << parameterTypeEnumToString(*iter) << "\"\n";
0217         }
0218         throw edm::Exception(errors::LogicError)
0219             << "Types used for wildcards in a node of a ParameterSetDescription\n"
0220             << "\"ifExists\" expression must be different from types used for other parameters\n"
0221             << "in other nodes.  The following duplicate types were detected:\n"
0222             << ss.str() << "\n";
0223       }
0224     }
0225   }
0226 }  // namespace edm