CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch1/src/FWCore/ParameterSet/src/ParameterSwitchBase.cc

Go to the documentation of this file.
00001 
00002 #include "FWCore/ParameterSet/interface/ParameterSwitchBase.h"
00003 
00004 #include "FWCore/ParameterSet/interface/DocFormatHelper.h"
00005 #include "FWCore/Utilities/interface/EDMException.h"
00006 
00007 #include <iomanip>
00008 #include <ostream>
00009 #include <sstream>
00010 
00011 namespace edm {
00012 
00013   ParameterSwitchBase::~ParameterSwitchBase() { }
00014 
00015   void
00016   ParameterSwitchBase::
00017   throwDuplicateCaseValues(std::string const& switchLabel) const {
00018     throw Exception(errors::LogicError)
00019       << "When adding a ParameterSwitch to a ParameterSetDescription the values\n"
00020       << "associated with the different cases must be unique.  Duplicate\n"
00021       << "values were found for the switch with label: \"" << switchLabel
00022       << "\"\n";
00023   }
00024 
00025   void
00026   ParameterSwitchBase::
00027   insertAndCheckLabels(std::string const& switchLabel,
00028                        std::set<std::string>& usedLabels,
00029                        std::set<std::string>& labels) const {
00030 
00031     std::pair<std::set<std::string>::iterator,bool> status = labels.insert(switchLabel);
00032     if(status.second == false) {
00033       throw Exception(errors::LogicError)
00034         << "The label used for the switch parameter in a ParameterSetDescription\n"
00035         << "must be different from the labels used in the associated cases.  The following\n"
00036         << "duplicate label was found: \"" << switchLabel << "\"\n";
00037     }
00038     usedLabels.insert(labels.begin(), labels.end());
00039   }
00040 
00041   void
00042   ParameterSwitchBase::
00043   insertAndCheckTypes(ParameterTypes switchType,
00044                       std::set<ParameterTypes> const& caseParameterTypes,
00045                       std::set<ParameterTypes> const& caseWildcardTypes,
00046                       std::set<ParameterTypes>& parameterTypes,
00047                       std::set<ParameterTypes>& wildcardTypes) const {
00048 
00049     if(caseWildcardTypes.find(switchType) != caseWildcardTypes.end()) {
00050       throw Exception(errors::LogicError)
00051         << "The type used for the switch parameter in a ParameterSetDescription\n"
00052         << "must be different from the types used for wildcards in the associated cases.  The following\n"
00053         << "duplicate type was found: \"" << parameterTypeEnumToString(switchType) << "\"\n";
00054     }
00055     parameterTypes.insert(switchType);
00056     parameterTypes.insert(caseParameterTypes.begin(), caseParameterTypes.end());
00057     wildcardTypes.insert(caseWildcardTypes.begin(), caseWildcardTypes.end());
00058   }
00059 
00060   void
00061   ParameterSwitchBase::
00062   throwNoCaseForDefault(std::string const& switchLabel) const {
00063     throw Exception(errors::LogicError)
00064       << "The default value used for the switch parameter in a ParameterSetDescription\n"
00065       << "must match the value used to select one of the associated cases.  This is not\n"
00066       << "true for the switch named \"" << switchLabel << "\"\n";
00067   }
00068 
00069   void
00070   ParameterSwitchBase::
00071   throwNoCaseForSwitchValue(std::string const& message) const {
00072     throw Exception(errors::Configuration)
00073       << message;
00074   }
00075 
00076   void
00077   ParameterSwitchBase::
00078   printBase(std::ostream& os,
00079             bool optional,
00080             bool writeToCfi,
00081             DocFormatHelper& dfh,
00082             std::string const& switchLabel,
00083             bool isTracked,
00084             std::string const& typeString) const {
00085 
00086     if(dfh.pass() == 0) {
00087       dfh.setAtLeast1(switchLabel.size() + 9U);
00088       if(isTracked) {
00089         dfh.setAtLeast2(typeString.size());
00090       } else {
00091         dfh.setAtLeast2(typeString.size() + 10U);
00092       }
00093       dfh.setAtLeast3(8U);
00094     }
00095     if(dfh.pass() == 1) {
00096 
00097       dfh.indent(os);
00098 
00099       if(dfh.brief()) {
00100 
00101         std::stringstream ss;
00102         ss << switchLabel << " (switch)";
00103         std::ios::fmtflags oldFlags = os.flags();
00104         os << std::left << std::setw(dfh.column1()) << ss.str();
00105         os << " ";
00106 
00107         os << std::setw(dfh.column2());
00108         if(isTracked) {
00109           os << typeString;
00110         } else {
00111           std::stringstream ss1;
00112           ss1 << "untracked " << typeString;
00113           os << ss1.str();
00114         }
00115 
00116         os << " " << std::setw(dfh.column3());
00117         if(optional)  os << "optional";
00118         else  os << "";
00119 
00120         if(!writeToCfi) os << " (do not write to cfi)";
00121 
00122         os << " see Section " << dfh.section() << "." << dfh.counter() << "\n";
00123         os.flags(oldFlags);
00124       } else {
00125         // not brief
00126 
00127         os << switchLabel << " (switch)\n";
00128 
00129         dfh.indent2(os);
00130         os << "type: ";
00131         if(!isTracked) os << "untracked ";
00132         os << typeString << " ";
00133 
00134         if(optional)  os << "optional";
00135 
00136         if(!writeToCfi) os << " (do not write to cfi)";
00137         os << "\n";
00138 
00139         dfh.indent2(os);
00140         os << "see Section " << dfh.section() << "." << dfh.counter() << "\n";
00141 
00142         if(!comment().empty()) {
00143           DocFormatHelper::wrapAndPrintText(os,
00144                                             comment(),
00145                                             dfh.startColumn2(),
00146                                             dfh.commentWidth());
00147         }
00148         os << "\n";
00149       }
00150     }
00151   }
00152 
00153   bool
00154   ParameterSwitchBase::
00155   hasNestedContent_() {
00156     return true;
00157   }
00158 
00159   void
00160   ParameterSwitchBase::
00161   printNestedContentBase(std::ostream& os,
00162                          DocFormatHelper& dfh,
00163                          DocFormatHelper& new_dfh,
00164                          std::string const& switchLabel) {
00165 
00166     int indentation = dfh.indentation();
00167     if(dfh.parent() != DocFormatHelper::TOP) {
00168       indentation -= DocFormatHelper::offsetSectionContent();
00169     }
00170 
00171     std::stringstream ss;
00172     ss << dfh.section() << "." << dfh.counter();
00173     std::string newSection = ss.str();
00174 
00175     printSpaces(os, indentation);
00176     os << "Section " << newSection
00177        << " " << switchLabel << " (switch):\n";
00178 
00179     if(!dfh.brief()) {
00180       printSpaces(os, indentation);
00181       os << "The value of \"" << switchLabel << "\" controls which other parameters\n";
00182       printSpaces(os, indentation);
00183       os << "are required or allowed to be in the PSet.\n";
00184     }
00185     if(!dfh.brief()) os << "\n";
00186 
00187     new_dfh.init();
00188     new_dfh.setSection(newSection);
00189     new_dfh.setIndentation(indentation + DocFormatHelper::offsetSectionContent());
00190     new_dfh.setParent(DocFormatHelper::OTHER);
00191   }
00192 
00193 
00194   void
00195   ParameterSwitchBase::
00196   printCase(std::pair<bool, value_ptr<ParameterDescriptionNode> > const& p,
00197             std::ostream& os,
00198             bool /*optional*/,
00199             DocFormatHelper& dfh,
00200             std::string const& switchLabel) {
00201     if(dfh.pass() == 0) {
00202       p.second->print(os, false, true, dfh);
00203     }
00204     if(dfh.pass() == 1) {
00205       dfh.indent(os);
00206       os << "if " << switchLabel << " = ";
00207       if(p.first) os << "True";
00208       else os << "False";
00209       os << "\n";
00210       p.second->print(os, false, true, dfh);
00211     }
00212     if(dfh.pass() == 2) {
00213       p.second->printNestedContent(os, false, dfh);
00214     }
00215   }
00216 
00217   void
00218   ParameterSwitchBase::
00219   printCase(std::pair<int, value_ptr<ParameterDescriptionNode> > const& p,
00220             std::ostream& os,
00221             bool /*optional*/,
00222             DocFormatHelper& dfh,
00223             std::string const& switchLabel) {
00224     if(dfh.pass() == 0) {
00225       p.second->print(os, false, true, dfh);
00226     }
00227     if(dfh.pass() == 1) {
00228       dfh.indent(os);
00229       os << "if " << switchLabel << " = " << p.first << "\n";
00230       p.second->print(os, false, true, dfh);
00231     }
00232     if(dfh.pass() == 2) {
00233       p.second->printNestedContent(os, false, dfh);
00234     }
00235   }
00236 
00237   void
00238   ParameterSwitchBase::
00239   printCase(std::pair<std::string, value_ptr<ParameterDescriptionNode> > const& p,
00240             std::ostream& os,
00241             bool /*optional*/,
00242             DocFormatHelper& dfh,
00243             std::string const& switchLabel) {
00244     if(dfh.pass() == 0) {
00245       p.second->print(os, false, true, dfh);
00246     }
00247     if(dfh.pass() == 1) {
00248       dfh.indent(os);
00249       os << "if " << switchLabel << " = \"" << p.first << "\"\n";
00250       p.second->print(os, false, true, dfh);
00251     }
00252     if(dfh.pass() == 2) {
00253       p.second->printNestedContent(os, false, dfh);
00254     }
00255   }
00256 
00257   bool
00258   ParameterSwitchBase::
00259   partiallyExists_(ParameterSet const& pset) const {
00260     return exists(pset);
00261   }
00262 
00263   int
00264   ParameterSwitchBase::
00265   howManyXORSubNodesExist_(ParameterSet const& pset) const {
00266     return exists(pset) ? 1 : 0;
00267   }
00268 }