File indexing completed on 2021-10-01 22:40:37
0001 #ifndef FWCore_ParameterSet_PluginDescription_h
0002 #define FWCore_ParameterSet_PluginDescription_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085 #include "FWCore/ParameterSet/interface/ParameterDescriptionNode.h"
0086 #include "FWCore/ParameterSet/interface/PluginDescriptionAdaptorBase.h"
0087 #include "FWCore/PluginManager/interface/PluginFactory.h"
0088 #include "FWCore/PluginManager/interface/PluginManager.h"
0089 #include "FWCore/PluginManager/interface/standard.h"
0090 #include "FWCore/Utilities/interface/thread_safety_macros.h"
0091
0092
0093 #include <string>
0094
0095
0096 namespace edm {
0097 template <typename T>
0098 class PluginDescription : public ParameterDescriptionNode {
0099 public:
0100
0101
0102
0103
0104 PluginDescription(std::string typeLabel, bool typeLabelIsTracked)
0105 : typeLabel_{std::move(typeLabel)}, typeLabelIsTracked_{typeLabelIsTracked} {}
0106
0107
0108
0109
0110
0111
0112 PluginDescription(std::string typeLabel, std::string defaultType, bool typeLabelIsTracked)
0113 : typeLabel_{std::move(typeLabel)},
0114 defaultType_{std::move(defaultType)},
0115 typeLabelIsTracked_{typeLabelIsTracked} {}
0116
0117
0118 ParameterDescriptionNode* clone() const final { return new PluginDescription<T>(*this); }
0119
0120 protected:
0121 void checkAndGetLabelsAndTypes_(std::set<std::string>& usedLabels,
0122 std::set<ParameterTypes>& parameterTypes,
0123 std::set<ParameterTypes>& wildcardTypes) const final {}
0124
0125 void validate_(ParameterSet& pset, std::set<std::string>& validatedLabels, bool optional) const final {
0126 loadDescription(findType(pset)).validate(pset);
0127
0128 auto n = pset.getParameterNames();
0129 validatedLabels.insert(n.begin(), n.end());
0130 }
0131
0132 void writeCfi_(
0133 std::ostream& os, bool optional, bool& startWithComma, int indentation, bool& wroteSomething) const final {
0134 if (not defaultType_.empty()) {
0135 if (!edmplugin::PluginManager::isAvailable()) {
0136 auto conf = edmplugin::standard::config();
0137 conf.allowNoCache();
0138 edmplugin::PluginManager::configure(conf);
0139 }
0140 loadDescription(defaultType_).writeCfi(os, startWithComma, indentation);
0141 wroteSomething = true;
0142 }
0143 }
0144
0145 bool hasNestedContent_() const final { return true; }
0146
0147 void printNestedContent_(std::ostream& os, bool , DocFormatHelper& dfh) const final {
0148 int indentation = dfh.indentation();
0149
0150 using CreatedType = PluginDescriptionAdaptorBase<typename T::CreatedType>;
0151 using Factory = edmplugin::PluginFactory<CreatedType*()>;
0152
0153 {
0154 std::stringstream ss;
0155 ss << dfh.section() << "." << dfh.counter();
0156 std::string newSection = ss.str();
0157
0158 printSpaces(os, indentation);
0159 os << "Section " << newSection << " " << Factory::get()->category() << " Plugins description:\n";
0160 if (!dfh.brief())
0161 os << "\n";
0162 }
0163
0164
0165 unsigned int pluginCount = 0;
0166 std::string previousName;
0167 for (auto const& info :
0168 edmplugin::PluginManager::get()->categoryToInfos().find(Factory::get()->category())->second) {
0169
0170 if (previousName == info.name_) {
0171 continue;
0172 }
0173
0174 std::stringstream ss;
0175 ss << dfh.section() << "." << dfh.counter();
0176 std::string newSection = ss.str();
0177 printSpaces(os, indentation);
0178 os << "Section " << newSection << "." << ++pluginCount << " " << info.name_ << " Plugin description:\n";
0179 if (!dfh.brief())
0180 os << "\n";
0181
0182 DocFormatHelper new_dfh(dfh);
0183 new_dfh.init();
0184 new_dfh.setSection(newSection);
0185
0186 loadDescription(info.name_).print(os, new_dfh);
0187
0188 previousName = info.name_;
0189 }
0190 }
0191
0192 bool exists_(ParameterSet const& pset) const final {
0193 return pset.existsAs<std::string>(typeLabel_, typeLabelIsTracked_);
0194 }
0195
0196 bool partiallyExists_(ParameterSet const& pset) const final { return exists_(pset); }
0197
0198 int howManyXORSubNodesExist_(ParameterSet const& pset) const final { return exists(pset) ? 1 : 0; }
0199
0200 private:
0201 std::string findType(edm::ParameterSet const& iPSet) const {
0202 if (typeLabelIsTracked_) {
0203 if (iPSet.existsAs<std::string>(typeLabel_) || defaultType_.empty()) {
0204 return iPSet.getParameter<std::string>(typeLabel_);
0205 } else {
0206 return defaultType_;
0207 }
0208 }
0209 if (defaultType_.empty()) {
0210 return iPSet.getUntrackedParameter<std::string>(typeLabel_);
0211 }
0212 return iPSet.getUntrackedParameter<std::string>(typeLabel_, defaultType_);
0213 }
0214
0215 ParameterSetDescription loadDescription(std::string const& iName) const {
0216 using CreatedType = PluginDescriptionAdaptorBase<typename T::CreatedType>;
0217 std::unique_ptr<CreatedType> a(edmplugin::PluginFactory<CreatedType*()>::get()->create(iName));
0218
0219 ParameterSetDescription desc = a->description();
0220
0221
0222 if (typeLabelIsTracked_) {
0223 if (defaultType_.empty()) {
0224 desc.add<std::string>(typeLabel_);
0225 } else {
0226 desc.add<std::string>(typeLabel_, defaultType_);
0227 }
0228 } else {
0229 if (defaultType_.empty()) {
0230 desc.addUntracked<std::string>(typeLabel_);
0231 } else {
0232 desc.addUntracked<std::string>(typeLabel_, defaultType_);
0233 }
0234 }
0235 return desc;
0236 }
0237
0238
0239 std::string typeLabel_;
0240 std::string defaultType_;
0241 bool typeLabelIsTracked_;
0242 };
0243 }
0244
0245 #endif