Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-03-26 01:51:14

0001 
0002 #include "FWCore/ParameterSet/interface/ParameterSwitchBase.h"
0003 
0004 #include "FWCore/ParameterSet/interface/DocFormatHelper.h"
0005 #include "FWCore/Utilities/interface/EDMException.h"
0006 
0007 #include <iomanip>
0008 #include <ostream>
0009 #include <sstream>
0010 
0011 namespace edm {
0012 
0013   ParameterSwitchBase::~ParameterSwitchBase() {}
0014 
0015   void ParameterSwitchBase::throwDuplicateCaseValues(std::string const& switchLabel) const {
0016     throw Exception(errors::LogicError) << "When adding a ParameterSwitch to a ParameterSetDescription the values\n"
0017                                         << "associated with the different cases must be unique.  Duplicate\n"
0018                                         << "values were found for the switch with label: \"" << switchLabel << "\"\n";
0019   }
0020 
0021   void ParameterSwitchBase::insertAndCheckLabels(std::string const& switchLabel,
0022                                                  std::set<std::string>& usedLabels,
0023                                                  std::set<std::string>& labels) const {
0024     std::pair<std::set<std::string>::iterator, bool> status = labels.insert(switchLabel);
0025     if (status.second == false) {
0026       throw Exception(errors::LogicError)
0027           << "The label used for the switch parameter in a ParameterSetDescription\n"
0028           << "must be different from the labels used in the associated cases.  The following\n"
0029           << "duplicate label was found: \"" << switchLabel << "\"\n";
0030     }
0031     usedLabels.insert(labels.begin(), labels.end());
0032   }
0033 
0034   void ParameterSwitchBase::insertAndCheckTypes(ParameterTypes switchType,
0035                                                 std::set<ParameterTypes> const& caseParameterTypes,
0036                                                 std::set<ParameterTypes> const& caseWildcardTypes,
0037                                                 std::set<ParameterTypes>& parameterTypes,
0038                                                 std::set<ParameterTypes>& wildcardTypes) const {
0039     if (caseWildcardTypes.find(switchType) != caseWildcardTypes.end()) {
0040       throw Exception(errors::LogicError)
0041           << "The type used for the switch parameter in a ParameterSetDescription\n"
0042           << "must be different from the types used for wildcards in the associated cases.  The following\n"
0043           << "duplicate type was found: \"" << parameterTypeEnumToString(switchType) << "\"\n";
0044     }
0045     parameterTypes.insert(switchType);
0046     parameterTypes.insert(caseParameterTypes.begin(), caseParameterTypes.end());
0047     wildcardTypes.insert(caseWildcardTypes.begin(), caseWildcardTypes.end());
0048   }
0049 
0050   void ParameterSwitchBase::throwNoCaseForDefault(std::string const& switchLabel) const {
0051     throw Exception(errors::LogicError)
0052         << "The default value used for the switch parameter in a ParameterSetDescription\n"
0053         << "must match the value used to select one of the associated cases.  This is not\n"
0054         << "true for the switch named \"" << switchLabel << "\"\n";
0055   }
0056 
0057   void ParameterSwitchBase::throwNoCaseForSwitchValue(std::string const& message) const {
0058     throw Exception(errors::Configuration) << message;
0059   }
0060 
0061   void ParameterSwitchBase::printBase(std::ostream& os,
0062                                       Modifier modifier,
0063                                       bool writeToCfi,
0064                                       DocFormatHelper& dfh,
0065                                       std::string const& switchLabel,
0066                                       bool isTracked,
0067                                       std::string const& typeString) const {
0068     if (dfh.pass() == 0) {
0069       dfh.setAtLeast1(switchLabel.size() + 9U);
0070       if (isTracked) {
0071         dfh.setAtLeast2(typeString.size());
0072       } else {
0073         dfh.setAtLeast2(typeString.size() + 10U);
0074       }
0075       dfh.setAtLeast3(8U);
0076     }
0077     if (dfh.pass() == 1) {
0078       dfh.indent(os);
0079 
0080       const bool optional = (Modifier::kOptional == modifier);
0081       const bool obsolete = (Modifier::kObsolete == modifier);
0082       if (dfh.brief()) {
0083         std::stringstream ss;
0084         ss << switchLabel << " (switch)";
0085         std::ios::fmtflags oldFlags = os.flags();
0086         os << std::left << std::setw(dfh.column1()) << ss.str();
0087         os << " ";
0088 
0089         os << std::setw(dfh.column2());
0090         if (isTracked) {
0091           os << typeString;
0092         } else {
0093           std::stringstream ss1;
0094           ss1 << "untracked " << typeString;
0095           os << ss1.str();
0096         }
0097 
0098         os << " " << std::setw(dfh.column3());
0099         if (optional)
0100           os << "optional";
0101         else if (obsolete)
0102           os << "obsolete";
0103         else
0104           os << "";
0105 
0106         if (!writeToCfi)
0107           os << " (do not write to cfi)";
0108 
0109         os << " see Section " << dfh.section() << "." << dfh.counter() << "\n";
0110         os.flags(oldFlags);
0111       } else {
0112         // not brief
0113 
0114         os << switchLabel << " (switch)\n";
0115 
0116         dfh.indent2(os);
0117         os << "type: ";
0118         if (!isTracked)
0119           os << "untracked ";
0120         os << typeString << " ";
0121 
0122         if (optional)
0123           os << "optional";
0124         if (obsolete)
0125           os << "obsolete";
0126 
0127         if (!writeToCfi)
0128           os << " (do not write to cfi)";
0129         os << "\n";
0130 
0131         dfh.indent2(os);
0132         os << "see Section " << dfh.section() << "." << dfh.counter() << "\n";
0133 
0134         if (!comment().empty()) {
0135           DocFormatHelper::wrapAndPrintText(os, comment(), dfh.startColumn2(), dfh.commentWidth());
0136         }
0137         os << "\n";
0138       }
0139     }
0140   }
0141 
0142   bool ParameterSwitchBase::hasNestedContent_() const { return true; }
0143 
0144   void ParameterSwitchBase::printNestedContentBase(std::ostream& os,
0145                                                    DocFormatHelper& dfh,
0146                                                    DocFormatHelper& new_dfh,
0147                                                    std::string const& switchLabel) const {
0148     int indentation = dfh.indentation();
0149     if (dfh.parent() != DocFormatHelper::TOP) {
0150       indentation -= DocFormatHelper::offsetSectionContent();
0151     }
0152 
0153     std::stringstream ss;
0154     ss << dfh.section() << "." << dfh.counter();
0155     std::string newSection = ss.str();
0156 
0157     printSpaces(os, indentation);
0158     os << "Section " << newSection << " " << switchLabel << " (switch):\n";
0159 
0160     if (!dfh.brief()) {
0161       printSpaces(os, indentation);
0162       os << "The value of \"" << switchLabel << "\" controls which other parameters\n";
0163       printSpaces(os, indentation);
0164       os << "are required or allowed to be in the PSet.\n";
0165     }
0166     if (!dfh.brief())
0167       os << "\n";
0168 
0169     new_dfh.init();
0170     new_dfh.setSection(newSection);
0171     new_dfh.setIndentation(indentation + DocFormatHelper::offsetSectionContent());
0172     new_dfh.setParent(DocFormatHelper::OTHER);
0173   }
0174 
0175   void ParameterSwitchBase::printCase(std::pair<bool, value_ptr<ParameterDescriptionNode> > const& p,
0176                                       std::ostream& os,
0177                                       bool /*optional*/,
0178                                       DocFormatHelper& dfh,
0179                                       std::string const& switchLabel) {
0180     if (dfh.pass() == 0) {
0181       p.second->print(os, Modifier::kNone, true, dfh);
0182     }
0183     if (dfh.pass() == 1) {
0184       dfh.indent(os);
0185       os << "if " << switchLabel << " = ";
0186       if (p.first)
0187         os << "True";
0188       else
0189         os << "False";
0190       os << "\n";
0191       p.second->print(os, Modifier::kNone, true, dfh);
0192     }
0193     if (dfh.pass() == 2) {
0194       p.second->printNestedContent(os, false, dfh);
0195     }
0196   }
0197 
0198   void ParameterSwitchBase::printCase(std::pair<int, value_ptr<ParameterDescriptionNode> > const& p,
0199                                       std::ostream& os,
0200                                       bool /*optional*/,
0201                                       DocFormatHelper& dfh,
0202                                       std::string const& switchLabel) {
0203     if (dfh.pass() == 0) {
0204       p.second->print(os, Modifier::kNone, true, dfh);
0205     }
0206     if (dfh.pass() == 1) {
0207       dfh.indent(os);
0208       os << "if " << switchLabel << " = " << p.first << "\n";
0209       p.second->print(os, Modifier::kNone, true, dfh);
0210     }
0211     if (dfh.pass() == 2) {
0212       p.second->printNestedContent(os, false, dfh);
0213     }
0214   }
0215 
0216   void ParameterSwitchBase::printCase(std::pair<std::string, value_ptr<ParameterDescriptionNode> > const& p,
0217                                       std::ostream& os,
0218                                       bool /*optional*/,
0219                                       DocFormatHelper& dfh,
0220                                       std::string const& switchLabel) {
0221     if (dfh.pass() == 0) {
0222       p.second->print(os, Modifier::kNone, true, dfh);
0223     }
0224     if (dfh.pass() == 1) {
0225       dfh.indent(os);
0226       os << "if " << switchLabel << " = \"" << p.first << "\"\n";
0227       p.second->print(os, Modifier::kNone, true, dfh);
0228     }
0229     if (dfh.pass() == 2) {
0230       p.second->printNestedContent(os, false, dfh);
0231     }
0232   }
0233 
0234   bool ParameterSwitchBase::partiallyExists_(ParameterSet const& pset) const { return exists(pset); }
0235 
0236   int ParameterSwitchBase::howManyXORSubNodesExist_(ParameterSet const& pset) const { return exists(pset) ? 1 : 0; }
0237 }  // namespace edm