Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
// -*- C++ -*-
//
// Package:     ParameterSet
// Class  :     ParameterDescriptionBase
//
// Implementation:
//     <Notes on implementation>
//
// Original Author:  Chris Jones
//         Created:  Thu Aug  2 15:35:43 EDT 2007
//

#include "FWCore/ParameterSet/interface/ParameterDescriptionBase.h"

#include "FWCore/ParameterSet/interface/DocFormatHelper.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/Utilities/interface/EDMException.h"

#include <iomanip>
#include <iostream>

namespace edm {

  ParameterDescriptionBase::ParameterDescriptionBase(
      std::string const& iLabel, ParameterTypes iType, bool isTracked, bool hasDefault, Comment const& iComment)
      : ParameterDescriptionNode(iComment),
        label_(iLabel),
        type_(iType),
        isTracked_(isTracked),
        hasDefault_(hasDefault) {
    if (label_.empty()) {
      throw Exception(errors::LogicError) << "Empty string used as a label for a parameter.  This is\n"
                                             "not allowed.\n";
    }
  }

  ParameterDescriptionBase::ParameterDescriptionBase(
      char const* iLabel, ParameterTypes iType, bool isTracked, bool hasDefault, Comment const& iComment)
      : ParameterDescriptionNode(iComment),
        label_(iLabel),
        type_(iType),
        isTracked_(isTracked),
        hasDefault_(hasDefault) {
    if (label_.empty()) {
      throw Exception(errors::LogicError) << "Empty string used as a label for a parameter.  This is\n"
                                             "not allowed.\n";
    }
  }

  ParameterDescriptionBase::~ParameterDescriptionBase() {}

  void ParameterDescriptionBase::throwParameterWrongTrackiness() const {
    std::string tr("a tracked");
    std::string shouldBe("untracked");
    if (isTracked()) {
      tr = "an untracked";
      shouldBe = "tracked";
    }

    throw Exception(errors::Configuration) << "In the configuration, parameter \"" << label()
                                           << "\" is defined "
                                              "as "
                                           << tr << " " << parameterTypeEnumToString(type()) << ".\n"
                                           << "It should be " << shouldBe << ".\n";
  }

  void ParameterDescriptionBase::throwParameterWrongType() const {
    std::string tr("an untracked");
    if (isTracked())
      tr = "a tracked";

    throw Exception(errors::Configuration) << "Parameter \"" << label()
                                           << "\" should be defined "
                                              "as "
                                           << tr << " " << parameterTypeEnumToString(type()) << ".\n"
                                           << "The type in the configuration is incorrect.\n";
  }

  void ParameterDescriptionBase::throwMissingRequiredNoDefault() const {
    std::string tr("untracked");
    if (isTracked())
      tr = "tracked";

    throw Exception(errors::Configuration)
        << "Missing required parameter.  It should have label \"" << label() << "\" and have type \"" << tr << " "
        << parameterTypeEnumToString(type()) << "\".\n"
        << "The description has no default.  The parameter must be defined "
           "in the configuration\n";
  }

  void ParameterDescriptionBase::checkAndGetLabelsAndTypes_(std::set<std::string>& usedLabels,
                                                            std::set<ParameterTypes>& parameterTypes,
                                                            std::set<ParameterTypes>& /*wildcardTypes*/) const {
    usedLabels.insert(label());
    parameterTypes.insert(type());
  }

  void ParameterDescriptionBase::validate_(ParameterSet& pset,
                                           std::set<std::string>& validatedLabels,
                                           Modifier modifier) const {
    bool exists = exists_(pset, isTracked());

    if (exists) {
      validatedLabels.insert(label());
    } else if (exists_(pset, !isTracked())) {
      throwParameterWrongTrackiness();
    } else if (pset.exists(label())) {
      throwParameterWrongType();
    }

    if (exists and modifier == Modifier::kObsolete) {
      edm::LogWarning("Configuration") << "ignoring obsolete parameter '" << label() << "'";
      return;
    }
    if (!exists && modifier == Modifier::kNone) {
      if (hasDefault()) {
        insertDefault_(pset);
        validatedLabels.insert(label());
      } else {
        throwMissingRequiredNoDefault();
      }
    }
  }

  void ParameterDescriptionBase::writeCfi_(std::ostream& os,
                                           Modifier modifier,
                                           bool& startWithComma,
                                           int indentation,
                                           CfiOptions& options,
                                           bool& wroteSomething) const {
    if (label().empty() or label()[0] == '@') {
      return;
    }

    auto check = cfi::needToSwitchToTyped(label(), options);
    if (check.first) {
      CfiOptions fullOp = cfi::Typed{};
      writeFullCfi(os, modifier, startWithComma, indentation, fullOp, wroteSomething);
    } else if (shouldWriteUntyped(options)) {
      if (modifier != Modifier::kObsolete) {
        writeLabelValueCfi(os, modifier == Modifier::kOptional, startWithComma, indentation, options, wroteSomething);
      }
    } else {
      writeFullCfi(os, modifier, startWithComma, indentation, options, wroteSomething);
    }
  }

