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