alpaqa pi-pico
Nonconvex constrained optimization
Loading...
Searching...
No Matches
any-ptr.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <any>
4#include <type_traits>
5#include <typeinfo>
6
7namespace alpaqa {
8
9/// Like std::any, but storing just the pointer, without any dynamic allocation.
10class any_ptr {
11 public:
12 any_ptr() = default;
13 template <class T>
17
18 template <class T>
19 T *cast() const {
20 if (!ptr_type)
21 return nullptr;
22 if (typeid(T) != *ptr_type)
23 throw std::bad_any_cast();
24 if (std::is_const_v<T> != is_const)
25 throw std::bad_any_cast();
26 return reinterpret_cast<T *>(ptr);
27 }
28
29 private:
30 void *ptr = nullptr;
31 const std::type_info *ptr_type = nullptr;
32 bool is_const = true;
33};
34
35} // namespace alpaqa
Like std::any, but storing just the pointer, without any dynamic allocation.
Definition any-ptr.hpp:10
any_ptr(T *ptr)
Definition any-ptr.hpp:14
any_ptr()=default
const std::type_info * ptr_type
Definition any-ptr.hpp:31
T * cast() const
Definition any-ptr.hpp:19
constexpr const auto inf
Definition config.hpp:112