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