Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:35:39

0001 #ifndef CondFormats_HcalObjects_StorableDoubleMap_h
0002 #define CondFormats_HcalObjects_StorableDoubleMap_h
0003 
0004 #include "FWCore/Utilities/interface/Exception.h"
0005 
0006 #include "boost/serialization/map.hpp"
0007 
0008 #include <string>
0009 #include <memory>
0010 #include <map>
0011 
0012 template <typename T>
0013 class StorableDoubleMap {
0014 public:
0015   typedef T value_type;
0016 
0017   inline ~StorableDoubleMap() { clear(); }
0018 
0019   inline void add(const std::string& name, const std::string& category, std::unique_ptr<T> ptr) {
0020     delete data_[category][name];
0021     data_[category][name] = ptr.release();
0022   }
0023 
0024   void clear();
0025 
0026   inline bool empty() const { return data_.empty(); }
0027 
0028   const T* get(const std::string& name, const std::string& category) const;
0029 
0030   bool exists(const std::string& name, const std::string& category) const;
0031 
0032   bool operator==(const StorableDoubleMap& r) const;
0033 
0034   inline bool operator!=(const StorableDoubleMap& r) const { return !(*this == r); }
0035 
0036 private:
0037   typedef std::map<std::string, T*> PtrMap;
0038   typedef std::map<std::string, PtrMap> DataMap;
0039   DataMap data_;
0040 
0041   friend class boost::serialization::access;
0042 
0043   template <class Archive>
0044   inline void serialize(Archive& ar, unsigned /* version */) {
0045     ar & data_;
0046   }
0047 };
0048 
0049 template <typename T>
0050 void StorableDoubleMap<T>::clear() {
0051   const typename DataMap::iterator end = data_.end();
0052   for (typename DataMap::iterator dit = data_.begin(); dit != end; ++dit) {
0053     const typename PtrMap::iterator pend = dit->second.end();
0054     for (typename PtrMap::iterator pit = dit->second.begin(); pit != pend; ++pit)
0055       delete pit->second;
0056   }
0057   data_.clear();
0058 }
0059 
0060 template <typename T>
0061 bool StorableDoubleMap<T>::exists(const std::string& name, const std::string& category) const {
0062   typename DataMap::const_iterator dit = data_.find(category);
0063   if (dit == data_.end())
0064     return false;
0065   else
0066     return !(dit->second.find(name) == dit->second.end());
0067 }
0068 
0069 template <typename T>
0070 const T* StorableDoubleMap<T>::get(const std::string& name, const std::string& category) const {
0071   typename DataMap::const_iterator dit = data_.find(category);
0072   if (dit == data_.end())
0073     throw cms::Exception("In StorableDoubleMap::get: unknown category");
0074   typename PtrMap::const_iterator pit = dit->second.find(name);
0075   if (pit == dit->second.end())
0076     throw cms::Exception("In StorableDoubleMap::get: unknown name");
0077   return pit->second;
0078 }
0079 
0080 template <typename T>
0081 bool StorableDoubleMap<T>::operator==(const StorableDoubleMap& r) const {
0082   if (data_.size() != r.data_.size())
0083     return false;
0084   typename DataMap::const_iterator dit = data_.begin();
0085   const typename DataMap::const_iterator end = data_.end();
0086   typename DataMap::const_iterator rit = r.data_.begin();
0087   for (; dit != end; ++dit, ++rit) {
0088     if (dit->first != rit->first)
0089       return false;
0090     if (dit->second.size() != rit->second.size())
0091       return false;
0092     typename PtrMap::const_iterator pit = dit->second.begin();
0093     const typename PtrMap::const_iterator pend = dit->second.end();
0094     typename PtrMap::const_iterator rpit = rit->second.begin();
0095     for (; pit != pend; ++pit, ++rpit) {
0096       if (pit->first != rpit->first)
0097         return false;
0098       if (*(pit->second) != *(rpit->second))
0099         return false;
0100     }
0101   }
0102   return true;
0103 }
0104 
0105 #endif  // CondFormats_HcalObjects_StorableDoubleMap_h