Skip to content

Commit 95cccf8

Browse files
theStackryanofsky
andcommitted
util: introduce helper AnyPtr to access std::any instances
Co-authored-by: Russell Yanofsky <[email protected]>
1 parent 1c7be9a commit 95cccf8

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/util/system.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <util/threadnames.h>
2626
#include <util/time.h>
2727

28+
#include <any>
2829
#include <exception>
2930
#include <map>
3031
#include <optional>
@@ -500,6 +501,18 @@ inline void insert(std::set<TsetT>& dst, const Tsrc& src) {
500501
dst.insert(src.begin(), src.end());
501502
}
502503

504+
/**
505+
* Helper function to access the contained object of a std::any instance.
506+
* Returns a pointer to the object if passed instance has a value and the type
507+
* matches, nullptr otherwise.
508+
*/
509+
template<typename T>
510+
T* AnyPtr(const std::any& any) noexcept
511+
{
512+
T* const* ptr = std::any_cast<T*>(&any);
513+
return ptr ? *ptr : nullptr;
514+
}
515+
503516
#ifdef WIN32
504517
class WinCmdLineArgs
505518
{

0 commit comments

Comments
 (0)