Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:28:57

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_(
0122       std::ostream& os, bool optional, bool& startWithComma, int indentation, bool& wroteSomething) const {
0123     wroteSomething = true;
0124     if (startWithComma)
0125       os << ",";
0126     startWithComma = true;
0127 
0128     os << "\n";
0129     printSpaces(os, indentation);
0130 
0131     os << label() << " = cms.";
0132 
0133     if (!hasDefault()) {
0134       if (optional) {
0135         os << "optional.";
0136       } else {
0137         os << "required.";
0138       }
0139       if (!isTracked())
0140         os << "untracked.";
0141       os << parameterTypeEnumToString(type());
0142     } else {
0143       if (!isTracked())
0144         os << "untracked.";
0145       os << parameterTypeEnumToString(type()) << "(";
0146       writeCfi_(os, indentation);
0147       os << ")";
0148     }
0149   }
0150 
0151   void ParameterDescriptionBase::print_(std::ostream& os, bool optional, bool writeToCfi, DocFormatHelper& dfh) const {
0152     if (dfh.pass() == 0) {
0153       dfh.setAtLeast1(label().size());
0154       if (isTracked()) {
0155         dfh.setAtLeast2(parameterTypeEnumToString(type()).size());
0156       } else {
0157         dfh.setAtLeast2(parameterTypeEnumToString(type()).size() + 10U);
0158       }
0159       if (optional) {
0160         dfh.setAtLeast3(8U);
0161       }
0162     } else {
0163       if (dfh.brief()) {
0164         std::ios::fmtflags oldFlags = os.flags();
0165 
0166         dfh.indent(os);
0167         os << std::left << std::setw(dfh.column1()) << label() << " ";
0168 
0169         if (isTracked()) {
0170           os << std::setw(dfh.column2()) << parameterTypeEnumToString(type());
0171         } else {
0172           std::stringstream ss;
0173           ss << "untracked ";
0174           ss << parameterTypeEnumToString(type());
0175           os << std::setw(dfh.column2()) << ss.str();
0176         }
0177         os << " ";
0178 
0179         os << std::setw(dfh.column3());
0180         if (optional) {
0181           os << "optional";
0182         } else {
0183           os << "";
0184         }
0185         os << " ";
0186         os.flags(oldFlags);
0187         printDefault_(os, writeToCfi, dfh);
0188       } else {
0189         // not brief
0190         dfh.indent(os);
0191         os << label() << "\n";
0192 
0193         dfh.indent2(os);
0194         os << "type: ";
0195         if (!isTracked())
0196           os << "untracked ";
0197 
0198         os << parameterTypeEnumToString(type()) << " ";
0199 
0200         if (optional)
0201           os << "optional";
0202         os << "\n";
0203 
0204         dfh.indent2(os);
0205         printDefault_(os, writeToCfi, dfh);
0206 
0207         if (!comment().empty()) {
0208           DocFormatHelper::wrapAndPrintText(os, comment(), dfh.startColumn2(), dfh.commentWidth());
0209         }
0210         os << "\n";
0211       }
0212     }
0213   }
0214 
0215   void ParameterDescriptionBase::printDefault_(std::ostream& os, bool writeToCfi, DocFormatHelper& dfh) const {
0216     if (!dfh.brief())
0217       os << "default: ";
0218     if (writeToCfi && hasDefault()) {
0219       if (hasNestedContent()) {
0220         os << "see Section " << dfh.section() << "." << dfh.counter();
0221       } else {
0222         if (dfh.brief()) {
0223           writeDoc_(os, dfh.indentation());
0224         } else {
0225           writeDoc_(os, dfh.startColumn2());
0226         }
0227       }
0228     } else if (!writeToCfi) {
0229       os << "none (do not write to cfi)";
0230     } else {
0231       os << "none";
0232     }
0233     os << "\n";
0234   }
0235 
0236   void ParameterDescriptionBase::printNestedContent_(std::ostream& os, bool /*optional*/, DocFormatHelper& dfh) const {
0237     int indentation = dfh.indentation();
0238     if (dfh.parent() != DocFormatHelper::TOP) {
0239       indentation -= DocFormatHelper::offsetSectionContent();
0240     }
0241 
0242     printSpaces(os, indentation);
0243     os << "Section " << dfh.section() << "." << dfh.counter() << " " << label() << " default contents: ";
0244     writeDoc_(os, indentation + 2);
0245     os << "\n";
0246     if (!dfh.brief())
0247       os << "\n";
0248   }
0249 
0250   bool ParameterDescriptionBase::partiallyExists_(ParameterSet const& pset) const { return exists(pset); }
0251 
0252   int ParameterDescriptionBase::howManyXORSubNodesExist_(ParameterSet const& pset) const {
0253     return exists(pset) ? 1 : 0;
0254   }
0255 }  // namespace edm