00001 #ifndef FWCore_Utilities_ThreadSafeRegistry_h
00002 #define FWCore_Utilities_ThreadSafeRegistry_h
00003
00004 #include <map>
00005 #include <vector>
00006 #include "boost/thread.hpp"
00007
00008
00009
00026
00027
00028 #pragma GCC visibility push(default)
00029 namespace edm {
00030 namespace detail {
00031 struct empty { };
00032
00033 template <typename KEY, typename T, typename E=empty>
00034 class ThreadSafeRegistry {
00035 public:
00036 typedef KEY key_type;
00037 typedef T value_type;
00038 typedef E extra_type;
00039 typedef typename std::map<key_type, value_type> collection_type;
00040 typedef typename collection_type::size_type size_type;
00041
00042 typedef typename collection_type::const_iterator const_iterator;
00043
00044 typedef typename std::vector<value_type> vector_type;
00045
00046 static ThreadSafeRegistry* instance();
00047
00053 bool getMapped(key_type const& k, value_type& result) const;
00054
00058 value_type const* getMapped(key_type const& k) const;
00059
00067 bool insertMapped(value_type const& v);
00068
00071 void insertCollection(collection_type const& c);
00072 void insertCollection(vector_type const& c);
00073
00075 bool empty() const;
00076
00078 bool notEmpty() const;
00079
00081 size_type size() const;
00082
00085 const_iterator begin() const;
00086 const_iterator end() const;
00087
00089 void print(std::ostream& os) const;
00090
00092 collection_type& data();
00093 collection_type const& data() const;
00094
00098 extra_type& extra();
00099 extra_type const& extra() const;
00100
00101 private:
00102 ThreadSafeRegistry();
00103 ~ThreadSafeRegistry();
00104
00105
00106 ThreadSafeRegistry(ThreadSafeRegistry<KEY,T,E> const&);
00107
00108 ThreadSafeRegistry<KEY,T,E>&
00109 operator= (ThreadSafeRegistry<KEY,T,E> const&);
00110
00111 collection_type data_;
00112 extra_type extra_;
00113 };
00114
00115 template <typename KEY, typename T, typename E>
00116 inline
00117 std::ostream&
00118 operator<< (std::ostream& os, ThreadSafeRegistry<KEY,T,E> const& reg) {
00119 reg.print(os);
00120 return os;
00121 }
00122
00123 template <typename KEY, typename T, typename E>
00124 void
00125 ThreadSafeRegistry<KEY,T,E>::insertCollection(collection_type const& c) {
00126 for (typename collection_type::const_iterator it = c.begin(), itEnd = c.end(); it != itEnd; ++it) {
00127 insertMapped(it->second);
00128 }
00129 }
00130
00131 template <typename KEY, typename T, typename E>
00132 void
00133 ThreadSafeRegistry<KEY,T,E>::insertCollection(vector_type const& c) {
00134 for (typename vector_type::const_iterator it = c.begin(), itEnd = c.end(); it != itEnd; ++it) {
00135 insertMapped(*it);
00136 }
00137 }
00138
00139 template <typename KEY, typename T, typename E>
00140 inline
00141 bool
00142 ThreadSafeRegistry<KEY,T,E>::empty() const {
00143 return data_.empty();
00144 }
00145
00146 template <typename KEY, typename T, typename E>
00147 inline
00148 bool
00149 ThreadSafeRegistry<KEY,T,E>::notEmpty() const {
00150 return !empty();
00151 }
00152
00153 template <typename KEY, typename T, typename E>
00154 inline
00155 typename ThreadSafeRegistry<KEY,T,E>::size_type
00156 ThreadSafeRegistry<KEY,T,E>::size() const {
00157 return data_.size();
00158 }
00159
00160 template <typename KEY, typename T, typename E>
00161 inline
00162 typename ThreadSafeRegistry<KEY,T,E>::const_iterator
00163 ThreadSafeRegistry<KEY,T,E>::begin() const {
00164 return data_.begin();
00165 }
00166
00167 template <typename KEY, typename T, typename E>
00168 inline
00169 typename ThreadSafeRegistry<KEY,T,E>::const_iterator
00170 ThreadSafeRegistry<KEY,T,E>::end() const {
00171 return data_.end();
00172 }
00173
00174 template <typename KEY, typename T, typename E>
00175 void
00176 ThreadSafeRegistry<KEY,T,E>::print(std::ostream& os) const {
00177 os << "Registry with " << size() << " entries\n";
00178 for (const_iterator i=begin(), e=end(); i!=e; ++i) {
00179 os << i->first << " " << i->second << '\n';
00180 }
00181 }
00182
00183 template <typename KEY, typename T, typename E>
00184 inline
00185 typename ThreadSafeRegistry<KEY,T,E>::collection_type&
00186 ThreadSafeRegistry<KEY,T,E>::data() {
00187 return data_;
00188 }
00189
00190 template <typename KEY, typename T, typename E>
00191 inline
00192 typename ThreadSafeRegistry<KEY,T,E>::extra_type&
00193 ThreadSafeRegistry<KEY,T,E>::extra() {
00194 return extra_;
00195 }
00196
00197 template <typename KEY, typename T, typename E>
00198 inline
00199 typename ThreadSafeRegistry<KEY,T,E>::extra_type const&
00200 ThreadSafeRegistry<KEY,T,E>::extra() const {
00201 return extra_;
00202 }
00203
00204 template <typename KEY, typename T, typename E>
00205 inline
00206 typename ThreadSafeRegistry<KEY,T,E>::collection_type const&
00207 ThreadSafeRegistry<KEY,T,E>::data() const {
00208 return data_;
00209 }
00210
00211 template <typename KEY, typename T, typename E>
00212 ThreadSafeRegistry<KEY,T,E>::ThreadSafeRegistry() :
00213 data_()
00214 { }
00215
00216
00217 template <typename KEY, typename T, typename E>
00218 ThreadSafeRegistry<KEY,T,E>::~ThreadSafeRegistry()
00219 { }
00220
00221 }
00222 }
00223 #pragma GCC visibility pop
00224 #endif // FWCore_Utilities_ThreadSafeRegistry_h