aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Falbesoner <sebastian.falbesoner@gmail.com>2021-03-05 14:33:41 +0100
committerSebastian Falbesoner <sebastian.falbesoner@gmail.com>2021-03-29 22:37:59 +0200
commit95cccf8a4b392959c1fd7ec0647e04eb13880865 (patch)
treea33339687a25a3680bd2ab52ef21da4ee2c8f3b0
parent1c7be9ab90af14d24f4668b02d9f07cec6f88a78 (diff)
util: introduce helper AnyPtr to access std::any instances
Co-authored-by: Russell Yanofsky <russ@yanofsky.org>
-rw-r--r--src/util/system.h13
1 files changed, 13 insertions, 0 deletions
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
{