libtins 4.5
Loading...
Searching...
No Matches
type_traits.h
1/*
2 * Copyright (c) 2017, Matias Fontanini
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30#ifndef TINS_TYPE_TRAITS_H
31#define TINS_TYPE_TRAITS_H
32
33#include <stdint.h>
34#include <tins/cxxstd.h>
35#if TINS_IS_CXX11
36 #include <type_traits>
37 #include <utility>
38#endif
39
40namespace Tins {
41namespace Internals {
46template<bool, typename T = void>
47struct enable_if {
48 typedef T type;
49};
50
51template<typename T>
52struct enable_if<false, T> {
53
54};
55
56template <typename T>
57struct type_to_type {
58 typedef T type;
59};
60
61template<typename T>
62struct is_unsigned_integral {
63 static const bool value = false;
64};
65
66template<>
67struct is_unsigned_integral<uint8_t> {
68 static const bool value = true;
69};
70
71template<>
72struct is_unsigned_integral<uint16_t> {
73 static const bool value = true;
74};
75
76template<>
77struct is_unsigned_integral<uint32_t> {
78 static const bool value = true;
79};
80
81template<>
82struct is_unsigned_integral<uint64_t> {
83 static const bool value = true;
84};
85
86#if TINS_IS_CXX11 && !defined(_MSC_VER)
87
88// Template metaprogramming trait to determine if a functor can accept another parameter as an argument
89template <typename T, typename P, typename=void>
90struct accepts_type : std::false_type { };
91
92template <typename T, typename P>
93struct accepts_type<T, P,
94 typename std::enable_if<
95 std::is_same< decltype( std::declval<T>()(std::declval<P>()) ), bool>::value
96 >::type
97> : std::true_type { };
98
99// use enable_if to invoke the Packet&& version of the sniff_loop handler if possible - otherwise fail to old behavior
100template <typename Functor, typename Packet>
101bool invoke_loop_cb(Functor& f, Packet& p,
102 typename std::enable_if<accepts_type<Functor, Packet>::value, bool>::type* = 0) {
103 return f(std::move(p));
104}
105
106template <typename Functor, typename Packet>
107bool invoke_loop_cb(Functor& f, Packet& p,
108 typename std::enable_if<!accepts_type<Functor, Packet>::value && accepts_type<Functor, Packet&>::value, bool>::type* = 0) {
109 return f(p);
110}
111
112template <typename Functor, typename Packet>
113bool invoke_loop_cb(Functor& f, Packet& p,
114 typename std::enable_if<!accepts_type<Functor, Packet>::value && !accepts_type<Functor, Packet&>::value, bool>::type* = 0) {
115 return f(*p.pdu());
116}
117
118#endif
119
124} // Internals
125} // Tins
126
127#endif // TINS_TYPE_TRAITS_H
The Tins namespace.
Definition address_range.h:38