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

#include <OgnTestDynamicAttributeRawDataDatabase.h>


namespace omni {
namespace graph {
namespace test {

namespace
{
    template<typename TAttribute, typename TValue>
    void writeRawData(TAttribute& runtimeAttribute, TValue value)
    {
        uint8_t* dstData{ nullptr };
        size_t size{ 0 };
        runtimeAttribute.rawData(dstData, size);
        memcpy(dstData, &value, size);
    }
}

// This node validates the APIs for accessing and mutating the dynamic node attributes (inputs, outputs and state)
class OgnTestDynamicAttributeRawData
{
public:
    static bool compute(OgnTestDynamicAttributeRawDataDatabase& db)
    {
        auto dynamicInputs = db.getDynamicInputs();
        auto dynamicOutputs = db.getDynamicOutputs();
        auto dynamicStates = db.getDynamicStates();
        if (dynamicInputs.empty() || dynamicOutputs.empty() || dynamicStates.empty())
        {
            return false;
        }

        int sum = 0;
        for (auto const& input : dynamicInputs)
        {
            ConstRawPtr dataPtr{ nullptr };
            size_t size{ 0 };
            input().rawData(dataPtr, size);

            sum += *reinterpret_cast<int const*>(dataPtr);
        }

        // ensure that dynamic output and state attributes can be written to using the raw data pointer
        writeRawData(dynamicOutputs[0](), sum);
        writeRawData(dynamicStates[0](), sum);

        return true;
    }
};

REGISTER_OGN_NODE()

} // test
} // namespace graph
} // namespace omni
