ParameterSwitch

Macros

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
#ifndef FWCore_ParameterSet_ParameterSwitch_h
#define FWCore_ParameterSet_ParameterSwitch_h

#include "FWCore/ParameterSet/interface/ParameterSwitchBase.h"
#include "FWCore/ParameterSet/interface/ParameterDescriptionNode.h"
#include "FWCore/Utilities/interface/value_ptr.h"
#include "FWCore/ParameterSet/interface/ParameterDescription.h"
#include "FWCore/ParameterSet/interface/ParameterDescriptionCases.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/Algorithms.h"
#include "FWCore/ParameterSet/interface/DocFormatHelper.h"

#include <map>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <sstream>
#include <ostream>
#include <iomanip>

namespace edm {

  template <class T>
  class ParameterSwitch : public ParameterSwitchBase {
  public:
    typedef std::map<T, edm::value_ptr<ParameterDescriptionNode> > CaseMap;
    typedef typename std::map<T, edm::value_ptr<ParameterDescriptionNode> >::const_iterator CaseMapConstIter;

    ParameterSwitch(ParameterDescription<T> const& switchParameter,
                    std::unique_ptr<ParameterDescriptionCases<T> > cases)
        : switch_(switchParameter), cases_(*cases->caseMap()) {
      if (cases->duplicateCaseValues()) {
        throwDuplicateCaseValues(switchParameter.label());
      }
    }

    ParameterDescriptionNode* clone() const override { return new ParameterSwitch(*this); }

  private:
    void checkAndGetLabelsAndTypes_(std::set<std::string>& usedLabels,
                                    std::set<ParameterTypes>& parameterTypes,
                                    std::set<ParameterTypes>& wildcardTypes) const override {
      std::set<std::string> caseLabels;
      std::set<ParameterTypes> caseParameterTypes;
      std::set<ParameterTypes> caseWildcardTypes;
      for_all(cases_,
              std::bind(&ParameterSwitch::checkCaseLabels,
                        std::placeholders::_1,
                        std::ref(caseLabels),
                        std::ref(caseParameterTypes),
                        std::ref(caseWildcardTypes)));

      insertAndCheckLabels(switch_.label(), usedLabels, caseLabels);

      insertAndCheckTypes(switch_.type(), caseParameterTypes, caseWildcardTypes, parameterTypes, wildcardTypes);

      if (cases_.find(switch_.getDefaultValue()) == cases_.end()) {
        throwNoCaseForDefault(switch_.label());
      }
    }

    void validate_(ParameterSet& pset, std::set<std::string>& validatedLabels, Modifier modifier) const override {
      switch_.validate(pset, validatedLabels, modifier);
      if (switch_.exists(pset)) {
        T switchValue;
        if (switch_.isTracked()) {
          switchValue = pset.getParameter<T>(switch_.label());
        } else {
          switchValue = pset.getUntrackedParameter<T>(switch_.label());
        }
        typename CaseMap::const_iterator selectedCase = cases_.find(switchValue);
        if (selectedCase != cases_.end()) {
          selectedCase->second->validate(pset, validatedLabels, Modifier::kNone);
        } else {
          std::stringstream ss;
          ss << "The switch parameter with label \"" << switch_.label() << "\" has been assigned an illegal value.\n"
             << "The value from the configuration is \"" << switchValue << "\".\n"
             << "The allowed values are:\n";

          for (CaseMapConstIter iter = cases_.begin(), iEnd = cases_.end(); iter != iEnd; ++iter) {
            ss << "  " << iter->first << "\n";
          }
          throwNoCaseForSwitchValue(ss.str());
        }
      }
    }

    void writeCfi_(std::ostream& os,
                   Modifier modifier,
                   bool& startWithComma,
                   int indentation,
                   CfiOptions& options,
                   bool& wroteSomething) const override {
      switch_.writeCfi(os, modifier, startWithComma, indentation, options, wroteSomething);

      if (std::holds_alternative<cfi::ClassFile>(options)) {
        std::set<std::string> labels;
        std::set<ParameterTypes> parameterTypes;
        std::set<ParameterTypes> wildcardTypes;
        for (auto const& n : cases_) {
          n.second->checkAndGetLabelsAndTypes(labels, parameterTypes, wildcardTypes);
        }
        for (auto const& l : labels) {
          cfi::parameterMustBeTyped(options, l);
        }
      }

      typename CaseMap::const_iterator selectedCase = cases_.find(switch_.getDefaultValue());
      if (selectedCase != cases_.end()) {
        selectedCase->second->writeCfi(os, modifier, startWithComma, indentation, options, wroteSomething);
      }
    }

    void print_(std::ostream& os, Modifier modifier, bool writeToCfi, DocFormatHelper& dfh) const override {
      printBase(os,
                modifier,
                writeToCfi,
                dfh,
                switch_.label(),
                switch_.isTracked(),
                parameterTypeEnumToString(switch_.type()));
    }

    void printNestedContent_(std::ostream& os, bool optional, DocFormatHelper& dfh) const override {
      DocFormatHelper new_dfh(dfh);
      printNestedContentBase(os, dfh, new_dfh, switch_.label());

      switch_.print(os, modifierIsOptional(optional), true, new_dfh);
      for_all(cases_,
              std::bind(&ParameterSwitchBase::printCaseT<T>,
                        std::placeholders::_1,
                        std::ref(os),
                        optional,
                        std::ref(new_dfh),
                        std::cref(switch_.label())));

      new_dfh.setPass(1);
      new_dfh.setCounter(0);

      new_dfh.indent(os);
      os << "switch:\n";
      switch_.print(os, modifierIsOptional(optional), true, new_dfh);
      for_all(cases_,
              std::bind(&ParameterSwitchBase::printCaseT<T>,
                        std::placeholders::_1,
                        std::ref(os),
                        optional,
                        std::ref(new_dfh),
                        std::cref(switch_.label())));

      new_dfh.setPass(2);
      new_dfh.setCounter(0);

      switch_.printNestedContent(os, optional, new_dfh);
      for_all(cases_,
              std::bind(&ParameterSwitchBase::printCaseT<T>,
                        std::placeholders::_1,
                        std::ref(os),
                        optional,
                        std::ref(new_dfh),
                        std::cref(switch_.label())));
    }

    bool exists_(ParameterSet const& pset) const override { return switch_.exists(pset); }

    static void checkCaseLabels(std::pair<T, edm::value_ptr<ParameterDescriptionNode> > const& thePair,
                                std::set<std::string>& labels,
                                std::set<ParameterTypes>& parameterTypes,
                                std::set<ParameterTypes>& wildcardTypes) {
      thePair.second->checkAndGetLabelsAndTypes(labels, parameterTypes, wildcardTypes);
    }

    ParameterDescription<T> switch_;
    CaseMap cases_;
  };
}  // namespace edm
#endif