Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:06:09

0001 #ifndef PODIO_COLLECTIONBUFFERFACTORY_H
0002 #define PODIO_COLLECTIONBUFFERFACTORY_H
0003 
0004 #include "podio/CollectionBuffers.h"
0005 #include "podio/SchemaEvolution.h"
0006 
0007 #include <functional>
0008 #include <optional>
0009 #include <unordered_map>
0010 #include <vector>
0011 
0012 namespace podio {
0013 
0014 /// The CollectionBufferFactory allows one to create buffers of known datatypes,
0015 /// which can then be populated by e.g. readers. In order to support schema
0016 /// evolution, the buffers have a version and this factory will also require a
0017 /// schema version to create buffers.
0018 ///
0019 /// It is implemented as a singleton, which is populated at the time a shared
0020 /// datamodel library is loaded. It is assumed that that happens early on in the
0021 /// startup of an application, such that only a single thread will access the
0022 /// factory instance for registering datatypes. Since the necessary creation
0023 /// functions are part of the core datamodel library, this should be very easy
0024 /// to achieve by simply linking to that library. Once the factory is populated
0025 /// it can be safely accessed from multiple threads concurrently to obtain
0026 /// buffers.
0027 class CollectionBufferFactory {
0028   /// Internal storage is a map to an array of creation functions, where the
0029   /// version determines the place in that array. This should be a viable
0030   /// approach because we know the "latest and greatest" schema version
0031   using CreationFuncT = std::function<podio::CollectionReadBuffers(bool)>;
0032   using VersionMapT = std::vector<CreationFuncT>;
0033   using MapT = std::unordered_map<std::string, VersionMapT>;
0034 
0035 public:
0036   /// The buffer factory is a singleton so we disable all copy and move
0037   /// constructors explicitly
0038   CollectionBufferFactory(CollectionBufferFactory const&) = delete;
0039   CollectionBufferFactory& operator=(CollectionBufferFactory const&) = delete;
0040   CollectionBufferFactory(CollectionBufferFactory&&) = delete;
0041   CollectionBufferFactory& operator=(CollectionBufferFactory&&) = delete;
0042   ~CollectionBufferFactory() = default;
0043 
0044   /// Mutable instance only used for the initial registration of functions
0045   /// during library loading
0046   static CollectionBufferFactory& mutInstance();
0047   /// Get the factory instance
0048   static CollectionBufferFactory const& instance();
0049 
0050   /// Create buffers for a given collection type of a given schema version.
0051   ///
0052   /// @param collType The collection type name (e.g. from collection->getTypeName())
0053   /// @param version The schema version the created buffers should have
0054   /// @param subsetColl Should the buffers be for a subset collection or not
0055   ///
0056   /// @return CollectionReadBuffers if a creation function for this collection
0057   /// type has been registered, otherwise an empty optional
0058   std::optional<podio::CollectionReadBuffers> createBuffers(const std::string& collType, SchemaVersionT version,
0059                                                             bool subsetColl) const;
0060   /// Register a creation function for a given collection type and schema version.
0061   ///
0062   /// @param collType The collection type name (i.e. what
0063   ///                 collection->getTypeName() returns)
0064   /// @param version The schema version for which this creation function is
0065   ///                valid
0066   /// @param creationFunc The function that when invoked returns buffers for
0067   ///                     this collection type and schema version. The
0068   ///                     signature has to be
0069   ///                     podio::CollectionReadBuffers(bool) where the boolean
0070   ///                     parameter steers whether the buffers are for a subset
0071   ///                     collection or not.
0072   void registerCreationFunc(const std::string& collType, SchemaVersionT version, const CreationFuncT& creationFunc);
0073 
0074 private:
0075   CollectionBufferFactory() = default;
0076 
0077   MapT m_funcMap{}; ///< Map to the creation functions
0078 };
0079 
0080 } // namespace podio
0081 
0082 #endif // PODIO_COLLECTIONBUFFERFACTORY_H