CMS 3D CMS Logo

StorableDoubleMap.h
Go to the documentation of this file.
1 #ifndef CondFormats_HcalObjects_StorableDoubleMap_h
2 #define CondFormats_HcalObjects_StorableDoubleMap_h
3 
4 #include <string>
5 #include <memory>
7 
8 #include "boost/serialization/map.hpp"
9 
10 template <typename T>
12 public:
13  typedef T value_type;
14 
15  inline ~StorableDoubleMap() { clear(); }
16 
17  inline void add(const std::string& name, const std::string& category, std::unique_ptr<T> ptr) {
18  delete data_[category][name];
19  data_[category][name] = ptr.release();
20  }
21 
22  void clear();
23 
24  inline bool empty() const { return data_.empty(); }
25 
26  const T* get(const std::string& name, const std::string& category) const;
27 
28  bool exists(const std::string& name, const std::string& category) const;
29 
30  bool operator==(const StorableDoubleMap& r) const;
31 
32  inline bool operator!=(const StorableDoubleMap& r) const { return !(*this == r); }
33 
34 private:
35  typedef std::map<std::string, T*> PtrMap;
36  typedef std::map<std::string, PtrMap> DataMap;
37  DataMap data_;
38 
39  friend class boost::serialization::access;
40 
41  template <class Archive>
42  inline void serialize(Archive& ar, unsigned /* version */) {
43  ar& data_;
44  }
45 };
46 
47 template <typename T>
49  const typename DataMap::iterator end = data_.end();
50  for (typename DataMap::iterator dit = data_.begin(); dit != end; ++dit) {
51  const typename PtrMap::iterator pend = dit->second.end();
52  for (typename PtrMap::iterator pit = dit->second.begin(); pit != pend; ++pit)
53  delete pit->second;
54  }
55  data_.clear();
56 }
57 
58 template <typename T>
60  typename DataMap::const_iterator dit = data_.find(category);
61  if (dit == data_.end())
62  return false;
63  else
64  return !(dit->second.find(name) == dit->second.end());
65 }
66 
67 template <typename T>
69  typename DataMap::const_iterator dit = data_.find(category);
70  if (dit == data_.end())
71  throw cms::Exception("In StorableDoubleMap::get: unknown category");
72  typename PtrMap::const_iterator pit = dit->second.find(name);
73  if (pit == dit->second.end())
74  throw cms::Exception("In StorableDoubleMap::get: unknown name");
75  return pit->second;
76 }
77 
78 template <typename T>
80  if (data_.size() != r.data_.size())
81  return false;
82  typename DataMap::const_iterator dit = data_.begin();
83  const typename DataMap::const_iterator end = data_.end();
84  typename DataMap::const_iterator rit = r.data_.begin();
85  for (; dit != end; ++dit, ++rit) {
86  if (dit->first != rit->first)
87  return false;
88  if (dit->second.size() != rit->second.size())
89  return false;
90  typename PtrMap::const_iterator pit = dit->second.begin();
91  const typename PtrMap::const_iterator pend = dit->second.end();
92  typename PtrMap::const_iterator rpit = rit->second.begin();
93  for (; pit != pend; ++pit, ++rpit) {
94  if (pit->first != rpit->first)
95  return false;
96  if (*(pit->second) != *(rpit->second))
97  return false;
98  }
99  }
100  return true;
101 }
102 
103 #endif // CondFormats_HcalObjects_StorableDoubleMap_h
void serialize(Archive &ar, unsigned)
bool operator==(const StorableDoubleMap &r) const
void add(const std::string &name, const std::string &category, std::unique_ptr< T > ptr)
bool empty() const
#define end
Definition: vmac.h:39
bool exists(const std::string &name, const std::string &category) const
bool operator!=(const StorableDoubleMap &r) const
std::map< std::string, T * > PtrMap
const T * get(const std::string &name, const std::string &category) const
std::map< std::string, PtrMap > DataMap
long double T