File indexing completed on 2024-04-06 12:12:01
0001 #ifndef Framework_es_Label_h
0002 #define Framework_es_Label_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #include <memory>
0023 #include <string>
0024 #include <string_view>
0025 #include <vector>
0026
0027
0028 #include "FWCore/Utilities/interface/EDMException.h"
0029 #include "FWCore/Utilities/interface/Algorithms.h"
0030
0031
0032
0033 namespace edm::es {
0034
0035 template <typename T, int ILabel>
0036 struct L {
0037 using element_type = T;
0038
0039 L() = default;
0040 explicit L(std::shared_ptr<T> iP) : product_{std::move(iP)} {}
0041 explicit L(std::unique_ptr<T> iP) : product_{std::move(iP)} {}
0042 explicit L(T* iP) : product_(iP) {}
0043
0044 T& operator*() { return *product_; }
0045 T* operator->() { return product_.get(); }
0046 T const& operator*() const { return *product_; }
0047 T const* operator->() const { return product_.get(); }
0048 mutable std::shared_ptr<T> product_{nullptr};
0049 };
0050
0051 template <int ILabel, typename T>
0052 L<T, ILabel> l(std::shared_ptr<T>& iP) {
0053 return L<T, ILabel>{iP};
0054 }
0055
0056 struct Label {
0057 Label() = default;
0058 Label(const char* iLabel) : default_{iLabel} {}
0059 Label(const std::string& iString) : default_{iString} {}
0060 Label(const std::string& iString, unsigned int const iIndex) : labels_(iIndex + 1, def()) {
0061 labels_[iIndex] = iString;
0062 }
0063
0064 Label& operator()(const std::string& iString, unsigned int const iIndex) {
0065 if (iIndex == labels_.size()) {
0066 labels_.push_back(iString);
0067 } else if (iIndex > labels_.size()) {
0068 std::vector<std::string> temp(iIndex + 1, def());
0069 copy_all(labels_, temp.begin());
0070 labels_.swap(temp);
0071 } else {
0072 if (labels_[iIndex] != def()) {
0073 Exception e(errors::Configuration, "Duplicate Label");
0074 e << "The index " << iIndex << " was previously assigned the label \"" << labels_[iIndex]
0075 << "\" and then was later assigned \"" << iString << "\"";
0076 e.raise();
0077 }
0078 labels_[iIndex] = iString;
0079 }
0080 return *this;
0081 }
0082 Label& operator()(int iIndex, const std::string& iString) { return (*this)(iString, iIndex); }
0083
0084 static const std::string& def() {
0085 static const std::string s_def("\n\t");
0086 return s_def;
0087 }
0088
0089 std::vector<std::string> labels_{};
0090 std::string default_{};
0091 };
0092
0093 inline Label label(const std::string& iString, int iIndex) { return Label(iString, iIndex); }
0094 inline Label label(int iIndex, const std::string& iString) { return Label(iString, iIndex); }
0095 }
0096
0097 #endif