neuray API Programmer's Manual

interface_implement.h File Reference

Description

Mixin class template for deriving interface implementations.

Code Example

interface_implement.h

‎/***************************************************************************************************
 * Copyright 2023 NVIDIA Corporation. All rights reserved.
 **************************************************************************************************/

#ifndef MI_BASE_INTERFACE_IMPLEMENT_H
#define MI_BASE_INTERFACE_IMPLEMENT_H

#include <mi/base/types.h>
#include <mi/base/uuid.h>
#include <mi/base/atom.h>

namespace mi {
namespace base {

// Forward declaration
class IInterface;

template <class I>
class Interface_implement : public I
{
public:
    typedef I Interface;

    Interface_implement( Uint32 initial=1)
        : m_refcnt( initial)
    {
    }

    Interface_implement( const Interface_implement< I>& other)
        : m_refcnt( 1)
    {
        // avoid warning
        (void) other;
    }

    Interface_implement< I>& operator=( const Interface_implement< I>& other)
    {
        // Note: no call of operator= on m_refcount
        // avoid warning
        (void) other;
        return *this;
    }

#if (__cplusplus >= 201103L)
    Interface_implement(Interface_implement&& other)
    : m_refcnt{other.m_refcnt.swap(0)}
    {
    }

    Interface_implement& operator=(Interface_implement&& other)
    {
        other.m_refcnt = m_refcnt.swap(other.m_refcnt);
        return *this;
    }
#endif

    virtual Uint32 retain() const
    {
        return ++m_refcnt;
    }

    virtual Uint32 release() const
    {
        Uint32 cnt = --m_refcnt;
        if( !cnt)
            delete this;
        return cnt;
    }

    virtual const IInterface* get_interface( const Uuid& interface_id) const
    {
        return I::get_interface_static( this, interface_id);
    }

    virtual IInterface* get_interface( const Uuid& interface_id)
    {
        return I::get_interface_static( this, interface_id);
    }

    using I::get_interface;

    Uuid 
               get_iid() const
    {
        return typename I::IID();
    }

protected:
    Atom32& refcount() const { return m_refcnt; }

protected:
    virtual ~Interface_implement() {}

private:
    mutable Atom32 m_refcnt;
};


template <class I1, class I2>
class Interface_implement_2 : public I1, public I2
{
public:
    Interface_implement_2( Uint32 initial=1)
        : m_refcnt( initial)
    {
    }

    Interface_implement_2( const Interface_implement_2< I1, I2>& other)
        : m_refcnt( 1)
    {
        // avoid warning
        (void) other;
    }

    Interface_implement_2< I1, I2>& operator=( const Interface_implement_2< I1, I2>& other)
    {
        // Note: no call of operator= on m_refcount
        // avoid warning
        (void) other;
        return *this;
    }

    virtual Uint32 
               retain() const
    {
        return ++m_refcnt;
    }

    virtual Uint32 
               release() const
    {
        Uint32 cnt = --m_refcnt;
        if( !cnt)
            delete this;
        return cnt;
    }

    virtual const IInterface* get_interface( const Uuid& interface_id) const
    {
        const IInterface* iptr = I1::get_interface_static( static_cast<const I1*>(this),
                                                           interface_id);
        if ( iptr == 0)
            iptr = I2::get_interface_static( static_cast<const I2*>(this), interface_id);
        return iptr;
    }

    virtual IInterface* get_interface( const Uuid& interface_id)
    {
        IInterface* iptr = I1::get_interface_static(static_cast<I1*>(this),interface_id);
        if ( iptr == 0)
            iptr = I2::get_interface_static( static_cast<I2*>(this), interface_id);
        return iptr;
    }

    using I1::get_interface;

    Uuid 
               get_iid() const
    {
        return typename I1::IID();
    }

protected:
    virtual ~Interface_implement_2() {}

private:
    mutable Atom32 m_refcnt;
};


template <class I>
class Interface_implement_singleton : public I
{
public:
    virtual Uint32 
               retain() const
    {
        return 1;
    }

    virtual Uint32 
               release() const
    {
        return 1;
    }

    virtual const IInterface* get_interface( const Uuid& interface_id) const
    {
        return I::get_interface_static( this, interface_id);
    }

    virtual IInterface* get_interface( const Uuid& interface_id)
    {
        return I::get_interface_static( this, interface_id);
    }

    using I::get_interface;

    Uuid 
               get_iid() const
    {
        return typename I::IID();
    }

protected:
    virtual ~Interface_implement_singleton() {}
};

 // end group mi_base_iinterface

} // namespace base
} // namespace mi

#endif // MI_BASE_INTERFACE_IMPLEMENT_H

Namespaces

namespace 
Common namespace for APIs of NVIDIA Advanced Rendering Center GmbH. More...
namespace 
Namespace for the Base API. More...

Classes

class 
More...
class 
Mixin class template for deriving interface implementations from two interfaces. More...
class 
Mixin class template for deriving singleton interface implementations, where the reference count is fixed to one. More...