/*
 * Copyright (C) 2020-2021 Aloysius Indrayanto
 *
 * This file is part of the eCxx library, see LICENSE file for the license details.
 */


#include "TestUtility.h"


//
// Test for eCxx::function<>
//
static eCxx::int8_t times3(eCxx::int8_t v)
{ return v * 3; }

static eCxx::function<eCxx::int8_t(eCxx::int8_t)> f_times3 = times3;

static eCxx::function<eCxx::int8_t(eCxx::int8_t)> f_plus7  = [](eCxx::int8_t v) -> eCxx::int8_t { return v + 7; };


//
// Test for eCxx::Ext::function<>
//
struct Test {
    eCxx::Ext::function<void(eCxx::uint32_t*)> fp;

    inline Test(eCxx::Ext::function<void(eCxx::uint32_t*)> fp_)
    : fp(fp_)
    {}
};


static eCxx::Ext::SharedPtr<Test> createTestHandler(eCxx::Ext::function<void(eCxx::uint32_t&)> f)
{
    eCxx::Ext::SharedPtr<Test> handler = eCxx::Ext::makeSharedPtr<Test>( [f](eCxx::uint32_t* ptrVar) {
        f(*ptrVar);
    } );

    return handler;
}


//
// Test application
//
int main()
{
    _CODESTR_NL1_CODERES_NL2( "%d", (int) f_times3(-9) );
    _CODESTR_NL1_CODERES_NL2( "%d", (int) f_plus7(11) );
    _DIVIDERLINE_DASHSTR_NL2();

    _EXECUTECODE_CODESTR_NL2( eCxx::uint32_t var = 1000; );
    eCxx::Ext::SharedPtr<Test> handler = createTestHandler( [](eCxx::uint32_t& refVar) {
        _CODESTR_NL1_CODERES_NL1( "%d", (int) refVar );
        _EXECUTECODE_CODESTR_NL2( ++refVar; );
    } );
    handler->fp(&var);
    _CODESTR_NL1_CODERES_NL2( "%d", (int) var );

    return 0;
}
