Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:03:30

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>), theIDs_() {
0016     for (std::vector<ParameterSet>::const_iterator i = vpset.begin(), e = vpset.end(); i != e; ++i) {
0017       theVPSet_->push_back(*i);
0018     }
0019   }
0020 
0021   VParameterSetEntry::VParameterSetEntry(std::string const& rep)
0022       : tracked_(rep[0] == '+'), theVPSet_(), theIDs_(new std::vector<ParameterSetID>) {
0023     assert(rep[0] == '+' || rep[0] == '-');
0024     std::vector<std::string> temp;
0025     // need a substring that starts at the '{'
0026     std::string bracketedRepr(rep.begin() + 2, rep.end());
0027     split(std::back_inserter(temp), bracketedRepr, '{', ',', '}');
0028     theIDs_->reserve(temp.size());
0029     for (std::vector<std::string>::const_iterator i = temp.begin(), e = temp.end(); i != e; ++i) {
0030       theIDs_->push_back(ParameterSetID(*i));
0031     }
0032   }
0033 
0034   void VParameterSetEntry::toString(std::string& result) const {
0035     assert(theIDs_);
0036     result += tracked_ ? "+q" : "-q";
0037     result += '{';
0038     std::string start;
0039     std::string const between(",");
0040     for (std::vector<ParameterSetID>::const_iterator i = theIDs_->begin(), e = theIDs_->end(); i != e; ++i) {
0041       result += start;
0042       i->toString(result);
0043       start = between;
0044     }
0045     result += '}';
0046   }
0047 
0048   void VParameterSetEntry::toDigest(cms::Digest& digest) const {
0049     assert(theIDs_);
0050     digest.append(tracked_ ? "+q{" : "-q{", 3);
0051     bool started = false;
0052     for (std::vector<ParameterSetID>::const_iterator i = theIDs_->begin(), e = theIDs_->end(); i != e; ++i) {
0053       if (started)
0054         digest.append(",", 1);
0055       i->toDigest(digest);
0056       started = true;
0057     }
0058     digest.append("}", 1);
0059   }
0060 
0061   std::string VParameterSetEntry::toString() const {
0062     std::string result;
0063     toString(result);
0064     return result;
0065   }
0066 
0067   std::vector<ParameterSet> const& VParameterSetEntry::vpset() const {
0068     fillVPSet();
0069     return *theVPSet_;
0070   }
0071 
0072   // NOTE: This function, and other non-const functions of this class
0073   // that expose internals, may be used in a way that causes the cached
0074   // "theVPSet_" and "theIDs_" to be inconsistent.
0075   // THIS PROBLEM NEEDS TO BE ADDRESSED
0076   std::vector<ParameterSet>& VParameterSetEntry::vpsetForUpdate() {
0077     fillVPSet();
0078     return *theVPSet_;
0079   }
0080 
0081   void VParameterSetEntry::fillVPSet() const {
0082     if (nullptr == theVPSet_.load()) {
0083       auto tmp = std::make_unique<std::vector<ParameterSet>>();
0084       tmp->reserve(theIDs_->size());
0085       for (auto const& theID : *theIDs_) {
0086         tmp->push_back(getParameterSet(theID));
0087       }
0088       VParameterSet* expected = nullptr;
0089       if (theVPSet_.compare_exchange_strong(expected, tmp.get())) {
0090         // theVPSet_ was equal to nullptr and now is equal to tmp.get()
0091         tmp.release();
0092       }
0093     }
0094   }
0095 
0096   // NOTE: This function, and other non-const functions of this class
0097   // that expose internals, may be used in a way that causes the cached
0098   // "theVPSet_" and "theIDs_" to be inconsistent.
0099   // THIS PROBLEM NEEDS TO BE ADDRESSED
0100   ParameterSet& VParameterSetEntry::psetInVector(int i) {
0101     assert(theVPSet_);
0102     return theVPSet_->at(i);
0103   }
0104 
0105   std::vector<ParameterSet>::size_type VParameterSetEntry::size() const { return vpset().size(); }
0106 
0107   void VParameterSetEntry::registerPsetsAndUpdateIDs() {
0108     fillVPSet();
0109     theIDs_ = value_ptr<std::vector<ParameterSetID>>(new std::vector<ParameterSetID>);
0110     theIDs_->resize(theVPSet_->size());
0111     for (std::vector<ParameterSet>::iterator i = theVPSet_->begin(), e = theVPSet_->end(); i != e; ++i) {
0112       if (!i->isRegistered()) {
0113         i->registerIt();
0114       }
0115       theIDs_->at(i - theVPSet_->begin()) = i->id();
0116     }
0117   }
0118 
0119   std::string VParameterSetEntry::dump(unsigned int indent) const {
0120     std::string indentation(indent, ' ');
0121     std::ostringstream os;
0122     std::vector<ParameterSet> const& vps = vpset();
0123     os << "VPSet " << (isTracked() ? "tracked" : "untracked") << " = ({" << std::endl;
0124     std::string start;
0125     std::string const between(",\n");
0126     for (std::vector<ParameterSet>::const_iterator i = vps.begin(), e = vps.end(); i != e; ++i) {
0127       os << start << indentation << i->dump(indent);
0128       start = between;
0129     }
0130     if (!vps.empty()) {
0131       os << std::endl;
0132     }
0133     os << indentation << "})";
0134     return os.str();
0135   }
0136 
0137   std::ostream& operator<<(std::ostream& os, VParameterSetEntry const& vpsetEntry) {
0138     os << vpsetEntry.dump();
0139     return os;
0140   }
0141 }  // namespace edm