CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros 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  {
39  public:
40  typedef N name_type;
41  typedef I pimpl_type;
42  typedef K key_type;
44  typedef Rep_type* prep_type;
45  typedef std::map<name_type,prep_type> registry_type;
46  typedef typename registry_type::iterator 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 &);
74  Store & operator=(const Store &);
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 = 0;
83  auto result = reg_.insert(std::make_pair(n,tmp));
84  if (result.second) {
85  if (readOnly_) throw cms::Exception("DetectorDescriptionStore")<<" Store has been locked. Illegal attempt to add " << n << " to a global store.";
86  // ELSE
87  result.first->second = new Rep_type(n,(I)0);
88  }
89  return result.first->second;
90  }
91 
92  template<class N, class I, class K>
93  typename Store<N,I,K>::prep_type
95  pimpl_type p)
96  {
97  if (readOnly_) throw cms::Exception("DetectorDescriptionStore")<<" Store has been locked. Illegal attempt to add " << n << " to a global store.";
98  // ELSE
99  prep_type tmp = 0;
100  auto result = reg_.insert(std::make_pair(n,tmp));
101  if (!result.second) {
102  delete result.first->second->second;
103  result.first->second->second = p;
104  }
105  else {
106  result.first->second = new Rep_type(n,p);
107  }
108  return result.first->second;
109  }
110 
111  template<class N, class I, class K>
113  {
114  for( auto it : reg_ ) {
115  delete it.second->second;
116  it.second->second = 0;
117  delete it.second;
118  it.second = 0;
119  }
120  }
121 
122  template<class N, class I, class K>
123  bool Store<N,I,K>::isDefined(const name_type & n ) const
124  {
125  if (readOnly_) edm::LogWarning("DetectorDescriptionStore") << " Store is locked and most likely empty. isDefined will be false.";
126  auto it = reg_.find(n);
127  bool result(false);
128  if (it != reg_.end()) {
129  if (it.second->second) {
130  result=true;
131  }
132  }
133  return result;
134  }
135 
136  template<class N, class I, class K>
137  void Store<N, I, K>::swap ( Store<N, I, K>& storeToSwap ) {
138  reg_.swap(storeToSwap.reg_);
139  storeToSwap.readOnly_ = readOnly_;
140  }
141 
142 } // namespace DDI
143 
144 #endif
Store()
Definition: Store.h:68
void swap(Store &)
Definition: Store.h:137
auto begin()
Definition: Store.h:48
~Store()
Definition: Store.h:112
bool readOnly() const
Definition: Store.h:66
Store & operator=(const Store &)
Rep_type * prep_type
Definition: Store.h:44
registry_type::iterator iterator
Definition: Store.h:46
K key_type
Definition: Store.h:42
prep_type create(const name_type &)
Definition: Store.h:80
std::map< name_type, prep_type > registry_type
Definition: Store.h:45
void setReadOnly(bool b)
Definition: Store.h:65
bool readOnly_
Definition: Store.h:75
rep_type< name_type, pimpl_type > Rep_type
Definition: Store.h:43
I pimpl_type
Definition: Store.h:41
tuple result
Definition: query.py:137
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
N name_type
Definition: Store.h:40
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
auto end()
Definition: Store.h:49
auto size() const
Definition: Store.h:50
volatile std::atomic< bool > shutdown_flag false
bool isDefined(const name_type &n) const
Definition: Store.h:123