Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #ifndef PODIO_DATAMODELREGISTRY_H
0002 #define PODIO_DATAMODELREGISTRY_H
0003 
0004 #include "podio/podioVersion.h"
0005 
0006 #include <optional>
0007 #include <string>
0008 #include <string_view>
0009 #include <tuple>
0010 #include <unordered_map>
0011 #include <utility>
0012 #include <vector>
0013 
0014 namespace podio {
0015 
0016 /// Type alias for storing the names of all Relations and VectorMembers for all
0017 /// datatypes of an EDM. Populated for each EDM at code generation time.
0018 /// The structure is of each element in the outer vector is:
0019 /// - get<0>: The name of the datatype
0020 /// - get<1>: The names of all Relations, where OneToManyRelations comes before
0021 ///   OneToOneRelations (in the order as they appear in the YAML file)
0022 /// - get<2>: The names of all VectorMembers (in the order of the file YAML)
0023 using RelationNameMapping =
0024     std::vector<std::tuple<std::string_view, std::vector<std::string_view>, std::vector<std::string_view>>>;
0025 
0026 /// Information on the names of the OneTo[One|Many]Relations as well as the
0027 /// VectorMembers of a datatype
0028 ///
0029 /// The contents are populated by the code generation, where we simply generate
0030 /// static vectors that we make available as const& here.
0031 struct RelationNames {
0032   /// The names of the relations (OneToMany before OneToOne)
0033   const std::vector<std::string_view>& relations;
0034   /// The names of the vector members
0035   const std::vector<std::string_view>& vectorMembers;
0036 };
0037 
0038 /// Global registry holding information about datamodels and datatypes defined
0039 /// therein that are currently known by podio (i.e. which have been dynamically
0040 /// loaded).
0041 ///
0042 /// This is a singleton which is (statically) populated during dynamic loading
0043 /// of generated EDMs. In this context an **EDM refers to the shared library**
0044 /// that is compiled from the generated code from a datamodel definition in YAML
0045 /// format. When we refer to a **datamodel** in this context we talk about the
0046 /// entity as a whole, i.e. its definition in a YAML file, but also the concrete
0047 /// implementation as an EDM, as well as all other information that is related
0048 /// to it. In the API of this registry this will be used, unless we want to
0049 /// highlight that we are referring to a specific part of a datamodel.
0050 class DatamodelRegistry {
0051 public:
0052   /// Get the registry
0053   static const DatamodelRegistry& instance();
0054 
0055   // Mutable instance only used for the initial registration!
0056   static DatamodelRegistry& mutInstance();
0057 
0058   ~DatamodelRegistry() = default;
0059   DatamodelRegistry(const DatamodelRegistry&) = delete;
0060   DatamodelRegistry& operator=(const DatamodelRegistry&) = delete;
0061   DatamodelRegistry(DatamodelRegistry&&) = delete;
0062   DatamodelRegistry& operator=(const DatamodelRegistry&&) = delete;
0063 
0064   /// Dedicated index value for collections that don't have a datamodel
0065   /// definition (e.g. UserDataCollection)
0066   static constexpr size_t NoDefinitionNecessary = -1;
0067   /// Dedicated index value for error checking, used to default init the generated RegistryIndex
0068   static constexpr size_t NoDefinitionAvailable = -2;
0069 
0070   /// Get the definition (in JSON format) of the datamodel with the given
0071   /// edmName.
0072   ///
0073   /// If no datamodel with the given name can be found, an empty datamodel
0074   /// definition, i.e. an empty JSON object ("{}"), is returned.
0075   ///
0076   /// @param name The name of the datamodel
0077   ///
0078   /// @returns The high level definition of the datamodel in JSON format
0079   const std::string_view getDatamodelDefinition(std::string_view name) const;
0080 
0081   /// Get the definition (in JSON format) of the datamodel with the given
0082   /// index.
0083   ///
0084   /// If no datamodel is found under the given index, an empty datamodel
0085   /// definition, i.e. an empty JSON object ("{}"), is returned.
0086   ///
0087   /// @param index The datamodel definition index that can be obtained from each
0088   ///              collection
0089   ///
0090   /// @returns The high level definition of the datamodel in JSON format
0091   const std::string_view getDatamodelDefinition(size_t index) const;
0092 
0093   /// Get the name of the datamodel that is stored under the given index.
0094   ///
0095   /// If no datamodel is found under the given index, an empty string is returned
0096   ///
0097   /// @param index The datamodel definition index that can be obtained from each
0098   ///              collection
0099   ///
0100   /// @returns The name of the datamodel
0101   const std::string& getDatamodelName(size_t index) const;
0102 
0103   std::optional<podio::version::Version> getDatamodelVersion(const std::string& name) const;
0104 
0105   /// Register a datamodel and return its index in the registry.
0106   ///
0107   /// This is the hook that is called during dynamic loading of an EDM to
0108   /// register information for this EDM. If an EDM has already been registered
0109   /// under this name, than the index to the existing EDM in the registry will be
0110   /// returned.
0111   ///
0112   /// @param name The name of the EDM that should be registered
0113   /// @param definition The datamodel definition from which this EDM has been
0114   /// generated in JSON format
0115   /// @param relationNames the names of the relations and vector members for all
0116   /// datatypes that are defined for this EDM
0117   ///
0118   /// @returns The index of this datamodel in the registry
0119   size_t registerDatamodel(std::string name, std::string_view definition,
0120                            const podio::RelationNameMapping& relationNames);
0121 
0122   size_t registerDatamodel(std::string name, std::string_view definition,
0123                            const podio::RelationNameMapping& relationNames, podio::version::Version version);
0124 
0125   /// Get the names of the relations and vector members of a datatype
0126   RelationNames getRelationNames(std::string_view typeName) const;
0127 
0128 private:
0129   DatamodelRegistry() = default;
0130   /// The stored definitions
0131   std::vector<std::pair<std::string, std::string_view>> m_definitions{};
0132 
0133   std::unordered_map<std::string_view, RelationNames> m_relations{};
0134 
0135   std::unordered_map<std::string, podio::version::Version> m_datamodelVersions{};
0136 };
0137 } // namespace podio
0138 
0139 #endif // PODIO_DATAMODELREGISTRY_H