Skip to content

Commit

Permalink
Updated RegisterTypes to allow overwrite of the typename to use and o…
Browse files Browse the repository at this point in the history
…verwrite the typename for ElectrodeTable
  • Loading branch information
oruebel committed Sep 21, 2024
1 parent 8832b04 commit f244d8c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
16 changes: 16 additions & 0 deletions docs/pages/devdocs/registered_types.dox
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@
* we can look up the corresponding class for an object in a file based on the
* ``neurodata_type`` and ``namespace`` attributes stored in the file.
*
* \note
* A special version of the ``REGISTER_SUBCLASS`` macro, called ``REGISTER_SUBCLASS_WITH_TYPENAME``,
* allows setting the typename explicitly as a third argument. This is for the **special case**
* where we want to implement a class for a modified type that does not have its
* own `neurodata_type` in the NWB schema. An example is `ElectrodesTable` in NWB <v2.7, which
* did not have an assigned `neurodata_type`, but was implemented as a regular
* `DynamicTable`. To allow us to define a class `ElectrodeTable` to help with writing the table
* we can then use ``REGISTER_SUBCLASS_WITH_TYPENAME(ElectrodeTable, "core", "DynamicTable")``
* in the `ElectrodesTable` class. This ensures that the `neurodata_type` attribute is set
* correctly to `DynamicTable` on write instead of `ElectrodesTable`. However, on read this
* will by default not use the `ElectrodesTable` class but the regular `DynamicTable` class
* since that is what the schema is indicating to use. In the registry, the class will still
* be registered under the ``core::ElectrodesTable`` key, but with "DynamicTable" as the
* typename value and the `ElectrodesTable.getTypeName` automatic override returning
* the indicated typename instead of the classname.
*
*
* **Example:**
*
Expand Down
22 changes: 19 additions & 3 deletions src/nwb/RegisteredType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ class RegisteredType
std::shared_ptr<IO::BaseIO> m_io;
};


/**
* @brief Macro to register a subclass with the RegisteredType class registry.
*
Expand All @@ -197,29 +198,44 @@ class RegisteredType
* @param T The subclass type to register. The name must match the type in the
* schema.
* @param NAMESPACE The namespace of the subclass type in the format schema
* @param TYPENAME The name of the type (usually the class name).
*/
#define REGISTER_SUBCLASS(T, NAMESPACE) \
#define REGISTER_SUBCLASS_WITH_TYPENAME(T, NAMESPACE, TYPENAME) \
static bool registerSubclass() \
{ \
AQNWB::NWB::RegisteredType::registerSubclass( \
NAMESPACE "::" #T, \
[](const std::string& path, std::shared_ptr<IO::BaseIO> io) \
-> std::unique_ptr<AQNWB::NWB::RegisteredType> \
{ return std::make_unique<T>(path, io); }, \
#T, \
TYPENAME, \
NAMESPACE); \
return true; \
} \
static bool registered_; \
virtual std::string getTypeName() const override \
{ \
return #T; \
return TYPENAME; \
} \
virtual std::string getNamespace() const override \
{ \
return NAMESPACE; \
}

/**
* @brief Macro to register a subclass with the RegisteredType class registry.
*
* This macro is a convenience wrapper around the main REGISTER_SUBCLASS macro,
* providing a default value for TYPENAME.
*
* @param T The subclass type to register. The name must match the type in the
* schema.
* @param NAMESPACE The namespace of the subclass type in the format schema
*/
#define REGISTER_SUBCLASS(T, NAMESPACE) REGISTER_SUBCLASS_WITH_TYPENAME(T, NAMESPACE, #T)



/**
* @brief Macro to initialize the static member `registered_` to trigger
* registration.
Expand Down
3 changes: 2 additions & 1 deletion src/nwb/file/ElectrodeTable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class ElectrodeTable : public DynamicTable
{
public:
// Register the ElectrodeTable as a subclass of Container
REGISTER_SUBCLASS(ElectrodeTable, "core")
//REGISTER_SUBCLASS(ElectrodeTable, "core")
REGISTER_SUBCLASS_WITH_TYPENAME(ElectrodeTable, "core", "DynamicTable")

/**
* @brief Constructor.
Expand Down

0 comments on commit f244d8c

Please sign in to comment.