1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +01:00

Move sniffer callback traits into detail/type_traits.h

This commit is contained in:
Matias Fontanini
2017-04-30 09:45:06 -07:00
parent 730e69463c
commit 92bda42ac1
4 changed files with 40 additions and 34 deletions

View File

@@ -31,6 +31,10 @@
#define TINS_TYPE_TRAITS_H
#include <stdint.h>
#include "cxxstd.h"
#if TINS_IS_CXX11
#include <type_traits>
#endif
namespace Tins {
namespace Internals {
@@ -73,6 +77,40 @@ struct is_unsigned_integral<uint64_t> {
static const bool value = true;
};
#if TINS_IS_CXX11 && !defined(_MSC_VER)
// Template metaprogramming trait to determine if a functor can accept another parameter as an argument
template <typename T, typename P, typename=void>
struct accepts_type : std::false_type { };
template <typename T, typename P>
struct accepts_type<T, P,
typename std::enable_if<
std::is_same< decltype( std::declval<T>()(std::declval<P>()) ), bool>::value
>::type
> : std::true_type { };
// use enable_if to invoke the Packet&& version of the sniff_loop handler if possible - otherwise fail to old behavior
template <typename Functor, typename Packet>
bool invoke_loop_cb(Functor& f, Packet& p,
typename std::enable_if<accepts_type<Functor, Packet>::value, bool>::type* = 0) {
return f(std::move(p));
}
template <typename Functor, typename Packet>
bool invoke_loop_cb(Functor& f, Packet& p,
typename std::enable_if<!accepts_type<Functor, Packet>::value && accepts_type<Functor, Packet&>::value, bool>::type* = 0) {
return f(p);
}
template <typename Functor, typename Packet>
bool invoke_loop_cb(Functor& f, Packet& p,
typename std::enable_if<!accepts_type<Functor, Packet>::value && !accepts_type<Functor, Packet&>::value, bool>::type* = 0) {
return f(*p.pdu());
}
#endif
/**
* \endcond
*/

View File

@@ -30,9 +30,6 @@
#ifndef TINS_INTERNALS_H
#define TINS_INTERNALS_H
#if TINS_IS_CXX11
#include <type_traits>
#endif
#include <string>
#include <stdint.h>
#include "constants.h"
@@ -190,36 +187,6 @@ inline bool is_dot3(const uint8_t* ptr, size_t sz) {
return (sz >= 13 && ptr[12] < 8);
}
#if TINS_IS_CXX11 && !defined(_MSC_VER)
// Template metaprogramming trait to determine if a functor can accept another parameter as an argument
template <typename T, typename P, typename=void>
struct accepts_type : std::false_type { };
template <typename T, typename P>
struct accepts_type<T, P,
typename std::enable_if<
std::is_same< decltype( std::declval<T>()(std::declval<P>()) ), bool>::value
>::type
> : std::true_type { };
// use enable_if to invoke the Packet&& version of the sniff_loop handler if possible - otherwise fail to old behavior
template <typename Functor, typename Packet>
bool invoke_loop_cb(Functor& f, Packet& p, typename std::enable_if<accepts_type<Functor, Packet>::value, bool>::type* = 0) {
return f(std::move(p));
}
template <typename Functor, typename Packet>
bool invoke_loop_cb(Functor& f, Packet& p, typename std::enable_if<!accepts_type<Functor, Packet>::value && accepts_type<Functor, Packet&>::value, bool>::type* = 0) {
return f(p);
}
template <typename Functor, typename Packet>
bool invoke_loop_cb(Functor& f, Packet& p, typename std::enable_if<!accepts_type<Functor, Packet>::value && !accepts_type<Functor, Packet&>::value, bool>::type* = 0) {
return f(*p.pdu());
}
#endif
} // namespace Internals
} // namespace Tins
/**

View File

@@ -40,7 +40,7 @@
#include "cxxstd.h"
#include "macros.h"
#include "exceptions.h"
#include "internals.h"
#include "detail/type_traits.h"
#ifdef TINS_HAVE_PCAP

View File

@@ -45,6 +45,7 @@
#include "pktap.h"
#include "sll.h"
#include "ppi.h"
#include "internals.h"
using std::string;
using std::runtime_error;