Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // Package:     ParameterSet
0004 // Class  :     ParameterDescriptionBase
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  Thu Aug  2 15:35:43 EDT 2007
0011 //
0012 
0013 #include "FWCore/ParameterSet/interface/ParameterDescriptionBase.h"
0014 
0015 #include "FWCore/ParameterSet/interface/DocFormatHelper.h"
0016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0017 #include "FWCore/Utilities/interface/EDMException.h"
0018 
0019 #include <iomanip>
0020 #include <iostream>
0021 
0022 namespace edm {
0023 
0024   ParameterDescriptionBase::ParameterDescriptionBase(
0025       std::string const& iLabel, ParameterTypes iType, bool isTracked, bool hasDefault, Comment const& iComment)
0026       : ParameterDescriptionNode(iComment),
0027         label_(iLabel),
0028         type_(iType),
0029         isTracked_(isTracked),
0030         hasDefault_(hasDefault) {
0031     if (label_.empty()) {
0032       throw Exception(errors::LogicError) << "Empty string used as a label for a parameter.  This is\n"
0033                                              "not allowed.\n";
0034     }
0035   }
0036 
0037   ParameterDescriptionBase::ParameterDescriptionBase(
0038       char const* iLabel, ParameterTypes iType, bool isTracked, bool hasDefault, Comment const& iComment)
0039       : ParameterDescriptionNode(iComment),
0040         label_(iLabel),
0041         type_(iType),
0042         isTracked_(isTracked),
0043         hasDefault_(hasDefault) {
0044     if (label_.empty()) {
0045       throw Exception(errors::LogicError) << "Empty string used as a label for a parameter.  This is\n"
0046                                              "not allowed.\n";
0047     }
0048   }
0049 
0050   ParameterDescriptionBase::~ParameterDescriptionBase() {}
0051 
0052   void ParameterDescriptionBase::throwParameterWrongTrackiness() const {
0053     std::string tr("a tracked");
0054     std::string shouldBe("untracked");
0055     if (isTracked()) {
0056       tr = "an untracked";
0057       shouldBe = "tracked";
0058     }
0059 
0060     throw Exception(errors::Configuration) << "In the configuration, parameter \"" << label()
0061                                            << "\" is defined "
0062                                               "as "
0063                                            << tr << " " << parameterTypeEnumToString(type()) << ".\n"
0064                                            << "It should be " << shouldBe << ".\n";
0065   }
0066 
0067   void ParameterDescriptionBase::throwParameterWrongType() const {
0068     std::string tr("an untracked");
0069     if (isTracked())
0070       tr = "a tracked";
0071 
0072     throw Exception(errors::Configuration) << "Parameter \"" << label()
0073                                            << "\" should be defined "
0074                                               "as "
0075                                            << tr << " " << parameterTypeEnumToString(type()) << ".\n"
0076                                            << "The type in the configuration is incorrect.\n";
0077   }
0078 
0079   void ParameterDescriptionBase::throwMissingRequiredNoDefault() const {
0080     std::string tr("untracked");
0081     if (isTracked())
0082       tr = "tracked";
0083 
0084     throw Exception(errors::Configuration)
0085         << "Missing required parameter.  It should have label \"" << label() << "\" and have type \"" << tr << " "
0086         << parameterTypeEnumToString(type()) << "\".\n"
0087         << "The description has no default.  The parameter must be defined "
0088            "in the configuration\n";
0089   }
0090 
0091   void ParameterDescriptionBase::checkAndGetLabelsAndTypes_(std::set<std::string>& usedLabels,
0092                                                             std::set<ParameterTypes>& parameterTypes,
0093                                                             std::set<ParameterTypes>& /*wildcardTypes*/) const {
0094     usedLabels.insert(label());
0095     parameterTypes.insert(type());
0096   }
0097 
0098   void ParameterDescriptionBase::validate_(ParameterSet& pset,
0099                                            std::set<std::string>& validatedLabels,
0100                                            bool optional) const {
0101     bool exists = exists_(pset, isTracked());
0102 
0103     if (exists) {
0104       validatedLabels.insert(label());
0105     } else if (exists_(pset, !isTracked())) {
0106       throwParameterWrongTrackiness();
0107     } else if (pset.exists(label())) {
0108       throwParameterWrongType();
0109     }
0110 
0111     if (!exists && !optional) {
0112       if (hasDefault()) {
0113         insertDefault_(pset);
0114         validatedLabels.insert(label());
0115       } else {
0116         throwMissingRequiredNoDefault();
0117       }
0118     }
0119   }
0120 
0121   void ParameterDescriptionBase::writeCfi_(std::ostream& os,
0122                                            bool optional,
0123                                            bool& startWithComma,
0124                                            int indentation,
0125                                            CfiOptions& options,
0126                                            bool& wroteSomething) const {
0127     if (label().empty() or label()[0] == '@') {
0128       return;
0129     }
0130 
0131     auto check = cfi::needToSwitchToTyped(label(), options);
0132     if (check.first) {
0133       CfiOptions fullOp = cfi::Typed{};
0134       writeFullCfi(os, optional, startWithComma, indentation, fullOp, wroteSomething);
0135     } else if (shouldWriteUntyped(options)) {
0136       writeLabelValueCfi(os, optional, startWithComma, indentation, options, wroteSomething);
0137     } else {
0138       writeFullCfi(os, optional, startWithComma, indentation, options, wroteSomething);
0139     }
0140   }
0141 
0142   void ParameterDescriptionBase::writeLabelValueCfi(std::ostream& os,
0143                                                     bool optional,
0144                                                     bool& startWithComma,
0145                                                     int indentation,
0146                                                     CfiOptions& options,
0147                                                     bool& wroteSomething) const {
0148     constexpr std::string_view k_endList = "]";
0149     constexpr std::string_view k_endParenthesis = ")";
0150     constexpr std::string_view k_endBasicType = "";
0151     if (not hasDefault()) {
0152       return;
0153     }
0154     wroteSomething = true;
0155     if (startWithComma)
0156       os << ",";
0157     startWithComma = true;
0158     os << "\n";
0159     printSpaces(os, indentation);
0160 
0161     os << label() << " = ";
0162     std::string_view endDelimiter = k_endBasicType;
0163     switch (type()) {
0164       case k_vdouble:
0165       case k_vint32:
0166       case k_vint64:
0167       case k_vstringRaw:
0168       case k_vuint32:
0169       case k_vuint64:
0170       case k_VESInputTag:
0171       case k_VEventID:
0172       case k_VEventRange:
0173       case k_VInputTag:
0174       case k_VLuminosityBlockID:
0175       case k_VLuminosityBlockRange:
0176       case k_VPSet:
0177         os << "[";
0178         endDelimiter = k_endList;
0179         break;
0180       case k_PSet:
0181         os << "dict(";
0182         endDelimiter = k_endParenthesis;
0183         break;
0184       case k_EventID:
0185       case k_EventRange:
0186       case k_ESInputTag:
0187       case k_InputTag:
0188       case k_LuminosityBlockID:
0189       case k_LuminosityBlockRange:
0190         os << "(";
0191         endDelimiter = k_endParenthesis;
0192         break;
0193       default:
0194         break;
0195     }
0196     writeCfi_(os, indentation, options);
0197     os << endDelimiter;
0198     return;
0199   }
0200 
0201   void ParameterDescriptionBase::writeFullCfi(std::ostream& os,
0202                                               bool optional,
0203                                               bool& startWithComma,
0204                                               int indentation,
0205                                               CfiOptions& options,
0206                                               bool& wroteSomething) const {
0207     wroteSomething = true;
0208     if (startWithComma)
0209       os << ",";
0210     startWithComma = true;
0211 
0212     os << "\n";
0213     printSpaces(os, indentation);
0214 
0215     os << label() << " = cms.";
0216 
0217     if (!hasDefault()) {
0218       if (optional) {
0219         os << "optional.";
0220       } else {
0221         os << "required.";
0222       }
0223       if (!isTracked())
0224         os << "untracked.";
0225       os << parameterTypeEnumToString(type());
0226     } else {
0227       if (!isTracked())
0228         os << "untracked.";
0229       os << parameterTypeEnumToString(type()) << "(";
0230       writeCfi_(os, indentation, options);
0231       os << ")";
0232     }
0233   }
0234   void ParameterDescriptionBase::print_(std::ostream& os, bool optional, bool writeToCfi, DocFormatHelper& dfh) const {
0235     if (dfh.pass() == 0) {
0236       dfh.setAtLeast1(label().size());
0237       if (isTracked()) {
0238         dfh.setAtLeast2(parameterTypeEnumToString(type()).size());
0239       } else {
0240         dfh.setAtLeast2(parameterTypeEnumToString(type()).size() + 10U);
0241       }
0242       if (optional) {
0243         dfh.setAtLeast3(8U);
0244       }
0245     } else {
0246       if (dfh.brief()) {
0247         std::ios::fmtflags oldFlags = os.flags();
0248 
0249         dfh.indent(os);
0250         os << std::left << std::setw(dfh.column1()) << label() << " ";
0251 
0252         if (isTracked()) {
0253           os << std::setw(dfh.column2()) << parameterTypeEnumToString(type());
0254         } else {
0255           std::stringstream ss;
0256           ss << "untracked ";
0257           ss << parameterTypeEnumToString(type());
0258           os << std::setw(dfh.column2()) << ss.str();
0259         }
0260         os << " ";
0261 
0262         os << std::setw(dfh.column3());
0263         if (optional) {
0264           os << "optional";
0265         } else {
0266           os << "";
0267         }
0268         os << " ";
0269         os.flags(oldFlags);
0270         printDefault_(os, writeToCfi, dfh);
0271       } else {
0272         // not brief
0273         dfh.indent(os);
0274         os << label() << "\n";
0275 
0276         dfh.indent2(os);
0277         os << "type: ";
0278         if (!isTracked())
0279           os << "untracked ";
0280 
0281         os << parameterTypeEnumToString(type()) << " ";
0282 
0283         if (optional)
0284           os << "optional";
0285         os << "\n";
0286 
0287         dfh.indent2(os);
0288         printDefault_(os, writeToCfi, dfh);
0289 
0290         if (!comment().empty()) {
0291           DocFormatHelper::wrapAndPrintText(os, comment(), dfh.startColumn2(), dfh.commentWidth());
0292         }
0293         os << "\n";
0294       }
0295     }
0296   }
0297 
0298   void ParameterDescriptionBase::printDefault_(std::ostream& os, bool writeToCfi, DocFormatHelper& dfh) const {
0299     if (!dfh.brief())
0300       os << "default: ";
0301     if (writeToCfi && hasDefault()) {
0302       if (hasNestedContent()) {
0303         os << "see Section " << dfh.section() << "." << dfh.counter();
0304       } else {
0305         if (dfh.brief()) {
0306           writeDoc_(os, dfh.indentation());
0307         } else {
0308           writeDoc_(os, dfh.startColumn2());
0309         }
0310       }
0311     } else if (!writeToCfi) {
0312       os << "none (do not write to cfi)";
0313     } else {
0314       os << "none";
0315     }
0316     os << "\n";
0317   }
0318 
0319   void ParameterDescriptionBase::printNestedContent_(std::ostream& os, bool /*optional*/, DocFormatHelper& dfh) const {
0320     int indentation = dfh.indentation();
0321     if (dfh.parent() != DocFormatHelper::TOP) {
0322       indentation -= DocFormatHelper::offsetSectionContent();
0323     }
0324 
0325     printSpaces(os, indentation);
0326     os << "Section " << dfh.section() << "." << dfh.counter() << " " << label() << " default contents: ";
0327     writeDoc_(os, indentation + 2);
0328     os << "\n";
0329     if (!dfh.brief())
0330       os << "\n";
0331   }
0332 
0333   bool ParameterDescriptionBase::partiallyExists_(ParameterSet const& pset) const { return exists(pset); }
0334 
0335   int ParameterDescriptionBase::howManyXORSubNodesExist_(ParameterSet const& pset) const {
0336     return exists(pset) ? 1 : 0;
0337   }
0338 }  // namespace edm