/*
 *  TaskManager.h
 *
 *  Created by leiwang@telenav.cn on 12/28/2011.
 *  Copyright 2011 TeleNav, Inc. All rights reserved.
 *
 */

#ifndef TASKMANAGER_H_H
#define TASKMANAGER_H_H
#include "foundation/private/config.hpp"

#pragma message("'foundation/TaskManager.h' is deprecated.")

#include "foundation/Function.h"
#include "foundation/SharedPtr.h"
#include "stdint.h"

namespace Tn
{
    namespace Foundation
    {
        /**
         * call this macro to make sure caller's information could be mark down.
         */

        #define RUN_TASK(...) RunTask(__FILE__,__LINE__,__VA_ARGS__)

        enum TaskPriority
        {
            TaskPriority_High = 0x0,
            TaskPriority_Medium,
            TaskPriority_Low
        };

        class Task
        {
            public:
                virtual ~Task(){};
        };

        class Logger;

        class TaskManager
        {
            public:

                /** 
                 * brief - schedules a functor for asynchronous execution.
                 * param func[in] The functor to be executed.
                 * param delay[in] delay time to run in millisecond.
                 * param prio[in] task's priority indicate importance.
                 * param IsPeriodical[in] If false, the functor will be executed once only. Otherwise executed cyclically.
                 * return a pointer by which you can remove the relative task.
                 */ 
                virtual shared_ptr<Task> RunTask(const std::string& file, const uint32_t line,function<void()> func,
                    uint32_t delay = 0,TaskPriority prio = TaskPriority_Low,bool IsPeriodical = false) = 0;

                /** 
                 * brief - schedules a new task, which is different from but has
                 *      similar fields with the given one, for asynchronous execution.
                 * param task[in] a task contains all info which will be set into a new task.
                 * param delay[in] delay time to run in millisecond.
                 * return a new task handle.
                 */ 
                virtual shared_ptr<Task> RunTask(const std::string& file, const uint32_t line,shared_ptr<Task> task,
                    uint32_t delay = 0) = 0;

                /**
                 * brief - remove a task associate with a taskHandle given.
                 * param taskHandle[in] task unique identifier.
                 */
                virtual void Remove(shared_ptr<Task> taskHandle) = 0;

                /**
                 * brief - resize the poolsize of threadpool that handled by TaskManager.
                 * param newsize[in] new poolsize of threadpool.
                 */
                virtual void Resize(uint32_t newsize) = 0;

                /**
                 * brief - set logger, which write down time stamp of both task's begin and end.
                 * param logger[in] the logger used to mark down callers' information.
                 */
                virtual void SetLogger(const shared_ptr<Logger>& logger) = 0;

                /**
                 * brief - shut down task manager.
                 *      It is better to call this function when App exit.
                 *      And once task manager is shut down, it can not be restarted.
                 */
                virtual void ShutDown() = 0;

        };

    }
}

#endif // TASKMANAGER_H_H
