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 
45  // The following two are not implemented.
47 
49 
55  bool getMapped(key_type const& k, value_type& result) const;
56 
60  value_type const* getMapped(key_type const& k) const;
61 
69  bool insertMapped(value_type const& v);
70 
73  void insertCollection(collection_type const& c);
74  void insertCollection(vector_type const& c);
75 
77  bool empty() const;
78 
80  bool notEmpty() const;
81 
83  size_type size() const;
84 
86  void print(std::ostream& os) const;
87 
88  private:
91 
92  mutable std::mutex mutex_;
94  };
95 
96  template <typename KEY, typename T, typename E>
97  inline std::ostream& operator<<(std::ostream& os, ThreadSafeRegistry<KEY, T> const& reg) {
98  reg.print(os);
99  return os;
100  }
101 
102  template <typename KEY, typename T>
104  for (auto const& item : c) {
105  insertMapped(item.second);
106  }
107  }
108 
109  template <typename KEY, typename T>
111  for (auto const& item : c) {
112  insertMapped(item);
113  }
114  }
115 
116  template <typename KEY, typename T>
117  inline bool ThreadSafeRegistry<KEY, T>::empty() const {
118  std::lock_guard<std::mutex> guard(mutex_);
119  return data_.empty();
120  }
121 
122  template <typename KEY, typename T>
124  return !empty();
125  }
126 
127  template <typename KEY, typename T>
129  std::lock_guard<std::mutex> guard(mutex_);
130  return data_.size();
131  }
132 
133  template <typename KEY, typename T>
134  void ThreadSafeRegistry<KEY, T>::print(std::ostream& os) const {
135  std::lock_guard<std::mutex> guard(mutex_);
136  os << "Registry with " << size() << " entries\n";
137  for (auto const& item : data_) {
138  os << item.first << " " << item.second << '\n';
139  }
140  }
141 
142  template <typename KEY, typename T>
144 
145  template <typename KEY, typename T>
147 
148  } // namespace detail
149 } // namespace edm
150 #pragma GCC visibility pop
151 #endif // FWCore_Utilities_ThreadSafeRegistry_h
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)
size_type size() const
Return the number of contained value_type objects.
std::vector< value_type > vector_type
bool getMapped(key_type const &k, value_type &result) const
static std::mutex mutex
Definition: Proxy.cc:8
uint16_t size_type
static ThreadSafeRegistry * instance()
void print(std::ostream &os) const
Print the contents of this registry to the given ostream.
void insertCollection(collection_type const &c)
bool notEmpty() const
Return true if there are any contained value_type objects.
HLT enums.
ThreadSafeRegistry< KEY, T > & operator=(ThreadSafeRegistry< KEY, T > const &)=delete
long double T