omni::core::ImplementsCast

Defined in omni/core/IObject.h

template<typename T, typename ...Rest>
struct ImplementsCast : public T, public Rest

Helper template for implementing the cast function for one or more interfaces.

Implementations of interfaces (usually hidden in .cpp files) are well served to use this template.

This template provides the following useful feature:

  • It provides a omni::core::IObject_abi::cast_abi() implementation that supports multiple inheritance.

Using omni::core::ImplementsCast is recommended in cases where you want the default implementation of the cast function, but want to override the behavior of omni::core::IObject_abi::acquire_abi() and omni::core::IObject_abi::release_abi(). If the default implementation of cast, acquire, and release functions is desired, then using Implements is recommended.

A possible usage implementing your own acquire/release semantics:

class MyIFooAndIBarImpl : public omni::core::ImplementsCast<IFoo, IBar>
{
public:
    inline void acquire() noexcept
    {
        // note: this implementation is needed to disambiguate which `cast` to call when using multiple
        // inheritance. it has zero-overhead.
        static_cast<IFoo*>(this)->acquire();
    }

    inline void release() noexcept
    {
        // note: this implementation is needed to disambiguate which `cast` to call when using multiple
        // inheritance. it has zero-overhead.
        static_cast<IFoo*>(this)->release();
    }
protected:
    std::atomic<uint32_t> m_refCount{ 1 }; 

    virtual void acquire_abi() noexcept override
    {
        uint32_t count = m_refCount.fetch_add(1, std::memory_order_relaxed) + 1;
        CARB_LOG_INFO("Increased count to %u", count);
    }

    virtual void release_abi() noexcept override
    {
        uint32_t count = m_refCount.fetch_sub(1, std::memory_order_release) - 1;
        CARB_LOG_INFO("Reduced count to %u", count);
        if (0 == count)
        {
            std::atomic_thread_fence(std::memory_order_acquire);
            delete this;
        }
    }

    /* ... */
};

Subclassed by omni::core::Implements< omni::str::IReadOnlyCString >

Public Functions

inline void *cast(omni::core::TypeId id) noexcept

See omni::core::IObject::cast.

Protected Functions

virtual ~ImplementsCast() noexcept = default
inline virtual void *cast_abi(TypeId id) noexcept override

Returns a pointer to the interface defined by the given type id if this object implements the type id’s interface.

Objects can support multiple interfaces, even interfaces that are in different inheritance chains.

The returned object will have omni::core::IObject::acquire() called on it before it is returned, meaning it is up to the caller to call omni::core::IObject::release() on the returned pointer.

The returned pointer can be safely reinterpret_cast<> to the type id’s C++ class. For example, “omni.windowing.IWindow” can be cast to omni::windowing::IWindow.

Do not directly use this method, rather use a wrapper function like omni::core::cast() or omni::core::ObjectPtr::as().

Thread Safety

This method is thread safe.