Skip to content

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed May 3, 2023
1 parent f0f7d4e commit 9a6c1b1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
44 changes: 40 additions & 4 deletions include/podio/CollectionBufferFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,71 @@
namespace podio {

/**
* Registry like type that provides empty buffers for collections
* The CollectionBufferFactory allows to create buffers of known datatypes,
* which can then be populated by e.g. readers. In order to support schema
* evolution, the buffers have a version and this factory will also require a
* schema version to create buffers.
*
* It is implemented as a singleton, which is populated at the time a shared
* datamodel library is loaded. It is assumed that that happens early on in the
* startup of an appliation, such that only a single thread will access the
* factory instance for registering datatypes. Since the necessary creation
* functions are part of the core datamodel library, this should be very easy to
* achieve by simply linking to that library. Once the factory is populated it
* can be safely accessed from multiple threads concurrently to obtain buffers.
*/
class CollectionBufferFactory {
/// Internal storage is a map to an array of creation functions, where the
/// version determines the place in that array. This should be a viable
/// approach because we now the "latest and greatest" schema version
/// approach because we know the "latest and greatest" schema version
using CreationFuncT = std::function<podio::CollectionReadBuffers(bool)>;
using VersionMapT = std::vector<CreationFuncT>;
using MapT = std::unordered_map<std::string, VersionMapT>;

public:
/// The buffer factory is a singleton so we disable all copy and move
/// constructors explicitly
CollectionBufferFactory(CollectionBufferFactory const&) = delete;
CollectionBufferFactory& operator=(CollectionBufferFactory const&) = delete;
CollectionBufferFactory(CollectionBufferFactory&&) = delete;
CollectionBufferFactory& operator=(CollectionBufferFactory&&) = delete;
~CollectionBufferFactory() = default;

/// Mutable instance only used for the initial registration of functions
/// during library loading
static CollectionBufferFactory& mutInstance();
/// Get the factory instance
static CollectionBufferFactory const& instance();

/**
* Create buffers for a given collection type of a given schema version.
*
* @param collType The collection type name (e.g. from collection->getTypeName())
* @param version The schema version the created buffers should have
* @param susbsetColl Should the buffers be for a subset collection or not
*
* @return CollectionReadBuffers if a creation function for this collection
* type has been registered, otherwise an empty optional
*/
std::optional<podio::CollectionReadBuffers> createBuffers(const std::string& collType, SchemaVersionT version,
bool subsetColl) const;

/**
* Register a creation function for a given collection type and schema version.
*
* @param collType The collection type name (i.e. what
* collection->getTypeName() returns)
* @param version The schema version for which this creation function is valid
* @param creationFunc The function that when invoked returns buffers for this
* collection type and schema version. The signature has to be
* podio::CollectionReadBuffers(bool) where the boolean parameter steers
* whether the buffers are for a subset collection or not.
*/
void registerCreationFunc(const std::string& collType, SchemaVersionT version, const CreationFuncT& creationFunc);

private:
CollectionBufferFactory() = default;

MapT m_funcMap{};
MapT m_funcMap{}; ///< Map to the creation functions
};

} // namespace podio
Expand Down
5 changes: 5 additions & 0 deletions python/templates/Collection.cc.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ podio::SchemaVersionT {{ collection_type }}::getSchemaVersion() const {
return {{ package_name }}::meta::schemaVersion;
}

// anonymous namespace for registration with the CollectionBufferFactory. This
// ensures that we don't have to make up arbitrary namespace names here, since
// none of this is publicly visible
namespace {
podio::CollectionReadBuffers createBuffers(bool isSubset) {
auto readBuffers = podio::CollectionReadBuffers{};
Expand Down Expand Up @@ -205,6 +208,8 @@ podio::CollectionReadBuffers createBuffers(bool isSubset) {
return readBuffers;
}

// The usual trick with an IIFE and a static variable inside a funtion and then
// making sure to call that function during shared library loading
bool registerCollection() {
const static auto reg = []() {
auto& factory = podio::CollectionBufferFactory::mutInstance();
Expand Down

0 comments on commit 9a6c1b1

Please sign in to comment.