Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:57

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                                       bool optional,
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       if (dfh.brief()) {
0081         std::stringstream ss;
0082         ss << switchLabel << " (switch)";
0083         std::ios::fmtflags oldFlags = os.flags();
0084         os << std::left << std::setw(dfh.column1()) << ss.str();
0085         os << " ";
0086 
0087         os << std::setw(dfh.column2());
0088         if (isTracked) {
0089           os << typeString;
0090         } else {
0091           std::stringstream ss1;
0092           ss1 << "untracked " << typeString;
0093           os << ss1.str();
0094         }
0095 
0096         os << " " << std::setw(dfh.column3());
0097         if (optional)
0098           os << "optional";
0099         else
0100           os << "";
0101 
0102         if (!writeToCfi)
0103           os << " (do not write to cfi)";
0104 
0105         os << " see Section " << dfh.section() << "." << dfh.counter() << "\n";
0106         os.flags(oldFlags);
0107       } else {
0108         // not brief
0109 
0110         os << switchLabel << " (switch)\n";
0111 
0112         dfh.indent2(os);
0113         os << "type: ";
0114         if (!isTracked)
0115           os << "untracked ";
0116         os << typeString << " ";
0117 
0118         if (optional)
0119           os << "optional";
0120 
0121         if (!writeToCfi)
0122           os << " (do not write to cfi)";
0123         os << "\n";
0124 
0125         dfh.indent2(os);
0126         os << "see Section " << dfh.section() << "." << dfh.counter() << "\n";
0127 
0128         if (!comment().empty()) {
0129           DocFormatHelper::wrapAndPrintText(os, comment(), dfh.startColumn2(), dfh.commentWidth());
0130         }
0131         os << "\n";
0132       }
0133     }
0134   }
0135 
0136   bool ParameterSwitchBase::hasNestedContent_() const { return true; }
0137 
0138   void ParameterSwitchBase::printNestedContentBase(std::ostream& os,
0139                                                    DocFormatHelper& dfh,
0140                                                    DocFormatHelper& new_dfh,
0141                                                    std::string const& switchLabel) const {
0142     int indentation = dfh.indentation();
0143     if (dfh.parent() != DocFormatHelper::TOP) {
0144       indentation -= DocFormatHelper::offsetSectionContent();
0145     }
0146 
0147     std::stringstream ss;
0148     ss << dfh.section() << "." << dfh.counter();
0149     std::string newSection = ss.str();
0150 
0151     printSpaces(os, indentation);
0152     os << "Section " << newSection << " " << switchLabel << " (switch):\n";
0153 
0154     if (!dfh.brief()) {
0155       printSpaces(os, indentation);
0156       os << "The value of \"" << switchLabel << "\" controls which other parameters\n";
0157       printSpaces(os, indentation);
0158       os << "are required or allowed to be in the PSet.\n";
0159     }
0160     if (!dfh.brief())
0161       os << "\n";
0162 
0163     new_dfh.init();
0164     new_dfh.setSection(newSection);
0165     new_dfh.setIndentation(indentation + DocFormatHelper::offsetSectionContent());
0166     new_dfh.setParent(DocFormatHelper::OTHER);
0167   }
0168 
0169   void ParameterSwitchBase::printCase(std::pair<bool, value_ptr<ParameterDescriptionNode> > const& p,
0170                                       std::ostream& os,
0171                                       bool /*optional*/,
0172                                       DocFormatHelper& dfh,
0173                                       std::string const& switchLabel) {
0174     if (dfh.pass() == 0) {
0175       p.second->print(os, false, true, dfh);
0176     }
0177     if (dfh.pass() == 1) {
0178       dfh.indent(os);
0179       os << "if " << switchLabel << " = ";
0180       if (p.first)
0181         os << "True";
0182       else
0183         os << "False";
0184       os << "\n";
0185       p.second->print(os, false, true, dfh);
0186     }
0187     if (dfh.pass() == 2) {
0188       p.second->printNestedContent(os, false, dfh);
0189     }
0190   }
0191 
0192   void ParameterSwitchBase::printCase(std::pair<int, value_ptr<ParameterDescriptionNode> > const& p,
0193                                       std::ostream& os,
0194                                       bool /*optional*/,
0195                                       DocFormatHelper& dfh,
0196                                       std::string const& switchLabel) {
0197     if (dfh.pass() == 0) {
0198       p.second->print(os, false, true, dfh);
0199     }
0200     if (dfh.pass() == 1) {
0201       dfh.indent(os);
0202       os << "if " << switchLabel << " = " << p.first << "\n";
0203       p.second->print(os, false, true, dfh);
0204     }
0205     if (dfh.pass() == 2) {
0206       p.second->printNestedContent(os, false, dfh);
0207     }
0208   }
0209 
0210   void ParameterSwitchBase::printCase(std::pair<std::string, value_ptr<ParameterDescriptionNode> > const& p,
0211                                       std::ostream& os,
0212                                       bool /*optional*/,
0213                                       DocFormatHelper& dfh,
0214                                       std::string const& switchLabel) {
0215     if (dfh.pass() == 0) {
0216       p.second->print(os, false, true, dfh);
0217     }
0218     if (dfh.pass() == 1) {
0219       dfh.indent(os);
0220       os << "if " << switchLabel << " = \"" << p.first << "\"\n";
0221       p.second->print(os, false, true, dfh);
0222     }
0223     if (dfh.pass() == 2) {
0224       p.second->printNestedContent(os, false, dfh);
0225     }
0226   }
0227 
0228   bool ParameterSwitchBase::partiallyExists_(ParameterSet const& pset) const { return exists(pset); }
0229 
0230   int ParameterSwitchBase::howManyXORSubNodesExist_(ParameterSet const& pset) const { return exists(pset) ? 1 : 0; }
0231 }  // namespace edm