CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/FWCore/Utilities/interface/ThreadSafeIndexedRegistry.h

Go to the documentation of this file.
00001 #ifndef FWCore_Utilities_ThreadSafeIndexedRegistry_h
00002 #define FWCore_Utilities_ThreadSafeIndexedRegistry_h
00003 
00004 #include <vector>
00005 #include "boost/thread.hpp"
00006 
00007 // ----------------------------------------------------------------------
00008 
00016 // ----------------------------------------------------------------------
00017 
00018 #pragma GCC visibility push(default)
00019 namespace edm {
00020   namespace detail {
00021     struct Empty { };
00022 
00023     template <typename T, typename E=Empty>
00024     class ThreadSafeIndexedRegistry {
00025     public:
00026       typedef T     value_type;
00027       typedef E     extra_type;
00028       typedef typename std::vector<value_type> collection_type;
00029       typedef typename collection_type::size_type      size_type;
00030 
00031       typedef typename collection_type::const_iterator const_iterator;
00032 
00033       static ThreadSafeIndexedRegistry* instance();
00034 
00040       void getMapped(size_type index, value_type& result) const;
00041 
00044       bool insertMapped(value_type const& v);
00045 
00048       void insertCollection(collection_type const& c);
00049 
00051       bool empty() const;
00052 
00054       bool notEmpty() const;
00055 
00057       size_type size() const;
00058 
00061       const_iterator begin() const;
00062       const_iterator end() const;
00063 
00065       void print(std::ostream& os) const;
00066 
00068       collection_type& data();
00069       collection_type const& data() const;
00070 
00074       extra_type& extra();
00075       extra_type const& extra() const;      
00076 
00077     private:
00078       ThreadSafeIndexedRegistry();
00079       ~ThreadSafeIndexedRegistry();
00080 
00081       // The following two are not implemented.
00082       ThreadSafeIndexedRegistry(ThreadSafeIndexedRegistry<T, E> const&); 
00083     
00084       ThreadSafeIndexedRegistry<T, E>& 
00085       operator= (ThreadSafeIndexedRegistry<T, E> const&);
00086 
00087       collection_type data_;
00088       extra_type      extra_;
00089 
00090     };
00091 
00092     template <typename T, typename E>
00093     inline
00094     std::ostream&
00095     operator<< (std::ostream& os, ThreadSafeIndexedRegistry<T, E> const& reg) {
00096       reg.print(os);
00097       return os;
00098     }
00099 
00100     template <typename T, typename E>
00101     void 
00102     ThreadSafeIndexedRegistry<T, E>::insertCollection(collection_type const& c) {
00103       for (typename collection_type::const_iterator it = c.begin(), itEnd = c.end(); it != itEnd; ++it) {
00104         insertMapped(*it);
00105       }
00106     }
00107 
00108     template <typename T, typename E>
00109     inline
00110     bool
00111     ThreadSafeIndexedRegistry<T, E>::empty() const {
00112       return data_.empty();
00113     }
00114     
00115     template <typename T, typename E>
00116     inline
00117     bool
00118     ThreadSafeIndexedRegistry<T, E>::notEmpty() const {
00119       return !empty();
00120     }
00121 
00122     template <typename T, typename E>
00123     inline
00124     typename ThreadSafeIndexedRegistry<T, E>::size_type
00125     ThreadSafeIndexedRegistry<T, E>::size() const {
00126       return data_.size();
00127     }
00128 
00129     template <typename T, typename E>
00130     inline
00131     typename ThreadSafeIndexedRegistry<T, E>::const_iterator
00132     ThreadSafeIndexedRegistry<T, E>::begin() const {
00133       return data_.begin();
00134     }
00135 
00136     template <typename T, typename E>
00137     inline
00138     typename ThreadSafeIndexedRegistry<T, E>::const_iterator
00139     ThreadSafeIndexedRegistry<T, E>::end() const {
00140       return data_.end();
00141     }
00142     
00143     template <typename T, typename E>
00144     void
00145     ThreadSafeIndexedRegistry<T, E>::print(std::ostream& os) const {
00146       os << "Registry with " << size() << " entries\n";
00147       for (const_iterator i = begin(), e = end(); i != e; ++i) {
00148           os << i - begin() << " " << i << '\n';
00149       }
00150     }
00151 
00152     template <typename T, typename E>
00153     inline
00154     typename ThreadSafeIndexedRegistry<T, E>::collection_type&
00155     ThreadSafeIndexedRegistry<T, E>::data() {
00156       return data_;
00157     }
00158 
00159     template <typename T, typename E>
00160     inline
00161     typename ThreadSafeIndexedRegistry<T, E>::extra_type&
00162     ThreadSafeIndexedRegistry<T, E>::extra() {
00163       return extra_;
00164     }
00165 
00166     template <typename T, typename E>
00167     inline
00168     typename ThreadSafeIndexedRegistry<T, E>::extra_type const&
00169     ThreadSafeIndexedRegistry<T, E>::extra() const {
00170       return extra_;
00171     }
00172 
00173     template <typename T, typename E>
00174     inline
00175     typename ThreadSafeIndexedRegistry<T, E>::collection_type const&
00176     ThreadSafeIndexedRegistry<T, E>::data() const {
00177       return data_;
00178     }
00179 
00180     template <typename T, typename E> 
00181     ThreadSafeIndexedRegistry<T, E>::ThreadSafeIndexedRegistry() : 
00182       data_()
00183     { }
00184 
00185 
00186     template <typename T, typename E> 
00187     ThreadSafeIndexedRegistry<T, E>::~ThreadSafeIndexedRegistry() 
00188     { }
00189 
00190   } // namespace detail
00191 } // namespace edm
00192 #pragma GCC visibility pop
00193 
00194 #endif //  FWCore_Utilities_ThreadSafeIndexedRegistry_h