libtins  4.0
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 
40 namespace Tins {
41 namespace Internals {
46 template<bool, typename T = void>
47 struct enable_if {
48  typedef T type;
49 };
50 
51 template<typename T>
52 struct enable_if<false, T> {
53 
54 };
55 
56 template <typename T>
57 struct type_to_type {
58  typedef T type;
59 };
60 
61 template<typename T>
62 struct is_unsigned_integral {
63  static const bool value = false;
64 };
65 
66 template<>
67 struct is_unsigned_integral<uint8_t> {
68  static const bool value = true;
69 };
70 
71 template<>
72 struct is_unsigned_integral<uint16_t> {
73  static const bool value = true;
74 };
75 
76 template<>
77 struct is_unsigned_integral<uint32_t> {
78  static const bool value = true;
79 };
80 
81 template<>
82 struct 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
89 template <typename T, typename P, typename=void>
90 struct accepts_type : std::false_type { };
91 
92 template <typename T, typename P>
93 struct 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
100 template <typename Functor, typename Packet>
101 bool 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 
106 template <typename Functor, typename Packet>
107 bool 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 
112 template <typename Functor, typename Packet>
113 bool 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
Definition: hw_address.h:456
The Tins namespace.
Definition: address_range.h:38