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

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

#include <algorithm>
#include <sstream>
#include <ostream>
#include <iomanip>

namespace edm {

  IfExistsDescription::IfExistsDescription(ParameterDescriptionNode const& node_left,
                                           ParameterDescriptionNode const& node_right)
      : node_left_(node_left.clone()), node_right_(node_right.clone()) {}

  IfExistsDescription::IfExistsDescription(std::unique_ptr<ParameterDescriptionNode> node_left,
                                           ParameterDescriptionNode const& node_right)
      : node_left_(std::move(node_left)), node_right_(node_right.clone()) {}

  IfExistsDescription::IfExistsDescription(ParameterDescriptionNode const& node_left,
                                           std::unique_ptr<ParameterDescriptionNode> node_right)
      : node_left_(node_left.clone()), node_right_(std::move(node_right)) {}

  IfExistsDescription::IfExistsDescription(std::unique_ptr<ParameterDescriptionNode> node_left,
                                           std::unique_ptr<ParameterDescriptionNode> node_right)
      : node_left_(std::move(node_left)), node_right_(std::move(node_right)) {}

  void IfExistsDescription::checkAndGetLabelsAndTypes_(std::set<std::string>& usedLabels,
                                                       std::set<ParameterTypes>& parameterTypes,
                                                       std::set<ParameterTypes>& wildcardTypes) const {
    std::set<std::string> labelsLeft;
    std::set<ParameterTypes> parameterTypesLeft;
    std::set<ParameterTypes> wildcardTypesLeft;
    node_left_->checkAndGetLabelsAndTypes(labelsLeft, parameterTypesLeft, wildcardTypesLeft);

    std::set<std::string> labelsRight;
    std::set<ParameterTypes> parameterTypesRight;
    std::set<ParameterTypes> wildcardTypesRight;
    node_right_->checkAndGetLabelsAndTypes(labelsRight, parameterTypesRight, wildcardTypesRight);

    throwIfDuplicateLabels(labelsLeft, labelsRight);
    throwIfDuplicateTypes(wildcardTypesLeft, parameterTypesRight);
    throwIfDuplicateTypes(wildcardTypesRight, parameterTypesLeft);

    usedLabels.insert(labelsLeft.begin(), labelsLeft.end());
    usedLabels.insert(labelsRight.begin(), labelsRight.end());

    parameterTypes.insert(parameterTypesRight.begin(), parameterTypesRight.end());
    parameterTypes.insert(parameterTypesLeft.begin(), parameterTypesLeft.end());

    wildcardTypes.insert(wildcardTypesRight.begin(), wildcardTypesRight.end());
    wildcardTypes.insert(wildcardTypesLeft.begin(), wildcardTypesLeft.end());
  }

  void IfExistsDescription::validate_(ParameterSet& pset,
                                      std::set<std::string>& validatedLabels,
                                      Modifier modifier) const {
    bool leftExists = node_left_->exists(pset);
    bool rightExists = node_right_->exists(pset);

    if (!leftExists && !rightExists) {
      return;
    } else if (leftExists && rightExists) {
      node_left_->validate(pset, validatedLabels, Modifier::kNone);
      node_right_->validate(pset, validatedLabels, Modifier::kNone);
    } else if (leftExists && !rightExists) {
      node_left_->validate(pset, validatedLabels, Modifier::kNone);
      if (modifier == Modifier::kNone)
        node_right_->validate(pset, validatedLabels, Modifier::kNone);
    } else if (!leftExists && rightExists) {
      node_left_->validate(pset, validatedLabels, Modifier::kNone);
      node_right_->validate(pset, validatedLabels, Modifier::kNone);
    }
  }

  void IfExistsDescription::writeCfi_(std::ostream& os,
                                      Modifier modifier,
                                      bool& startWithComma,
                                      int indentation,
                                      CfiOptions& options,
                                      bool& wroteSomething) const {
    node_left_->writeCfi(os, modifier, startWithComma, indentation, options, wroteSomething);
    node_right_->writeCfi(os, modifier, startWithComma, indentation, options, wroteSomething);
    parameterMustBeTyped(options);
  }

