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) :
59  labels_(iIndex+1, def())
60  {
61  labels_[iIndex] = iString;
62  }
63 
64  Label& operator()(const std::string& iString, unsigned int const iIndex)
65  {
66  if (iIndex == labels_.size()) {
67  labels_.push_back(iString);
68  } else if(iIndex > labels_.size()) {
69  std::vector<std::string> temp(iIndex+1,def());
70  copy_all(labels_, temp.begin());
71  labels_.swap(temp);
72  } else {
73  if( labels_[iIndex] != def() ) {
74  Exception e(errors::Configuration,"Duplicate Label");
75  e <<"The index "<<iIndex<<" was previously assigned the label \""
76  <<labels_[iIndex]<<"\" and then was later assigned \""
77  <<iString<<"\"";
78  e.raise();
79  }
80  labels_[iIndex] = iString;
81  }
82  return *this;
83  }
84  Label& operator()(int iIndex, const std::string& iString) {
85  return (*this)(iString, iIndex);
86  }
87 
88  static const std::string& def() {
89  static const std::string s_def("\n\t");
90  return s_def;
91  }
92 
93  std::vector<std::string> labels_{};
94  std::string default_{};
95  };
96 
97  inline Label label(const std::string& iString, int iIndex) {
98  return Label(iString, iIndex);
99  }
100  inline Label label(int iIndex, const std::string& iString) {
101  return Label(iString, iIndex);
102  }
103 }
104 
105 #endif
static const std::string & def()
Definition: es_Label.h:88
Label label(const std::string &iString, int iIndex)
Definition: es_Label.h:97
Label(const char *iLabel)
Definition: es_Label.h:56
L(T *iP)
Definition: es_Label.h:42
void raise()
Definition: Exception.h:104
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:84
Label & operator()(const std::string &iString, unsigned int const iIndex)
Definition: es_Label.h:64
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:24
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