1 | // Copyright (C) 2023 Ahmad Samir <a.samirh78@gmail.com> |
2 | // Copyright (C) 2023 The Qt Company Ltd. |
3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
4 | |
5 | #ifndef Q20MAP_H |
6 | #define Q20MAP_H |
7 | |
8 | #include <QtCore/qtconfigmacros.h> |
9 | |
10 | #include <map> |
11 | #if __has_include(<memory_resource>) |
12 | # include <memory_resource> |
13 | #endif |
14 | |
15 | // |
16 | // W A R N I N G |
17 | // ------------- |
18 | // |
19 | // This file is not part of the Qt API. Types and functions defined in this |
20 | // file can reliably be replaced by their std counterparts, once available. |
21 | // You may use these definitions in your own code, but be aware that we |
22 | // will remove them once Qt depends on the C++ version that supports |
23 | // them in namespace std. There will be NO deprecation warning, the |
24 | // definitions will JUST go away. |
25 | // |
26 | // If you can't agree to these terms, don't use these definitions! |
27 | // |
28 | // We mean it. |
29 | // |
30 | |
31 | QT_BEGIN_NAMESPACE |
32 | |
33 | namespace q20 { |
34 | // like std::erase/std::erase_if for std::map |
35 | #if defined(__cpp_lib_erase_if) && __cpp_lib_erase_if >= 202002L // the one returning size_type |
36 | using std::erase_if; |
37 | #else |
38 | |
39 | // Make it more specialized than the compiler's, so that our implementation is preferred over |
40 | // the compiler's (which may be present, but return void instead of the number of erased elements). |
41 | |
42 | #define MAKE_OVERLOAD(map, allocator) \ |
43 | template <typename Key, typename T, typename Compare, typename Pred> \ |
44 | constexpr typename std::map<Key, T, Compare, std::allocator<std::pair<const Key, T>>>::size_type \ |
45 | erase_if(std::map<Key, T, Compare, std::allocator<std::pair<const Key, T>>> &c, Pred p) \ |
46 | { \ |
47 | const auto origSize = c.size(); \ |
48 | for (auto it = c.begin(), end = c.end(); it != end; /* erasing */) { \ |
49 | if (p(*it)) \ |
50 | it = c.erase(it); \ |
51 | else \ |
52 | ++it; \ |
53 | } \ |
54 | return origSize - c.size(); \ |
55 | } \ |
56 | /* end */ |
57 | |
58 | MAKE_OVERLOAD(map, allocator) |
59 | MAKE_OVERLOAD(multimap, allocator) |
60 | #ifdef __cpp_lib_polymorphic_allocator |
61 | MAKE_OVERLOAD(map, pmr::polymorphic_allocator) |
62 | MAKE_OVERLOAD(multimap, pmr::polymorphic_allocator) |
63 | #endif // __cpp_lib_polymorphic_allocator |
64 | |
65 | #undef MAKE_OVERLOAD |
66 | |
67 | #endif // __cpp_lib_erase_if |
68 | } // namespace q20 |
69 | |
70 | QT_END_NAMESPACE |
71 | |
72 | #endif /* Q20MAP_H */ |
73 | |