From 95cccf8a4b392959c1fd7ec0647e04eb13880865 Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Fri, 5 Mar 2021 14:33:41 +0100 Subject: util: introduce helper AnyPtr to access std::any instances Co-authored-by: Russell Yanofsky --- src/util/system.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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 #include +#include #include #include #include @@ -500,6 +501,18 @@ inline void insert(std::set& 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 +T* AnyPtr(const std::any& any) noexcept +{ + T* const* ptr = std::any_cast(&any); + return ptr ? *ptr : nullptr; +} + #ifdef WIN32 class WinCmdLineArgs { -- cgit v1.2.3