CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
HcalItemColl.h
Go to the documentation of this file.
1 #ifndef CondFormats_HcalObjects_HcalItemColl_h
2 #define CondFormats_HcalObjects_HcalItemColl_h
3 
4 #include "boost/serialization/access.hpp"
5 #include "boost/serialization/version.hpp"
6 #include "boost/serialization/shared_ptr.hpp"
7 #include "boost/serialization/vector.hpp"
8 
9 #include <memory>
10 #include <vector>
11 
12 //
13 // This collection manages only pointers and references.
14 // In particular, it can be used for storing objects in
15 // an inheritance hierarchy by their base pointers.
16 // The pointee objects are owned by this collection.
17 // If copies of this collection are made, all copies
18 // will share the ownership of the same set of objects.
19 //
20 template <typename Item>
21 class HcalItemColl {
22 public:
23  typedef Item value_type;
24 
25  // The following method adds a new item to the collection
26  inline void push_back(std::unique_ptr<Item> ptr) { data_.push_back(std::shared_ptr<Item>(ptr.release())); }
27 
28  // Other modifiers
29  inline void clear() { data_.clear(); }
30  inline void reserve(const unsigned n) { data_.reserve(n); }
31 
32  // Some inspectors
33  inline std::size_t size() const { return data_.size(); }
34  inline bool empty() const { return data_.empty(); }
35 
36  // The following function returns nullptr if the index is out of range
37  inline const Item* get(const unsigned index) const {
38  if (index < data_.size())
39  return data_[index].get();
40  else
41  return nullptr;
42  }
43 
44  // The following function throws an exception if the index is out of range
45  inline const Item& at(const unsigned index) const { return *data_.at(index); }
46 
47  // Deep comparison for equality is useful for testing serialization
48  bool operator==(const HcalItemColl& r) const {
49  const std::size_t sz = data_.size();
50  if (sz != r.data_.size())
51  return false;
52  for (std::size_t i = 0; i < sz; ++i)
53  if (!(*data_[i] == *r.data_[i]))
54  return false;
55  return true;
56  }
57 
58  inline bool operator!=(const HcalItemColl& r) const { return !(*this == r); }
59 
60 private:
61  std::vector<std::shared_ptr<Item> > data_;
62 
63  friend class boost::serialization::access;
64 
65  template <class Archive>
66  inline void serialize(Archive& ar, unsigned /* version */) {
67  ar& data_;
68  }
69 };
70 
71 // boost serialization version number for this template
72 namespace boost {
73  namespace serialization {
74  template <typename Item>
75  struct version<HcalItemColl<Item> > {
76  BOOST_STATIC_CONSTANT(int, value = 1);
77  };
78  } // namespace serialization
79 } // namespace boost
80 
81 #endif // CondFormats_HcalObjects_HcalItemColl_h
void reserve(const unsigned n)
Definition: HcalItemColl.h:30
void serialize(Archive &ar, unsigned)
Definition: HcalItemColl.h:66
bool operator!=(const HcalItemColl &r) const
Definition: HcalItemColl.h:58
bool operator==(const HcalItemColl &r) const
Definition: HcalItemColl.h:48
std::vector< std::shared_ptr< Item > > data_
Definition: HcalItemColl.h:61
std::size_t size() const
Definition: HcalItemColl.h:33
void clear()
Definition: HcalItemColl.h:29
const Item & at(const unsigned index) const
Definition: HcalItemColl.h:45
bool empty() const
Definition: HcalItemColl.h:34
void push_back(std::unique_ptr< Item > ptr)
Definition: HcalItemColl.h:26