// 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 IBackgroundTask.h
//!
//! @brief Defines @ref omni::graph::exec::unstable::IBackgroundTask.
#pragma once

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

#include <utility>

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

// forward declarations needed by interface declaration
class IBackgroundResult;
class IBackgroundResultWriter;
class IBackgroundTask;
class IBackgroundTask_abi;

//! Class representing a background task.
class IBackgroundTask_abi
    : public omni::core::Inherits<omni::graph::exec::unstable::IBase, OMNI_TYPE_ID("omni.graph.exec.unstable.IBackgroundTask")>
{
protected:
    //! Returns a @c std::future like object used to check if the background task has completed.
    //!
    //! A error is returned if this method is called more than once.
    //!
    //! This method is not thread safe.
    virtual OMNI_ATTR("throw_result") omni::core::Result
        getBackgroundResult_abi(OMNI_ATTR("not_null, throw_if_null, out, *return") IBackgroundResult** out) noexcept = 0;

    //! Completes async computation by setting a functor to the result as a shared state of the promise.
    //!
    //! It is the responsibility of the @ref omni::graph::exec::unstable::IBackgroundResult user to call @ref
    //! omni::graph::exec::unstable::IBackgroundResult::write() to invoke this given @ref
    //! omni::graph::exec::unstable::IBackgroundResultWriter.  This allows the task's waiter to optionally not consume
    //! the result of the task.
    //!
    //! @ref omni::core::IObject::acquire() is called on the given writer.
    //!
    //! A error is returned if this method is called more than once.
    //!
    //! This method is not thread safe.
    virtual OMNI_ATTR("no_api, throw_result") omni::core::Result
        setResultWriter_abi(OMNI_ATTR("not_null, throw_if_null") IBackgroundResultWriter* writer) noexcept = 0;
};

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

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

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

//! @copydoc omni::graph::exec::unstable::IBackgroundTask_abi
//!
//! @ingroup groupOmniGraphExecInterfaces
class omni::graph::exec::unstable::IBackgroundTask
    : public omni::core::Generated<omni::graph::exec::unstable::IBackgroundTask_abi>
{
public:
    //! Marks the task as ready and sets a completion writer that can be invoked by @ref IBackgroundResult.
    //!
    //! The supplied function should have the signature of `Status(ExecutionTask*)`.
    template <typename Fn>
    void setResultWriter(Fn&& fn);
};

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

template <typename Fn>
void omni::graph::exec::unstable::IBackgroundTask::setResultWriter(Fn&& fn)
{
    class Writer : public Implements<IBackgroundResultWriter>
    {
    public:
        Writer(Fn&& fn) : m_fn(std::move(fn))
        {
        }

    protected:
        Status write_abi(ExecutionTask* info) noexcept override
        {
            return m_fn(*info);
        }

    private:
        Fn m_fn;
    };

    setResultWriter_abi(omni::core::steal(new Writer(std::forward<Fn>(fn))).get());
}

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