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  T const& operator*() const { return *product_; }
47  T const* operator->() const { return product_.get(); }
48  mutable std::shared_ptr<T> product_{nullptr};
49  };
50 
51  template <int ILabel, typename T>
52  L<T, ILabel> l(std::shared_ptr<T>& iP) {
53  return L<T, ILabel>{iP};
54  }
55 
56  struct Label {
57  Label() = default;
58  Label(const char* iLabel) : default_{iLabel} {}
59  Label(const std::string& iString) : default_{iString} {}
60  Label(const std::string& iString, unsigned int const iIndex) : labels_(iIndex + 1, def()) {
61  labels_[iIndex] = iString;
62  }
63 
64  Label& operator()(const std::string& iString, unsigned int const iIndex) {
65  if (iIndex == labels_.size()) {
66  labels_.push_back(iString);
67  } else if (iIndex > labels_.size()) {
68  std::vector<std::string> temp(iIndex + 1, def());
69  copy_all(labels_, temp.begin());
70  labels_.swap(temp);
71  } else {
72  if (labels_[iIndex] != def()) {
73  Exception e(errors::Configuration, "Duplicate Label");
74  e << "The index " << iIndex << " was previously assigned the label \"" << labels_[iIndex]
75  << "\" and then was later assigned \"" << iString << "\"";
76  e.raise();
77  }
78  labels_[iIndex] = iString;
79  }
80  return *this;
81  }
82  Label& operator()(int iIndex, const std::string& iString) { return (*this)(iString, iIndex); }
83 
84  static const std::string& def() {
85  static const std::string s_def("\n\t");
86  return s_def;
87  }
88 
89  std::vector<std::string> labels_{};
91  };
92 
93  inline Label label(const std::string& iString, int iIndex) { return Label(iString, iIndex); }
94  inline Label label(int iIndex, const std::string& iString) { return Label(iString, iIndex); }
95 } // namespace edm::es
96 
97 #endif
static const std::string & def()
Definition: es_Label.h:84
Label label(const std::string &iString, int iIndex)
Definition: es_Label.h:93
Label(const char *iLabel)
Definition: es_Label.h:58
L(T *iP)
Definition: es_Label.h:42
Label(const std::string &iString)
Definition: es_Label.h:59
T const * operator->() const
Definition: es_Label.h:47
L()=default
T & operator*()
Definition: es_Label.h:44
L< T, ILabel > l(std::shared_ptr< T > &iP)
Definition: es_Label.h:52
std::shared_ptr< T > product_
Definition: es_Label.h:48
L(std::shared_ptr< T > iP)
Definition: es_Label.h:40
Label()=default
Label & operator()(int iIndex, const std::string &iString)
Definition: es_Label.h:82
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 const & operator*() const
Definition: es_Label.h:46
T element_type
Definition: es_Label.h:37
std::string default_
Definition: es_Label.h:90
Func copy_all(ForwardSequence &s, Func f)
wrappers for copy
Definition: Algorithms.h:20
T * operator->()
Definition: es_Label.h:45
std::vector< std::string > labels_
Definition: es_Label.h:89
long double T
def move(src, dest)
Definition: eostools.py:511
Label(const std::string &iString, unsigned int const iIndex)
Definition: es_Label.h:60