Skip to content

Commit

Permalink
Modernize standard library type traits
Browse files Browse the repository at this point in the history
* traits<...>::type -> traits_t<...>

* traits<...>::value -> traits_v<...>
  • Loading branch information
rmisev committed Oct 19, 2024
1 parent 8b4f8ad commit 2532f3d
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 47 deletions.
50 changes: 25 additions & 25 deletions include/upa/str_arg.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ using string_view = std::string_view;

template<class CharT>
struct is_char_type : std::integral_constant<bool,
std::is_same<CharT, char>::value ||
std::is_same_v<CharT, char> ||
#ifdef __cpp_char8_t
std::is_same<CharT, char8_t>::value ||
std::is_same_v<CharT, char8_t> ||
#endif
std::is_same<CharT, char16_t>::value ||
std::is_same<CharT, char32_t>::value ||
std::is_same<CharT, wchar_t>::value
std::is_same_v<CharT, char16_t> ||
std::is_same_v<CharT, char32_t> ||
std::is_same_v<CharT, wchar_t>
> {};

template<class SizeT>
struct is_size_type : std::integral_constant<bool,
std::is_convertible<SizeT, std::size_t>::value ||
std::is_convertible<SizeT, std::ptrdiff_t>::value
std::is_convertible_v<SizeT, std::size_t> ||
std::is_convertible_v<SizeT, std::ptrdiff_t>
> {};


