any.h raw

   1  // Copyright (c) 2023-present The Bitcoin 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_ANY_H
   6  #define BITCOIN_UTIL_ANY_H
   7  
   8  #include <any>
   9  
  10  namespace util {
  11  
  12  /**
  13   * Helper function to access the contained object of a std::any instance.
  14   * Returns a pointer to the object if passed instance has a value and the type
  15   * matches, nullptr otherwise.
  16   */
  17  template<typename T>
  18  T* AnyPtr(const std::any& any) noexcept
  19  {
  20      T* const* ptr = std::any_cast<T*>(&any);
  21      return ptr ? *ptr : nullptr;
  22  }
  23  
  24  } // namespace util
  25  
  26  #endif // BITCOIN_UTIL_ANY_H
  27