This commit is contained in:
139
include/sp/common/GenericHandler.h
Normal file
139
include/sp/common/GenericHandler.h
Normal file
@@ -0,0 +1,139 @@
|
||||
#pragma once
|
||||
|
||||
#include <tuple>
|
||||
|
||||
namespace sp
|
||||
{
|
||||
// This class is inspired by https://arobenko.gitbooks.io/comms-protocols-cpp/content/
|
||||
|
||||
// TCommon is common interface class for all the messages
|
||||
// TAll is all the message types, that need to be handled, bundled in std::tuple
|
||||
template <typename TAll>
|
||||
class GenericHandler;
|
||||
|
||||
// Big boy to process packets 20 by 20, preventing needlessly copying vtable many times at each inheritance stage
|
||||
template <typename T1, typename T2, typename T3, typename T4, typename T5,
|
||||
typename T6, typename T7, typename T8, typename T9, typename T10,
|
||||
typename T11, typename T12, typename T13, typename T14, typename T15,
|
||||
typename T16, typename T17, typename T18, typename T19, typename T20,
|
||||
typename... TRest>
|
||||
class GenericHandler<std::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T11, T13, T14, T15, T16, T17, T18, T19, T20, TRest...> > : public GenericHandler<std::tuple<TRest...> >
|
||||
{
|
||||
using Base = GenericHandler<std::tuple<TRest...> >;
|
||||
public:
|
||||
using Base::Handle; // Don't hide all Handle() functions from base classes
|
||||
virtual void Handle(const T1& msg) {}
|
||||
virtual void Handle(const T2& msg) {}
|
||||
virtual void Handle(const T3& msg) {}
|
||||
virtual void Handle(const T4& msg) {}
|
||||
virtual void Handle(const T5& msg) {}
|
||||
virtual void Handle(const T6& msg) {}
|
||||
virtual void Handle(const T7& msg) {}
|
||||
virtual void Handle(const T8& msg) {}
|
||||
virtual void Handle(const T9& msg) {}
|
||||
virtual void Handle(const T10& msg) {}
|
||||
virtual void Handle(const T11& msg) {}
|
||||
virtual void Handle(const T12& msg) {}
|
||||
virtual void Handle(const T13& msg) {}
|
||||
virtual void Handle(const T14& msg) {}
|
||||
virtual void Handle(const T15& msg) {}
|
||||
virtual void Handle(const T16& msg) {}
|
||||
virtual void Handle(const T17& msg) {}
|
||||
virtual void Handle(const T18& msg) {}
|
||||
virtual void Handle(const T19& msg) {}
|
||||
virtual void Handle(const T20& msg) {}
|
||||
};
|
||||
|
||||
// 10 by 10
|
||||
template <typename T1, typename T2, typename T3, typename T4, typename T5,
|
||||
typename T6, typename T7, typename T8, typename T9, typename T10,
|
||||
typename... TRest>
|
||||
class GenericHandler<std::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TRest...> > : public GenericHandler<std::tuple<TRest...> >
|
||||
{
|
||||
using Base = GenericHandler<std::tuple<TRest...> >;
|
||||
public:
|
||||
using Base::Handle; // Don't hide all Handle() functions from base classes
|
||||
virtual void Handle(const T1& msg) {}
|
||||
virtual void Handle(const T2& msg) {}
|
||||
virtual void Handle(const T3& msg) {}
|
||||
virtual void Handle(const T4& msg) {}
|
||||
virtual void Handle(const T5& msg) {}
|
||||
virtual void Handle(const T6& msg) {}
|
||||
virtual void Handle(const T7& msg) {}
|
||||
virtual void Handle(const T8& msg) {}
|
||||
virtual void Handle(const T9& msg) {}
|
||||
virtual void Handle(const T10& msg) {}
|
||||
};
|
||||
|
||||
// 5 by 5
|
||||
template <
|
||||
typename T1, typename T2, typename T3, typename T4, typename T5,
|
||||
typename... TRest>
|
||||
class GenericHandler<std::tuple<T1, T2, T3, T4, T5, TRest...> > : public GenericHandler<std::tuple<TRest...> >
|
||||
{
|
||||
using Base = GenericHandler<std::tuple<TRest...> >;
|
||||
public:
|
||||
using Base::Handle; // Don't hide all Handle() functions from base classes
|
||||
virtual void Handle(const T1& msg) {}
|
||||
virtual void Handle(const T2& msg) {}
|
||||
virtual void Handle(const T3& msg) {}
|
||||
virtual void Handle(const T4& msg) {}
|
||||
virtual void Handle(const T5& msg) {}
|
||||
};
|
||||
|
||||
// Deal with rest with 4 types
|
||||
template <typename T1, typename T2, typename T3, typename T4>
|
||||
class GenericHandler<std::tuple<T1, T2, T3, T4> >
|
||||
{
|
||||
public:
|
||||
virtual ~GenericHandler() {}
|
||||
virtual void Handle(const T1& msg) {}
|
||||
virtual void Handle(const T2& msg) {}
|
||||
virtual void Handle(const T3& msg) {}
|
||||
virtual void Handle(const T4& msg) {}
|
||||
// virtual void Handle(const TCommon&) {} //Nothing to do
|
||||
};
|
||||
|
||||
// Deal with rest with 3 types
|
||||
template < typename T1, typename T2, typename T3>
|
||||
class GenericHandler<std::tuple<T1, T2, T3> >
|
||||
{
|
||||
public:
|
||||
virtual ~GenericHandler() {}
|
||||
virtual void Handle(const T1& msg) {}
|
||||
virtual void Handle(const T2& msg) {}
|
||||
virtual void Handle(const T3& msg) {}
|
||||
// virtual void Handle(const TCommon&) {} //Nothing to do
|
||||
};
|
||||
|
||||
// Deal with rest with 2 types
|
||||
template <typename T1, typename T2>
|
||||
class GenericHandler<std::tuple<T1, T2> >
|
||||
{
|
||||
public:
|
||||
virtual ~GenericHandler() {}
|
||||
virtual void Handle(const T1& msg) {}
|
||||
virtual void Handle(const T2& msg) {}
|
||||
// virtual void Handle(const TCommon&) {} //Nothing to do
|
||||
};
|
||||
|
||||
// Deal with rest with 1 type
|
||||
template <typename T1>
|
||||
class GenericHandler<std::tuple<T1> >
|
||||
{
|
||||
public:
|
||||
virtual ~GenericHandler() {}
|
||||
virtual void Handle(const T1& msg) {}
|
||||
// virtual void Handle(const TCommon&) {} //Nothing to do
|
||||
};
|
||||
|
||||
// Deal with rest with 0 type
|
||||
template <>
|
||||
class GenericHandler<std::tuple<> >
|
||||
{
|
||||
public:
|
||||
virtual ~GenericHandler() {}
|
||||
// virtual void Handle(const TCommon&) {} //Nothing to do
|
||||
};
|
||||
|
||||
} // sp
|
||||
@@ -1,60 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <cxxabi.h>
|
||||
#include <string>
|
||||
|
||||
namespace sp {
|
||||
|
||||
template <typename T>
|
||||
std::string GetBasicClassName(const T& a_Value) {
|
||||
int status;
|
||||
char* demangled = abi::__cxa_demangle(typeid(a_Value).name(), 0, 0, &status);
|
||||
if (status != 0)
|
||||
return "";
|
||||
return std::string(demangled);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::string GetBasicClassName() {
|
||||
int status;
|
||||
char* demangled = abi::__cxa_demangle(typeid(T).name(), 0, 0, &status);
|
||||
if (status != 0)
|
||||
return "";
|
||||
return std::string(demangled);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
struct Reflector {
|
||||
static std::string GetClassName() {
|
||||
return GetBasicClassName<T>();
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Reflector<std::string> {
|
||||
static std::string GetClassName() {
|
||||
return "std::string";
|
||||
}
|
||||
};
|
||||
|
||||
template <typename K, typename V>
|
||||
struct Reflector<std::map<K, V>> {
|
||||
static std::string GetClassName();
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct Reflector<std::vector<T>> {
|
||||
static std::string GetClassName();
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
std::string Reflector<std::vector<T>>::GetClassName() {
|
||||
return "std::vector<" + Reflector<T>::GetClassName() + ">";
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
std::string Reflector<std::map<K, V>>::GetClassName() {
|
||||
return "std::map<" + Reflector<K>::GetClassName() + ", " + Reflector<V>::GetClassName() + ">";
|
||||
}
|
||||
|
||||
} // namespace sp
|
||||
@@ -7,12 +7,14 @@
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <ostream>
|
||||
#include <functional>
|
||||
|
||||
namespace sp {
|
||||
|
||||
class DataBuffer;
|
||||
|
||||
using ReadFunc = std::function<void(std::uint8_t&)>;
|
||||
|
||||
/**
|
||||
* \class VarInt
|
||||
* \brief Variable-length format such that smaller numbers use fewer bytes.
|
||||
@@ -57,6 +59,7 @@ class VarInt {
|
||||
*/
|
||||
friend DataBuffer& operator>>(DataBuffer& in, VarInt& var);
|
||||
|
||||
void Read(const ReadFunc& read);
|
||||
};
|
||||
|
||||
} // namespace sp
|
||||
|
||||
Reference in New Issue
Block a user