/*
 * @brief 该模块用于接收命令并执行命令, 然后将执行结果发送回发送着.
*/

#pragma once

#include <iostream>
#include <map>
#include "defines.hpp"

template<typename T>
class SingletonIter {
public:
    //获取单例的指针
    static T *iter() {
        static T* instance = nullptr;
        if (instance == nullptr) {
            instance = new T();
            instance->init();
        }
        return instance;
    }

    virtual ~SingletonIter() = default;

    SingletonIter(const SingletonIter &) = delete;                    //关闭拷贝构造函数
    SingletonIter &operator=(const SingletonIter &) = delete;        //关闭赋值函数

protected:
    //构造函数需要是 protected，这样子类才能继承；
    SingletonIter() {
    }

    virtual Error_manager init() {
        return Error_code::FAILED;
    }
};

template <class T>
class Executor: public SingletonIter<T> {
    friend class SingletonIter<T>;
    using listenMap = std::map<std::string, void*>;
public:
    ~Executor() = default;

    Error_manager init() {
        return Error_code::FAILED;
    }

    virtual int addListen(const std::string &topic, void* func) {
        listen_map.insert(std::pair<const std::string, void*>(topic, func));
        return true;
    }

    virtual int addListen(listenMap &listen) {
         listen_map.insert(listen.begin(), listen.end());
         return true;
     }

    virtual int setListen() {
        return true;
    }

    virtual int eraseListen() {
        return true;
    }

    virtual int clear() {
        listen_map.clear();
        return true;
    }

    virtual size_t size() {
        return listen_map.size();
    }
protected:
    Executor() = default;

protected:
    listenMap listen_map;
};

