Line data Source code
1 : // Copyright (c) 2021-2026 The Dash Core developers 2 : // Distributed under the MIT software license, see the accompanying 3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : 5 : #ifndef BITCOIN_UTIL_HELPERS_H 6 : #define BITCOIN_UTIL_HELPERS_H 7 : 8 : #include <memory> 9 : #include <optional> 10 : #include <ranges> 11 : #include <string_view> 12 : 13 : namespace util { 14 : template <typename X, typename Z> 15 195006 : constexpr inline auto find_if_opt(const X& ds, const Z& fn) 16 : { 17 195006 : const auto it = std::ranges::find_if(ds, fn); 18 195006 : if (it != std::end(ds)) { 19 195005 : return std::make_optional(*it); 20 : } 21 1 : return std::optional<std::decay_t<decltype(*it)>>{}; 22 195006 : } 23 : 24 : template <typename T> 25 113458 : constexpr inline auto irange(const T& max) 26 : { 27 113458 : return std::views::iota(T{0}, max); 28 : } 29 : 30 : template <typename T> 31 22120 : inline bool shared_ptr_equal(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs) 32 : requires requires { *lhs == *rhs; } 33 : { 34 22120 : if (lhs == rhs) return true; // Same object or both blank 35 432 : if (!lhs || !rhs) return false; // Inequal initialization state 36 431 : return *lhs == *rhs; // Deep comparison 37 22120 : } 38 : 39 : template <typename T> 40 10383 : inline bool shared_ptr_not_equal(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs) 41 : requires requires { *lhs != *rhs; } 42 : { 43 10383 : if (lhs == rhs) return false; // Same object or both blank 44 41 : if (!lhs || !rhs) return true; // Inequal initialization state 45 40 : return *lhs != *rhs; // Deep comparison 46 10383 : } 47 : 48 2 : inline constexpr std::string_view to_string(bool value) { return value ? "true" : "false"; } 49 : } // namespace util 50 : 51 : #endif // BITCOIN_UTIL_HELPERS_H