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:
@@ -31,6 +31,10 @@
|
|||||||
#define TINS_TYPE_TRAITS_H
|
#define TINS_TYPE_TRAITS_H
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include "cxxstd.h"
|
||||||
|
#if TINS_IS_CXX11
|
||||||
|
#include <type_traits>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace Tins {
|
namespace Tins {
|
||||||
namespace Internals {
|
namespace Internals {
|
||||||
@@ -73,6 +77,40 @@ struct is_unsigned_integral<uint64_t> {
|
|||||||
static const bool value = true;
|
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
|
* \endcond
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -30,9 +30,6 @@
|
|||||||
#ifndef TINS_INTERNALS_H
|
#ifndef TINS_INTERNALS_H
|
||||||
#define TINS_INTERNALS_H
|
#define TINS_INTERNALS_H
|
||||||
|
|
||||||
#if TINS_IS_CXX11
|
|
||||||
#include <type_traits>
|
|
||||||
#endif
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "constants.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);
|
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 Internals
|
||||||
} // namespace Tins
|
} // namespace Tins
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -40,7 +40,7 @@
|
|||||||
#include "cxxstd.h"
|
#include "cxxstd.h"
|
||||||
#include "macros.h"
|
#include "macros.h"
|
||||||
#include "exceptions.h"
|
#include "exceptions.h"
|
||||||
#include "internals.h"
|
#include "detail/type_traits.h"
|
||||||
|
|
||||||
#ifdef TINS_HAVE_PCAP
|
#ifdef TINS_HAVE_PCAP
|
||||||
|
|
||||||
|
|||||||
@@ -45,6 +45,7 @@
|
|||||||
#include "pktap.h"
|
#include "pktap.h"
|
||||||
#include "sll.h"
|
#include "sll.h"
|
||||||
#include "ppi.h"
|
#include "ppi.h"
|
||||||
|
#include "internals.h"
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
using std::runtime_error;
|
using std::runtime_error;
|
||||||
|
|||||||
Reference in New Issue
Block a user