File indexing completed on 2025-03-26 01:51:12
0001 #ifndef FWCore_ParameterSet_ParameterDescriptionNode_h
0002 #define FWCore_ParameterSet_ParameterDescriptionNode_h
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include "FWCore/Utilities/interface/value_ptr.h"
0012
0013 #include <string>
0014 #include <set>
0015 #include <iosfwd>
0016 #include <memory>
0017 #include <variant>
0018 #include <optional>
0019 #include <unordered_map>
0020 #include <vector>
0021 #include <cassert>
0022
0023 namespace edm {
0024
0025 class ParameterSet;
0026 template <typename T>
0027 class ParameterDescriptionCases;
0028 class DocFormatHelper;
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038 enum ParameterTypes {
0039 k_int32 = 'I',
0040 k_vint32 = 'i',
0041 k_uint32 = 'U',
0042 k_vuint32 = 'u',
0043 k_int64 = 'L',
0044 k_vint64 = 'l',
0045 k_uint64 = 'X',
0046 k_vuint64 = 'x',
0047 k_double = 'D',
0048 k_vdouble = 'd',
0049 k_bool = 'B',
0050 k_stringRaw = 'Z',
0051 k_vstringRaw = 'z',
0052 k_stringHex = 'S',
0053 k_vstringHex = 's',
0054 k_EventID = 'E',
0055 k_VEventID = 'e',
0056 k_LuminosityBlockID = 'M',
0057 k_VLuminosityBlockID = 'm',
0058 k_InputTag = 't',
0059 k_VInputTag = 'v',
0060 k_ESInputTag = 'g',
0061 k_VESInputTag = 'G',
0062 k_FileInPath = 'F',
0063 k_LuminosityBlockRange = 'A',
0064 k_VLuminosityBlockRange = 'a',
0065 k_EventRange = 'R',
0066 k_VEventRange = 'r',
0067 k_PSet = 'Q',
0068 k_VPSet = 'q'
0069 };
0070
0071 std::string parameterTypeEnumToString(ParameterTypes iType);
0072
0073 enum class ParameterModifier : unsigned char { kNone, kOptional, kObsolete };
0074 inline ParameterModifier modifierIsOptional(bool iOptional) {
0075 return iOptional ? ParameterModifier::kOptional : ParameterModifier::kNone;
0076 }
0077
0078 namespace cfi {
0079 struct Paths {
0080
0081
0082
0083 std::optional<std::unordered_map<std::string, Paths>> nodes_;
0084 };
0085 struct Typed {};
0086 struct ClassFile {
0087 void parameterMustBeTyped() {
0088 Paths* p = &fullPaths_;
0089 for (auto n : presentPath_) {
0090 if (not p->nodes_) {
0091 p->nodes_ = std::unordered_map<std::string, Paths>();
0092 }
0093 p = &(p->nodes_.value().emplace(n, Paths{})).first->second;
0094 }
0095 }
0096 void pushNode(std::string_view iNode) { presentPath_.push_back(iNode); }
0097 void popNode() {
0098 assert(not presentPath_.empty());
0099 presentPath_.pop_back();
0100 }
0101
0102 Paths releasePaths() { return std::move(fullPaths_); }
0103
0104 private:
0105 std::vector<std::string_view> presentPath_;
0106 Paths fullPaths_;
0107 };
0108 struct Untyped {
0109 Untyped(Paths iPaths) : paths_(iPaths) {}
0110 bool needToSwitchToTyped(std::string_view iNode) {
0111 presentPath_.push_back(iNode);
0112 if (not paths_.nodes_.has_value()) {
0113 return false;
0114 }
0115 const Paths* p = &paths_;
0116 for (auto const& n : presentPath_) {
0117 if (not paths_.nodes_.has_value()) {
0118 return false;
0119 }
0120 auto f = paths_.nodes_->find(std::string(n));
0121 if (f == paths_.nodes_->end()) {
0122 return false;
0123 }
0124 p = &f->second;
0125 }
0126 return not p->nodes_.has_value();
0127 }
0128 void popNode() {
0129 assert(not presentPath_.empty());
0130 presentPath_.pop_back();
0131 }
0132
0133 private:
0134 std::vector<std::string_view> presentPath_;
0135 Paths paths_;
0136 };
0137
0138 using CfiOptions = std::variant<cfi::Typed, cfi::ClassFile, cfi::Untyped>;
0139
0140 inline void parameterMustBeTyped(CfiOptions& iOps) noexcept {
0141 if (std::holds_alternative<cfi::ClassFile>(iOps)) {
0142 std::get<cfi::ClassFile>(iOps).parameterMustBeTyped();
0143 }
0144 }
0145 inline void parameterMustBeTyped(CfiOptions& iOps, std::string_view iNode) noexcept {
0146 if (std::holds_alternative<cfi::ClassFile>(iOps)) {
0147 auto& d = std::get<cfi::ClassFile>(iOps);
0148 d.pushNode(iNode);
0149 d.parameterMustBeTyped();
0150 d.popNode();
0151 }
0152 }
0153 [[nodiscard]] inline bool shouldWriteUntyped(CfiOptions const& iOps) noexcept {
0154 return std::holds_alternative<cfi::Untyped>(iOps);
0155 }
0156
0157 struct NodeGuard {
0158 NodeGuard(CfiOptions& iOp) : options_(&iOp) {}
0159 NodeGuard() = delete;
0160 NodeGuard(NodeGuard const&) = delete;
0161 NodeGuard& operator=(NodeGuard const&) = delete;
0162 NodeGuard(NodeGuard&& iOther) : options_{iOther.options_} { iOther.options_ = nullptr; }
0163 NodeGuard& operator=(NodeGuard&& iOther) {
0164 NodeGuard temp{std::move(iOther)};
0165 options_ = temp.options_;
0166 temp.options_ = nullptr;
0167 return *this;
0168 }
0169 ~NodeGuard() {
0170 if (nullptr == options_) {
0171 return;
0172 }
0173 if (std::holds_alternative<ClassFile>(*options_)) {
0174 std::get<ClassFile>(*options_).popNode();
0175 } else if (std::holds_alternative<Untyped>(*options_)) {
0176 std::get<Untyped>(*options_).popNode();
0177 }
0178 }
0179 CfiOptions* options_;
0180 };
0181
0182 [[nodiscard]] inline std::pair<bool, NodeGuard> needToSwitchToTyped(std::string_view iNode,
0183 CfiOptions& iOpt) noexcept {
0184 if (std::holds_alternative<Untyped>(iOpt)) {
0185 return std::pair(std::get<Untyped>(iOpt).needToSwitchToTyped(iNode), NodeGuard(iOpt));
0186 } else if (std::holds_alternative<ClassFile>(iOpt)) {
0187 std::get<ClassFile>(iOpt).pushNode(iNode);
0188 }
0189 return std::pair(false, NodeGuard(iOpt));
0190 }
0191 }
0192 using CfiOptions = cfi::CfiOptions;
0193
0194 struct ParameterTypeToEnum {
0195 template <class T>
0196 static ParameterTypes toEnum();
0197 };
0198
0199 class Comment {
0200 public:
0201 Comment();
0202 explicit Comment(std::string const& iComment);
0203 explicit Comment(char const* iComment);
0204 std::string const& comment() const { return comment_; }
0205
0206 private:
0207 std::string comment_;
0208 };
0209
0210 class ParameterDescriptionNode {
0211 public:
0212 using Modifier = ParameterModifier;
0213
0214 ParameterDescriptionNode() {}
0215
0216 explicit ParameterDescriptionNode(Comment const& iComment) : comment_(iComment.comment()) {}
0217
0218 virtual ~ParameterDescriptionNode();
0219
0220 virtual ParameterDescriptionNode* clone() const = 0;
0221
0222 std::string const& comment() const { return comment_; }
0223 void setComment(std::string const& value);
0224 void setComment(char const* value);
0225
0226
0227
0228
0229
0230
0231
0232 void validate(ParameterSet& pset, std::set<std::string>& validatedLabels, Modifier modifier) const {
0233 validate_(pset, validatedLabels, modifier);
0234 }
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246 void writeCfi(std::ostream& os,
0247 Modifier modifier,
0248 bool& startWithComma,
0249 int indentation,
0250 CfiOptions& options,
0251 bool& wroteSomething) const {
0252 writeCfi_(os, modifier, startWithComma, indentation, options, wroteSomething);
0253 }
0254
0255
0256 void print(std::ostream& os, Modifier modifier, bool writeToCfi, DocFormatHelper& dfh) const;
0257
0258 bool hasNestedContent() const { return hasNestedContent_(); }
0259
0260 void printNestedContent(std::ostream& os, bool optional, DocFormatHelper& dfh) const;
0261
0262
0263
0264
0265
0266
0267
0268
0269 bool exists(ParameterSet const& pset) const { return exists_(pset); }
0270
0271
0272
0273
0274
0275 bool partiallyExists(ParameterSet const& pset) const { return partiallyExists_(pset); }
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288 int howManyXORSubNodesExist(ParameterSet const& pset) const { return howManyXORSubNodesExist_(pset); }
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342 void checkAndGetLabelsAndTypes(std::set<std::string>& usedLabels,
0343 std::set<ParameterTypes>& parameterTypes,
0344 std::set<ParameterTypes>& wildcardTypes) const {
0345 checkAndGetLabelsAndTypes_(usedLabels, parameterTypes, wildcardTypes);
0346 }
0347
0348 virtual bool isWildcard() const { return false; }
0349 static void printSpaces(std::ostream& os, int n);
0350
0351 protected:
0352 virtual void checkAndGetLabelsAndTypes_(std::set<std::string>& usedLabels,
0353 std::set<ParameterTypes>& parameterTypes,
0354 std::set<ParameterTypes>& wildcardTypes) const = 0;
0355
0356 virtual void validate_(ParameterSet& pset, std::set<std::string>& validatedLabels, Modifier modifier) const = 0;
0357
0358 virtual void writeCfi_(std::ostream& os,
0359 Modifier modifier,
0360 bool& startWithComma,
0361 int indentation,
0362 CfiOptions&,
0363 bool& wroteSomething) const = 0;
0364
0365 virtual void print_(std::ostream&, Modifier , bool , DocFormatHelper&) const {}
0366
0367 virtual bool hasNestedContent_() const { return false; }
0368
0369 virtual void printNestedContent_(std::ostream&, bool , DocFormatHelper&) const {}
0370
0371 virtual bool exists_(ParameterSet const& pset) const = 0;
0372
0373 virtual bool partiallyExists_(ParameterSet const& pset) const = 0;
0374
0375 virtual int howManyXORSubNodesExist_(ParameterSet const& pset) const = 0;
0376
0377 std::string comment_;
0378 };
0379
0380 template <>
0381 struct value_ptr_traits<ParameterDescriptionNode> {
0382 static ParameterDescriptionNode* clone(ParameterDescriptionNode const* p) { return p->clone(); }
0383 static void destroy(ParameterDescriptionNode* p) { delete p; }
0384 };
0385
0386
0387
0388 std::unique_ptr<ParameterDescriptionCases<bool>> operator>>(bool caseValue, ParameterDescriptionNode const& node);
0389
0390 std::unique_ptr<ParameterDescriptionCases<int>> operator>>(int caseValue, ParameterDescriptionNode const& node);
0391
0392 std::unique_ptr<ParameterDescriptionCases<std::string>> operator>>(std::string const& caseValue,
0393 ParameterDescriptionNode const& node);
0394
0395 std::unique_ptr<ParameterDescriptionCases<std::string>> operator>>(char const* caseValue,
0396 ParameterDescriptionNode const& node);
0397
0398 std::unique_ptr<ParameterDescriptionCases<bool>> operator>>(bool caseValue,
0399 std::unique_ptr<ParameterDescriptionNode> node);
0400
0401 std::unique_ptr<ParameterDescriptionCases<int>> operator>>(int caseValue,
0402 std::unique_ptr<ParameterDescriptionNode> node);
0403
0404 std::unique_ptr<ParameterDescriptionCases<std::string>> operator>>(std::string const& caseValue,
0405 std::unique_ptr<ParameterDescriptionNode> node);
0406
0407 std::unique_ptr<ParameterDescriptionCases<std::string>> operator>>(char const* caseValue,
0408 std::unique_ptr<ParameterDescriptionNode> node);
0409
0410
0411
0412 std::unique_ptr<ParameterDescriptionNode> operator&&(ParameterDescriptionNode const& node_left,
0413 ParameterDescriptionNode const& node_right);
0414
0415 std::unique_ptr<ParameterDescriptionNode> operator&&(std::unique_ptr<ParameterDescriptionNode> node_left,
0416 ParameterDescriptionNode const& node_right);
0417
0418 std::unique_ptr<ParameterDescriptionNode> operator&&(ParameterDescriptionNode const& node_left,
0419 std::unique_ptr<ParameterDescriptionNode> node_right);
0420
0421 std::unique_ptr<ParameterDescriptionNode> operator&&(std::unique_ptr<ParameterDescriptionNode> node_left,
0422 std::unique_ptr<ParameterDescriptionNode> node_right);
0423
0424
0425
0426 std::unique_ptr<ParameterDescriptionNode> operator||(ParameterDescriptionNode const& node_left,
0427 ParameterDescriptionNode const& node_right);
0428
0429 std::unique_ptr<ParameterDescriptionNode> operator||(std::unique_ptr<ParameterDescriptionNode> node_left,
0430 ParameterDescriptionNode const& node_right);
0431
0432 std::unique_ptr<ParameterDescriptionNode> operator||(ParameterDescriptionNode const& node_left,
0433 std::unique_ptr<ParameterDescriptionNode> node_right);
0434
0435 std::unique_ptr<ParameterDescriptionNode> operator||(std::unique_ptr<ParameterDescriptionNode> node_left,
0436 std::unique_ptr<ParameterDescriptionNode> node_right);
0437
0438
0439
0440 std::unique_ptr<ParameterDescriptionNode> operator^(ParameterDescriptionNode const& node_left,
0441 ParameterDescriptionNode const& node_right);
0442
0443 std::unique_ptr<ParameterDescriptionNode> operator^(std::unique_ptr<ParameterDescriptionNode> node_left,
0444 ParameterDescriptionNode const& node_right);
0445
0446 std::unique_ptr<ParameterDescriptionNode> operator^(ParameterDescriptionNode const& node_left,
0447 std::unique_ptr<ParameterDescriptionNode> node_right);
0448
0449 std::unique_ptr<ParameterDescriptionNode> operator^(std::unique_ptr<ParameterDescriptionNode> node_left,
0450 std::unique_ptr<ParameterDescriptionNode> node_right);
0451 }
0452 #endif