any.h 481 B

123456789101112131415161718192021222324252627
  1. //
  2. // any.h
  3. // program_args
  4. //
  5. // Created by Sam Jaffe on 2/25/23.
  6. //
  7. #pragma once
  8. #include <typeindex>
  9. namespace program::detail {
  10. class Any {
  11. public:
  12. Any() = default;
  13. template <typename T> Any(T const * ptr) : type_(typeid(T)), ptr_(ptr) {}
  14. template <typename T> T const * get() const {
  15. if (type_ != typeid(T)) { return nullptr; }
  16. return static_cast<T const *>(ptr_);
  17. }
  18. private:
  19. std::type_index type_ = typeid(void);
  20. void const * ptr_ = nullptr;
  21. };
  22. }