Expand All @@ -62,14 +62,14 @@ class str_arg {
// output value type
using value_type =
// wchar_t type will be converted to char16_t or char32_t type equivalent by size
typename std::conditional<std::is_same<CharT, wchar_t>::value, std::conditional<sizeof(wchar_t) == sizeof(char16_t), char16_t, char32_t>::type,
std::conditional_t<std::is_same_v<CharT, wchar_t>, std::conditional_t<sizeof(wchar_t) == sizeof(char16_t), char16_t, char32_t>,
#ifdef __cpp_char8_t
// char8_t type will be converted to char type
typename std::conditional<std::is_same<CharT, char8_t>::value, char, input_type>::type
std::conditional_t<std::is_same_v<CharT, char8_t>, char, input_type>
#else
input_type
#endif
>::type;
>;

// constructors
str_arg(const str_arg&) noexcept = default;
Expand Down Expand Up @@ -123,7 +123,7 @@ class str_arg {
// String type helpers

template<class T>
using remove_cvptr_t = typename std::remove_cv<typename std::remove_pointer<T>::type>::type;
using remove_cvptr_t = std::remove_cv_t<std::remove_pointer_t<T>>;

namespace detail {
// See: https://stackoverflow.com/a/9154394
Expand Down Expand Up @@ -170,7 +170,7 @@ struct str_arg_char<CharT*, CharT*> : std::remove_cv<CharT> {
template<class CharT, class SizeT>
struct str_arg_char<CharT*, SizeT> : std::enable_if<
is_size_type<SizeT>::value,
typename std::remove_cv<CharT>::type> {
std::remove_cv_t<CharT>> {

template <typename T>
static str_arg<T> to_str_arg(const T* s, std::size_t length) {
Expand All @@ -191,7 +191,7 @@ struct str_arg_char<CharT*> : std::remove_cv<CharT> {
// one string class argument
template<class StrT>
struct str_arg_char<StrT> : std::enable_if<
std::is_pointer<detail::data_member_t<StrT>>::value &&
std::is_pointer_v<detail::data_member_t<StrT>> &&
is_size_type<detail::length_member_t<StrT>>::value,
remove_cvptr_t<detail::data_member_t<StrT>>> {

Expand All @@ -205,16 +205,16 @@ struct str_arg_char<StrT> : std::enable_if<
// String arguments helper types

template<class ...Args>
using str_arg_char_s = str_arg_char<typename std::decay<Args>::type...>;
using str_arg_char_s = str_arg_char<std::decay_t<Args>...>;

template<class ...Args>
using str_arg_char_t = typename str_arg_char_s<Args...>::type;


template<class ...Args>
using enable_if_str_arg_t = typename std::enable_if<
using enable_if_str_arg_t = std::enable_if_t<
is_char_type<str_arg_char_t<Args...>>::value,
int>::type;
int>;


// String arguments helper function
Expand All @@ -229,28 +229,28 @@ inline auto make_str_arg(Args&&... args) -> str_arg<str_arg_char_t<Args...>> {

template<class CharT>
struct is_char8_type : std::integral_constant<bool,
std::is_same<CharT, char>::value
std::is_same_v<CharT, char>
#ifdef __cpp_char8_t
|| std::is_same<CharT, char8_t>::value
|| std::is_same_v<CharT, char8_t>
#endif
> {};

template<class ...Args>
using enable_if_str_arg_to_char8_t = typename std::enable_if<
using enable_if_str_arg_to_char8_t = std::enable_if_t<
is_char8_type<str_arg_char_t<Args...>>::value,
int>::type;
int>;

template<class CharT>
struct is_charW_type : std::integral_constant<bool,
std::is_same<CharT, char16_t>::value ||
std::is_same<CharT, char32_t>::value ||
std::is_same<CharT, wchar_t>::value
std::is_same_v<CharT, char16_t> ||
std::is_same_v<CharT, char32_t> ||
std::is_same_v<CharT, wchar_t>
> {};

template<class ...Args>
using enable_if_str_arg_to_charW_t = typename std::enable_if<
using enable_if_str_arg_to_charW_t = std::enable_if_t<
is_charW_type<str_arg_char_t<Args...>>::value,
int>::type;
int>;


inline std::string&& make_string(std::string&& str) {
Expand Down
6 changes: 3 additions & 3 deletions include/upa/url.h
Original file line number Diff line number Diff line change
Expand Up @@ -1596,7 +1596,7 @@ namespace detail {
template <typename CharT>
inline validation_errc url_parser::url_parse(url_serializer& urls, const CharT* first, const CharT* last, const url* base, State state_override)
{
using UCharT = typename std::make_unsigned<CharT>::type;
using UCharT = std::make_unsigned_t<CharT>;

// remove all ASCII tab or newline from URL
simple_buffer<CharT> buff_no_ws;
Expand Down Expand Up @@ -2388,7 +2388,7 @@ inline void url_parser::parse_path(url_serializer& urls, const CharT* first, con

template <typename CharT>
inline bool url_parser::do_path_segment(const CharT* pointer, const CharT* last, std::string& output) {
using UCharT = typename std::make_unsigned<CharT>::type;
using UCharT = std::make_unsigned_t<CharT>;

// TODO-WARN: 2. [ 1 ... 2 ] validation error.
bool success = true;
Expand All @@ -2413,7 +2413,7 @@ inline bool url_parser::do_path_segment(const CharT* pointer, const CharT* last,

template <typename CharT>
inline bool url_parser::do_simple_path(const CharT* pointer, const CharT* last, std::string& output) {
using UCharT = typename std::make_unsigned<CharT>::type;
using UCharT = std::make_unsigned_t<CharT>;

// 3. of "opaque path state"
// TODO-WARN: 3. [ 1 ... 2 ] validation error.
Expand Down
4 changes: 2 additions & 2 deletions include/upa/url_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ inline bool contains_forbidden_host_char(const CharT* first, const CharT* last)

template <typename CharT>
inline validation_errc host_parser::parse_host(const CharT* first, const CharT* last, bool is_opaque, host_output& dest) {
using UCharT = typename std::make_unsigned<CharT>::type;
using UCharT = std::make_unsigned_t<CharT>;

// 1. Non-"file" special URL's cannot have an empty host.
// 2. For "file" URL's empty host is set in the file_host_state 1.2
Expand Down Expand Up @@ -314,7 +314,7 @@ inline validation_errc host_parser::parse_opaque_host(const CharT* first, const

//TODO: UTF-8 percent encode it using the C0 control percent-encode set
//detail::append_utf8_percent_encoded(first, last, detail::CHAR_C0_CTRL, str_host);
using UCharT = typename std::make_unsigned<CharT>::type;
using UCharT = std::make_unsigned_t<CharT>;

const CharT* pointer = first;
while (pointer < last) {
Expand Down
2 changes: 1 addition & 1 deletion include/upa/url_ip.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ inline validation_errc ipv4_parse_number(const CharT* first, const CharT* last,
//
template <typename CharT>
inline validation_errc ipv4_parse(const CharT* first, const CharT* last, uint32_t& ipv4) {
using UCharT = typename std::make_unsigned<CharT>::type;
using UCharT = std::make_unsigned_t<CharT>;

// 2. If the last item in parts is the empty string, then
// 1. IPv4-empty-part validation error. (TODO-WARN)
Expand Down
2 changes: 1 addition & 1 deletion include/upa/url_percent_encode.h
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ inline bool append_utf8_percent_encoded_char(const CharT*& first, const CharT* l

template<typename CharT>
inline void append_utf8_percent_encoded(const CharT* first, const CharT* last, const code_point_set& cpset, std::string& output) {
using UCharT = typename std::make_unsigned<CharT>::type;
using UCharT = std::make_unsigned_t<CharT>;

for (auto it = first; it < last; ) {
const auto uch = static_cast<UCharT>(*it);
Expand Down
16 changes: 8 additions & 8 deletions include/upa/url_search_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ template <typename T>
auto iterable_value(long) -> void;

template<class T>
using iterable_value_t = typename std::remove_cv<typename std::remove_reference<
using iterable_value_t = std::remove_cv_t<std::remove_reference_t<
decltype(iterable_value<T>(0))
>::type>::type;
>>;

// is iterable over the std::pair values
template<class T>
struct is_iterable_pairs : is_pair<iterable_value_t<T>> {};

// enable if `Base` is not the base class of `T`
template<class Base, class T>
using enable_if_not_base_of_t = typename std::enable_if<
!std::is_base_of<Base, typename std::decay<T>::type>::value, int
>::type;
using enable_if_not_base_of_t = std::enable_if_t<
!std::is_base_of_v<Base, std::decay_t<T>>, int
>;

} // namespace detail

Expand Down Expand Up @@ -102,7 +102,7 @@ class url_search_params
///
/// @param[in,out] other @c url_search_params object to move from
url_search_params(url_search_params&& other)
noexcept(std::is_nothrow_move_constructible<name_value_list>::value);
noexcept(std::is_nothrow_move_constructible_v<name_value_list>);

/// @brief Parsing constructor.
///
Expand All @@ -120,7 +120,7 @@ class url_search_params
template<class ConT,
// do not hide the copy and move constructors:
detail::enable_if_not_base_of_t<url_search_params, ConT> = 0,
typename std::enable_if<detail::is_iterable_pairs<ConT>::value, int>::type = 0
std::enable_if_t<detail::is_iterable_pairs<ConT>::value, int> = 0
>
explicit url_search_params(ConT&& cont) {
for (const auto& p : cont) {
Expand Down Expand Up @@ -447,7 +447,7 @@ inline url_search_params::url_search_params(const url_search_params& other)
// Move constructor

inline url_search_params::url_search_params(url_search_params&& other)
noexcept(std::is_nothrow_move_constructible<name_value_list>::value)
noexcept(std::is_nothrow_move_constructible_v<name_value_list>)
: params_(std::move(other.params_))
, is_sorted_(other.is_sorted_)
{}
Expand Down
14 changes: 7 additions & 7 deletions include/upa/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace upa::util {
// __attribute__((no_sanitize("unsigned-integer-overflow"))).

// Utility class to get unsigned (abs) max, min values of (signed) integer type
template <typename T, typename UT = typename std::make_unsigned<T>::type>
template <typename T, typename UT = std::make_unsigned_t<T>>
struct unsigned_limit {
static constexpr UT max() noexcept {
return static_cast<UT>(std::numeric_limits<T>::max());
Expand All @@ -46,8 +46,8 @@ struct unsigned_limit {
// Returns difference between a and b (a - b), if result is not representable
// by the type Out - throws exception.
template <typename Out, typename T,
typename UT = typename std::make_unsigned<T>::type,
typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
typename UT = std::make_unsigned_t<T>,
std::enable_if_t<std::is_integral_v<T>, int> = 0>
#if defined(__clang__)
__attribute__((no_sanitize("unsigned-integer-overflow")))
#endif
Expand All @@ -56,7 +56,7 @@ inline Out checked_diff(T a, T b) {
const UT diff = static_cast<UT>(static_cast<UT>(a) - static_cast<UT>(b));
if (diff <= unsigned_limit<Out>::max())
return static_cast<Out>(diff);
} else if (std::is_signed<Out>::value) {
} else if (std::is_signed_v<Out>) {
// b > a ==> diff >= 1
const UT diff = static_cast<UT>(static_cast<UT>(b) - static_cast<UT>(a));
if (diff <= unsigned_limit<Out>::min())
Expand All @@ -67,7 +67,7 @@ inline Out checked_diff(T a, T b) {

// Cast integer value to corresponding unsigned type

template <typename T, typename UT = typename std::make_unsigned<T>::type>
template <typename T, typename UT = std::make_unsigned_t<T>>
constexpr auto to_unsigned(T n) noexcept -> UT {
return static_cast<UT>(n);
}
Expand Down Expand Up @@ -105,14 +105,14 @@ inline std::size_t add_sizes(std::size_t size1, std::size_t size2, std::size_t m
#ifdef _MSC_VER
// the value_type of dest and src are the same (char)
template <class StrT,
typename std::enable_if<std::is_same<typename StrT::value_type, char>::value, int>::type = 0>
std::enable_if_t<std::is_same_v<typename StrT::value_type, char>, int> = 0>
inline void append(std::string& dest, const StrT& src) {
dest.append(src.begin(), src.end());
}

// the value_type of dest and src are different
template <class StrT,
typename std::enable_if<!std::is_same<typename StrT::value_type, char>::value, int>::type = 0>
std::enable_if_t<!std::is_same_v<typename StrT::value_type, char>, int> = 0>
inline void append(std::string& dest, const StrT& src) {
dest.reserve(add_sizes(dest.size(), src.size(), dest.max_size()));
for (const auto c : src)
Expand Down

0 comments on commit 2532f3d

Please sign in to comment.