File indexing completed on 2024-04-15 22:37:04
0001 #include "FWCore/ParameterSet/interface/VParameterSetEntry.h"
0002 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0003 #include "FWCore/ParameterSet/src/split.h"
0004 #include "FWCore/Utilities/interface/Digest.h"
0005
0006 #include <cassert>
0007 #include <ostream>
0008 #include <sstream>
0009
0010 namespace edm {
0011
0012 VParameterSetEntry::VParameterSetEntry() : tracked_(false), theVPSet_(), theIDs_() {}
0013
0014 VParameterSetEntry::VParameterSetEntry(std::vector<ParameterSet> const& vpset, bool isTracked)
0015 : tracked_(isTracked), theVPSet_(new std::vector<ParameterSet>(vpset)), theIDs_() {}
0016
0017 VParameterSetEntry::VParameterSetEntry(std::string_view rep)
0018 : tracked_(rep[0] == '+'), theVPSet_(), theIDs_(new std::vector<ParameterSetID>) {
0019 assert(rep[0] == '+' || rep[0] == '-');
0020 std::vector<std::string_view> temp;
0021
0022 std::string_view bracketedRepr = rep.substr(2);
0023 split(std::back_inserter(temp), bracketedRepr, '{', ',', '}');
0024 theIDs_->reserve(temp.size());
0025 for (auto const& id : temp) {
0026 theIDs_->emplace_back(std::string(id));
0027 }
0028 }
0029
0030 void VParameterSetEntry::toString(std::string& result) const {
0031 assert(theIDs_);
0032 result += tracked_ ? "+q" : "-q";
0033 result += '{';
0034 std::string start;
0035 std::string const between(",");
0036 for (std::vector<ParameterSetID>::const_iterator i = theIDs_->begin(), e = theIDs_->end(); i != e; ++i) {
0037 result += start;
0038 i->toString(result);
0039 start = between;
0040 }
0041 result += '}';
0042 }
0043
0044 void VParameterSetEntry::toDigest(cms::Digest& digest) const {
0045 assert(theIDs_);
0046 digest.append(tracked_ ? "+q{" : "-q{", 3);
0047 bool started = false;
0048 for (std::vector<ParameterSetID>::const_iterator i = theIDs_->begin(), e = theIDs_->end(); i != e; ++i) {
0049 if (started)
0050 digest.append(",", 1);
0051 i->toDigest(digest);
0052 started = true;
0053 }
0054 digest.append("}", 1);
0055 }
0056
0057 std::string VParameterSetEntry::toString() const {
0058 std::string result;
0059 toString(result);
0060 return result;
0061 }
0062
0063 std::vector<ParameterSet> const& VParameterSetEntry::vpset() const {
0064 fillVPSet();
0065 return *theVPSet_;
0066 }
0067
0068
0069
0070
0071
0072 std::vector<ParameterSet>& VParameterSetEntry::vpsetForUpdate() {
0073 fillVPSet();
0074 return *theVPSet_;
0075 }
0076
0077 void VParameterSetEntry::fillVPSet() const {
0078 if (nullptr == theVPSet_.load()) {
0079 auto tmp = std::make_unique<std::vector<ParameterSet>>();
0080 tmp->reserve(theIDs_->size());
0081 for (auto const& theID : *theIDs_) {
0082 tmp->push_back(getParameterSet(theID));
0083 }
0084 VParameterSet* expected = nullptr;
0085 if (theVPSet_.compare_exchange_strong(expected, tmp.get())) {
0086
0087 tmp.release();
0088 }
0089 }
0090 }
0091
0092
0093
0094
0095
0096 ParameterSet& VParameterSetEntry::psetInVector(int i) {
0097 assert(theVPSet_);
0098 return theVPSet_->at(i);
0099 }
0100
0101 std::vector<ParameterSet>::size_type VParameterSetEntry::size() const {
0102 return theIDs_ ? theIDs_->size() : (theVPSet_ ? vpset().size() : 0);
0103 }
0104
0105 void VParameterSetEntry::registerPsetsAndUpdateIDs() {
0106 fillVPSet();
0107 theIDs_ = value_ptr<std::vector<ParameterSetID>>(new std::vector<ParameterSetID>);
0108 theIDs_->resize(theVPSet_->size());
0109 for (std::vector<ParameterSet>::iterator i = theVPSet_->begin(), e = theVPSet_->end(); i != e; ++i) {
0110 if (!i->isRegistered()) {
0111 i->registerIt();
0112 }
0113 theIDs_->at(i - theVPSet_->begin()) = i->id();
0114 }
0115 }
0116
0117 std::string VParameterSetEntry::dump(unsigned int indent) const {
0118 std::string indentation(indent, ' ');
0119 std::ostringstream os;
0120 std::vector<ParameterSet> const& vps = vpset();
0121 os << "VPSet " << (isTracked() ? "tracked" : "untracked") << " = ({" << std::endl;
0122 std::string start;
0123 std::string const between(",\n");
0124 for (std::vector<ParameterSet>::const_iterator i = vps.begin(), e = vps.end(); i != e; ++i) {
0125 os << start << indentation << i->dump(indent);
0126 start = between;
0127 }
0128 if (!vps.empty()) {
0129 os << std::endl;
0130 }
0131 os << indentation << "})";
0132 return os.str();
0133 }
0134
0135 std::ostream& operator<<(std::ostream& os, VParameterSetEntry const& vpsetEntry) {
0136 os << vpsetEntry.dump();
0137 return os;
0138 }
0139 }