Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 08:33:17

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include <any>
0012 #include <string>
0013 #include <string_view>
0014 
0015 #include <podio/Frame.h>
0016 #include <podio/UserDataCollection.h>
0017 
0018 namespace Acts::podio_detail {
0019 
0020 struct ConstDynamicColumnBase {
0021   explicit ConstDynamicColumnBase(std::string_view name) : m_name{name} {}
0022 
0023   virtual ~ConstDynamicColumnBase() = default;
0024 
0025   virtual std::any get(std::size_t i) const = 0;
0026 
0027   virtual std::size_t size() const = 0;
0028 
0029  protected:
0030   std::string m_name;
0031 };
0032 
0033 template <typename T>
0034 struct ConstDynamicColumn : public ConstDynamicColumnBase {
0035   ConstDynamicColumn(std::string_view name,
0036                      const podio::UserDataCollection<T>& collection)
0037       : ConstDynamicColumnBase(name), m_collection{collection} {}
0038 
0039   std::any get(std::size_t i) const override {
0040     return &m_collection.vec().at(i);
0041   }
0042   std::size_t size() const override { return m_collection.size(); }
0043 
0044   const podio::UserDataCollection<T>& m_collection;
0045 };
0046 
0047 struct DynamicColumnBase : public ConstDynamicColumnBase {
0048   explicit DynamicColumnBase(std::string_view name)
0049       : ConstDynamicColumnBase{name} {}
0050 
0051   virtual std::any get(std::size_t i) = 0;
0052   std::any get(std::size_t i) const override = 0;
0053 
0054   virtual void add() = 0;
0055   virtual void clear() = 0;
0056   virtual void erase(std::size_t i) = 0;
0057   virtual void copyFrom(std::size_t dstIdx, const DynamicColumnBase& src,
0058                         std::size_t srcIdx) = 0;
0059   virtual void copyFrom(std::size_t dstIdx, const std::any& srcPtr) = 0;
0060 
0061   virtual std::unique_ptr<DynamicColumnBase> clone(
0062       bool empty = false) const = 0;
0063 
0064   virtual std::unique_ptr<ConstDynamicColumnBase> asConst() const = 0;
0065 
0066   virtual void releaseInto(podio::Frame& frame, const std::string& prefix) = 0;
0067 };
0068 
0069 template <typename T>
0070 struct DynamicColumn : public DynamicColumnBase {
0071   DynamicColumn(std::string_view name,
0072                 podio::UserDataCollection<T> collection = {})
0073       : DynamicColumnBase(name), m_collection{std::move(collection)} {}
0074 
0075   std::any get(std::size_t i) override { return &m_collection.vec().at(i); }
0076 
0077   std::any get(std::size_t i) const override {
0078     return &m_collection.vec().at(i);
0079   }
0080 
0081   void add() override { m_collection.vec().emplace_back(); }
0082   void clear() override { m_collection.clear(); }
0083   void erase(std::size_t i) override {
0084     m_collection.vec().erase(m_collection.vec().begin() + i);
0085   }
0086   std::size_t size() const override { return m_collection.size(); }
0087 
0088   std::unique_ptr<DynamicColumnBase> clone(bool empty) const override {
0089     if (empty) {
0090       return std::make_unique<DynamicColumn<T>>(m_name);
0091     }
0092     podio::UserDataCollection<T> copy;
0093     copy.vec().reserve(m_collection.size());
0094     for (const T& v : m_collection) {
0095       copy.push_back(v);
0096     }
0097     return std::make_unique<DynamicColumn<T>>(m_name, std::move(copy));
0098   }
0099 
0100   std::unique_ptr<ConstDynamicColumnBase> asConst() const override {
0101     return std::make_unique<ConstDynamicColumn<T>>(m_name, m_collection);
0102   }
0103 
0104   void copyFrom(std::size_t dstIdx, const DynamicColumnBase& src,
0105                 std::size_t srcIdx) override {
0106     const auto* other = dynamic_cast<const DynamicColumn<T>*>(&src);
0107     assert(other != nullptr &&
0108            "Source column is not of same type as destination");
0109     m_collection.vec().at(dstIdx) = other->m_collection.vec().at(srcIdx);
0110   }
0111 
0112   void copyFrom(std::size_t dstIdx, const std::any& srcPtr) override {
0113     const auto* other = std::any_cast<const T*>(srcPtr);
0114     assert(other != nullptr &&
0115            "Source column is not of same type as destination");
0116     m_collection.vec().at(dstIdx) = *other;
0117   }
0118 
0119   void releaseInto(podio::Frame& frame, const std::string& prefix) override {
0120     frame.put(std::move(m_collection), prefix + m_name);
0121   }
0122 
0123   podio::UserDataCollection<T> m_collection;
0124 };
0125 
0126 }  // namespace Acts::podio_detail