  void IfExistsDescription::print_(std::ostream& os, Modifier modifier, bool writeToCfi, DocFormatHelper& dfh) const {
    if (dfh.pass() == 1) {
      dfh.indent(os);
      os << "IfExists pair:";

      const bool optional = (modifier == Modifier::kOptional);
      const bool obsolete = (modifier == Modifier::kObsolete);
      if (dfh.brief()) {
        if (optional)
          os << " optional";
        if (obsolete)
          os << " obsolete";

        if (!writeToCfi)
          os << " (do not write to cfi)";

        os << " see Section " << dfh.section() << "." << dfh.counter() << "\n";
      }
      // not brief
      else {
        os << "\n";
        dfh.indent2(os);

        if (optional)
          os << "optional";
        if (obsolete)
          os << " obsolete";
        if (!writeToCfi)
          os << " (do not write to cfi)";
        if (optional || !writeToCfi) {
          os << "\n";
          dfh.indent2(os);
        }

        os << "see Section " << dfh.section() << "." << dfh.counter() << "\n";

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

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

    std::stringstream ss;
    ss << dfh.section() << "." << dfh.counter();
    std::string newSection = ss.str();

    printSpaces(os, indentation);
    os << "Section " << newSection;
    if (optional)
      os << " optional";
    os << " IfExists pair description:\n";
    printSpaces(os, indentation);
    if (optional) {
      os << "If the first parameter exists, then the second is allowed to exist\n";
    } else {
      os << "If the first parameter exists, then the second is required to exist\n";
    }
    if (!dfh.brief())
      os << "\n";

    DocFormatHelper new_dfh(dfh);
    new_dfh.init();
    new_dfh.setSection(newSection);
    new_dfh.setIndentation(indentation + DocFormatHelper::offsetSectionContent());
    new_dfh.setParent(DocFormatHelper::OTHER);

    node_left_->print(os, Modifier::kNone, true, new_dfh);
    node_right_->print(os, Modifier::kNone, true, new_dfh);

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

    node_left_->print(os, Modifier::kNone, true, new_dfh);
    node_right_->print(os, Modifier::kNone, true, new_dfh);

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

    node_left_->printNestedContent(os, false, new_dfh);
    node_right_->printNestedContent(os, false, new_dfh);
  }

  bool IfExistsDescription::exists_(ParameterSet const& pset) const {
    bool leftExists = node_left_->exists(pset);
    bool rightExists = node_right_->exists(pset);

    if (leftExists && rightExists)
      return true;
    else if (!leftExists && !rightExists)
      return true;
    return false;
  }

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

  int IfExistsDescription::howManyXORSubNodesExist_(ParameterSet const& pset) const { return exists(pset) ? 1 : 0; }

  void IfExistsDescription::throwIfDuplicateLabels(std::set<std::string> const& labelsLeft,
                                                   std::set<std::string> const& labelsRight) const {
    std::set<std::string> duplicateLabels;
    std::insert_iterator<std::set<std::string> > insertIter(duplicateLabels, duplicateLabels.begin());
    std::set_intersection(labelsLeft.begin(), labelsLeft.end(), labelsRight.begin(), labelsRight.end(), insertIter);
    if (!duplicateLabels.empty()) {
      std::stringstream ss;
      for (std::set<std::string>::const_iterator iter = duplicateLabels.begin(), iEnd = duplicateLabels.end();
           iter != iEnd;
           ++iter) {
        ss << " \"" << *iter << "\"\n";
      }
      throw edm::Exception(errors::LogicError) << "Labels used in a node of a ParameterSetDescription\n"
                                               << "\"ifExists\" expression must be not be the same as labels used\n"
                                               << "in other nodes of the expression.  The following duplicate\n"
                                               << "labels were detected:\n"
                                               << ss.str() << "\n";
    }
  }

  void IfExistsDescription::throwIfDuplicateTypes(std::set<ParameterTypes> const& types1,
                                                  std::set<ParameterTypes> const& types2) const {
    if (!types1.empty()) {
      std::set<ParameterTypes> duplicateTypes;
      std::insert_iterator<std::set<ParameterTypes> > insertIter(duplicateTypes, duplicateTypes.begin());
      std::set_intersection(types1.begin(), types1.end(), types2.begin(), types2.end(), insertIter);
      if (!duplicateTypes.empty()) {
        std::stringstream ss;
        for (std::set<ParameterTypes>::const_iterator iter = duplicateTypes.begin(), iEnd = duplicateTypes.end();
             iter != iEnd;
             ++iter) {
          ss << " \"" << parameterTypeEnumToString(*iter) << "\"\n";
        }
        throw edm::Exception(errors::LogicError)
            << "Types used for wildcards in a node of a ParameterSetDescription\n"
            << "\"ifExists\" expression must be different from types used for other parameters\n"
            << "in other nodes.  The following duplicate types were detected:\n"
            << ss.str() << "\n";
      }
    }
  }
}  // namespace edm