CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ThreadSafeRegistry.h
Go to the documentation of this file.
1 #ifndef FWCore_Utilities_ThreadSafeRegistry_h
2 #define FWCore_Utilities_ThreadSafeRegistry_h
3 
4 #include <map>
5 #include <vector>
6 #include "boost/thread.hpp"
7 
8 // ----------------------------------------------------------------------
9 
26 // ----------------------------------------------------------------------
27 
28 namespace edm {
29  namespace detail {
30  struct empty { };
31 
32  template <typename KEY, typename T, typename E=empty>
34  public:
35  typedef KEY key_type;
36  typedef T value_type;
37  typedef E extra_type;
38  typedef typename std::map<key_type, value_type> collection_type;
40 
41  typedef typename collection_type::const_iterator const_iterator;
42 
43  typedef typename std::vector<value_type> vector_type;
44 
45  static ThreadSafeRegistry* instance();
46 
52  bool getMapped(key_type const& k, value_type& result) const;
53 
57  value_type const* getMapped(key_type const& k) const;
58 
66  bool insertMapped(value_type const& v);
67 
70  void insertCollection(collection_type const& c);
71  void insertCollection(vector_type const& c);
72 
74  bool empty() const;
75 
77  bool notEmpty() const;
78 
80  size_type size() const;
81 
84  const_iterator begin() const;
85  const_iterator end() const;
86 
88  void print(std::ostream& os) const;
89 
92  collection_type const& data() const;
93 
97  extra_type& extra();
98  extra_type const& extra() const;
99 
100  private:
103 
104  // The following two are not implemented.
106 
109 
112 
114 
116  };
117 
118  template <typename KEY, typename T, typename E>
119  inline
120  std::ostream&
121  operator<< (std::ostream& os, ThreadSafeRegistry<KEY,T,E> const& reg) {
122  reg.print(os);
123  return os;
124  }
125 
126  // ----------------------------------------------------------------------
127  // Declarations of static data for classes instantiated from the
128  // class template.
129 
130  template <typename KEY, typename T, typename E>
131  ThreadSafeRegistry<KEY,T,E>* ThreadSafeRegistry<KEY,T,E>::instance_ = 0;
132 
133  template <typename KEY, typename T, typename E>
134  boost::mutex ThreadSafeRegistry<KEY,T,E>::registry_mutex;
135 
136  // ----------------------------------------------------------------------
137 
138  template <typename KEY, typename T, typename E>
139  ThreadSafeRegistry<KEY,T,E>*
141  if (instance_ == 0) {
142  boost::mutex::scoped_lock lock(registry_mutex);
143  if (instance_ == 0) {
144  static ThreadSafeRegistry<KEY,T,E> me;
145  instance_ = &me;
146  }
147  }
148  return instance_;
149  }
150 
151  template <typename KEY, typename T, typename E>
152  bool
154  bool found;
156  {
157  // This scope limits the lifetime of the lock to the shorted
158  // required interval.
159  boost::mutex::scoped_lock lock(registry_mutex);
160  i = data_.find(k);
161  found = (i != data_.end());
162  }
163  if (found) result = i->second;
164  return found;
165  }
166 
167  template <typename KEY, typename T, typename E>
170  bool found;
172  {
173  // This scope limits the lifetime of the lock to the shorted
174  // required interval.
175  boost::mutex::scoped_lock lock(registry_mutex);
176  i = data_.find(k);
177  found = (i != data_.end());
178  }
179  return found ? &(i->second) : static_cast<value_type const*> (0);
180  }
181 
182  template <typename KEY, typename T, typename E>
183  bool
185  bool newly_added = false;
186  boost::mutex::scoped_lock lock(registry_mutex);
187 
188  key_type id = v.id();
189  if (data_.find(id) == data_.end()) {
190  data_[id] = v;
191  newly_added = true;
192  }
193  return newly_added;
194  }
195 
196  template <typename KEY, typename T, typename E>
197  void
199  for (typename collection_type::const_iterator it = c.begin(), itEnd = c.end(); it != itEnd; ++it) {
200  insertMapped(it->second);
201  }
202  }
203 
204  template <typename KEY, typename T, typename E>
205  void
207  for (typename vector_type::const_iterator it = c.begin(), itEnd = c.end(); it != itEnd; ++it) {
208  insertMapped(*it);
209  }
210  }
211 
212  template <typename KEY, typename T, typename E>
213  inline
214  bool
216  return data_.empty();
217  }
218 
219  template <typename KEY, typename T, typename E>
220  inline
221  bool
223  return !empty();
224  }
225 
226  template <typename KEY, typename T, typename E>
227  inline
230  return data_.size();
231  }
232 
233  template <typename KEY, typename T, typename E>
234  inline
237  return data_.begin();
238  }
239 
240  template <typename KEY, typename T, typename E>
241  inline
244  return data_.end();
245  }
246 
247  template <typename KEY, typename T, typename E>
248  void
249  ThreadSafeRegistry<KEY,T,E>::print(std::ostream& os) const {
250  os << "Registry with " << size() << " entries\n";
251  for (const_iterator i=begin(), e=end(); i!=e; ++i) {
252  os << i->first << " " << i->second << '\n';
253  }
254  }
255 
256  template <typename KEY, typename T, typename E>
257  inline
260  return data_;
261  }
262 
263  template <typename KEY, typename T, typename E>
264  inline
267  return extra_;
268  }
269 
270  template <typename KEY, typename T, typename E>
271  inline
274  return extra_;
275  }
276 
277  template <typename KEY, typename T, typename E>
278  inline
281  return data_;
282  }
283 
284  template <typename KEY, typename T, typename E>
286  data_()
287  { }
288 
289 
290  template <typename KEY, typename T, typename E>
292  { }
293 
294  } // namespace detail
295 } // namespace edm
296 
297 #endif // FWCore_Utilities_ThreadSafeRegistry_h
int i
Definition: DBlmapReader.cc:9
static boost::mutex mutex
Definition: LHEProxy.cc:11
bool empty() const
Return true if there are no contained value_type objects.
bool getMapped(key_type const &k, value_type &result) const
size_type size() const
Return the number of contained value_type objects.
bool insertMapped(value_type const &v)
uint16_t size_type
void print(std::ostream &os) const
Print the contents of this registry to the given ostream.
std::map< key_type, value_type > collection_type
collection_type::const_iterator const_iterator
tuple result
Definition: query.py:137
#define end
Definition: vmac.h:38
void insertCollection(collection_type const &c)
int k[5][pyjets_maxn]
ThreadSafeRegistry< KEY, T, E > & operator=(ThreadSafeRegistry< KEY, T, E > const &)
static ThreadSafeRegistry * instance_
#define begin
Definition: vmac.h:31
std::vector< value_type > vector_type
static ThreadSafeRegistry * instance()
collection_type & data()
Provide access to the contained collection.
long double T
tuple size
Write out results.
mathSSE::Vec4< T > v
collection_type::size_type size_type
bool notEmpty() const
Return true if there are any contained value_type objects.