// Copyright (c) 2022-2023, 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.
//

//! @file INodeFactory.h
//!
//! @brief Defines @ref omni::graph::exec::unstable::INodeFactory.
#pragma once

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

namespace omni
{
namespace graph
{
namespace exec
{
namespace unstable
{

// forward declarations needed by interface declaration
class IGraphBuilder;
class IDef;
class INode;
class INodeFactory;
class INodeFactory_abi;

//! Factory interface for creating @ref omni::graph::exec::unstable::INode objects.
//!
//! Usually used in conjunction with @ref omni::graph::exec::unstable::INodeGraphDef.
//!
//! See @ref omni::graph::exec::unstable::createNodeFactory() to generate one of these objects from an invocable object
//! (e.g. @c std::function).
class INodeFactory_abi
    : public omni::core::Inherits<omni::graph::exec::unstable::IBase, OMNI_TYPE_ID("omni.graph.exec.unstable.INodeFactory")>
{
protected:
    //! Creates and returns a new node within a topology this factory came from.
    //!
    //! It is legal to pass nullptr as a definition, or either @ref omni::graph::exec::unstable::INodeDef
    //! or @ref omni::graph::exec::unstable::INodeGraphDef
    //!
    //! The returned @ref omni::graph::exec::unstable::INode will have @ref omni::core::IObject::acquire() called on it.
    virtual OMNI_ATTR("throw_result") omni::core::Result
        createNode_abi(OMNI_ATTR("in, not_null, throw_if_null, c_str") const char* name,
                       IDef* def,
                       OMNI_ATTR("not_null, throw_if_null, out, *return") INode** out) noexcept = 0;
};

//! Smart pointer managing an instance of @ref INodeFactory.
using NodeFactoryPtr = omni::core::ObjectPtr<INodeFactory>;

//! Generates an @ref INodeFactory from an invocable object such as a function pointer, functor, etc.
//!
//! The given function should have the signature `omni::core::ObjectPtr<INode>(char*, IDef*)`.
template <typename Fn>
NodeFactoryPtr createNodeFactory(Fn&& fn);

} // namespace unstable
} // namespace exec
} // namespace graph
} // namespace omni

// generated API declaration
#define OMNI_BIND_INCLUDE_INTERFACE_DECL
#include <omni/graph/exec/unstable/INodeFactory.gen.h>

//! @copydoc omni::graph::exec::unstable::INodeFactory_abi
//!
//! @ingroup groupOmniGraphExecInterfaces
class omni::graph::exec::unstable::INodeFactory
    : public omni::core::Generated<omni::graph::exec::unstable::INodeFactory_abi>
{
};

// additional headers needed for API implementation
#include <omni/graph/exec/unstable/IGraphBuilder.h>
#include <omni/graph/exec/unstable/INode.h>

#ifndef DOXYGEN_BUILD

template <typename Fn>
omni::graph::exec::unstable::NodeFactoryPtr omni::graph::exec::unstable::createNodeFactory(Fn&& fn)
{
    class FactoryImpl : public Implements<INodeFactory>
    {
    public:
        FactoryImpl(Fn&& fn) : m_fn(std::move(fn))
        {
        }

    protected:
        omni::core::Result createNode_abi(const char* name, IDef* def, INode** out) noexcept override
        {
            try
            {
                NodePtr newNode = m_fn(name, def); // may throw
                *out = newNode.detach();
                return omni::core::kResultSuccess;
            }
            OMNI_GRAPH_EXEC_CATCH_ABI_EXCEPTION()
        }

        Fn m_fn;
    };

    return omni::core::steal(new FactoryImpl(std::forward<Fn>(fn)));
}

#endif // DOXYGEN_BUILD

// generated API implementation
#define OMNI_BIND_INCLUDE_INTERFACE_IMPL
#include <omni/graph/exec/unstable/INodeFactory.gen.h>
