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 5656719 : constexpr inline auto find_if_opt(const X& ds, const Z& fn) 16 : { 17 5656719 : const auto it = std::ranges::find_if(ds, fn); 18 5656719 : if (it != std::end(ds)) { 19 5656709 : return std::make_optional(*it); 20 : } 21 10 : return std::optional<std::decay_t<decltype(*it)>>{}; 22 5656719 : } 23 : 24 : template <typename T> 25 3399599 : constexpr inline auto irange(const T& max) 26 : { 27 3399599 : return std::views::iota(T{0}, max); 28 : } 29 : 30 : template <typename T> 31 543756 : inline bool shared_ptr_equal(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs) 32 : requires requires { *lhs == *rhs; } 33 : { 34 543756 : if (lhs == rhs) return true; // Same object or both blank 35 16539 : if (!lhs || !rhs) return false; // Inequal initialization state 36 16538 : return *lhs == *rhs; // Deep comparison 37 543756 : } 38 : 39 : template <typename T> 40 760010 : 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 760010 : if (lhs == rhs) return false; // Same object or both blank 44 9832 : if (!lhs || !rhs) return true; // Inequal initialization state 45 9831 : return *lhs != *rhs; // Deep comparison 46 760010 : } 47 : 48 6692 : inline constexpr std::string_view to_string(bool value) { return value ? "true" : "false"; } 49 : } // namespace util 50 : 51 : #endif // BITCOIN_UTIL_HELPERS_H