CMS 3D CMS Logo

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 <ostream>
7 #include <mutex>
8 
9 // ----------------------------------------------------------------------
10 
27 // ----------------------------------------------------------------------
28 
29 #pragma GCC visibility push(default)
30 namespace edm {
31  namespace detail {
32 
33  template <typename KEY, typename T>
35  public:
36  typedef KEY key_type;
37  typedef T value_type;
38  typedef typename std::map<key_type, value_type> collection_type;
40 
41  typedef typename std::vector<value_type> vector_type;
42 
43  static ThreadSafeRegistry* instance();
44 
50  bool getMapped(key_type const& k, value_type& result) const;
51 
55  value_type const* getMapped(key_type const& k) const;
56 
64  bool insertMapped(value_type const& v);
65 
68  void insertCollection(collection_type const& c);
69  void insertCollection(vector_type const& c);
70 
72  bool empty() const;
73 
75  bool notEmpty() const;
76 
78  size_type size() const;
79 
81  void print(std::ostream& os) const;
82 
83  private:
86 
87  // The following two are not implemented.
89 
92 
93  mutable std::mutex mutex_;
94  collection_type data_;
95  };
96 
97  template <typename KEY, typename T, typename E>
98  inline
99  std::ostream&
100  operator<< (std::ostream& os, ThreadSafeRegistry<KEY,T> const& reg) {
101  reg.print(os);
102  return os;
103  }
104 
105  template <typename KEY, typename T>
106  void
108  for (auto const& item: c ) {
109  insertMapped(item.second);
110  }
111  }
112 
113  template <typename KEY, typename T>
114  void
116  for (auto const& item: c) {
117  insertMapped(item);
118  }
119  }
120 
121  template <typename KEY, typename T>
122  inline
123  bool
125  std::lock_guard<std::mutex> guard(mutex_);
126  return data_.empty();
127  }
128 
129  template <typename KEY, typename T>
130  inline
131  bool
133  return !empty();
134  }
135 
136  template <typename KEY, typename T>
137  inline
140  std::lock_guard<std::mutex> guard(mutex_);
141  return data_.size();
142  }
143 
144  template <typename KEY, typename T>
145  void
146  ThreadSafeRegistry<KEY,T>::print(std::ostream& os) const {
147  std::lock_guard<std::mutex> guard(mutex_);
148  os << "Registry with " << size() << " entries\n";
149  for (auto const& item: data_) {
150  os << item.first << " " << item.second << '\n';
151  }
152  }
153 
154  template <typename KEY, typename T>
156  data_()
157  { }
158 
159 
160  template <typename KEY, typename T>
162  { }
163 
164  } // namespace detail
165 } // namespace edm
166 #pragma GCC visibility pop
167 #endif // FWCore_Utilities_ThreadSafeRegistry_h
size_type size() const
Return the number of contained value_type objects.
bool empty() const
Return true if there are no contained value_type objects.
std::map< key_type, value_type > collection_type
collection_type::size_type size_type
bool insertMapped(value_type const &v)
static boost::mutex mutex
Definition: LHEProxy.cc:11
std::vector< value_type > vector_type
uint16_t size_type
static ThreadSafeRegistry * instance()
bool getMapped(key_type const &k, value_type &result) const
void insertCollection(collection_type const &c)
ThreadSafeRegistry< KEY, T > & operator=(ThreadSafeRegistry< KEY, T > const &)
int k[5][pyjets_maxn]
void print(std::ostream &os) const
Print the contents of this registry to the given ostream.
bool notEmpty() const
Return true if there are any contained value_type objects.
HLT enums.
long double T