File indexing completed on 2024-05-19 04:25:08

0001 /*
0002  *  SPDX-FileCopyrightText: 2019 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 #ifndef KISCPPQUIRKS_H
0007 #define KISCPPQUIRKS_H
0008 
0009 #include <type_traits>
0010 #include <optional>
0011 #include <version>
0012 #include <QDebug>
0013 
0014 namespace std {
0015 
0016 template <bool is_const, class T>
0017 struct add_const_if
0018 {
0019     using type = std::conditional_t<is_const, std::add_const_t<T>, T>;
0020 };
0021 
0022 template <bool is_const, class T>
0023 using add_const_if_t = typename add_const_if<is_const, T>::type;
0024 
0025 /**
0026  * copy_const returns type Dst with exactly the same const-qualifier
0027  * as type Src. In other words, it copies "constness" property from
0028  * type Src to Dst.
0029  */
0030 template <typename Src, typename Dst>
0031 struct copy_const {
0032     using type = add_const_if_t<std::is_const_v<Src>, std::remove_const_t<Dst>>;
0033 };
0034 
0035 template <typename Src, typename Dst>
0036 using copy_const_t = typename copy_const<Src, Dst>::type;
0037 
0038 } // namespace std
0039 
0040 template <typename T>
0041 [[maybe_unused]]
0042 QDebug operator<<(QDebug dbg, const std::optional<T> &t)
0043 {
0044 
0045     if (t) {
0046         dbg.nospace() << "std::optional(" << *t << ")";
0047     } else {
0048         dbg.nospace() << "std::optional(nullopt)";
0049     }
0050 
0051     return dbg.space();
0052 }
0053 
0054 
0055 #endif // KISCPPQUIRKS_H