TrinityCore
Loading...
Searching...
No Matches
advstd.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITY_ADVSTD_H
19#define TRINITY_ADVSTD_H
20
21#include <version>
22
23#ifdef __cpp_lib_bit_cast
24#include <bit>
25#else
26#include <cstring> // for memcpy
27#endif
28#include <compare>
29
30// this namespace holds implementations of upcoming stdlib features that our c++ version doesn't have yet
31namespace advstd
32{
33// libc++ is missing these two
34[[nodiscard]] constexpr bool is_eq(std::partial_ordering cmp) noexcept { return cmp == 0; }
35[[nodiscard]] constexpr bool is_neq(std::partial_ordering cmp) noexcept { return cmp != 0; }
36
37#ifdef __cpp_lib_bit_cast
38using std::bit_cast;
39#else
40// libstdc++ v10 is missing this
41template <typename To, typename From,
42 std::enable_if_t<std::conjunction_v<
43 std::bool_constant<sizeof(To) == sizeof(From)>,
44 std::is_trivially_copyable<To>,
45 std::is_trivially_copyable<From>>, int> = 0>
46[[nodiscard]] constexpr To bit_cast(From const& from) noexcept
47{
48 To to;
49 std::memcpy(&to, &from, sizeof(To));
50 return to;
51}
52#endif
53}
54
55// std::forward_like
56#ifdef __cpp_lib_forward_like
57#include <utility>
58namespace advstd
59{
60using std::forward_like;
61}
62#else
63namespace advstd
64{
65template <class T, class U>
66[[nodiscard]] constexpr decltype(auto) forward_like(U&& value) noexcept
67{
68 using ValueU = std::remove_reference_t<U>;
69 using ValueConstU = std::conditional_t<std::is_const_v<std::remove_reference_t<T>>, ValueU const, ValueU>;
70 return static_cast<std::conditional_t<std::is_rvalue_reference_v<T&&>, ValueConstU&&, ValueConstU&>>(value);
71}
72}
73#endif
74
75// std::ranges::contains
76#include <algorithm>
77#ifndef __cpp_lib_ranges_contains
78#include <functional> // for std::ranges::equal_to, std::identity
79#include <iterator> // for std::input_iterator, std::sentinel_for, std::projected
80#endif
81
83{
84#ifndef __cpp_lib_ranges_contains
86{
87 template<std::input_iterator I, std::sentinel_for<I> S, class T, class Proj = std::identity>
88 requires std::indirect_binary_predicate<std::ranges::equal_to, std::projected<I, Proj>, T const*>
89 [[nodiscard]] inline constexpr bool operator()(I first, S last, T const& value, Proj proj = {}) const
90 {
91 return std::ranges::find(std::move(first), last, value, proj) != last;
92 }
93
94 template<std::ranges::input_range R, class T, class Proj = std::identity>
95 requires std::indirect_binary_predicate<std::ranges::equal_to, std::projected<std::ranges::iterator_t<R>, Proj>, T const*>
96 [[nodiscard]] inline constexpr bool operator()(R&& r, T const& value, Proj proj = {}) const
97 {
98 auto first = std::ranges::begin(r);
99 auto last = std::ranges::end(r);
100 return std::ranges::find(std::move(first), last, value, proj) != last;
101 }
102} inline constexpr contains;
103#else
104using std::ranges::contains;
105#endif
106}
107
108#endif
struct advstd::ranges::Contains contains
constexpr bool is_eq(std::partial_ordering cmp) noexcept
Definition advstd.h:34
constexpr decltype(auto) forward_like(U &&value) noexcept
Definition advstd.h:66
constexpr bool is_neq(std::partial_ordering cmp) noexcept
Definition advstd.h:35
constexpr To bit_cast(From const &from) noexcept
Definition advstd.h:46
constexpr bool operator()(R &&r, T const &value, Proj proj={}) const
Definition advstd.h:96
constexpr bool operator()(I first, S last, T const &value, Proj proj={}) const
Definition advstd.h:89