CMS 3D CMS Logo

HcalItemArrayColl.h
Go to the documentation of this file.
1 #ifndef CondFormats_HcalObjects_HcalItemArrayColl_h
2 #define CondFormats_HcalObjects_HcalItemArrayColl_h
3 
4 #include <memory>
5 #include <array>
6 
7 #include "boost/array.hpp"
8 #include "boost/serialization/access.hpp"
9 #include "boost/serialization/version.hpp"
10 #include "boost/serialization/shared_ptr.hpp"
11 #include "boost/serialization/vector.hpp"
12 #include "boost/serialization/array.hpp"
13 
14 //
15 // This collection manages arrays of pointers and references.
16 // In particular, it can be used for storing objects in
17 // an inheritance hierarchy by their base pointers.
18 // The pointee objects are owned by this collection.
19 //
20 template<typename Item, unsigned N>
22 {
23 public:
24  typedef Item value_type;
25  typedef std::array<std::unique_ptr<Item>,N> InputArray;
26  static constexpr unsigned arraySize() {return N;}
27 
28  // The following method adds a new array of pointers to the collection.
29  // This class will take ownership of the pointee objects.
30  void push_back(InputArray& arr)
31  {
32  StoredArray st;
33  for (unsigned i=0; i<N; ++i)
34  st[i] = boost::shared_ptr<Item>(arr[i].release());
35  data_.push_back(st);
36  }
37 
38  // Other modifiers
39  inline void clear() {data_.clear();}
40  inline void reserve(const unsigned n) {data_.reserve(n);}
41 
42  // Some inspectors
43  inline std::size_t size() const {return data_.size();}
44  inline bool empty() const {return data_.empty();}
45 
46  // The following function returns nullptr if
47  // one of the argument indices is out of range
48  inline const Item* get(const unsigned itemIndex,
49  const unsigned arrayIndex) const
50  {
51  if (itemIndex < data_.size() && arrayIndex < N)
52  return data_[itemIndex][arrayIndex].get();
53  else
54  return nullptr;
55  }
56 
57  // The following function throws an exception if
58  // one of the argument indices is out of range
59  inline Item& at(const unsigned itemIndex, const unsigned arrayIndex) const
60  {return *data_.at(itemIndex).at(arrayIndex);}
61 
62  // Deep comparison for equality is useful for testing serialization
63  bool operator==(const HcalItemArrayColl& r) const
64  {
65  const std::size_t sz = data_.size();
66  if (sz != r.data_.size())
67  return false;
68  for (std::size_t i=0; i<sz; ++i)
69  for (unsigned j=0; j<N; ++j)
70  if (!(*data_[i][j] == *r.data_[i][j]))
71  return false;
72  return true;
73  }
74 
75  inline bool operator!=(const HcalItemArrayColl& r) const
76  {return !(*this == r);}
77 
78 private:
79  typedef boost::array<boost::shared_ptr<Item>,N> StoredArray;
80  std::vector<StoredArray> data_;
81 
83 
84  template<class Archive>
85  inline void serialize(Archive & ar, unsigned /* version */)
86  {
87  ar & data_;
88  }
89 };
90 
91 // boost serialization version number for this template
92 namespace boost {
93  namespace serialization {
94  template<typename Item, unsigned N>
95  struct version<HcalItemArrayColl<Item,N> >
96  {
97  BOOST_STATIC_CONSTANT(int, value = 1);
98  };
99  }
100 }
101 
102 #endif // CondFormats_HcalObjects_HcalItemArrayColl_h
Definition: CLHEP.h:16
friend class boost::serialization::access
void reserve(const unsigned n)
bool operator==(const HcalItemArrayColl &r) const
#define constexpr
static constexpr unsigned arraySize()
bool empty() const
bool operator!=(const HcalItemArrayColl &r) const
std::vector< StoredArray > data_
Definition: value.py:1
Item & at(const unsigned itemIndex, const unsigned arrayIndex) const
void push_back(InputArray &arr)
#define N
Definition: blowfish.cc:9
std::size_t size() const
std::array< std::unique_ptr< Item >, N > InputArray
boost::array< boost::shared_ptr< Item >, N > StoredArray
void serialize(Archive &ar, unsigned)