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 {
13 public:
14  typedef T value_type;
15 
16  inline ~StorableDoubleMap() {clear();}
17 
18  inline void add(const std::string& name, const std::string& category,
19  std::auto_ptr<T> ptr)
20  {delete data_[category][name]; data_[category][name] = ptr.release();}
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
33  {return !(*this == r);}
34 
35 private:
36  typedef std::map<std::string, T*> PtrMap;
37  typedef std::map<std::string, PtrMap> DataMap;
38  DataMap data_;
39 
40  friend class boost::serialization::access;
41 
42  template<class Archive>
43  inline void serialize(Archive & ar, unsigned /* version */)
44  {
45  ar & data_;
46  }
47 };
48 
49 
50 template<typename T>
52 {
53  const typename DataMap::iterator end = data_.end();
54  for (typename DataMap::iterator dit = data_.begin(); dit != end; ++dit)
55  {
56  const typename PtrMap::iterator pend = dit->second.end();
57  for (typename PtrMap::iterator pit = dit->second.begin();
58  pit != pend; ++pit)
59  delete pit->second;
60  }
61  data_.clear();
62 }
63 
64 template<typename T>
66  const std::string& category) const
67 {
68  typename DataMap::const_iterator dit = data_.find(category);
69  if (dit == data_.end())
70  return false;
71  else
72  return !(dit->second.find(name) == dit->second.end());
73 }
74 
75 template<typename T>
77  const std::string& category) const
78 {
79  typename DataMap::const_iterator dit = data_.find(category);
80  if (dit == data_.end()) throw cms::Exception(
81  "In StorableDoubleMap::get: unknown category");
82  typename PtrMap::const_iterator pit = dit->second.find(name);
83  if (pit == dit->second.end()) throw cms::Exception(
84  "In StorableDoubleMap::get: unknown name");
85  return pit->second;
86 }
87 
88 template<typename T>
90 {
91  if (data_.size() != r.data_.size())
92  return false;
93  typename DataMap::const_iterator dit = data_.begin();
94  const typename DataMap::const_iterator end = data_.end();
95  typename DataMap::const_iterator rit = r.data_.begin();
96  for (; dit != end; ++dit, ++rit)
97  {
98  if (dit->first != rit->first)
99  return false;
100  if (dit->second.size() != rit->second.size())
101  return false;
102  typename PtrMap::const_iterator pit = dit->second.begin();
103  const typename PtrMap::const_iterator pend = dit->second.end();
104  typename PtrMap::const_iterator rpit = rit->second.begin();
105  for (; pit != pend; ++pit, ++rpit)
106  {
107  if (pit->first != rpit->first)
108  return false;
109  if (*(pit->second) != *(rpit->second))
110  return false;
111  }
112  }
113  return true;
114 }
115 
116 #endif // CondFormats_HcalObjects_StorableDoubleMap_h
void serialize(Archive &ar, unsigned)
void add(const std::string &name, const std::string &category, std::auto_ptr< T > ptr)
bool operator==(const StorableDoubleMap &r) const
bool empty() const
#define end
Definition: vmac.h:37
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