CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/FWCore/ParameterSet/src/ParameterDescriptionBase.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     ParameterSet
00004 // Class  :     ParameterDescriptionBase
00005 //
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:  Chris Jones
00010 //         Created:  Thu Aug  2 15:35:43 EDT 2007
00011 //
00012 
00013 #include "FWCore/ParameterSet/interface/ParameterDescriptionBase.h"
00014 
00015 #include "FWCore/ParameterSet/interface/DocFormatHelper.h"
00016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00017 #include "FWCore/Utilities/interface/EDMException.h"
00018 
00019 #include <iomanip>
00020 #include <iostream>
00021 
00022 namespace edm {
00023 
00024   ParameterDescriptionBase::ParameterDescriptionBase(std::string const& iLabel,
00025                                                      ParameterTypes iType,
00026                                                      bool isTracked,
00027                                                      bool hasDefault)
00028     :label_(iLabel),
00029      type_(iType),
00030      isTracked_(isTracked),
00031      hasDefault_(hasDefault) {
00032     if(label_.empty()) {
00033       throw Exception(errors::LogicError)
00034         << "Empty string used as a label for a parameter.  This is\n"
00035            "not allowed.\n";
00036     }
00037   }
00038 
00039   ParameterDescriptionBase::ParameterDescriptionBase(char const* iLabel,
00040                                                      ParameterTypes iType,
00041                                                      bool isTracked,
00042                                                      bool hasDefault)
00043     :label_(iLabel),
00044      type_(iType),
00045      isTracked_(isTracked),
00046      hasDefault_(hasDefault) {
00047     if(label_.empty()) {
00048       throw Exception(errors::LogicError)
00049         << "Empty string used as a label for a parameter.  This is\n"
00050            "not allowed.\n";
00051     }
00052   }
00053 
00054   ParameterDescriptionBase::~ParameterDescriptionBase() { }
00055 
00056   void
00057   ParameterDescriptionBase::throwParameterWrongTrackiness() const {
00058     std::string tr("a tracked");
00059     std::string shouldBe("untracked");
00060     if(isTracked()) {
00061       tr = "an untracked";
00062       shouldBe = "tracked";
00063     }
00064 
00065     throw Exception(errors::Configuration)
00066       << "In the configuration, parameter \"" << label() << "\" is defined "
00067       "as " << tr << " " << parameterTypeEnumToString(type()) << ".\n"
00068       << "It should be " << shouldBe << ".\n";
00069   }
00070 
00071   void
00072   ParameterDescriptionBase::throwParameterWrongType() const {
00073     std::string tr("an untracked");
00074     if(isTracked()) tr = "a tracked";
00075 
00076     throw Exception(errors::Configuration)
00077       << "Parameter \"" << label() << "\" should be defined "
00078       "as " << tr << " " << parameterTypeEnumToString(type()) << ".\n"
00079       << "The type in the configuration is incorrect.\n";
00080   }
00081 
00082   void
00083   ParameterDescriptionBase::throwMissingRequiredNoDefault() const {
00084 
00085     std::string tr("untracked");
00086     if(isTracked()) tr = "tracked";
00087 
00088     throw Exception(errors::Configuration)
00089       << "Missing required parameter.  It should have label \""
00090       << label() << "\" and have type \""
00091       << tr << " " << parameterTypeEnumToString(type()) << "\".\n"
00092       << "The description has no default.  The parameter must be defined "
00093          "in the configuration\n";
00094   }
00095 
00096   void
00097   ParameterDescriptionBase::
00098   checkAndGetLabelsAndTypes_(std::set<std::string>& usedLabels,
00099                              std::set<ParameterTypes>& parameterTypes,
00100                              std::set<ParameterTypes>& /*wildcardTypes*/) const {
00101     usedLabels.insert(label());
00102     parameterTypes.insert(type());
00103   }
00104 
00105   void
00106   ParameterDescriptionBase::
00107   validate_(ParameterSet& pset,
00108             std::set<std::string>& validatedLabels,
00109             bool optional) const {
00110 
00111     bool exists = exists_(pset, isTracked());
00112 
00113     if(exists) {
00114       validatedLabels.insert(label());
00115     } else if(exists_(pset, !isTracked())) {
00116       throwParameterWrongTrackiness();
00117     } else if(pset.exists(label())) {
00118       throwParameterWrongType();
00119     }
00120 
00121     if(!exists && !optional) {
00122       if(hasDefault()) {
00123         insertDefault_(pset);
00124         validatedLabels.insert(label());
00125       } else {
00126         throwMissingRequiredNoDefault();
00127       }
00128     }
00129   }
00130 
00131   void
00132   ParameterDescriptionBase::
00133   writeCfi_(std::ostream& os,
00134            bool& startWithComma,
00135            int indentation,
00136            bool& wroteSomething) const {
00137 
00138     if(!hasDefault()) return;
00139 
00140     wroteSomething = true;
00141     if(startWithComma) os << ",";
00142     startWithComma = true;
00143 
00144     os << "\n";
00145     printSpaces(os, indentation);
00146 
00147     os << label()
00148        << " = cms.";
00149     if(!isTracked()) os << "untracked.";
00150     os << parameterTypeEnumToString(type())
00151        << "(";
00152     writeCfi_(os, indentation);
00153     os << ")";
00154   }
00155 
00156   void
00157   ParameterDescriptionBase::
00158   print_(std::ostream& os,
00159          bool optional,
00160          bool writeToCfi,
00161          DocFormatHelper& dfh) {
00162     if(dfh.pass() == 0) {
00163       dfh.setAtLeast1(label().size());
00164       if(isTracked()) {
00165         dfh.setAtLeast2(parameterTypeEnumToString(type()).size());
00166       } else {
00167         dfh.setAtLeast2(parameterTypeEnumToString(type()).size() + 10U);
00168       }
00169       if(optional) {
00170         dfh.setAtLeast3(8U);
00171       }
00172     } else {
00173 
00174       if(dfh.brief()) {
00175         std::ios::fmtflags oldFlags = os.flags();
00176 
00177         dfh.indent(os);
00178         os << std::left << std::setw(dfh.column1()) << label() << " ";
00179 
00180         if(isTracked()) {
00181           os << std::setw(dfh.column2()) << parameterTypeEnumToString(type());
00182         } else {
00183           std::stringstream ss;
00184           ss << "untracked ";
00185           ss << parameterTypeEnumToString(type());
00186           os << std::setw(dfh.column2()) << ss.str();
00187         }
00188         os << " ";
00189 
00190         os << std::setw(dfh.column3());
00191         if(optional) {
00192           os << "optional";
00193         } else {
00194           os << "";
00195         }
00196         os << " ";
00197         os.flags(oldFlags);
00198         printDefault_(os, writeToCfi, dfh);
00199       } else {
00200         // not brief
00201         dfh.indent(os);
00202         os << label() << "\n";
00203 
00204         dfh.indent2(os);
00205         os << "type: ";
00206         if(!isTracked()) os << "untracked ";
00207 
00208         os << parameterTypeEnumToString(type()) << " ";
00209 
00210         if(optional)  os << "optional";
00211         os << "\n";
00212 
00213         dfh.indent2(os);
00214         printDefault_(os, writeToCfi, dfh);
00215 
00216         if(!comment().empty()) {
00217           DocFormatHelper::wrapAndPrintText(os,
00218                                             comment(),
00219                                             dfh.startColumn2(),
00220                                             dfh.commentWidth());
00221         }
00222         os << "\n";
00223       }
00224     }
00225   }
00226 
00227   void
00228   ParameterDescriptionBase::
00229   printDefault_(std::ostream& os,
00230                   bool writeToCfi,
00231                   DocFormatHelper& dfh) {
00232     if(!dfh.brief()) os << "default: ";
00233     if(writeToCfi && hasDefault()) {
00234       if(hasNestedContent()) {
00235         os << "see Section " << dfh.section()
00236            << "." << dfh.counter();
00237       } else {
00238         if(dfh.brief()) {
00239           writeDoc_(os, dfh.indentation());
00240         } else {
00241           writeDoc_(os, dfh.startColumn2());
00242         }
00243       }
00244     } else {
00245       os << "none (do not write to cfi)";
00246     }
00247     os << "\n";
00248   }
00249 
00250   void
00251   ParameterDescriptionBase::
00252   printNestedContent_(std::ostream& os,
00253                       bool /*optional*/,
00254                       DocFormatHelper& dfh) {
00255     int indentation = dfh.indentation();
00256     if(dfh.parent() != DocFormatHelper::TOP) {
00257       indentation -= DocFormatHelper::offsetSectionContent();
00258     }
00259 
00260     printSpaces(os, indentation);
00261     os << "Section " << dfh.section() << "." << dfh.counter()
00262        << " " << label() << " default contents: ";
00263     writeDoc_(os, indentation + 2);
00264     os << "\n";
00265     if(!dfh.brief()) os << "\n";
00266   }
00267 
00268   bool
00269   ParameterDescriptionBase::
00270   partiallyExists_(ParameterSet const& pset) const {
00271     return exists(pset);
00272   }
00273 
00274   int
00275   ParameterDescriptionBase::
00276   howManyXORSubNodesExist_(ParameterSet const& pset) const {
00277     return exists(pset) ? 1 : 0;
00278   }
00279 }