26 #ifndef NVNEURAL_REFOBJ_H
27 #define NVNEURAL_REFOBJ_H
29 #if __has_include(<typeinfo>)
39 #define NVNEURAL_REFOBJ_REFERENCE_CALLBACKS 0
43 ILogger* DefaultLogger();
66 template<
typename TObject>
73 template<
typename TObject>
91 template<
typename TBaseType>
106 template<
typename TObject>
113 template<
typename TObject>
134 template<
typename TInterface>
158 template<
typename TObject>
163 return static_cast<TInterface*
>(me);
173 template<
typename TObject>
178 return static_cast<const TInterface*
>(me);
204 template<
typename TInterface,
typename TIntermediateType>
208 static_assert(std::is_base_of<TInterface, TIntermediateType>::value,
"Interface is not a base of the intermediate type");
233 template<
typename TObject>
238 return static_cast<TInterface*
>(
static_cast<TIntermediateType*
>(me));
248 template<
typename TObject>
253 return static_cast<const TInterface*
>(
static_cast<const TIntermediateType*
>(me));
334 template<
typename... ParameterList>
342 logLifetimeChange(
"%p: Creating",
this);
351 const auto newRefCount = ++m_refCount;
355 logLifetimeChange(
"%p: %s: Increment ref count to %d",
this, thisTypeName(), newRefCount);
358 #if NVNEURAL_REFOBJ_REFERENCE_CALLBACKS
370 const auto newRefCount = --m_refCount;
374 logLifetimeChange(
"%p: %s: Decrement ref count to %d",
this, thisTypeName(), newRefCount);
377 #if NVNEURAL_REFOBJ_REFERENCE_CALLBACKS
384 logLifetimeChange(
"%p: %s: Destroying",
this, thisTypeName());
415 const auto pResult = queryInterfaceImpl(interfaceId,
static_cast<ParameterList*
>(
nullptr)...);
434 const auto pResult = queryInterfaceImpl(interfaceId,
static_cast<ParameterList*
>(
nullptr)...);
439 template<
typename ImplementsHelper,
typename... OtherHelpers>
440 constexpr
static bool enableLoggingImpl(ImplementsHelper* , OtherHelpers... others)
442 if (!ImplementsHelper::EnableLogging)
446 return enableLoggingImpl(others...);
448 constexpr
static bool enableLoggingImpl()
452 constexpr
static const bool LoggingEnabled = enableLoggingImpl(
static_cast<ParameterList*
>(
nullptr)...);
463 template<
typename ImplementsHelper,
typename... OtherHelpers>
464 void* queryInterfaceImpl(
IRefObject::TypeId interfaceId, ImplementsHelper* , OtherHelpers... others)
466 void*
const pResult = ImplementsHelper::querySingleInterface(interfaceId,
this);
472 return queryInterfaceImpl(interfaceId, others...);
476 template<
typename ImplementsHelper,
typename... OtherHelpers>
477 const void* queryInterfaceImpl(
IRefObject::TypeId interfaceId, ImplementsHelper* , OtherHelpers... others)
const
479 const void*
const pResult = ImplementsHelper::querySingleInterface(interfaceId,
this);
485 return queryInterfaceImpl(interfaceId, others...);
504 template<
typename... TFormatArgs>
505 static void logLifetimeChange(
const char* pFormatString, TFormatArgs... formatArgs)
513 pLogger->log(logVerbosity, pFormatString, formatArgs...);
518 const char* thisTypeName()
const
527 return typeid(*this).name();
534 #if NVNEURAL_REFOBJ_REFERENCE_CALLBACKS
535 virtual void addRefCallback()
const { }
536 virtual void releaseCallback()
const { }
540 mutable std::atomic<IRefObject::RefCount> m_refCount{1u};
ILogger * DefaultLogger()
Returns a pointer to the default logger for this module.
Definition: Logging.cpp:38
std::int32_t VerbosityLevel
Typedef for verbosity levels.
Definition: CoreTypes.h:424
static const TypeId typeID
Interface TypeId for InterfaceOf purposes.
Definition: CoreTypes.h:352
std::uint64_t TypeId
Every interface must define a unique TypeId. This should be randomized.
Definition: CoreTypes.h:349
std::uint32_t RefCount
Typedef used to track the number of active references to an object.
Definition: CoreTypes.h:346
Helper template to aid retrieval of interface IDs.
Definition: CoreTypes.h:403
No base types are provided by this trait.
Definition: RefObject.h:63
Disables log messages when the object refcount changes.
Definition: RefObject.h:61
static void * querySingleInterface(IRefObject::TypeId, TObject *)
No interfaces are provided by this trait.
Definition: RefObject.h:67
static const bool EnableLogging
Prevents logging messages.
Definition: RefObject.h:80
static const void * querySingleInterface(IRefObject::TypeId, const TObject *)
No interfaces are provided by this trait.
Definition: RefObject.h:74
Helper class for RefObjectBase indicating direct inheritance.
Definition: RefObject.h:136
static const void * querySingleInterface(IRefObject::TypeId interfaceId, const TObject *me)
Const querySingleInterface implementation.
Definition: RefObject.h:174
static const bool EnableLogging
This trait normally allows lifecycle logging.
Definition: RefObject.h:186
TInterface BaseType
Provide TInterface as a recommended base type.
Definition: RefObject.h:140
static void * querySingleInterface(IRefObject::TypeId interfaceId, TObject *me)
Non-const querySingleInterface implementation.
Definition: RefObject.h:159
Provide an empty struct as a recommended base type.
Definition: RefObject.h:215
Helper class for RefObjectBase indicating interface support without direct inheritance.
Definition: RefObject.h:206
static const void * querySingleInterface(IRefObject::TypeId interfaceId, const TObject *me)
Const querySingleInterface implementation.
Definition: RefObject.h:249
static const bool EnableLogging
This trait normally allows lifecycle logging.
Definition: RefObject.h:260
static void * querySingleInterface(IRefObject::TypeId interfaceId, TObject *me)
Non-const querySingleInterface implementation.
Definition: RefObject.h:234
Helper class for RefObjectBase indicating direct inheritance without providing interfaces.
Definition: RefObject.h:93
TBaseType BaseType
Provide TBaseType as a base type.
Definition: RefObject.h:97
static const bool EnableLogging
This trait normally allows lifecycle logging.
Definition: RefObject.h:122
static void * querySingleInterface(IRefObject::TypeId, TObject *)
Non-const querySingleInterface implementation. Returns nullptr.
Definition: RefObject.h:107
static const void * querySingleInterface(IRefObject::TypeId, const TObject *)
Const querySingleInterface implementation. Returns nullptr.
Definition: RefObject.h:114
Parameterized base class implementing common IRefObject operations.
Definition: RefObject.h:336
RefObjectBase()
Default constructor. Logs object creation.
Definition: RefObject.h:338
void * queryInterface(IRefObject::TypeId interfaceId) noexcept
Retrieves a new object interface pointer.
Definition: RefObject.h:401
IRefObject::RefCount addRef() const noexcept
Increment the object's reference count.
Definition: RefObject.h:349
IRefObject::RefCount release() const noexcept
Decrements the object's reference count and destroys the object if the reference count reaches zero.
Definition: RefObject.h:368
const void * queryInterface(IRefObject::TypeId interfaceId) const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: RefObject.h:420