Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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