Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:50:09

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