CMS 3D CMS Logo

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  {
39  public:
40  using name_type = N;
41  using pimpl_type = I;
42  using key_type = K;
44  using prep_type = Rep_type*;
45  using registry_type = std::map< name_type, prep_type>;
46  using iterator = typename registry_type::iterator;
47 
48  auto begin() { return reg_.begin(); }
49  auto end() { return reg_.end(); }
50  auto size() const { return reg_.size(); }
51 
52  // empty shell or fetch from registry
53  prep_type create(const name_type &);
54 
55  // full new object or replace existing with new one
57 
58  // clear all objects
59  void clear();
60 
61  // swap moves the registry from this guy to another of the same type
62  void swap ( Store& );
63 
64  bool isDefined(const name_type & n ) const;
65  void setReadOnly(bool b) { readOnly_ = b; }
66  bool readOnly() const { return readOnly_; }
67 
69  ~Store();
70 
71  protected:
72  std::map<name_type,prep_type> reg_;
73  Store(const Store &) = delete;
74  Store & operator=(const Store &) = delete;
75  bool readOnly_;
76  };
77 
78  template<class N, class I, class K>
79  typename Store<N,I,K>::prep_type
81  {
82  prep_type tmp = nullptr;
83  auto result = reg_.emplace( n, tmp );
84  if( result.second ) {
85  if( readOnly_ )
86  throw cms::Exception( "DetectorDescriptionStore" )
87  << " Store has been locked. Illegal attempt to add "
88  << n << " to a global store.";
89  // ELSE
90  result.first->second = new Rep_type( n, nullptr );
91  }
92  return result.first->second;
93  }
94 
95  template<class N, class I, class K>
98  pimpl_type p )
99  {
100  if( readOnly_ )
101  throw cms::Exception( "DetectorDescriptionStore" )
102  << " Store has been locked. Illegal attempt to add "
103  << n << " to a global store.";
104  // ELSE
105  prep_type tmp = nullptr;
106  auto result = reg_.emplace( n, tmp );
107  if( !result.second ) {
108  result.first->second->second.swap( p );
109  }
110  else {
111  result.first->second = new Rep_type( n, std::move( p ));
112  }
113  return result.first->second;
114  }
115 
116  template <typename I>
117  struct Finalize
118  {
119  static void cleanup( I&& ptr ) {
120  }
121  };
122 
123  template <typename I>
124  struct Finalize<I*>
125  {
126  static void cleanup( I* ptr ) {
127  delete ptr;
128  ptr = nullptr;
129  }
130  };
131 
132  template<class N, class I, class K>
134  {
135  for( auto it : reg_ ) {
136  Finalize<I>::cleanup( std::move( it.second->second ));
137  delete it.second;
138  it.second = nullptr;
139  }
140  }
141 
142  template<class N, class I, class K>
143  bool Store<N,I,K>::isDefined(const name_type & n ) const
144  {
145  if (readOnly_) edm::LogWarning("DetectorDescriptionStore") << " Store is locked and most likely empty. isDefined will be false.";
146  auto it = reg_.find(n);
147  bool result(false);
148  if (it != reg_.end()) {
149  if (it.second->second) {
150  result=true;
151  }
152  }
153  return result;
154  }
155 
156  template<class N, class I, class K>
157  void Store<N, I, K>::swap ( Store<N, I, K>& storeToSwap ) {
158  reg_.swap(storeToSwap.reg_);
159  storeToSwap.readOnly_ = readOnly_;
160  }
161 
162 } // namespace DDI
163 
164 #endif
Store()
Definition: Store.h:68
void swap(Store &)
Definition: Store.h:157
auto begin()
Definition: Store.h:48
~Store()
Definition: Store.h:133
bool readOnly() const
Definition: Store.h:66
N name_type
Definition: Store.h:40
std::map< name_type, prep_type > registry_type
Definition: Store.h:45
static void cleanup(I *ptr)
Definition: Store.h:126
DDName is used to identify DDD entities uniquely.
Definition: DDName.h:15
prep_type create(const name_type &)
Definition: Store.h:80
typename registry_type::iterator iterator
Definition: Store.h:46
static void cleanup(I &&ptr)
Definition: Store.h:119
void setReadOnly(bool b)
Definition: Store.h:65
bool readOnly_
Definition: Store.h:75
std::map< name_type, prep_type > reg_
Definition: Store.h:72
const std::complex< double > I
Definition: I.h:8
void clear()
#define N
Definition: blowfish.cc:9
double b
Definition: hdecay.h:120
rep_type< name_type, pimpl_type > Rep_type
Definition: Store.h:43
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
Store & operator=(const Store &)=delete
auto end()
Definition: Store.h:49
auto size() const
Definition: Store.h:50
bool isDefined(const name_type &n) const
Definition: Store.h:143
def move(src, dest)
Definition: eostools.py:511