CMS 3D CMS Logo

es_Label.h
Go to the documentation of this file.
1 #ifndef Framework_es_Label_h
2 #define Framework_es_Label_h
3 // -*- C++ -*-
4 //
5 // Package: Framework
6 // Class : es_Label
7 //
16 //
17 // Original Author: Chris Jones
18 // Created: Fri Sep 30 09:35:20 EDT 2005
19 //
20 
21 // system include files
22 #include <memory>
23 #include <string>
24 #include <string_view>
25 #include <vector>
26 
27 // user include files
30 
31 // forward declarations
32 
33 namespace edm::es {
34 
35  template <typename T, int ILabel>
36  struct L {
37  using element_type = T;
38 
39  L() = default;
40  explicit L(std::shared_ptr<T> iP) : product_{std::move(iP)} {}
41  explicit L(std::unique_ptr<T> iP) : product_{std::move(iP)} {}
42  explicit L(T* iP) : product_(iP) {}
43 
44  T& operator*() { return *product_; }
45  T* operator->() { return product_.get(); }
46  mutable std::shared_ptr<T> product_{nullptr};
47  };
48 
49  template <int ILabel, typename T>
50  L<T, ILabel> l(std::shared_ptr<T>& iP) {
51  return L<T, ILabel>{iP};
52  }
53 
54  struct Label {
55  Label() = default;
56  Label(const char* iLabel) : default_{iLabel} {}
57  Label(const std::string& iString) : default_{iString} {}
58  Label(const std::string& iString, unsigned int const iIndex) : labels_(iIndex + 1, def()) {
59  labels_[iIndex] = iString;
60  }
61 
62  Label& operator()(const std::string& iString, unsigned int const iIndex) {
63  if (iIndex == labels_.size()) {
64  labels_.push_back(iString);
65  } else if (iIndex > labels_.size()) {
66  std::vector<std::string> temp(iIndex + 1, def());
67  copy_all(labels_, temp.begin());
68  labels_.swap(temp);
69  } else {
70  if (labels_[iIndex] != def()) {
71  Exception e(errors::Configuration, "Duplicate Label");
72  e << "The index " << iIndex << " was previously assigned the label \"" << labels_[iIndex]
73  << "\" and then was later assigned \"" << iString << "\"";
74  e.raise();
75  }
76  labels_[iIndex] = iString;
77  }
78  return *this;
79  }
80  Label& operator()(int iIndex, const std::string& iString) { return (*this)(iString, iIndex); }
81 
82  static const std::string& def() {
83  static const std::string s_def("\n\t");
84  return s_def;
85  }
86 
87  std::vector<std::string> labels_{};
88  std::string default_{};
89  };
90 
91  inline Label label(const std::string& iString, int iIndex) { return Label(iString, iIndex); }
92  inline Label label(int iIndex, const std::string& iString) { return Label(iString, iIndex); }
93 } // namespace edm::es
94 
95 #endif
static const std::string & def()
Definition: es_Label.h:82
Label label(const std::string &iString, int iIndex)
Definition: es_Label.h:91
Label(const char *iLabel)
Definition: es_Label.h:56
L(T *iP)
Definition: es_Label.h:42
void raise()
Definition: Exception.h:98
Label(const std::string &iString)
Definition: es_Label.h:57
L()=default
T & operator*()
Definition: es_Label.h:44
L< T, ILabel > l(std::shared_ptr< T > &iP)
Definition: es_Label.h:50
std::shared_ptr< T > product_
Definition: es_Label.h:46
L(std::shared_ptr< T > iP)
Definition: es_Label.h:40
Label & operator()(int iIndex, const std::string &iString)
Definition: es_Label.h:80
Label & operator()(const std::string &iString, unsigned int const iIndex)
Definition: es_Label.h:62
L(std::unique_ptr< T > iP)
Definition: es_Label.h:41
T element_type
Definition: es_Label.h:37
Func copy_all(ForwardSequence &s, Func f)
wrappers for copy
Definition: Algorithms.h:20
T * operator->()
Definition: es_Label.h:45
long double T
JetCorrectorParameters::Definitions def
Definition: classes.h:6
def move(src, dest)
Definition: eostools.py:511
Label(const std::string &iString, unsigned int const iIndex)
Definition: es_Label.h:58