61 lines
1.3 KiB
C++
61 lines
1.3 KiB
C++
#ifndef OGPC_PMANAGER_H_
|
|
#define OGPC_PMANAGER_H_
|
|
|
|
#include "BinaryBuffer.h"
|
|
#include <map>
|
|
#include <functional>
|
|
#include <cstdint>
|
|
#include <tuple>
|
|
#include <type_traits>
|
|
|
|
inline namespace {
|
|
|
|
template <typename T>
|
|
struct FuncArgTraits;
|
|
|
|
template <typename R, typename... T>
|
|
struct FuncArgTraits<R(*)(T...)> {
|
|
using type = std::tuple<T...>;
|
|
};
|
|
|
|
template <typename R, typename... T>
|
|
struct FuncArgTraits<std::function<R(T...)>> {
|
|
using type = std::tuple<T...>;
|
|
};
|
|
|
|
template <typename Callable>
|
|
struct FuncArgTraits : FuncArgTraits<decltype(&Callable::operator())> {};
|
|
|
|
template <typename ReturnType, typename ClassType, typename... Args>
|
|
struct FuncArgTraits<ReturnType(ClassType::*)(Args...) const> {
|
|
using type = std::tuple<Args...>;
|
|
};
|
|
|
|
}
|
|
|
|
class ProcedureManager {
|
|
public:
|
|
ProcedureManager();
|
|
|
|
template <typename T>
|
|
void registerProcedure(const std::uint32_t &id, T&& func) {
|
|
procedures[id] = [&func](BinaryBuffer &b) -> BinaryBuffer {
|
|
using Arg = typename FuncArgTraits<typename std::decay<T>::type>::type;
|
|
Arg x;
|
|
b >> x;
|
|
auto y = std::apply(func, std::move(x));
|
|
BinaryBuffer res;
|
|
res << y;
|
|
return res;
|
|
};
|
|
}
|
|
|
|
void handleCall(FILE *in, FILE *out) const;
|
|
|
|
private:
|
|
std::map<std::uint32_t, std::function<BinaryBuffer(BinaryBuffer&)>> procedures;
|
|
};
|
|
|
|
#endif
|
|
|