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