omni/kit/exec/core/unstable/IExecutionControllerFactory.h

File members: omni/kit/exec/core/unstable/IExecutionControllerFactory.h

// Copyright (c) 2022-2024, NVIDIA CORPORATION. All rights reserved.
//
// NVIDIA CORPORATION and its licensors retain all intellectual property
// and proprietary rights in and to this software, related documentation
// and any modifications thereto. Any use, reproduction, disclosure or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA CORPORATION is strictly prohibited.
//

#pragma once

#include <omni/graph/exec/unstable/IBase.h>

namespace omni
{

namespace usd
{

class IUsdMutex;

}

namespace kit
{
namespace exec
{
namespace core
{
namespace unstable
{

// forward declarations needed by interface declaration
class IClearCallback;
class IExecutionController;
class IExecutionControllerFactory;
class IExecutionControllerFactory_abi;

class IExecutionControllerFactory_abi
    : public omni::core::Inherits<graph::exec::unstable::IBase,
                                  OMNI_TYPE_ID("omni.kit.exec.core.unstable.IExecutionControllerFactory")>
{
protected:
    virtual IExecutionController* createExecutionController_abi(OMNI_ATTR("not_null, c_str") const char* name,
                                                                OMNI_ATTR("in, out")
                                                                    omni::usd::IUsdMutex* const usdMutex) noexcept = 0;

    virtual void setDefaultExecutionController_abi(OMNI_ATTR("not_null") IExecutionController* controller) noexcept = 0;

    virtual IExecutionController* getDefaultExecutionController_abi() noexcept = 0;

    virtual void clear_abi(const uint64_t moduleHash) noexcept = 0;

    virtual void addClearCallback_abi(OMNI_ATTR("not_null") IClearCallback* callback) noexcept = 0;

    virtual void removeClearCallback_abi(IClearCallback* callback) noexcept = 0;

    virtual const uint64_t _generateUniqueHash_abi() noexcept = 0;
};

using ExecutionControllerFactoryPtr = omni::core::ObjectPtr<IExecutionControllerFactory>;

inline IExecutionControllerFactory* getExecutionControllerFactory() noexcept;

inline omni::core::ObjectPtr<IExecutionController> getDefaultExecutionController() noexcept;

inline const bool isDefaultExecutionControllerInExecute() noexcept;

template <typename Fn>
inline void addClearCallback(Fn&& fn) noexcept;

inline void removeClearCallbacks() noexcept;

#ifndef DOXYGEN_BUILD
namespace detail
{

inline std::vector<IClearCallback*>& getClearCallbacks() noexcept
{
    // we store raw pointers rather than ObjectPtr to avoid static destruction issues
    static std::vector<IClearCallback*> sCallbacks;
    return sCallbacks;
}

inline const uint64_t getModuleHash() noexcept;

} // namespace detail
#endif // DOXYGEN_BUILD

} // namespace unstable
} // namespace core
} // namespace exec
} // namespace kit
} // namespace omni

// generated API declaration
#define OMNI_BIND_INCLUDE_INTERFACE_DECL
#include <omni/kit/exec/core/unstable/IExecutionControllerFactory.gen.h>

class omni::kit::exec::core::unstable::IExecutionControllerFactory
    : public omni::core::Generated<omni::kit::exec::core::unstable::IExecutionControllerFactory_abi>
{
public:
    inline omni::core::ObjectPtr<omni::kit::exec::core::unstable::IExecutionController> createExecutionController(
        const char* name) noexcept;

    inline omni::core::ObjectPtr<omni::kit::exec::core::unstable::IExecutionController> createExecutionController(
        const char* name, omni::usd::IUsdMutex& usdMutex) noexcept;
};

inline omni::kit::exec::core::unstable::IExecutionControllerFactory* omni::kit::exec::core::unstable::
    getExecutionControllerFactory() noexcept
{
    // createType() always calls acquire() and returns an ObjectPtr to make sure release() is called. we don't want to
    // hold a ref here to avoid static destruction issues. here we allow the returned ObjectPtr to destruct (after
    // calling get()) to release our ref. we know the DLL in which the singleton was created is maintaining a ref and
    // will keep the singleton alive for the lifetime of the DLL.
    static auto sSingleton = omni::core::createType<IExecutionControllerFactory>().get();
    return sSingleton;
}

inline omni::core::ObjectPtr<omni::kit::exec::core::unstable::IExecutionController> omni::kit::exec::core::unstable::
    getDefaultExecutionController() noexcept
{
    return getExecutionControllerFactory()->getDefaultExecutionController();
}

inline const uint64_t omni::kit::exec::core::unstable::detail::getModuleHash() noexcept
{
    static std::vector<uint64_t> sUniqueHash;
    if (sUniqueHash.empty())
    {
        sUniqueHash.emplace_back(getExecutionControllerFactory()->_generateUniqueHash());
    }
    return sUniqueHash.front();
}

// additional headers needed for API implementation
#include <omni/core/IObject.h>
#include <omni/kit/exec/core/unstable/IClearCallback.h>
#include <omni/kit/exec/core/unstable/IExecutionController.h>

inline const bool omni::kit::exec::core::unstable::isDefaultExecutionControllerInExecute() noexcept
{
    const auto defaultExecutionController = getExecutionControllerFactory()->getDefaultExecutionController();
    return (defaultExecutionController && defaultExecutionController->getContext()->inExecute());
}

template <typename Fn>
inline void omni::kit::exec::core::unstable::addClearCallback(Fn&& fn) noexcept
{
    class Callback : public graph::exec::unstable::Implements<IClearCallback>
    {
    public:
        Callback(Fn&& fn) noexcept : m_fn(std::move(fn))
        {
        }

    protected:
        void onClear_abi() noexcept override
        {
            m_fn();
        }

        Fn m_fn;
    };

    Callback* callback = new Callback(std::forward<Fn>(fn));
    detail::getClearCallbacks().emplace_back(callback);
    getExecutionControllerFactory()->addClearCallback(callback);
}

inline void omni::kit::exec::core::unstable::removeClearCallbacks() noexcept
{
    auto factory = getExecutionControllerFactory();
    auto& callbacks = detail::getClearCallbacks();
    while (!callbacks.empty())
    {
        auto callback = callbacks.back();
        factory->removeClearCallback(callback);
        callback->release();
        callbacks.pop_back();
    }
}

inline omni::core::ObjectPtr<omni::kit::exec::core::unstable::IExecutionController> omni::kit::exec::core::unstable::
    IExecutionControllerFactory::createExecutionController(const char* name) noexcept
{
    return omni::core::steal(createExecutionController_abi(name, nullptr));
}

inline omni::core::ObjectPtr<omni::kit::exec::core::unstable::IExecutionController> omni::kit::exec::core::unstable::
    IExecutionControllerFactory::createExecutionController(const char* name, omni::usd::IUsdMutex& usdMutex) noexcept
{
    return omni::core::steal(createExecutionController_abi(name, &usdMutex));
}

// generated API implementation
#define OMNI_BIND_INCLUDE_INTERFACE_IMPL
#include <omni/kit/exec/core/unstable/IExecutionControllerFactory.gen.h>