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 
91 
92  mutable std::mutex mutex_;
93  collection_type data_;
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) {
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
static boost::mutex mutex
Definition: Proxy.cc:9
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)
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)
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.
ThreadSafeRegistry< KEY, T > & operator=(ThreadSafeRegistry< KEY, T > const &)=delete
long double T