  void ParameterDescriptionBase::writeLabelValueCfi(std::ostream& os,
                                                    bool optional,
                                                    bool& startWithComma,
                                                    int indentation,
                                                    CfiOptions& options,
                                                    bool& wroteSomething) const {
    constexpr std::string_view k_endList = "]";
    constexpr std::string_view k_endParenthesis = ")";
    constexpr std::string_view k_endBasicType = "";
    if (not hasDefault()) {
      return;
    }
    wroteSomething = true;
    if (startWithComma)
      os << ",";
    startWithComma = true;
    os << "\n";
    printSpaces(os, indentation);

    os << label() << " = ";
    std::string_view endDelimiter = k_endBasicType;
    switch (type()) {
      case k_vdouble:
      case k_vint32:
      case k_vint64:
      case k_vstringRaw:
      case k_vuint32:
      case k_vuint64:
      case k_VESInputTag:
      case k_VEventID:
      case k_VEventRange:
      case k_VInputTag:
      case k_VLuminosityBlockID:
      case k_VLuminosityBlockRange:
      case k_VPSet:
        os << "[";
        endDelimiter = k_endList;
        break;
      case k_PSet:
        os << "dict(";
        endDelimiter = k_endParenthesis;
        break;
      case k_EventID:
      case k_EventRange:
      case k_ESInputTag:
      case k_InputTag:
      case k_LuminosityBlockID:
      case k_LuminosityBlockRange:
        os << "(";
        endDelimiter = k_endParenthesis;
        break;
      default:
        break;
    }
    writeCfi_(os, indentation, options);
    os << endDelimiter;
    return;
  }

  void ParameterDescriptionBase::writeFullCfi(std::ostream& os,
                                              Modifier modifier,
                                              bool& startWithComma,
                                              int indentation,
                                              CfiOptions& options,
                                              bool& wroteSomething) const {
    wroteSomething = true;
    if (startWithComma)
      os << ",";
    startWithComma = true;

    os << "\n";
    printSpaces(os, indentation);

    os << label() << " = cms.";

    if (modifier == Modifier::kObsolete or !hasDefault()) {
      if (modifier == Modifier::kOptional) {
        os << "optional.";
      } else if (modifier == Modifier::kObsolete) {
        os << "obsolete.";
      } else {
        os << "required.";
      }
      if (!isTracked())
        os << "untracked.";
      os << parameterTypeEnumToString(type());
    } else {
      if (!isTracked())
        os << "untracked.";
      os << parameterTypeEnumToString(type()) << "(";
      writeCfi_(os, indentation, options);
      os << ")";
    }
  }
  void ParameterDescriptionBase::print_(std::ostream& os,
                                        Modifier modifier,
                                        bool writeToCfi,
                                        DocFormatHelper& dfh) const {
    const bool optional = (modifier == Modifier::kOptional);
    const bool obsolete = (modifier == Modifier::kObsolete);
    if (dfh.pass() == 0) {
      dfh.setAtLeast1(label().size());
      if (isTracked()) {
        dfh.setAtLeast2(parameterTypeEnumToString(type()).size());
      } else {
        dfh.setAtLeast2(parameterTypeEnumToString(type()).size() + 10U);
      }
      if (optional) {
        dfh.setAtLeast3(8U);
      }
    } else {
      if (dfh.brief()) {
        std::ios::fmtflags oldFlags = os.flags();

        dfh.indent(os);
        os << std::left << std::setw(dfh.column1()) << label() << " ";

        if (isTracked()) {
          os << std::setw(dfh.column2()) << parameterTypeEnumToString(type());
        } else {
          std::stringstream ss;
          ss << "untracked ";
          ss << parameterTypeEnumToString(type());
          os << std::setw(dfh.column2()) << ss.str();
        }
        os << " ";

        os << std::setw(dfh.column3());
        if (optional) {
          os << "optional";
        } else if (obsolete) {
          os << "obsolete";
        } else {
          os << "";
        }
        os << " ";
        os.flags(oldFlags);
        printDefault_(os, writeToCfi, dfh);
      } else {
        // not brief
        dfh.indent(os);
        os << label() << "\n";

        dfh.indent2(os);
        os << "type: ";
        if (!isTracked())
          os << "untracked ";

        os << parameterTypeEnumToString(type()) << " ";

        if (optional)
          os << "optional";
        if (obsolete)
          os << "obsolete";
        os << "\n";

        dfh.indent2(os);
        printDefault_(os, writeToCfi, dfh);

        if (!comment().empty()) {
          DocFormatHelper::wrapAndPrintText(os, comment(), dfh.startColumn2(), dfh.commentWidth());
        }
        os << "\n";
      }
    }
  }

  void ParameterDescriptionBase::printDefault_(std::ostream& os, bool writeToCfi, DocFormatHelper& dfh) const {
    if (!dfh.brief())
      os << "default: ";
    if (writeToCfi && hasDefault()) {
      if (hasNestedContent()) {
        os << "see Section " << dfh.section() << "." << dfh.counter();
      } else {
        if (dfh.brief()) {
          writeDoc_(os, dfh.indentation());
        } else {
          writeDoc_(os, dfh.startColumn2());
        }
      }
    } else if (!writeToCfi) {
      os << "none (do not write to cfi)";
    } else {
      os << "none";
    }
    os << "\n";
  }

  void ParameterDescriptionBase::printNestedContent_(std::ostream& os, bool /*optional*/, DocFormatHelper& dfh) const {
    int indentation = dfh.indentation();
    if (dfh.parent() != DocFormatHelper::TOP) {
      indentation -= DocFormatHelper::offsetSectionContent();
    }

    printSpaces(os, indentation);
    os << "Section " << dfh.section() << "." << dfh.counter() << " " << label() << " default contents: ";
    writeDoc_(os, indentation + 2);
    os << "\n";
    if (!dfh.brief())
      os << "\n";
  }

  bool ParameterDescriptionBase::partiallyExists_(ParameterSet const& pset) const { return exists(pset); }

  int ParameterDescriptionBase::howManyXORSubNodesExist_(ParameterSet const& pset) const {
    return exists(pset) ? 1 : 0;
  }
}  // namespace edm