| 1 |
/* |
| 2 |
Copyright (c) 2017 Arun Muralidharan |
| 3 |
|
| 4 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
of this software and associated documentation files (the "Software"), to deal |
| 6 |
in the Software without restriction, including without limitation the rights |
| 7 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 |
copies of the Software, and to permit persons to whom the Software is |
| 9 |
furnished to do so, subject to the following conditions: |
| 10 |
|
| 11 |
The above copyright notice and this permission notice shall be included in all |
| 12 |
copies or substantial portions of the Software. |
| 13 |
|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 |
SOFTWARE. |
| 21 |
*/ |
| 22 |
#ifndef CPP_JWT_META_HPP |
| 23 |
#define CPP_JWT_META_HPP |
| 24 |
|
| 25 |
#include <iterator> |
| 26 |
#include <type_traits> |
| 27 |
#include "jwt/string_view.hpp" |
| 28 |
|
| 29 |
namespace jwt { |
| 30 |
namespace detail { |
| 31 |
namespace meta { |
| 32 |
|
| 33 |
/** |
| 34 |
* The famous void_t trick. |
| 35 |
*/ |
| 36 |
template <typename... T> |
| 37 |
struct make_void |
| 38 |
{ |
| 39 |
using type = void; |
| 40 |
}; |
| 41 |
|
| 42 |
template <typename... T> |
| 43 |
using void_t = typename make_void<T...>::type; |
| 44 |
|
| 45 |
/** |
| 46 |
* A type tag representing an empty tag. |
| 47 |
* To be used to represent a `result-not-found` |
| 48 |
* situation. |
| 49 |
*/ |
| 50 |
struct empty_type {}; |
| 51 |
|
| 52 |
/** |
| 53 |
* A type list. |
| 54 |
*/ |
| 55 |
template <typename... T> struct list{}; |
| 56 |
|
| 57 |
|
| 58 |
/** |
| 59 |
*/ |
| 60 |
template <typename T, typename=void> |
| 61 |
struct has_create_json_obj_member: std::false_type |
| 62 |
{ |
| 63 |
}; |
| 64 |
|
| 65 |
template <typename T> |
| 66 |
struct has_create_json_obj_member<T, |
| 67 |
void_t< |
| 68 |
decltype( |
| 69 |
std::declval<T&&>().create_json_obj(), |
| 70 |
(void)0 |
| 71 |
) |
| 72 |
> |
| 73 |
>: std::true_type |
| 74 |
{ |
| 75 |
}; |
| 76 |
|
| 77 |
/** |
| 78 |
* Checks if the type `T` models MappingConcept. |
| 79 |
* |
| 80 |
* Requirements on type `T` for matching the requirements: |
| 81 |
* a. Must be able to construct jwt::string_view from the |
| 82 |
* `key_type` of the map. |
| 83 |
* b. Must be able to construct jwt::string_view from the |
| 84 |
* `mapped_type` of the map. |
| 85 |
* c. The type `T` must have an access operator i.e. operator[]. |
| 86 |
* d. The type `T` must have `begin` and `end` member functions |
| 87 |
* for iteration. |
| 88 |
* |
| 89 |
* NOTE: Requirements `a` and `b` means that the concept |
| 90 |
* type can only hold values that are string or constructible |
| 91 |
* to form a string_view (basically C strings and std::string) |
| 92 |
*/ |
| 93 |
template <typename T, typename=void> |
| 94 |
struct is_mapping_concept: std::false_type |
| 95 |
{ |
| 96 |
}; |
| 97 |
|
| 98 |
template <typename T> |
| 99 |
struct is_mapping_concept<T, |
| 100 |
void_t< |
| 101 |
typename std::enable_if< |
| 102 |
std::is_constructible<jwt::string_view, typename std::remove_reference_t<T>::key_type>::value, |
| 103 |
void |
| 104 |
>::type, |
| 105 |
|
| 106 |
typename std::enable_if< |
| 107 |
std::is_constructible<jwt::string_view, typename std::remove_reference_t<T>::mapped_type>::value, |
| 108 |
void |
| 109 |
>::type, |
| 110 |
|
| 111 |
decltype( |
| 112 |
std::declval<T&>().operator[](std::declval<typename std::remove_reference_t<T>::key_type>()), |
| 113 |
std::declval<T&>().begin(), |
| 114 |
std::declval<T&>().end(), |
| 115 |
(void)0 |
| 116 |
) |
| 117 |
> |
| 118 |
>: std::true_type |
| 119 |
{ |
| 120 |
}; |
| 121 |
|
| 122 |
|
| 123 |
/** |
| 124 |
* Checks if the type `T` models the ParameterConcept. |
| 125 |
* |
| 126 |
* Requirements on type `T` for matching the requirements: |
| 127 |
* a. The type must have a `get` method. |
| 128 |
*/ |
| 129 |
template <typename T, typename=void> |
| 130 |
struct is_parameter_concept: std::false_type |
| 131 |
{ |
| 132 |
}; |
| 133 |
|
| 134 |
template <typename T> |
| 135 |
struct is_parameter_concept<T, |
| 136 |
void_t< |
| 137 |
decltype( |
| 138 |
std::declval<T&>().get(), |
| 139 |
(void)0 |
| 140 |
) |
| 141 |
> |
| 142 |
>: std::true_type |
| 143 |
{ |
| 144 |
}; |
| 145 |
|
| 146 |
/** |
| 147 |
* Models SequenceConcept |
| 148 |
*/ |
| 149 |
template <typename T, typename=void> |
| 150 |
struct is_sequence_concept: std::false_type |
| 151 |
{ |
| 152 |
}; |
| 153 |
|
| 154 |
/// For array types |
| 155 |
template <typename T> |
| 156 |
struct is_sequence_concept<T, |
| 157 |
void_t< |
| 158 |
std::enable_if_t<std::is_array<std::decay_t<T>>::value>, |
| 159 |
|
| 160 |
std::enable_if_t< |
| 161 |
std::is_constructible<jwt::string_view, |
| 162 |
std::remove_reference_t<decltype(*std::begin(std::declval<T&>()))>>::value |
| 163 |
> |
| 164 |
> |
| 165 |
>: std::true_type |
| 166 |
{ |
| 167 |
}; |
| 168 |
|
| 169 |
template <typename T> |
| 170 |
struct is_sequence_concept<T, |
| 171 |
void_t< |
| 172 |
std::enable_if_t< |
| 173 |
std::is_base_of< |
| 174 |
std::forward_iterator_tag, |
| 175 |
typename std::remove_reference_t<T>::iterator::iterator_category |
| 176 |
>::value>, |
| 177 |
|
| 178 |
std::enable_if_t< |
| 179 |
std::is_constructible<jwt::string_view, typename std::remove_reference_t<T>::value_type>::value |
| 180 |
>, |
| 181 |
|
| 182 |
decltype( |
| 183 |
std::declval<T&>().begin(), |
| 184 |
std::declval<T&>().end(), |
| 185 |
(void)0 |
| 186 |
) |
| 187 |
> |
| 188 |
>: std::true_type |
| 189 |
{ |
| 190 |
}; |
| 191 |
|
| 192 |
|
| 193 |
/** |
| 194 |
* Find if a type is present in the typelist. |
| 195 |
* Eg: has_type<int, list<int, char, float>>{} == true |
| 196 |
* has_type<long, list<int, char, float>>{} == false |
| 197 |
*/ |
| 198 |
template <typename F, typename T> struct has_type; |
| 199 |
|
| 200 |
template <typename F> |
| 201 |
struct has_type<F, list<>>: std::false_type |
| 202 |
{ |
| 203 |
}; |
| 204 |
|
| 205 |
template <typename F, typename... T> |
| 206 |
struct has_type<F, list<F, T...>>: std::true_type |
| 207 |
{ |
| 208 |
}; |
| 209 |
|
| 210 |
template <typename F, typename H, typename... T> |
| 211 |
struct has_type<F, list<H,T...>>: has_type<F, list<T...>> |
| 212 |
{ |
| 213 |
}; |
| 214 |
|
| 215 |
/** |
| 216 |
* A pack of bools for the bool trick. |
| 217 |
*/ |
| 218 |
template <bool... V> |
| 219 |
struct bool_pack {}; |
| 220 |
|
| 221 |
/** |
| 222 |
*/ |
| 223 |
template <bool... B> |
| 224 |
using all_true = std::is_same<bool_pack<true, B...>, bool_pack<B..., true>>; |
| 225 |
|
| 226 |
/** |
| 227 |
*/ |
| 228 |
template <typename... T> |
| 229 |
using are_all_params = all_true<is_parameter_concept<T>::value...>; |
| 230 |
|
| 231 |
|
| 232 |
} // END namespace meta |
| 233 |
} // END namespace detail |
| 234 |
} // END namespace jwt |
| 235 |
|
| 236 |
#endif |