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_;
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
edm::detail::ThreadSafeRegistry::size_type
collection_type::size_type size_type
Definition: ThreadSafeRegistry.h:39
edm::detail::ThreadSafeRegistry::empty
bool empty() const
Return true if there are no contained value_type objects.
Definition: ThreadSafeRegistry.h:117
edm::detail::ThreadSafeRegistry::insertMapped
bool insertMapped(value_type const &v)
edm::detail::ThreadSafeRegistry::size
size_type size() const
Return the number of contained value_type objects.
Definition: ThreadSafeRegistry.h:128
edm::detail::ThreadSafeRegistry::~ThreadSafeRegistry
~ThreadSafeRegistry()
Definition: ThreadSafeRegistry.h:146
edm::detail::ThreadSafeRegistry::vector_type
std::vector< value_type > vector_type
Definition: ThreadSafeRegistry.h:41
edm::detail::ThreadSafeRegistry::collection_type
std::map< key_type, value_type > collection_type
Definition: ThreadSafeRegistry.h:38
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::detail::ThreadSafeRegistry::getMapped
bool getMapped(key_type const &k, value_type &result) const
edm::detail::ThreadSafeRegistry::mutex_
std::mutex mutex_
Definition: ThreadSafeRegistry.h:92
findQualityFiles.v
v
Definition: findQualityFiles.py:179
edm::detail::ThreadSafeRegistry
Definition: ThreadSafeRegistry.h:34
edm::detail::ThreadSafeRegistry::instance
static ThreadSafeRegistry * instance()
edm::detail::ThreadSafeRegistry::print
void print(std::ostream &os) const
Print the contents of this registry to the given ostream.
Definition: ThreadSafeRegistry.h:134
edm::detail::ThreadSafeRegistry::key_type
KEY key_type
Definition: ThreadSafeRegistry.h:36
trigger::size_type
uint16_t size_type
Definition: TriggerTypeDefs.h:18
edm::detail::ThreadSafeRegistry::value_type
T value_type
Definition: ThreadSafeRegistry.h:37
dqmdumpme.k
k
Definition: dqmdumpme.py:60
mutex
static boost::mutex mutex
Definition: Proxy.cc:9
edm::detail::ThreadSafeRegistry::insertCollection
void insertCollection(collection_type const &c)
Definition: ThreadSafeRegistry.h:103
edm::detail::ThreadSafeRegistry::ThreadSafeRegistry
ThreadSafeRegistry()
Definition: ThreadSafeRegistry.h:143
B2GTnPMonitor_cfi.item
item
Definition: B2GTnPMonitor_cfi.py:147
HltBtagPostValidation_cff.c
c
Definition: HltBtagPostValidation_cff.py:31
edm::detail::ThreadSafeRegistry::notEmpty
bool notEmpty() const
Return true if there are any contained value_type objects.
Definition: ThreadSafeRegistry.h:123
T
long double T
Definition: Basic3DVectorLD.h:48
relativeConstraints.empty
bool empty
Definition: relativeConstraints.py:46
mps_fire.result
result
Definition: mps_fire.py:303
edm::detail::ThreadSafeRegistry::operator=
ThreadSafeRegistry< KEY, T > & operator=(ThreadSafeRegistry< KEY, T > const &)=delete
edm::detail::ThreadSafeRegistry::data_
collection_type data_
Definition: ThreadSafeRegistry.h:93
edm::detail::operator<<
std::ostream & operator<<(std::ostream &os, ThreadSafeRegistry< KEY, T > const &reg)
Definition: ThreadSafeRegistry.h:97
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443