Botcraft 1.21.4
Loading...
Searching...
No Matches
GenericHandler.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <tuple>
4
5namespace ProtocolCraft
6{
7 // This class is inspired by https://arobenko.gitbooks.io/comms-protocols-cpp/content/
8
9 // TCommon is common interface class for all the messages
10 // TAll is all the message types, that need to be handled, bundled in std::tuple
11 template <typename TCommon, typename TAll>
13
14 // Big boy to process packets 20 by 20, preventing needlessly copying vtable many times at each inheritance stage
15 template <typename TCommon,
16 typename T1, typename T2, typename T3, typename T4, typename T5,
17 typename T6, typename T7, typename T8, typename T9, typename T10,
18 typename T11, typename T12, typename T13, typename T14, typename T15,
19 typename T16, typename T17, typename T18, typename T19, typename T20,
20 typename... TRest>
21 class GenericHandler<TCommon, 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<TCommon, std::tuple<TRest...> >
22 {
23 using Base = GenericHandler<TCommon, std::tuple<TRest...> >;
24 public:
25 using Base::Handle; // Don't hide all Handle() functions from base classes
26 virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
27 virtual void Handle(T2& msg) { this->Handle(static_cast<TCommon&>(msg)); }
28 virtual void Handle(T3& msg) { this->Handle(static_cast<TCommon&>(msg)); }
29 virtual void Handle(T4& msg) { this->Handle(static_cast<TCommon&>(msg)); }
30 virtual void Handle(T5& msg) { this->Handle(static_cast<TCommon&>(msg)); }
31 virtual void Handle(T6& msg) { this->Handle(static_cast<TCommon&>(msg)); }
32 virtual void Handle(T7& msg) { this->Handle(static_cast<TCommon&>(msg)); }
33 virtual void Handle(T8& msg) { this->Handle(static_cast<TCommon&>(msg)); }
34 virtual void Handle(T9& msg) { this->Handle(static_cast<TCommon&>(msg)); }
35 virtual void Handle(T10& msg) { this->Handle(static_cast<TCommon&>(msg)); }
36 virtual void Handle(T11& msg) { this->Handle(static_cast<TCommon&>(msg)); }
37 virtual void Handle(T12& msg) { this->Handle(static_cast<TCommon&>(msg)); }
38 virtual void Handle(T13& msg) { this->Handle(static_cast<TCommon&>(msg)); }
39 virtual void Handle(T14& msg) { this->Handle(static_cast<TCommon&>(msg)); }
40 virtual void Handle(T15& msg) { this->Handle(static_cast<TCommon&>(msg)); }
41 virtual void Handle(T16& msg) { this->Handle(static_cast<TCommon&>(msg)); }
42 virtual void Handle(T17& msg) { this->Handle(static_cast<TCommon&>(msg)); }
43 virtual void Handle(T18& msg) { this->Handle(static_cast<TCommon&>(msg)); }
44 virtual void Handle(T19& msg) { this->Handle(static_cast<TCommon&>(msg)); }
45 virtual void Handle(T20& msg) { this->Handle(static_cast<TCommon&>(msg)); }
46 };
47
48 // 10 by 10
49 template <typename TCommon,
50 typename T1, typename T2, typename T3, typename T4, typename T5,
51 typename T6, typename T7, typename T8, typename T9, typename T10,
52 typename... TRest>
53 class GenericHandler<TCommon, std::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TRest...> > : public GenericHandler<TCommon, std::tuple<TRest...> >
54 {
55 using Base = GenericHandler<TCommon, std::tuple<TRest...> >;
56 public:
57 using Base::Handle; // Don't hide all Handle() functions from base classes
58 virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
59 virtual void Handle(T2& msg) { this->Handle(static_cast<TCommon&>(msg)); }
60 virtual void Handle(T3& msg) { this->Handle(static_cast<TCommon&>(msg)); }
61 virtual void Handle(T4& msg) { this->Handle(static_cast<TCommon&>(msg)); }
62 virtual void Handle(T5& msg) { this->Handle(static_cast<TCommon&>(msg)); }
63 virtual void Handle(T6& msg) { this->Handle(static_cast<TCommon&>(msg)); }
64 virtual void Handle(T7& msg) { this->Handle(static_cast<TCommon&>(msg)); }
65 virtual void Handle(T8& msg) { this->Handle(static_cast<TCommon&>(msg)); }
66 virtual void Handle(T9& msg) { this->Handle(static_cast<TCommon&>(msg)); }
67 virtual void Handle(T10& msg) { this->Handle(static_cast<TCommon&>(msg)); }
68 };
69
70 // 5 by 5
71 template <typename TCommon,
72 typename T1, typename T2, typename T3, typename T4, typename T5,
73 typename... TRest>
74 class GenericHandler<TCommon, std::tuple<T1, T2, T3, T4, T5, TRest...> > : public GenericHandler<TCommon, std::tuple<TRest...> >
75 {
76 using Base = GenericHandler<TCommon, std::tuple<TRest...> >;
77 public:
78 using Base::Handle; // Don't hide all Handle() functions from base classes
79 virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
80 virtual void Handle(T2& msg) { this->Handle(static_cast<TCommon&>(msg)); }
81 virtual void Handle(T3& msg) { this->Handle(static_cast<TCommon&>(msg)); }
82 virtual void Handle(T4& msg) { this->Handle(static_cast<TCommon&>(msg)); }
83 virtual void Handle(T5& msg) { this->Handle(static_cast<TCommon&>(msg)); }
84 };
85
86 // Deal with rest with 4 types
87 template <typename TCommon, typename T1, typename T2, typename T3, typename T4>
88 class GenericHandler<TCommon, std::tuple<T1, T2, T3, T4> >
89 {
90 public:
91 virtual ~GenericHandler() {}
92 virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
93 virtual void Handle(T2& msg) { this->Handle(static_cast<TCommon&>(msg)); }
94 virtual void Handle(T3& msg) { this->Handle(static_cast<TCommon&>(msg)); }
95 virtual void Handle(T4& msg) { this->Handle(static_cast<TCommon&>(msg)); }
96 virtual void Handle(TCommon&) { } //Nothing to do
97 };
98
99 // Deal with rest with 3 types
100 template <typename TCommon, typename T1, typename T2, typename T3>
101 class GenericHandler<TCommon, std::tuple<T1, T2, T3> >
102 {
103 public:
104 virtual ~GenericHandler() {}
105 virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
106 virtual void Handle(T2& msg) { this->Handle(static_cast<TCommon&>(msg)); }
107 virtual void Handle(T3& msg) { this->Handle(static_cast<TCommon&>(msg)); }
108 virtual void Handle(TCommon&) { } //Nothing to do
109 };
110
111 // Deal with rest with 2 types
112 template <typename TCommon, typename T1, typename T2>
113 class GenericHandler<TCommon, std::tuple<T1, T2> >
114 {
115 public:
116 virtual ~GenericHandler() {}
117 virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
118 virtual void Handle(T2& msg) { this->Handle(static_cast<TCommon&>(msg)); }
119 virtual void Handle(TCommon&) { } //Nothing to do
120 };
121
122 // Deal with rest with 1 type
123 template <typename TCommon, typename T1>
124 class GenericHandler<TCommon, std::tuple<T1> >
125 {
126 public:
127 virtual ~GenericHandler() {}
128 virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
129 virtual void Handle(TCommon&) { } //Nothing to do
130 };
131
132 // Deal with rest with 0 type
133 template <typename TCommon>
134 class GenericHandler<TCommon, std::tuple<> >
135 {
136 public:
137 virtual ~GenericHandler() {}
138 virtual void Handle(TCommon&) { } //Nothing to do
139 };
140} //ProtocolCraft
STL namespace.