7#include <unordered_map>
8#include <unordered_set>
28constexpr auto DatasetField = AQNWB::Types::StorageObjectType::Dataset;
70 return std::filesystem::path(
m_path).filename().string();
77 inline std::shared_ptr<AQNWB::IO::BaseIO>
getIO()
const
79 auto ioPtr =
m_io.lock();
138 inline const std::unordered_map<std::string,
139 std::shared_ptr<IO::BaseRecordingData>>&
165 static std::unordered_set<std::string>&
getRegistry();
178 static std::unordered_map<
180 std::pair<std::function<std::shared_ptr<RegisteredType>(
181 const std::string&, std::shared_ptr<AQNWB::IO::BaseIO>)>,
182 std::pair<std::string, std::string>>>&
200 static std::shared_ptr<RegisteredType>
create(
201 const std::string& fullClassName,
202 const std::string& path,
203 std::shared_ptr<IO::BaseIO> io,
204 bool fallbackToBase =
false);
226 static std::shared_ptr<AQNWB::NWB::RegisteredType>
create(
227 const std::string& path,
228 std::shared_ptr<IO::BaseIO> io,
229 bool fallbackToBase =
false);
241 static inline std::shared_ptr<T>
create(
const std::string& path,
242 std::shared_ptr<AQNWB::IO::BaseIO> io)
244 static_assert(std::is_base_of<RegisteredType, T>::value,
245 "T must be a derived class of RegisteredType");
246 auto result = std::shared_ptr<T>(
new T(path, io));
247 result->registerRecordingObject();
311 typename std::enable_if<Types::IsDataStorageObjectType<SOT>::value,
313 inline std::unique_ptr<AQNWB::IO::ReadDataWrapper<SOT, VTYPE>>
readField(
314 const std::string& fieldPath)
const
316 auto ioPtr =
getIO();
318 std::cerr <<
"IO object has been deleted. Can't read field: " << fieldPath
322 return std::make_unique<AQNWB::IO::ReadDataWrapper<SOT, VTYPE>>(
334 inline std::shared_ptr<AQNWB::NWB::RegisteredType>
readField(
335 const std::string& fieldPath)
const
337 auto ioPtr =
getIO();
339 std::cerr <<
"IO object has been deleted. Can't read field: " << fieldPath
364 virtual std::unordered_map<std::string, std::string>
findOwnedTypes(
365 const std::unordered_set<std::string>& types = {},
380 std::shared_ptr<AQNWB::IO::BaseIO> io);
400 const std::string& fullClassName,
401 std::function<std::shared_ptr<RegisteredType>(
402 const std::string&, std::shared_ptr<AQNWB::IO::BaseIO>)>
404 const std::string& typeName,
405 const std::string& typeNamespace);
422 std::weak_ptr<IO::BaseIO>
m_io;
436 std::unordered_map<std::string, std::shared_ptr<IO::BaseRecordingData>>
458#define REGISTER_SUBCLASS_WITH_TYPENAME(T, BASE, NAMESPACE_VAR, TYPENAME) \
459 friend class AQNWB::NWB::RegisteredType; \
463 static bool registerSubclass() \
465 AQNWB::NWB::RegisteredType::registerSubclass( \
466 std::string(NAMESPACE_VAR) + "::" + #T, \
467 [](const std::string& path, std::shared_ptr<AQNWB::IO::BaseIO> io) \
468 -> std::shared_ptr<AQNWB::NWB::RegisteredType> \
469 { return RegisteredType::create<T>(path, io); }, \
474 static bool registered_; \
475 virtual std::string getTypeName() const override \
479 virtual std::string getNamespace() const override \
481 return NAMESPACE_VAR; \
483 static std::shared_ptr<T> create(const std::string& path, \
484 std::shared_ptr<AQNWB::IO::BaseIO> io) \
486 return RegisteredType::create<T>(path, io); \
501#define REGISTER_SUBCLASS(T, BASE, NAMESPACE) \
502 REGISTER_SUBCLASS_WITH_TYPENAME(T, BASE, NAMESPACE, #T)
513#define REGISTER_SUBCLASS_IMPL(T) bool T::registered_ = T::registerSubclass();
534#define DEFINE_ATTRIBUTE_FIELD(name, default_type, fieldPath, description) \
543 template<typename VTYPE = default_type> \
544 inline std::unique_ptr< \
545 AQNWB::IO::ReadDataWrapper<AQNWB::NWB::AttributeField, VTYPE>> \
548 auto ioPtr = getIO(); \
550 std::cerr << "IO object has been deleted. Can't read field: " \
551 << fieldPath << std::endl; \
554 return std::make_unique< \
555 AQNWB::IO::ReadDataWrapper<AQNWB::NWB::AttributeField, VTYPE>>( \
556 ioPtr, AQNWB::mergePaths(m_path, fieldPath)); \
580#define DEFINE_DATASET_FIELD( \
581 readName, writeName, default_type, fieldPath, description) \
590 template<typename VTYPE = default_type> \
591 inline std::unique_ptr< \
592 AQNWB::IO::ReadDataWrapper<AQNWB::NWB::DatasetField, VTYPE>> \
595 auto ioPtr = getIO(); \
597 std::cerr << "IO object has been deleted. Can't read field: " \
598 << fieldPath << std::endl; \
601 return std::make_unique< \
602 AQNWB::IO::ReadDataWrapper<AQNWB::NWB::DatasetField, VTYPE>>( \
603 ioPtr, AQNWB::mergePaths(m_path, fieldPath)); \
618 inline std::shared_ptr<AQNWB::IO::BaseRecordingData> writeName(bool reset = \
621 std::string fullPath = AQNWB::mergePaths(m_path, fieldPath); \
624 auto it = m_recordingDataCache.find(fullPath); \
625 if (it != m_recordingDataCache.end()) { \
630 auto ioPtr = getIO(); \
632 std::cerr << "IO object has been deleted. Can't access: " << fullPath \
636 auto dataset = ioPtr->getDataSet(fullPath); \
638 m_recordingDataCache[fullPath] = dataset; \
664#define DEFINE_REGISTERED_FIELD(name, registeredType, fieldPath, description) \
677 template<typename RTYPE = registeredType> \
678 inline std::shared_ptr<RTYPE> name() const \
680 std::string objectPath = AQNWB::mergePaths(m_path, fieldPath); \
681 auto ioPtr = getIO(); \
682 if (ioPtr != nullptr) { \
683 if (ioPtr->objectExists(objectPath)) { \
684 return RegisteredType::create<RTYPE>(objectPath, ioPtr); \
714#define DEFINE_UNNAMED_REGISTERED_FIELD( \
715 readName, writeName, registeredType, fieldPrefixPath, description) \
729 template<typename RTYPE = registeredType> \
730 inline std::shared_ptr<RTYPE> readName(const std::string& objectName) const \
732 std::string prefixPath = AQNWB::mergePaths(m_path, fieldPrefixPath); \
733 std::string objectPath = AQNWB::mergePaths(prefixPath, objectName); \
734 auto ioPtr = getIO(); \
736 std::cerr << "IO object has been deleted. Can't read field: " \
737 << objectPath << std::endl; \
740 if (ioPtr->objectExists(objectPath)) { \
741 return RegisteredType::create<RTYPE>(objectPath, ioPtr); \
759 template<typename RTYPE = registeredType> \
760 inline std::shared_ptr<RTYPE> writeName(const std::string& objectName) const \
762 std::string prefixPath = AQNWB::mergePaths(m_path, fieldPrefixPath); \
763 std::string objectPath = AQNWB::mergePaths(prefixPath, objectName); \
764 auto ioPtr = getIO(); \
766 std::cerr << "IO object has been deleted. Can't create field: " \
767 << objectPath << std::endl; \
770 return RegisteredType::create<RTYPE>(objectPath, ioPtr); \
794#define DEFINE_REFERENCED_REGISTERED_FIELD( \
795 name, registeredType, fieldPath, description) \
808 template<typename RTYPE = registeredType> \
809 inline std::shared_ptr<RTYPE> name() const \
812 std::string attrPath = AQNWB::mergePaths(m_path, fieldPath); \
813 auto ioPtr = getIO(); \
814 if (ioPtr != nullptr) { \
815 std::string objectPath = ioPtr->readReferenceAttribute(attrPath); \
816 if (ioPtr->objectExists(objectPath)) { \
817 return RegisteredType::create<RTYPE>(objectPath, ioPtr); \
820 } catch (const std::exception& e) { \
821 std::cerr << "WARNING Error occurred in " << #name << " " << e.what() \
AQNWB::Types::StorageObjectType StorageObjectType
Definition BaseIO.hpp:21
AQNWB::Types::SizeType SizeType
Definition Channel.hpp:8
std::shared_ptr< AQNWB::NWB::RegisteredType > readField(const std::string &fieldPath) const
Read a field that is itself a RegisteredType.
Definition RegisteredType.hpp:334
std::string getFullTypeName() const
Get the full name of the type, i.e., namespace::typename.
Definition RegisteredType.hpp:284
virtual std::string getTypeName() const
Get the name of the class type.
Definition RegisteredType.cpp:63
static std::shared_ptr< T > create(const std::string &path, std::shared_ptr< AQNWB::IO::BaseIO > io)
Factory method to create an instance of a subclass of RegisteredType by type.
Definition RegisteredType.hpp:241
std::string m_path
The path of the registered type.
Definition RegisteredType.hpp:410
static const std::string m_defaultUnregisteredDatasetTypeClass
Save the default RegisteredType to use for reading Dataset types that are not registered.
Definition RegisteredType.hpp:388
std::unordered_map< std::string, std::shared_ptr< IO::BaseRecordingData > > m_recordingDataCache
Cache for BaseRecordingData objects for datasets to retain recording state.
Definition RegisteredType.hpp:437
virtual std::string getNamespace() const
Get the schema namespace of the class type.
Definition RegisteredType.cpp:70
static std::shared_ptr< RegisteredType > create(const std::string &fullClassName, const std::string &path, std::shared_ptr< IO::BaseIO > io, bool fallbackToBase=false)
Create an instance of a registered subclass by name.
Definition RegisteredType.cpp:75
std::weak_ptr< IO::BaseIO > m_io
A weak pointer to the IO object.
Definition RegisteredType.hpp:422
virtual std::unordered_map< std::string, std::string > findOwnedTypes(const std::unordered_set< std::string > &types={}, const AQNWB::IO::SearchMode &search_mode=AQNWB::IO::SearchMode::STOP_ON_TYPE) const
Find all typed objects that are owned by this object, i.e., objects that have a neurodata_type and na...
Definition RegisteredType.cpp:153
SizeType registerRecordingObject()
Register this RegisteredType object with the RecordingObjects manager object of the I/O.
Definition RegisteredType.cpp:128
std::string getName() const
Get the name of the object.
Definition RegisteredType.hpp:68
RegisteredType(const std::string &path, std::shared_ptr< AQNWB::IO::BaseIO > io)
Constructor.
Definition RegisteredType.cpp:18
virtual AQNWB::Types::Status finalize()
Finalize the RegisteredType object.
Definition RegisteredType.cpp:148
static void registerSubclass(const std::string &fullClassName, std::function< std::shared_ptr< RegisteredType >(const std::string &, std::shared_ptr< AQNWB::IO::BaseIO >)> factoryFunction, const std::string &typeName, const std::string &typeNamespace)
Register a subclass name and its factory function in the registry.
Definition RegisteredType.cpp:49
std::string getPath() const
Gets the path of the registered type.
Definition RegisteredType.hpp:59
std::shared_ptr< AQNWB::IO::BaseIO > getIO() const
Get a shared pointer to the IO object.
Definition RegisteredType.hpp:77
virtual void clearRecordingDataCache()
Clear the BaseRecordingData object cache to reset the recording state.
Definition RegisteredType.hpp:149
static std::unordered_set< std::string > & getRegistry()
Get the registry of subclass names.
Definition RegisteredType.cpp:27
virtual ~RegisteredType()
Destructor.
Definition RegisteredType.cpp:25
SizeType getRecordingObjectIndex() const
Get the index of this object in m_io->m_recording_objects.
Definition RegisteredType.cpp:167
static const std::string m_defaultUnregisteredGroupTypeClass
Save the default RegisteredType to use for reading Group types that are not registered.
Definition RegisteredType.hpp:384
static std::unordered_map< std::string, std::pair< std::function< std::shared_ptr< RegisteredType >(const std::string &, std::shared_ptr< AQNWB::IO::BaseIO >)>, std::pair< std::string, std::string > > > & getFactoryMap()
Get the factory map for creating instances of subclasses.
Definition RegisteredType.cpp:38
const std::unordered_map< std::string, std::shared_ptr< IO::BaseRecordingData > > & getCacheRecordingData() const
Get the cache of BaseRecordingData objects.
Definition RegisteredType.hpp:140
std::unique_ptr< AQNWB::IO::ReadDataWrapper< SOT, VTYPE > > readField(const std::string &fieldPath) const
Support reading of arbitrary fields by their relative path.
Definition RegisteredType.hpp:313
bool isRegisteredRecordingObject() const
Check if this RegisteredType object is registered in the RecordingObjects manager object of the I/O.
Definition RegisteredType.hpp:95
Status
Represents the status of an operation.
Definition Types.hpp:23
SearchMode
Enum class for specifying the search mode for findTypes.
Definition BaseIO.hpp:156
@ STOP_ON_TYPE
Stop searching inside an object once a matching type is found.
Definition BaseIO.hpp:160
Namespace for all classes related to the NWB data standard.
constexpr auto AttributeField
Alias for AQNWB::Types::StorageObjectType::Attribute.
Definition RegisteredType.hpp:23
constexpr auto DatasetField
Alias for AQNWB::Types::StorageObjectType::Dataset.
Definition RegisteredType.hpp:28
The main namespace for AqNWB.
Definition Channel.hpp:11
static std::string mergePaths(const std::string &path1, const std::string &path2)
Merge two paths into a single path, handling extra trailing and starting "/".
Definition Utils.hpp:112
static bool isValidIndex(SizeType index)
Check if a SizeType index is valid (i.e., not equal to SizeTypeNotSet).
Definition Utils.hpp:210