Skip to content

Instantly share code, notes, and snippets.

@heatblazer
Last active March 5, 2021 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heatblazer/afed6dad5316e3ab731e474777c35e46 to your computer and use it in GitHub Desktop.
Save heatblazer/afed6dad5316e3ab731e474777c35e46 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <vector>
struct ResultT
{
//dummy data
std::string someData;
ResultT()
{
someData += "None|";
}
};
class RtpRFC
{
public:
bool valid() const { return false;}
RtpRFC& operator()(ResultT& rt) {
rt.someData += "RtpRFC|";
return *this;
}
};
struct StunRFC
{
StunRFC& operator()(ResultT& rt) {
rt.someData += "StunRFC|";
return *this;
}
bool valid() const {return true;}
};
template<typename T>
T packetHandlerT(ResultT& res, T protocol)
{
return protocol(res);
}
template <typename RFC, typename RES>
struct PairedWrapper
{
RFC first;
RES second;
PairedWrapper(RFC rfc, RES res) :
first{rfc},
second{res}
{}
};
/*terminator*/
ResultT VParse() {
return {};
}
template <typename T, typename...ARgs>
auto VParse(T type, ARgs...FArgs)
{
auto resultready = packetHandlerT(type.second, type.first);
if (resultready.valid())
return type.second;
// how to not pass the result ready here?
else {
return VParse(FArgs...);
}
}
int main(void)
{
ResultT someNetworkData[10];
// some loop with results
for(int i=0; i < 10; i++) {
auto res = someNetworkData[i];
auto outputstun = packetHandlerT<StunRFC>(res, StunRFC{});
auto outputrtp = packetHandlerT<RtpRFC>(res, RtpRFC{});
if (outputrtp.valid()); // do one
else if(outputstun.valid()); // do other
else; // if more ad more ifs
}
// parse specific w/Variardic
for(int i=0; i < 10; i++) {
auto res = someNetworkData[i];
//in the case we expect to see a sequence ot 1 or 2 rtp then STUN
auto result = VParse(PairedWrapper{RtpRFC{}, res} ,
PairedWrapper{RtpRFC{}, res},
PairedWrapper{RtpRFC{}, res},
PairedWrapper{StunRFC{}, res});
//get either StunRFC or RtpRfc or some other future protocol without
// going to the before example
//variardic template never shrinks, compile time reqursion
std::cout << result.someData << "\r\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment