File indexing completed on 2025-09-15 09:01:40
0001
0002
0003 #ifndef PODIODATAMODEL_ExampleClusterCollection_H
0004 #define PODIODATAMODEL_ExampleClusterCollection_H
0005
0006
0007 #include "PodioDatamodel/ExampleCluster.h"
0008 #include "PodioDatamodel/MutableExampleCluster.h"
0009 #include "PodioDatamodel/ExampleClusterObj.h"
0010 #include "PodioDatamodel/ExampleClusterCollectionData.h"
0011
0012
0013 #include "podio/ICollectionProvider.h"
0014 #include "podio/CollectionBase.h"
0015
0016 #if defined(PODIO_JSON_OUTPUT) && !defined(__CLING__)
0017 #include "nlohmann/json_fwd.hpp"
0018 #endif
0019
0020 #include <string_view>
0021 #include <vector>
0022 #include <algorithm>
0023 #include <ostream>
0024 #include <mutex>
0025 #include <memory>
0026 #include <cstddef>
0027
0028 namespace podio {
0029 struct RelationNames;
0030 }
0031
0032
0033
0034
0035 class ExampleClusterCollectionIterator {
0036 public:
0037 using value_type = ExampleCluster;
0038 using difference_type = ptrdiff_t;
0039 using reference = ExampleCluster;
0040 using pointer = ExampleCluster*;
0041 using iterator_category = std::input_iterator_tag;
0042
0043
0044 using iterator_concept = std::random_access_iterator_tag;
0045
0046 ExampleClusterCollectionIterator(size_t index, const ExampleClusterObjPointerContainer* collection) : m_index(index), m_object(podio::utils::MaybeSharedPtr<ExampleClusterObj>{nullptr}), m_collection(collection) {}
0047 ExampleClusterCollectionIterator() = default;
0048
0049 ExampleClusterCollectionIterator(const ExampleClusterCollectionIterator&) = default;
0050 ExampleClusterCollectionIterator(ExampleClusterCollectionIterator&&) = default;
0051 ExampleClusterCollectionIterator& operator=(const ExampleClusterCollectionIterator&) = default;
0052 ExampleClusterCollectionIterator& operator=(ExampleClusterCollectionIterator&&) = default;
0053 ~ExampleClusterCollectionIterator() = default;
0054
0055 auto operator<=>(const ExampleClusterCollectionIterator& other) const {
0056 return m_index <=> other.m_index;
0057 }
0058
0059 bool operator==(const ExampleClusterCollectionIterator& x) const {
0060 return m_index == x.m_index;
0061 }
0062
0063 reference operator*() const;
0064 pointer operator->();
0065 ExampleClusterCollectionIterator& operator++();
0066 ExampleClusterCollectionIterator operator++(int);
0067 ExampleClusterCollectionIterator& operator--();
0068 ExampleClusterCollectionIterator operator--(int);
0069 ExampleClusterCollectionIterator& operator+=(difference_type n);
0070 ExampleClusterCollectionIterator operator+(difference_type n) const;
0071 friend ExampleClusterCollectionIterator operator+(difference_type n, const ExampleClusterCollectionIterator& it);
0072 ExampleClusterCollectionIterator& operator-=(difference_type n);
0073 ExampleClusterCollectionIterator operator-(difference_type n) const;
0074 reference operator[](difference_type n) const;
0075 difference_type operator-(const ExampleClusterCollectionIterator& other) const;
0076
0077 private:
0078 size_t m_index{0};
0079 ExampleCluster m_object { podio::utils::MaybeSharedPtr<ExampleClusterObj>{nullptr} };
0080 const ExampleClusterObjPointerContainer* m_collection{nullptr};
0081 };
0082
0083
0084 class ExampleClusterMutableCollectionIterator {
0085 public:
0086 using value_type = ExampleCluster;
0087 using difference_type = ptrdiff_t;
0088 using reference = MutableExampleCluster;
0089 using pointer = MutableExampleCluster*;
0090 using iterator_category = std::input_iterator_tag;
0091
0092
0093 using iterator_concept = std::random_access_iterator_tag;
0094
0095 ExampleClusterMutableCollectionIterator(size_t index, const ExampleClusterObjPointerContainer* collection) : m_index(index), m_object(podio::utils::MaybeSharedPtr<ExampleClusterObj>{nullptr}), m_collection(collection) {}
0096 ExampleClusterMutableCollectionIterator() = default;
0097
0098 ExampleClusterMutableCollectionIterator(const ExampleClusterMutableCollectionIterator&) = default;
0099 ExampleClusterMutableCollectionIterator(ExampleClusterMutableCollectionIterator&&) = default;
0100 ExampleClusterMutableCollectionIterator& operator=(const ExampleClusterMutableCollectionIterator&) = default;
0101 ExampleClusterMutableCollectionIterator& operator=(ExampleClusterMutableCollectionIterator&&) = default;
0102 ~ExampleClusterMutableCollectionIterator() = default;
0103
0104 auto operator<=>(const ExampleClusterMutableCollectionIterator& other) const {
0105 return m_index <=> other.m_index;
0106 }
0107
0108 bool operator==(const ExampleClusterMutableCollectionIterator& x) const {
0109 return m_index == x.m_index;
0110 }
0111
0112 reference operator*() const;
0113 pointer operator->();
0114 ExampleClusterMutableCollectionIterator& operator++();
0115 ExampleClusterMutableCollectionIterator operator++(int);
0116 ExampleClusterMutableCollectionIterator& operator--();
0117 ExampleClusterMutableCollectionIterator operator--(int);
0118 ExampleClusterMutableCollectionIterator& operator+=(difference_type n);
0119 ExampleClusterMutableCollectionIterator operator+(difference_type n) const;
0120 friend ExampleClusterMutableCollectionIterator operator+(difference_type n, const ExampleClusterMutableCollectionIterator& it);
0121 ExampleClusterMutableCollectionIterator& operator-=(difference_type n);
0122 ExampleClusterMutableCollectionIterator operator-(difference_type n) const;
0123 reference operator[](difference_type n) const;
0124 difference_type operator-(const ExampleClusterMutableCollectionIterator& other) const;
0125
0126 private:
0127 size_t m_index{0};
0128 MutableExampleCluster m_object { podio::utils::MaybeSharedPtr<ExampleClusterObj>{nullptr} };
0129 const ExampleClusterObjPointerContainer* m_collection{nullptr};
0130 };
0131
0132
0133
0134
0135
0136 class ExampleClusterCollection : public podio::CollectionBase {
0137 public:
0138 using value_type = ExampleCluster;
0139 using mutable_type = MutableExampleCluster;
0140 using const_iterator = ExampleClusterCollectionIterator;
0141 using iterator = ExampleClusterMutableCollectionIterator;
0142 using difference_type = ptrdiff_t;
0143 using size_type = size_t;
0144 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
0145 using reverse_iterator = std::reverse_iterator<iterator>;
0146
0147 ExampleClusterCollection();
0148 ExampleClusterCollection(ExampleClusterCollectionData&& data, bool isSubsetColl);
0149
0150 ExampleClusterCollection(const ExampleClusterCollection& ) = delete;
0151 ExampleClusterCollection& operator=(const ExampleClusterCollection& ) = delete;
0152 ExampleClusterCollection(ExampleClusterCollection&&) = default;
0153 ExampleClusterCollection& operator=(ExampleClusterCollection&&) = default;
0154
0155
0156 ~ExampleClusterCollection() override;
0157
0158 constexpr static std::string_view typeName = "ExampleClusterCollection";
0159 constexpr static std::string_view valueTypeName = "ExampleCluster";
0160 constexpr static std::string_view dataTypeName = "ExampleClusterData";
0161
0162 void clear() final;
0163
0164
0165 void print(std::ostream& os=std::cout, bool flush=true) const final;
0166
0167
0168 ExampleClusterCollection* operator->() { return static_cast<ExampleClusterCollection*>(this); }
0169
0170
0171 MutableExampleCluster create();
0172
0173
0174
0175 template<typename... Args>
0176 MutableExampleCluster create(Args&&... args);
0177
0178
0179 std::size_t size() const final;
0180
0181
0182 std::size_t max_size() const final;
0183
0184
0185 bool empty() const final;
0186
0187
0188 const std::string_view getTypeName() const final { return typeName; }
0189
0190 const std::string_view getValueTypeName() const final { return valueTypeName; }
0191
0192 const std::string_view getDataTypeName() const final { return dataTypeName; }
0193
0194 podio::SchemaVersionT getSchemaVersion() const final;
0195
0196 bool isSubsetCollection() const final {
0197 return m_isSubsetColl;
0198 }
0199
0200 void setSubsetCollection(bool setSubset=true) final;
0201
0202
0203 ExampleCluster operator[](std::size_t index) const;
0204
0205 MutableExampleCluster operator[](std::size_t index);
0206
0207 ExampleCluster at(std::size_t index) const;
0208
0209 MutableExampleCluster at(std::size_t index);
0210
0211
0212
0213 void push_back(const MutableExampleCluster& object);
0214
0215 void push_back(const ExampleCluster& object);
0216
0217 void prepareForWrite() const final;
0218 void prepareAfterRead() final;
0219 bool setReferences(const podio::ICollectionProvider* collectionProvider) final;
0220
0221
0222 podio::CollectionWriteBuffers getBuffers() final;
0223
0224 void setID(uint32_t ID) final {
0225 m_collectionID = ID;
0226 if (!m_isSubsetColl) {
0227 std::for_each(m_storage.entries.begin(), m_storage.entries.end(),
0228 [ID] (ExampleClusterObj* obj) { obj->id = {obj->id.index, static_cast<uint32_t>(ID)}; }
0229 );
0230 }
0231 m_isValid = true;
0232 }
0233
0234 uint32_t getID() const final {
0235 return m_collectionID;
0236 }
0237
0238 bool isValid() const final {
0239 return m_isValid;
0240 }
0241
0242 size_t getDatamodelRegistryIndex() const final;
0243
0244
0245 iterator begin() {
0246 return iterator(0, &m_storage.entries);
0247 }
0248 const_iterator begin() const {
0249 return const_iterator(0, &m_storage.entries);
0250 }
0251 const_iterator cbegin() const {
0252 return begin();
0253 }
0254 iterator end() {
0255 return iterator(m_storage.entries.size(), &m_storage.entries);
0256 }
0257 const_iterator end() const {
0258 return const_iterator(m_storage.entries.size(), &m_storage.entries);
0259 }
0260 const_iterator cend() const {
0261 return end();
0262 }
0263
0264 reverse_iterator rbegin() {
0265 return reverse_iterator(end());
0266 }
0267 const_reverse_iterator rbegin() const {
0268 return const_reverse_iterator(end());
0269 }
0270 const_reverse_iterator crbegin() const {
0271 return rbegin();
0272 }
0273 reverse_iterator rend() {
0274 return reverse_iterator(begin());
0275 }
0276 const_reverse_iterator rend() const {
0277 return const_reverse_iterator(begin());
0278 }
0279 const_reverse_iterator crend() const {
0280 return rend();
0281 }
0282
0283
0284 std::vector<double> energy(const size_t nElem = 0) const;
0285
0286 private:
0287
0288
0289
0290 friend class ExampleClusterCollectionData;
0291
0292 bool m_isValid{false};
0293 mutable bool m_isPrepared{false};
0294 bool m_isSubsetColl{false};
0295 uint32_t m_collectionID{0};
0296 mutable std::unique_ptr<std::mutex> m_storageMtx{nullptr};
0297 mutable ExampleClusterCollectionData m_storage{};
0298 };
0299
0300 std::ostream& operator<<(std::ostream& o, const ExampleClusterCollection& v);
0301
0302 template<typename... Args>
0303 MutableExampleCluster ExampleClusterCollection::create(Args&&... args) {
0304 if (m_isSubsetColl) {
0305 throw std::logic_error("Cannot create new elements on a subset collection");
0306 }
0307 const int size = m_storage.entries.size();
0308 auto obj = new ExampleClusterObj({size, m_collectionID}, {std::forward<Args>(args)...});
0309 m_storage.entries.push_back(obj);
0310
0311
0312 obj->m_Hits = new std::vector<ExampleHit>();
0313 obj->m_Clusters = new std::vector<ExampleCluster>();
0314 m_storage.createRelations(obj);
0315 return MutableExampleCluster(podio::utils::MaybeSharedPtr(obj));
0316 }
0317
0318 #if defined(PODIO_JSON_OUTPUT) && !defined(__CLING__)
0319 void to_json(nlohmann::json& j, const ExampleClusterCollection& collection);
0320 #endif
0321
0322
0323
0324 #endif