Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-30 10:31:07

0001 #ifndef PODIO_UTILITIES_BACKINSERTITERATOR_H
0002 #define PODIO_UTILITIES_BACKINSERTITERATOR_H
0003 
0004 #include <cstddef>
0005 #include <iterator>
0006 #include <memory>
0007 #include <utility>
0008 
0009 namespace podio {
0010 
0011 /// An output iterator that forwards assigned handles to a collection's push_back.
0012 ///
0013 /// Unlike std::back_insert_iterator, this iterator preserves the handle type,
0014 /// allowing mutable handles (e.g. clones) to be inserted into owning collections.
0015 /// The collection's existing ownership and subset insertion rules still apply.
0016 /// The destination collection must outlive the iterator.
0017 template <typename Collection>
0018 class BackInsertIterator {
0019 public:
0020   using container_type = Collection;
0021   using iterator_category = std::output_iterator_tag;
0022   using value_type = void;
0023   using difference_type = std::ptrdiff_t;
0024   using pointer = void;
0025   using reference = void;
0026 
0027   BackInsertIterator() = default;
0028 
0029   explicit BackInsertIterator(Collection& collection) noexcept : m_collection(std::addressof(collection)) {
0030   }
0031 
0032   template <typename T>
0033     requires requires(Collection& collection, T&& value) { collection.push_back(std::forward<T>(value)); }
0034   BackInsertIterator& operator=(T&& value) {
0035     m_collection->push_back(std::forward<T>(value));
0036     return *this;
0037   }
0038 
0039   BackInsertIterator& operator*() noexcept {
0040     return *this;
0041   }
0042 
0043   BackInsertIterator& operator++() noexcept {
0044     return *this;
0045   }
0046 
0047   BackInsertIterator operator++(int) noexcept {
0048     return *this;
0049   }
0050 
0051 private:
0052   Collection* m_collection{nullptr};
0053 };
0054 
0055 /// Create an output iterator that preserves the type of handles passed to push_back.
0056 template <typename Collection>
0057 BackInsertIterator<Collection> back_inserter(Collection& collection) noexcept {
0058   return BackInsertIterator<Collection>(collection);
0059 }
0060 
0061 } // namespace podio
0062 
0063 #endif // PODIO_UTILITIES_BACKINSERTITERATOR_H