CMS 3D CMS Logo

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