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 {
28 public:
29  typedef Item value_type;
30  typedef std::array<std::unique_ptr<Item>,N> InputArray;
31  static constexpr unsigned arraySize() {return N;}
32 
33  // The following method adds a new array of pointers to the collection.
34  // This class will take ownership of the pointee objects.
35  void push_back(InputArray& arr)
36  {
37  StoredArray st;
38  for (unsigned i=0; i<N; ++i)
39  st[i] = boost::shared_ptr<Item>(arr[i].release());
40  data_.push_back(st);
41  }
42 
43  // Other modifiers
44  inline void clear() {data_.clear();}
45  inline void reserve(const unsigned n) {data_.reserve(n);}
46 
47  // Some inspectors
48  inline std::size_t size() const {return data_.size();}
49  inline bool empty() const {return data_.empty();}
50 
51  // The following function returns nullptr if
52  // one of the argument indices is out of range
53  inline const Item* get(const unsigned itemIndex,
54  const unsigned arrayIndex) const
55  {
56  if (itemIndex < data_.size() && arrayIndex < N)
57  return data_[itemIndex][arrayIndex].get();
58  else
59  return nullptr;
60  }
61 
62  // The following function throws an exception if
63  // one of the argument indices is out of range
64  inline Item& at(const unsigned itemIndex, const unsigned arrayIndex) const
65  {return *data_.at(itemIndex).at(arrayIndex);}
66 
67  // Deep comparison for equality is useful for testing serialization
68  bool operator==(const HcalItemArrayColl& r) const
69  {
70  const std::size_t sz = data_.size();
71  if (sz != r.data_.size())
72  return false;
73  for (std::size_t i=0; i<sz; ++i)
74  for (unsigned j=0; j<N; ++j)
75  if (!(*data_[i][j] == *r.data_[i][j]))
76  return false;
77  return true;
78  }
79 
80  inline bool operator!=(const HcalItemArrayColl& r) const
81  {return !(*this == r);}
82 
83 private:
84  typedef boost::array<boost::shared_ptr<Item>,N> StoredArray;
85  std::vector<StoredArray> data_;
86 
88 
89  template<class Archive>
90  inline void serialize(Archive & ar, unsigned /* version */)
91  {
92  ar & data_;
93  }
94 };
95 
96 // boost serialization version number for this template
97 namespace boost {
98  namespace serialization {
99  template<typename Item, unsigned N>
100  struct version<HcalItemArrayColl<Item,N> >
101  {
102  BOOST_STATIC_CONSTANT(int, value = 1);
103  };
104  }
105 }
106 
107 #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)