File indexing completed on 2024-05-23 03:13: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 namespace cfi {
0074 struct Paths {
0075
0076
0077
0078 std::optional<std::unordered_map<std::string, Paths>> nodes_;
0079 };
0080 struct Typed {};
0081 struct ClassFile {
0082 void parameterMustBeTyped() {
0083 Paths* p = &fullPaths_;
0084 for (auto n : presentPath_) {
0085 if (not p->nodes_) {
0086 p->nodes_ = std::unordered_map<std::string, Paths>();
0087 }
0088 p = &(p->nodes_.value().emplace(n, Paths{})).first->second;
0089 }
0090 }
0091 void pushNode(std::string_view iNode) { presentPath_.push_back(iNode); }
0092 void popNode() {
0093 assert(not presentPath_.empty());
0094 presentPath_.pop_back();
0095 }
0096
0097 Paths releasePaths() { return std::move(fullPaths_); }
0098
0099 private:
0100 std::vector<std::string_view> presentPath_;
0101 Paths fullPaths_;
0102 };
0103 struct Untyped {
0104 Untyped(Paths iPaths) : paths_(iPaths) {}
0105 bool needToSwitchToTyped(std::string_view iNode) {
0106 presentPath_.push_back(iNode);
0107 if (not paths_.nodes_.has_value()) {
0108 return false;
0109 }
0110 const Paths* p = &paths_;
0111 for (auto const& n : presentPath_) {
0112 if (not paths_.nodes_.has_value()) {
0113 return false;
0114 }
0115 auto f = paths_.nodes_->find(std::string(n));
0116 if (f == paths_.nodes_->end()) {
0117 return false;
0118 }
0119 p = &f->second;
0120 }
0121 return not p->nodes_.has_value();
0122 }
0123 void popNode() {
0124 assert(not presentPath_.empty());
0125 presentPath_.pop_back();
0126 }
0127
0128 private:
0129 std::vector<std::string_view> presentPath_;
0130 Paths paths_;
0131 };
0132
0133 using CfiOptions = std::variant<cfi::Typed, cfi::ClassFile, cfi::Untyped>;
0134
0135 inline void parameterMustBeTyped(CfiOptions& iOps) noexcept {
0136 if (std::holds_alternative<cfi::ClassFile>(iOps)) {
0137 std::get<cfi::ClassFile>(iOps).parameterMustBeTyped();
0138 }
0139 }
0140 inline void parameterMustBeTyped(CfiOptions& iOps, std::string_view iNode) noexcept {
0141 if (std::holds_alternative<cfi::ClassFile>(iOps)) {
0142 auto& d = std::get<cfi::ClassFile>(iOps);
0143 d.pushNode(iNode);
0144 d.parameterMustBeTyped();
0145 d.popNode();
0146 }
0147 }
0148 [[nodiscard]] inline bool shouldWriteUntyped(CfiOptions const& iOps) noexcept {
0149 return std::holds_alternative<cfi::Untyped>(iOps);
0150 }
0151
0152 struct NodeGuard {
0153 NodeGuard(CfiOptions& iOp) : options_(&iOp) {}
0154 NodeGuard() = delete;
0155 NodeGuard(NodeGuard const&) = delete;
0156 NodeGuard& operator=(NodeGuard const&) = delete;
0157 NodeGuard(NodeGuard&& iOther) : options_{iOther.options_} { iOther.options_ = nullptr; }
0158 NodeGuard& operator=(NodeGuard&& iOther) {
0159 NodeGuard temp{std::move(iOther)};
0160 options_ = temp.options_;
0161 temp.options_ = nullptr;
0162 return *this;
0163 }
0164 ~NodeGuard() {
0165 if (nullptr == options_) {
0166 return;
0167 }
0168 if (std::holds_alternative<ClassFile>(*options_)) {
0169 std::get<ClassFile>(*options_).popNode();
0170 } else if (std::holds_alternative<Untyped>(*options_)) {
0171 std::get<Untyped>(*options_).popNode();
0172 }
0173 }
0174 CfiOptions* options_;
0175 };
0176
0177 [[nodiscard]] inline std::pair<bool, NodeGuard> needToSwitchToTyped(std::string_view iNode,
0178 CfiOptions& iOpt) noexcept {
0179 if (std::holds_alternative<Untyped>(iOpt)) {
0180 return std::pair(std::get<Untyped>(iOpt).needToSwitchToTyped(iNode), NodeGuard(iOpt));
0181 } else if (std::holds_alternative<ClassFile>(iOpt)) {
0182 std::get<ClassFile>(iOpt).pushNode(iNode);
0183 }
0184 return std::pair(false, NodeGuard(iOpt));
0185 }
0186 }
0187 using CfiOptions = cfi::CfiOptions;
0188
0189 struct ParameterTypeToEnum {
0190 template <class T>
0191 static ParameterTypes toEnum();
0192 };
0193
0194 class Comment {
0195 public:
0196 Comment();
0197 explicit Comment(std::string const& iComment);
0198 explicit Comment(char const* iComment);
0199 std::string const& comment() const { return comment_; }
0200
0201 private:
0202 std::string comment_;
0203 };
0204
0205 class ParameterDescriptionNode {
0206 public:
0207 ParameterDescriptionNode() {}
0208
0209 explicit ParameterDescriptionNode(Comment const& iComment) : comment_(iComment.comment()) {}
0210
0211 virtual ~ParameterDescriptionNode();
0212
0213 virtual ParameterDescriptionNode* clone() const = 0;
0214
0215 std::string const& comment() const { return comment_; }
0216 void setComment(std::string const& value);
0217 void setComment(char const* value);
0218
0219
0220
0221
0222
0223
0224
0225 void validate(ParameterSet& pset, std::set<std::string>& validatedLabels, bool optional) const {
0226 validate_(pset, validatedLabels, optional);
0227 }
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239 void writeCfi(std::ostream& os,
0240 bool optional,
0241 bool& startWithComma,
0242 int indentation,
0243 CfiOptions& options,
0244 bool& wroteSomething) const {
0245 writeCfi_(os, optional, startWithComma, indentation, options, wroteSomething);
0246 }
0247
0248
0249 void print(std::ostream& os, bool optional, bool writeToCfi, DocFormatHelper& dfh) const;
0250
0251 bool hasNestedContent() const { return hasNestedContent_(); }
0252
0253 void printNestedContent(std::ostream& os, bool optional, DocFormatHelper& dfh) const;
0254
0255
0256
0257
0258
0259
0260
0261
0262 bool exists(ParameterSet const& pset) const { return exists_(pset); }
0263
0264
0265
0266
0267
0268 bool partiallyExists(ParameterSet const& pset) const { return partiallyExists_(pset); }
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281 int howManyXORSubNodesExist(ParameterSet const& pset) const { return howManyXORSubNodesExist_(pset); }
0282
0283
0284
0285
0286
0287
0288
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 void checkAndGetLabelsAndTypes(std::set<std::string>& usedLabels,
0336 std::set<ParameterTypes>& parameterTypes,
0337 std::set<ParameterTypes>& wildcardTypes) const {
0338 checkAndGetLabelsAndTypes_(usedLabels, parameterTypes, wildcardTypes);
0339 }
0340
0341 virtual bool isWildcard() const { return false; }
0342 static void printSpaces(std::ostream& os, int n);
0343
0344 protected:
0345 virtual void checkAndGetLabelsAndTypes_(std::set<std::string>& usedLabels,
0346 std::set<ParameterTypes>& parameterTypes,
0347 std::set<ParameterTypes>& wildcardTypes) const = 0;
0348
0349 virtual void validate_(ParameterSet& pset, std::set<std::string>& validatedLabels, bool optional) const = 0;
0350
0351 virtual void writeCfi_(std::ostream& os,
0352 bool optional,
0353 bool& startWithComma,
0354 int indentation,
0355 CfiOptions&,
0356 bool& wroteSomething) const = 0;
0357
0358 virtual void print_(std::ostream&, bool , bool , DocFormatHelper&) const {}
0359
0360 virtual bool hasNestedContent_() const { return false; }
0361
0362 virtual void printNestedContent_(std::ostream&, bool , DocFormatHelper&) const {}
0363
0364 virtual bool exists_(ParameterSet const& pset) const = 0;
0365
0366 virtual bool partiallyExists_(ParameterSet const& pset) const = 0;
0367
0368 virtual int howManyXORSubNodesExist_(ParameterSet const& pset) const = 0;
0369
0370 std::string comment_;
0371 };
0372
0373 template <>
0374 struct value_ptr_traits<ParameterDescriptionNode> {
0375 static ParameterDescriptionNode* clone(ParameterDescriptionNode const* p) { return p->clone(); }
0376 static void destroy(ParameterDescriptionNode* p) { delete p; }
0377 };
0378
0379
0380
0381 std::unique_ptr<ParameterDescriptionCases<bool>> operator>>(bool caseValue, ParameterDescriptionNode const& node);
0382
0383 std::unique_ptr<ParameterDescriptionCases<int>> operator>>(int caseValue, ParameterDescriptionNode const& node);
0384
0385 std::unique_ptr<ParameterDescriptionCases<std::string>> operator>>(std::string const& caseValue,
0386 ParameterDescriptionNode const& node);
0387
0388 std::unique_ptr<ParameterDescriptionCases<std::string>> operator>>(char const* caseValue,
0389 ParameterDescriptionNode const& node);
0390
0391 std::unique_ptr<ParameterDescriptionCases<bool>> operator>>(bool caseValue,
0392 std::unique_ptr<ParameterDescriptionNode> node);
0393
0394 std::unique_ptr<ParameterDescriptionCases<int>> operator>>(int caseValue,
0395 std::unique_ptr<ParameterDescriptionNode> node);
0396
0397 std::unique_ptr<ParameterDescriptionCases<std::string>> operator>>(std::string const& caseValue,
0398 std::unique_ptr<ParameterDescriptionNode> node);
0399
0400 std::unique_ptr<ParameterDescriptionCases<std::string>> operator>>(char const* caseValue,
0401 std::unique_ptr<ParameterDescriptionNode> node);
0402
0403
0404
0405 std::unique_ptr<ParameterDescriptionNode> operator&&(ParameterDescriptionNode const& node_left,
0406 ParameterDescriptionNode const& node_right);
0407
0408 std::unique_ptr<ParameterDescriptionNode> operator&&(std::unique_ptr<ParameterDescriptionNode> node_left,
0409 ParameterDescriptionNode const& node_right);
0410
0411 std::unique_ptr<ParameterDescriptionNode> operator&&(ParameterDescriptionNode const& node_left,
0412 std::unique_ptr<ParameterDescriptionNode> node_right);
0413
0414 std::unique_ptr<ParameterDescriptionNode> operator&&(std::unique_ptr<ParameterDescriptionNode> node_left,
0415 std::unique_ptr<ParameterDescriptionNode> node_right);
0416
0417
0418
0419 std::unique_ptr<ParameterDescriptionNode> operator||(ParameterDescriptionNode const& node_left,
0420 ParameterDescriptionNode const& node_right);
0421
0422 std::unique_ptr<ParameterDescriptionNode> operator||(std::unique_ptr<ParameterDescriptionNode> node_left,
0423 ParameterDescriptionNode const& node_right);
0424
0425 std::unique_ptr<ParameterDescriptionNode> operator||(ParameterDescriptionNode const& node_left,
0426 std::unique_ptr<ParameterDescriptionNode> node_right);
0427
0428 std::unique_ptr<ParameterDescriptionNode> operator||(std::unique_ptr<ParameterDescriptionNode> node_left,
0429 std::unique_ptr<ParameterDescriptionNode> node_right);
0430
0431
0432
0433 std::unique_ptr<ParameterDescriptionNode> operator^(ParameterDescriptionNode const& node_left,
0434 ParameterDescriptionNode const& node_right);
0435
0436 std::unique_ptr<ParameterDescriptionNode> operator^(std::unique_ptr<ParameterDescriptionNode> node_left,
0437 ParameterDescriptionNode const& node_right);
0438
0439 std::unique_ptr<ParameterDescriptionNode> operator^(ParameterDescriptionNode const& node_left,
0440 std::unique_ptr<ParameterDescriptionNode> node_right);
0441
0442 std::unique_ptr<ParameterDescriptionNode> operator^(std::unique_ptr<ParameterDescriptionNode> node_left,
0443 std::unique_ptr<ParameterDescriptionNode> node_right);
0444 }
0445 #endif