CppWAMP
C++11 client library for the WAMP protocol
traits.hpp
Go to the documentation of this file.
1 /*------------------------------------------------------------------------------
2  Copyright Butterfly Energy Systems 2014-2015, 2022.
3  Distributed under the Boost Software License, Version 1.0.
4  http://www.boost.org/LICENSE_1_0.txt
5 ------------------------------------------------------------------------------*/
6 
7 #ifndef CPPWAMP_TRAITS_HPP
8 #define CPPWAMP_TRAITS_HPP
9 
10 #include <tuple>
11 #include <type_traits>
12 #include <vector>
13 #include "api.hpp"
14 
15 //------------------------------------------------------------------------------
18 //------------------------------------------------------------------------------
19 
20 /* Hides EnableIf decorations in order to keep the generated Doxygen
21  documentation clean. */
22 #ifdef CPPWAMP_FOR_DOXYGEN
23 #define CPPWAMP_ENABLE_IF(cond)
24 #define CPPWAMP_ENABLED_TYPE(type, cond) type
25 #else
26 #define CPPWAMP_ENABLE_IF(cond) EnableIf<(cond)>
27 #define CPPWAMP_ENABLED_TYPE(type, cond) EnableIf<(cond), type>
28 #endif
29 
30 namespace wamp
31 {
32 
33 //------------------------------------------------------------------------------
35 //------------------------------------------------------------------------------
36 template<bool B, typename T = int>
37 using EnableIf = typename std::enable_if<B,T>::type;
38 
39 //------------------------------------------------------------------------------
41 //------------------------------------------------------------------------------
42 template<bool B, typename T = int>
43 using DisableIf = typename std::enable_if<!B,T>::type;
44 
45 //------------------------------------------------------------------------------
48 //------------------------------------------------------------------------------
49 template <typename T>
50 using ValueTypeOf =
51  typename std::remove_cv<typename std::remove_reference<T>::type>::type;
52 
53 //------------------------------------------------------------------------------
55 //------------------------------------------------------------------------------
56 template<typename T, typename U>
57 CPPWAMP_API constexpr bool isSameType() {return std::is_same<T, U>::value;}
58 
59 //------------------------------------------------------------------------------
61 //------------------------------------------------------------------------------
62 template <typename T>
63 CPPWAMP_API constexpr bool isBool()
64 {
65  // std::vector<bool>::const_reference is not just bool in clang/libc++.
66  return isSameType<T, bool>() ||
67  isSameType<T, std::vector<bool>::reference>() ||
68  isSameType<T, std::vector<bool>::const_reference>();
69 }
70 
71 //------------------------------------------------------------------------------
75 //------------------------------------------------------------------------------
76 template <typename T>
77 CPPWAMP_API constexpr bool isNumber()
78 {
79  return std::is_arithmetic<T>::value && !isSameType<T, bool>();
80 }
81 
82 //------------------------------------------------------------------------------
84 //------------------------------------------------------------------------------
85 template <typename T>
86 CPPWAMP_API constexpr bool isSignedInteger()
87 {
88  return std::is_integral<T>::value && std::is_signed<T>::value &&
89  !isSameType<T, bool>();
90 }
91 
92 //------------------------------------------------------------------------------
94 //------------------------------------------------------------------------------
95 template <typename T>
96 CPPWAMP_API constexpr bool isUnsignedInteger()
97 {
98  return std::is_integral<T>::value && !std::is_signed<T>::value &&
99  !isSameType<T, bool>();
100 }
101 
102 //------------------------------------------------------------------------------
104 //------------------------------------------------------------------------------
105 template<int N, typename... Ts> using NthTypeOf =
106  typename std::tuple_element<N, std::tuple<Ts...>>::type;
107 
108 //------------------------------------------------------------------------------
110 //------------------------------------------------------------------------------
111 template <bool B>
112 using BoolConstant = std::integral_constant<bool, B>;
113 
114 //------------------------------------------------------------------------------
116 //------------------------------------------------------------------------------
118 
119 //------------------------------------------------------------------------------
121 //------------------------------------------------------------------------------
123 
124 } // namespace wamp
125 
126 #endif // CPPWAMP_TRAITS_HPP
wamp::DisableIf
typename std::enable_if<!B, T >::type DisableIf
Metafunction used to disable overloads based on a boolean condition.
Definition: traits.hpp:43
wamp::FalseType
BoolConstant< false > FalseType
Equivalent to std::false_type provided in C++17.
Definition: traits.hpp:122
wamp::isSignedInteger
constexpr bool isSignedInteger()
Determines if the given type is a signed integer.
Definition: traits.hpp:86
wamp::TrueType
BoolConstant< true > TrueType
Equivalent to std::true_type provided in C++17.
Definition: traits.hpp:117
api.hpp
Defines macros related to exporting/importing APIs.
wamp::isSameType
constexpr bool isSameType()
Determines if a type is the same as another.
Definition: traits.hpp:57
wamp
Definition: anyhandler.hpp:36
wamp::isUnsignedInteger
constexpr bool isUnsignedInteger()
Determines if the given type is an unsigned integer.
Definition: traits.hpp:96
wamp::ValueTypeOf
typename std::remove_cv< typename std::remove_reference< T >::type >::type ValueTypeOf
Metafunction used to obtain the plain value type of a parameter passed by universal reference.
Definition: traits.hpp:51
wamp::EnableIf
typename std::enable_if< B, T >::type EnableIf
Metafunction used to enable overloads based on a boolean condition.
Definition: traits.hpp:37
wamp::isBool
constexpr bool isBool()
Determines if the given type is considered a boolean.
Definition: traits.hpp:63
wamp::BoolConstant
std::integral_constant< bool, B > BoolConstant
Equivalent to std::bool_constant provided in C++17.
Definition: traits.hpp:112
wamp::isNumber
constexpr bool isNumber()
Determines if the given type is considered a number.
Definition: traits.hpp:77
wamp::NthTypeOf
typename std::tuple_element< N, std::tuple< Ts... > >::type NthTypeOf
Metafunction that obtains the Nth type of a parameter pack.
Definition: traits.hpp:106