CMS 3D CMS Logo

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