#pragma once
#include "common.hpp"
#include "hooks.hpp"
#include "test.hpp"


/** simple array-wrapper, as we cannot typedef templates **/
template <class T> class FdTable {
    public:
        static const int size = (MAX_CLIENTS*4)+50; // client + upstream/parent (client splice + upstream splice are not supported by epoll but have to be considered here); leaves headroom for e.g. std-fds, accept fd, ipc fd, dns, ..

        INLINE FdTable() { reset(); } // should be rarely constructed, so this assumption might be ok to provide
        ~FdTable() {}

        INLINE void reset() { memset(&table, 0, sizeof(table)); }

        INLINE void set(int fd, const T& val) {
            assert(fd >= 0 && fd < size);
            table[fd] = val;
        }

        INLINE T& get(int fd) {
            assert(fd >= 0 && fd < size);
            return table[fd];
        }

        INLINE T& operator[](int fd) {
            assert(fd >= 0 && fd < size);
            return table[fd];
        }

    private:
        T table[size];
};


int get_maxfd(); ///< returns the maximum number of files a process can have open, one more than the largest possible value for a file descriptor.
int get_curr_maxfd(); ///< returns one more than the currently highest fd in use. TODO: could the FdTable track this?