CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Store.h
Go to the documentation of this file.
1 #ifndef DDI_Store_h
2 #define DDI_Store_h
3 
4 #include <map>
7 
8 //;
9 //FIXME: Store : implement readOnly-behaviour ..
10 namespace DDI {
11 
36  template <class N, class I, class K = I>
37  class Store {
38  public:
39  using name_type = N;
40  using pimpl_type = I;
41  using key_type = K;
43  using prep_type = Rep_type*;
44  using registry_type = std::map<name_type, prep_type>;
45  using iterator = typename registry_type::iterator;
46 
47  auto begin() { return reg_.begin(); }
48  auto end() { return reg_.end(); }
49  auto size() const { return reg_.size(); }
50 
51  // empty shell or fetch from registry
52  prep_type create(const name_type&);
53 
54  // full new object or replace existing with new one
56 
57  // clear all objects
58  void clear();
59 
60  // swap moves the registry from this guy to another of the same type
61  void swap(Store&);
62 
63  bool isDefined(const name_type& n) const;
64  void setReadOnly(bool b) { readOnly_ = b; }
65  bool readOnly() const { return readOnly_; }
66 
68  ~Store();
69  Store(const Store&) = delete;
70  Store& operator=(const Store&) = delete;
71 
72  protected:
73  std::map<name_type, prep_type> reg_;
74  bool readOnly_;
75  };
76 
77  template <class N, class I, class K>
79  prep_type tmp = nullptr;
80  auto result = reg_.emplace(n, tmp);
81  if (result.second) {
82  if (readOnly_)
83  throw cms::Exception("DetectorDescriptionStore")
84  << " Store has been locked. Illegal attempt to add " << n << " to a global store.";
85  // ELSE
86  result.first->second = new Rep_type(n, nullptr);
87  }
88  return result.first->second;
89  }
90 
91  template <class N, class I, class K>
93  if (readOnly_)
94  throw cms::Exception("DetectorDescriptionStore")
95  << " Store has been locked. Illegal attempt to add " << n << " to a global store.";
96  // ELSE
97  prep_type tmp = nullptr;
98  auto result = reg_.emplace(n, tmp);
99  if (!result.second) {
100  result.first->second->second.swap(p);
101  } else {
102  result.first->second = new Rep_type(n, std::move(p));
103  }
104  return result.first->second;
105  }
106 
107  template <typename I>
108  struct Finalize {
109  static void cleanup(I&& ptr) {}
110  };
111 
112  template <typename I>
113  struct Finalize<I*> {
114  static void cleanup(I* ptr) {
115  delete ptr;
116  ptr = nullptr;
117  }
118  };
119 
120  template <class N, class I, class K>
122  for (auto it : reg_) {
123  Finalize<I>::cleanup(std::move(it.second->second));
124  delete it.second;
125  it.second = nullptr;
126  }
127  }
128 
129  template <class N, class I, class K>
130  bool Store<N, I, K>::isDefined(const name_type& n) const {
131  if (readOnly_)
132  edm::LogWarning("DetectorDescriptionStore")
133  << " Store is locked and most likely empty. isDefined will be false.";
134  auto it = reg_.find(n);
135  bool result(false);
136  if (it != reg_.end()) {
137  if (it.second->second) {
138  result = true;
139  }
140  }
141  return result;
142  }
143 
144  template <class N, class I, class K>
146  reg_.swap(storeToSwap.reg_);
147  storeToSwap.readOnly_ = readOnly_;
148  }
149 
150 } // namespace DDI
151 
152 #endif
Store()
Definition: Store.h:67
std::map< name_type, prep_type > registry_type
Definition: Store.h:44
void swap(Store &)
Definition: Store.h:145
auto begin()
Definition: Store.h:47
~Store()
Definition: Store.h:121
std::map< name_type, prep_type > reg_
Definition: Store.h:73
bool readOnly() const
Definition: Store.h:65
N name_type
Definition: Store.h:39
static void cleanup(I *ptr)
Definition: Store.h:114
DDName is used to identify DDD entities uniquely.
Definition: DDName.h:17
prep_type create(const name_type &)
Definition: Store.h:78
typename registry_type::iterator iterator
Definition: Store.h:45
tuple result
Definition: mps_fire.py:311
static void cleanup(I &&ptr)
Definition: Store.h:109
void setReadOnly(bool b)
Definition: Store.h:64
bool readOnly_
Definition: Store.h:74
def move
Definition: eostools.py:511
const std::complex< double > I
Definition: I.h:8
void clear()
#define N
Definition: blowfish.cc:9
Rep_type * prep_type
Definition: Store.h:43
double b
Definition: hdecay.h:118
Store & operator=(const Store &)=delete
auto end()
Definition: Store.h:48
auto size() const
Definition: Store.h:49
Log< level::Warning, false > LogWarning
tmp
align.sh
Definition: createJobs.py:716
bool isDefined(const name_type &n) const
Definition: Store.h:130