aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorW. J. van der Laan <laanwj@protonmail.com>2021-03-31 19:56:56 +0200
committerW. J. van der Laan <laanwj@protonmail.com>2021-03-31 20:17:39 +0200
commit602b038d433b8f35116c7149175bba191f690bb6 (patch)
tree28fd80b938f2f0c07a5619095ee4ede2215ad655 /src/util
parentb14462083f82aeaa9a376978f210db5538db296f (diff)
parent916ab0195d567fd0a9097045e73a6654c453adea (diff)
downloadbitcoin-602b038d433b8f35116c7149175bba191f690bb6.tar.xz
Merge #21366: refactor: replace util::Ref with std::any (C++17)
916ab0195d567fd0a9097045e73a6654c453adea remove unused class util::Ref and its unit test (Sebastian Falbesoner) 8dbb87a3932f81e23ba7afd865b9aeeb535f0c20 refactor: replace util::Ref by std::any (C++17) (Sebastian Falbesoner) 95cccf8a4b392959c1fd7ec0647e04eb13880865 util: introduce helper AnyPtr to access std::any instances (Sebastian Falbesoner) Pull request description: As described in `util/ref.h`: "_This implements a small subset of the functionality in C++17's std::any class, and **can be dropped when the project updates to C++17**_". For accessing the contained object of a `std::any` instance, a helper template function `AnyPtr` is introduced (thanks to ryanofsky). ACKs for top commit: hebasto: re-ACK 916ab0195d567fd0a9097045e73a6654c453adea, with command ryanofsky: Code review ACK 916ab0195d567fd0a9097045e73a6654c453adea. Changes since last review: rebase and replacing types with `auto`. I might have used `const auto*` and `auto*` instead of plain `auto` because I think the qualifiers are useful, but this is all good. Tree-SHA512: fe2c3e4f5726f8ad40c61128339bb24ad11d2c261f71f7b934b1efe3e3279df14046452b0d9b566917ef61d5c7e0fd96ccbf35ff810357e305710f5002c27d47
Diffstat (limited to 'src/util')
-rw-r--r--src/util/ref.h38
-rw-r--r--src/util/system.h13
2 files changed, 13 insertions, 38 deletions
diff --git a/src/util/ref.h b/src/util/ref.h
deleted file mode 100644
index 9685ea9fec..0000000000
--- a/src/util/ref.h
+++ /dev/null
@@ -1,38 +0,0 @@
-// Copyright (c) 2020 The Bitcoin Core developers
-// Distributed under the MIT software license, see the accompanying
-// file COPYING or http://www.opensource.org/licenses/mit-license.php.
-
-#ifndef BITCOIN_UTIL_REF_H
-#define BITCOIN_UTIL_REF_H
-
-#include <util/check.h>
-
-#include <typeindex>
-
-namespace util {
-
-/**
- * Type-safe dynamic reference.
- *
- * This implements a small subset of the functionality in C++17's std::any
- * class, and can be dropped when the project updates to C++17
- * (https://github.com/bitcoin/bitcoin/issues/16684)
- */
-class Ref
-{
-public:
- Ref() = default;
- template<typename T> Ref(T& value) { Set(value); }
- template<typename T> T& Get() const { CHECK_NONFATAL(Has<T>()); return *static_cast<T*>(m_value); }
- template<typename T> void Set(T& value) { m_value = &value; m_type = std::type_index(typeid(T)); }
- template<typename T> bool Has() const { return m_value && m_type == std::type_index(typeid(T)); }
- void Clear() { m_value = nullptr; m_type = std::type_index(typeid(void)); }
-
-private:
- void* m_value = nullptr;
- std::type_index m_type = std::type_index(typeid(void));
-};
-
-} // namespace util
-
-#endif // BITCOIN_UTIL_REF_H
diff --git a/src/util/system.h b/src/util/system.h
index 291f3f5541..29657e56e2 100644
--- a/src/util/system.h
+++ b/src/util/system.h
@@ -25,6 +25,7 @@
#include <util/threadnames.h>
#include <util/time.h>
+#include <any>
#include <exception>
#include <map>
#include <optional>
@@ -500,6 +501,18 @@ inline void insert(std::set<TsetT>& dst, const Tsrc& src) {
dst.insert(src.begin(), src.end());
}
+/**
+ * Helper function to access the contained object of a std::any instance.
+ * Returns a pointer to the object if passed instance has a value and the type
+ * matches, nullptr otherwise.
+ */
+template<typename T>
+T* AnyPtr(const std::any& any) noexcept
+{
+ T* const* ptr = std::any_cast<T*>(&any);
+ return ptr ? *ptr : nullptr;
+}
+
#ifdef WIN32
class WinCmdLineArgs
{