Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:35:38

0001 #ifndef CondFormats_HcalObjects_HcalItemColl_h
0002 #define CondFormats_HcalObjects_HcalItemColl_h
0003 
0004 #include "boost/serialization/access.hpp"
0005 #include "boost/serialization/version.hpp"
0006 #include "boost/serialization/shared_ptr.hpp"
0007 #include "boost/serialization/vector.hpp"
0008 
0009 #include <memory>
0010 #include <vector>
0011 
0012 //
0013 // This collection manages only pointers and references.
0014 // In particular, it can be used for storing objects in
0015 // an inheritance hierarchy by their base pointers.
0016 // The pointee objects are owned by this collection.
0017 // If copies of this collection are made, all copies
0018 // will share the ownership of the same set of objects.
0019 //
0020 template <typename Item>
0021 class HcalItemColl {
0022 public:
0023   typedef Item value_type;
0024 
0025   // The following method adds a new item to the collection
0026   inline void push_back(std::unique_ptr<Item> ptr) { data_.push_back(std::shared_ptr<Item>(ptr.release())); }
0027 
0028   // Other modifiers
0029   inline void clear() { data_.clear(); }
0030   inline void reserve(const unsigned n) { data_.reserve(n); }
0031 
0032   // Some inspectors
0033   inline std::size_t size() const { return data_.size(); }
0034   inline bool empty() const { return data_.empty(); }
0035 
0036   // The following function returns nullptr if the index is out of range
0037   inline const Item* get(const unsigned index) const {
0038     if (index < data_.size())
0039       return data_[index].get();
0040     else
0041       return nullptr;
0042   }
0043 
0044   // The following function throws an exception if the index is out of range
0045   inline const Item& at(const unsigned index) const { return *data_.at(index); }
0046 
0047   // Deep comparison for equality is useful for testing serialization
0048   bool operator==(const HcalItemColl& r) const {
0049     const std::size_t sz = data_.size();
0050     if (sz != r.data_.size())
0051       return false;
0052     for (std::size_t i = 0; i < sz; ++i)
0053       if (!(*data_[i] == *r.data_[i]))
0054         return false;
0055     return true;
0056   }
0057 
0058   inline bool operator!=(const HcalItemColl& r) const { return !(*this == r); }
0059 
0060 private:
0061   std::vector<std::shared_ptr<Item> > data_;
0062 
0063   friend class boost::serialization::access;
0064 
0065   template <class Archive>
0066   inline void serialize(Archive& ar, unsigned /* version */) {
0067     ar & data_;
0068   }
0069 };
0070 
0071 // boost serialization version number for this template
0072 namespace boost {
0073   namespace serialization {
0074     template <typename Item>
0075     struct version<HcalItemColl<Item> > {
0076       BOOST_STATIC_CONSTANT(int, value = 1);
0077     };
0078   }  // namespace serialization
0079 }  // namespace boost
0080 
0081 #endif  // CondFormats_HcalObjects_HcalItemColl_h