Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-14 09:00:35

0001 #ifndef PODIO_COLLECTIONBUFFERS_H
0002 #define PODIO_COLLECTIONBUFFERS_H
0003 
0004 #include "podio/ObjectID.h"
0005 #include "podio/SchemaEvolution.h"
0006 
0007 #include <functional>
0008 #include <memory>
0009 #include <string>
0010 #include <string_view>
0011 #include <utility>
0012 #include <vector>
0013 
0014 namespace podio {
0015 
0016 class CollectionBase;
0017 
0018 template <typename T>
0019 using UVecPtr = std::unique_ptr<std::vector<T>>;
0020 
0021 using CollRefCollection = std::vector<UVecPtr<podio::ObjectID>>;
0022 using VectorMembersInfo = std::vector<std::pair<std::string, void*>>;
0023 
0024 /// Simple helper struct that bundles all the potentially necessary buffers that
0025 /// are necessary to represent a collection for I/O purposes.
0026 struct CollectionWriteBuffers {
0027   void* data{nullptr};
0028   void* vecPtr{nullptr};
0029   CollRefCollection* references{nullptr};
0030   VectorMembersInfo* vectorMembers{nullptr};
0031 
0032   template <typename DataT>
0033   std::vector<DataT>* dataAsVector() {
0034     return asVector<DataT>(data);
0035   }
0036 
0037   template <typename T>
0038   static std::vector<T>* asVector(void* raw) {
0039     // Are we at a beach? I can almost smell the C...
0040     return *static_cast<std::vector<T>**>(raw);
0041   }
0042 };
0043 
0044 struct CollectionReadBuffers {
0045   void* data{nullptr};
0046   CollRefCollection* references{nullptr};
0047   VectorMembersInfo* vectorMembers{nullptr};
0048   SchemaVersionT schemaVersion{0};
0049   std::string_view type{};
0050 
0051   using CreateFuncT = std::function<std::unique_ptr<podio::CollectionBase>(podio::CollectionReadBuffers, bool)>;
0052   using RecastFuncT = std::function<void(CollectionReadBuffers&)>;
0053 
0054   using DeleteFuncT = std::function<void(CollectionReadBuffers&)>;
0055 
0056   CollectionReadBuffers(void* d, CollRefCollection* ref, VectorMembersInfo* vec, SchemaVersionT version,
0057                         std::string_view typ, CreateFuncT&& createFunc, RecastFuncT&& recastFunc,
0058                         DeleteFuncT&& deleteFunc) :
0059       data(d),
0060       references(ref),
0061       vectorMembers(vec),
0062       schemaVersion(version),
0063       type(typ),
0064       createCollection(std::move(createFunc)),
0065       recast(std::move(recastFunc)),
0066       deleteBuffers(std::move(deleteFunc)) {
0067   }
0068 
0069   CollectionReadBuffers() = default;
0070   CollectionReadBuffers(const CollectionReadBuffers&) = default;
0071   CollectionReadBuffers& operator=(const CollectionReadBuffers&) = default;
0072 
0073   CollectionReadBuffers(CollectionWriteBuffers buffers) :
0074       data(buffers.data), references(buffers.references), vectorMembers(buffers.vectorMembers) {
0075   }
0076 
0077   template <typename DataT>
0078   std::vector<DataT>* dataAsVector() {
0079     return asVector<DataT>(data);
0080   }
0081 
0082   template <typename T>
0083   static std::vector<T>* asVector(void* raw) {
0084     // Are we at a beach? I can almost smell the C...
0085     return static_cast<std::vector<T>*>(raw);
0086   }
0087 
0088   CreateFuncT createCollection{};
0089 
0090   // This is a hacky workaround for the ROOT backend at the moment. There is
0091   // probably a better solution, but I haven't found it yet. The problem is the
0092   // following:
0093   //
0094   // When creating a pointer to a vector<T>, either via new or via
0095   // TClass::New(), we get a void*, that can be cast back to a vector with
0096   //
0097   //     static_cast<vector<T>*>(raw);
0098   //
0099   // However, as soon as we pass that same void* to TBranch::SetAddress this no
0100   // longer works and the actual cast has to be
0101   //
0102   //     *static_cast<vector<T>**>(raw);
0103   //
0104   // To make it possible to always use the first form, after we leave the Root
0105   // parts of reading, this function is populated in the createBuffers call of each
0106   // datatype where we have the necessary type information (from code
0107   // generation) to do the second cast and assign the result of that to the data
0108   // field again.
0109   RecastFuncT recast{};
0110 
0111   // Workaround for https://github.com/AIDASoft/podio/issues/500
0112   // We need a function that explicitly deletes the buffers, but for this we
0113   // need type information, so we attach a delete function at generation time
0114   DeleteFuncT deleteBuffers{};
0115 };
0116 
0117 } // namespace podio
0118 
0119 #endif // PODIO_COLLECTIONBUFFERS_H