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 I* pimpl_type;
43  typedef K key_type;
45  // direct way - no comfortable dereferencing of a pointer to rep_type
46  //typedef typename std::pair<name_type,pimpl_type> rep_type;
47  /*
48  struct rep_type {
49  rep_type(const name_type & n, pimpl_type p) : first(n), second(p) { }
50  key_type first;
51  pimpl_type second;
52  };
53  */
54  //typedef typename Rep_type<N,I> rep_type;
55 
56  typedef Rep_type* prep_type;
57 
58  typedef std::map<name_type,prep_type> registry_type;
59  typedef typename registry_type::iterator iterator;
60  typedef typename registry_type::const_iterator const_iterator;
62 
63  iterator begin() { return reg_.begin(); }
64  const_iterator begin() const { return reg_.begin(); }
65  iterator end() { return reg_.end(); }
66  const_iterator end() const { return reg_.end(); }
67  size_type size() const { return reg_.size(); }
68 
69  // empty shell or fetch from registry
70  prep_type create(const name_type &);
71 
72  // full new object or replace existing with new one
74 
75  // anonymous object, not stored
76  /* prep_type create(pimpl_type); */
77 
78  // clear all objects
79  void clear();
80 
81  // swap moves the registry from this guy to another of the same type
82  void swap ( Store& );
83 
84  bool isDefined(const name_type & n ) const;
85  void setReadOnly(bool b) { readOnly_ = b; }
86  bool readOnly() const { return readOnly_; }
87 
89  ~Store();
90 
91  protected:
93  Store(const Store &);
94  Store & operator=(const Store &);
95  bool readOnly_;
96  };
97 
98 
99  // rep_type as nested type inside Store, currently gives
100  // internal compiler error on gcc-2.95.2, but works on SUNWspro62Apr02Sun
101  //
102  // template<class N, class I, class K>
103  // Store<N,I,K>::rep_type::rep_type(const name_type & n, pimpl_type p)
104  // : std::pair(n,p)
105  // { }
106 
107  /* template<class N, class I, class K> */
108  /* void */
109  /* Store<N,I,K>::clear() */
110  /* { */
111  /* typename registry_type::iterator it = reg_.begin(); */
112  /* for (; it != reg_.end(); ++it) { */
113  /* delete it->second->second; */
114  /* delete it->second; */
115  /* } */
116  /* reg_.clear(); */
117  /* } */
118 
119  template<class N, class I, class K>
120  typename Store<N,I,K>::prep_type
122  {
123  prep_type tmp = 0;
124  std::pair<typename registry_type::iterator,bool> result
125  = reg_.insert(std::make_pair(n,tmp));
126  if (result.second) {
127  if (readOnly_) throw cms::Exception("DetectorDescriptionStore")<<" Store has been locked. Illegal attempt to add " << n << " to a global store.";
128  // ELSE
129  result.first->second = new Rep_type(n,(I)0);
130  }
131  return result.first->second;
132  }
133 
134 
135  template<class N, class I, class K>
136  typename Store<N,I,K>::prep_type
138  pimpl_type p)
139  {
140  if (readOnly_) throw cms::Exception("DetectorDescriptionStore")<<" Store has been locked. Illegal attempt to add " << n << " to a global store.";
141  // ELSE
142  prep_type tmp = 0;
143  std::pair<typename registry_type::iterator,bool> result
144  = reg_.insert(std::make_pair(n,tmp));
145  if (!result.second) {
146  delete result.first->second->second;
147  result.first->second->second = p;
148  //delete result.first->second->swap(p);
149  }
150  else {
151  result.first->second = new Rep_type(n,p);
152  }
153  return result.first->second;
154  }
155 
156  template<class N, class I, class K>
158  {
159  typename registry_type::iterator it = reg_.begin();
160  for(; it != reg_.end(); ++it) {
161  // std::cout << "deleting " << it->first << std::endl;
162  delete it->second->second; it->second->second = 0;
163  delete it->second; it->second = 0;
164  }
165  }
166 
167  /* template<class N, class I, class K> */
168  /* typename Store<N,I,K>::prep_type */
169  /* Store<N,I,K>::create(typename Store<N,I,K>::pimpl_type p) */
170  /* { */
171  /* if (readOnly_) throw cms::Exception("DetectorDescriptionStore")<<" Store has been locked. Illegal attempt to add " << name_type() << " to a global store."; */
172  /* return new Rep_type(name_type(),p); */
173  /* } */
174 
175  template<class N, class I, class K>
176  bool Store<N,I,K>::isDefined(const name_type & n ) const
177  {
178  if (readOnly_) edm::LogWarning("DetectorDescriptionStore") << " Store is locked and most likely empty. isDefined will be false.";
179  typename registry_type::const_iterator it = reg_.find(n);
180  bool result(false);
181  if (it != reg_.end()) {
182  if (it->second->second) {
183  result=true;
184  }
185  }
186  return result;
187  }
188 
189  template<class N, class I, class K>
190  void Store<N, I, K>::swap ( Store<N, I, K>& storeToSwap ) {
191  reg_.swap(storeToSwap.reg_);
192  storeToSwap.readOnly_ = readOnly_;
193  }
194 
195 } // namespace DDI
196 
197 #endif
Store()
Definition: Store.h:88
void swap(Store &)
Definition: Store.h:190
~Store()
Definition: Store.h:157
iterator begin()
Definition: Store.h:63
bool readOnly() const
Definition: Store.h:86
Store & operator=(const Store &)
Rep_type * prep_type
Definition: Store.h:56
registry_type::iterator iterator
Definition: Store.h:59
K key_type
Definition: Store.h:43
uint16_t size_type
prep_type create(const name_type &)
Definition: Store.h:121
std::map< name_type, prep_type > registry_type
Definition: Store.h:58
void setReadOnly(bool b)
Definition: Store.h:85
bool readOnly_
Definition: Store.h:95
rep_type< name_type, pimpl_type > Rep_type
Definition: Store.h:44
I pimpl_type
Definition: Store.h:41
const_iterator begin() const
Definition: Store.h:64
tuple result
Definition: query.py:137
const std::complex< double > I
Definition: I.h:8
registry_type reg_
Definition: Store.h:92
void clear()
#define N
Definition: blowfish.cc:9
registry_type::size_type size_type
Definition: Store.h:61
iterator end()
Definition: Store.h:65
double b
Definition: hdecay.h:120
N name_type
Definition: Store.h:40
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
const_iterator end() const
Definition: Store.h:66
registry_type::const_iterator const_iterator
Definition: Store.h:60
size_type size() const
Definition: Store.h:67
bool isDefined(const name_type &n) const
Definition: Store.h:176