File indexing completed on 2024-05-26 05:34:42
0001 /* 0002 SPDX-FileCopyrightText: 2018 Ivan Čukić <ivan.cukic(at)kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #ifndef VOY_UTILS_H 0008 #define VOY_UTILS_H 0009 0010 #include <utility> 0011 0012 // This file contains some things that would be cool to have in 0013 // the standard library. There might be a chance some of these will 0014 // end up in C++20. And a few additional utility functions and macros. 0015 0016 #define voy_fwd(Var) std::forward<decltype(Var)>(Var) 0017 #define voy_fwd_invoke(Fn, Var) std::invoke(Fn, voy_fwd(Var)) 0018 0019 namespace voy::utils { 0020 0021 // Private copy constructor and copy assignment ensure classes derived 0022 // from class noncopyable cannot be copied. 0023 namespace disable_adl_ { 0024 class non_copyable { 0025 protected: 0026 constexpr non_copyable() = default; 0027 ~non_copyable() = default; 0028 0029 non_copyable(non_copyable&& other) = default; 0030 non_copyable& operator=(non_copyable&& other) = default; 0031 0032 private: 0033 non_copyable(const non_copyable&) = delete; 0034 non_copyable& operator=(const non_copyable&) = delete; 0035 }; 0036 } 0037 0038 using non_copyable = disable_adl_::non_copyable; 0039 0040 0041 // Identity function 0042 0043 struct identity { 0044 template <typename T> 0045 auto operator() (T&& value) const 0046 { 0047 return voy_fwd(value); 0048 } 0049 }; 0050 0051 0052 // Overloaded lambdas 0053 0054 template <typename... Fs> 0055 struct overloaded: Fs... { 0056 using Fs::operator()...; 0057 }; 0058 0059 template <typename... Fs> 0060 overloaded(Fs...) -> overloaded<Fs...>; 0061 0062 0063 // Execute code when exiting scope 0064 0065 template <typename F> 0066 struct on_scope_exit { 0067 on_scope_exit(F f) 0068 : m_deinit{std::move(f)} 0069 { 0070 } 0071 0072 ~on_scope_exit() 0073 { 0074 m_deinit(); 0075 } 0076 0077 F m_deinit; 0078 }; 0079 0080 0081 } // namespace voy::utils 0082 0083 #endif // include guard 0084