#ifndef INCLUDED_BOBCAT_SIGNAL_
#define INCLUDED_BOBCAT_SIGNAL_

#include <csignal>
#include <vector>
#include <memory>

#include <bobcat/exception>

namespace FBB
{

class SignalHandler
{
    friend class Signal;

    public:
        virtual ~SignalHandler();
    private:
        virtual void signalHandler(size_t signum) = 0;
};

class Signal
{
    using  SignalHandlerVector = std::vector<SignalHandler *>;
    std::vector<SignalHandlerVector> d_signals;

    // d_signals is a vector of vectors of pointers to SignalHandlers
    // the row dimension refers to the signal number
    // each row holds a SignalHandler object handling that kind of signal


    static std::unique_ptr<Signal> s_signal;

    public:
        Signal(Signal const &) = delete;

        static Signal &instance();

        void add(size_t signum, SignalHandler &object);
        void remove(size_t signum, SignalHandler &object);

        void ignore(size_t signum);
        void reset(size_t signum);

    private:
        Signal();

        static void verify(size_t signum, char const *member);
        static void handler(int signum);                // called by remove
        void signal(int signum, sighandler_t handler);  // calls sigaction
};

}

#endif
