File indexing completed on 2024-04-06 12:13:13
0001 #ifndef FWCore_Utilities_ThreadSafeRegistry_h
0002 #define FWCore_Utilities_ThreadSafeRegistry_h
0003
0004 #include <map>
0005 #include <vector>
0006 #include <ostream>
0007 #include <mutex>
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 #pragma GCC visibility push(default)
0030 namespace edm {
0031 namespace detail {
0032
0033 template <typename KEY, typename T>
0034 class ThreadSafeRegistry {
0035 public:
0036 typedef KEY key_type;
0037 typedef T value_type;
0038 typedef typename std::map<key_type, value_type> collection_type;
0039 typedef typename collection_type::size_type size_type;
0040
0041 typedef typename std::vector<value_type> vector_type;
0042
0043 static ThreadSafeRegistry* instance();
0044
0045
0046 ThreadSafeRegistry(ThreadSafeRegistry<KEY, T> const&) = delete;
0047
0048 ThreadSafeRegistry<KEY, T>& operator=(ThreadSafeRegistry<KEY, T> const&) = delete;
0049
0050
0051
0052
0053
0054
0055 bool getMapped(key_type const& k, value_type& result) const;
0056
0057
0058
0059
0060 value_type const* getMapped(key_type const& k) const;
0061
0062
0063
0064
0065
0066
0067
0068
0069 bool insertMapped(value_type const& v);
0070
0071
0072
0073 void insertCollection(collection_type const& c);
0074 void insertCollection(vector_type const& c);
0075
0076
0077 bool empty() const;
0078
0079
0080 bool notEmpty() const;
0081
0082
0083 size_type size() const;
0084
0085
0086 void print(std::ostream& os) const;
0087
0088 private:
0089 ThreadSafeRegistry();
0090 ~ThreadSafeRegistry();
0091
0092 mutable std::mutex mutex_;
0093 collection_type data_;
0094 };
0095
0096 template <typename KEY, typename T, typename E>
0097 inline std::ostream& operator<<(std::ostream& os, ThreadSafeRegistry<KEY, T> const& reg) {
0098 reg.print(os);
0099 return os;
0100 }
0101
0102 template <typename KEY, typename T>
0103 void ThreadSafeRegistry<KEY, T>::insertCollection(collection_type const& c) {
0104 for (auto const& item : c) {
0105 insertMapped(item.second);
0106 }
0107 }
0108
0109 template <typename KEY, typename T>
0110 void ThreadSafeRegistry<KEY, T>::insertCollection(vector_type const& c) {
0111 for (auto const& item : c) {
0112 insertMapped(item);
0113 }
0114 }
0115
0116 template <typename KEY, typename T>
0117 inline bool ThreadSafeRegistry<KEY, T>::empty() const {
0118 std::lock_guard<std::mutex> guard(mutex_);
0119 return data_.empty();
0120 }
0121
0122 template <typename KEY, typename T>
0123 inline bool ThreadSafeRegistry<KEY, T>::notEmpty() const {
0124 return !empty();
0125 }
0126
0127 template <typename KEY, typename T>
0128 inline typename ThreadSafeRegistry<KEY, T>::size_type ThreadSafeRegistry<KEY, T>::size() const {
0129 std::lock_guard<std::mutex> guard(mutex_);
0130 return data_.size();
0131 }
0132
0133 template <typename KEY, typename T>
0134 void ThreadSafeRegistry<KEY, T>::print(std::ostream& os) const {
0135 std::lock_guard<std::mutex> guard(mutex_);
0136 os << "Registry with " << size() << " entries\n";
0137 for (auto const& item : data_) {
0138 os << item.first << " " << item.second << '\n';
0139 }
0140 }
0141
0142 template <typename KEY, typename T>
0143 ThreadSafeRegistry<KEY, T>::ThreadSafeRegistry() : data_() {}
0144
0145 template <typename KEY, typename T>
0146 ThreadSafeRegistry<KEY, T>::~ThreadSafeRegistry() {}
0147
0148 }
0149 }
0150 #pragma GCC visibility pop
0151 #endif