#include "AsynchronCall.h"

using namespace Framework;

// Content of the AsynchronCall class
// Constructor
//  f: The function to be called asynchronously
//  Must be called with new. The object deletes itself
AsynchronCall::AsynchronCall(std::function<void()> f)
    : Thread()
{
    this->finished = 0;
    this->f = f;
    start();
}

// Content of the AsynchronCall class
// Constructor
//  name: The name of the thread
//  f: The function to be called asynchronously
//  Must be called with new. The object deletes itself
AsynchronCall::AsynchronCall(const char* name, std::function<void()> f)
    : Thread()
{
    setName(name);
    this->finished = 0;
    this->f = f;
    start();
}

// Constructor
//  f: The function to be called asynchronously
//  finished: Set to 1 when the call has been processed
//  Must be called with new. The object deletes itself
AsynchronCall::AsynchronCall(std::function<void()> f, bool* finished)
    : Thread()
{
    this->finished = finished;
    this->f = f;
    if (finished) finished = 0;
    start();
}

// Constructor
//  name: The name of the thread
//  f: The function to be called asynchronously
//  finished: Set to 1 when the call has been processed
//  Must be called with new. The object deletes itself
AsynchronCall::AsynchronCall(
    const char* name, std::function<void()> f, bool* finished)
    : Thread()
{
    setName(name);
    this->finished = finished;
    this->f = f;
    if (finished) finished = 0;
    start();
}

void AsynchronCall::thread()
{
    f();
}

void AsynchronCall::threadEnd()
{
    Thread::threadEnd();
    if (finished) *finished = 1;
    release();
}