alpaqa
pi-pico
Nonconvex constrained optimization
Loading...
Searching...
No Matches
src
alpaqa
include
alpaqa
util
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
7
namespace
alpaqa
{
8
9
/// Like std::any, but storing just the pointer, without any dynamic allocation.
10
class
any_ptr
{
11
public
:
12
any_ptr
() =
default
;
13
template
<
class
T>
14
any_ptr
(
T
*
ptr
)
15
:
ptr
{
const_cast
<
void
*>(
reinterpret_cast
<
const
void
*>(
ptr
))},
16
ptr_type
{&
typeid
(
T
)},
is_const
{std::
is_const_v
<
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
alpaqa::any_ptr
Like std::any, but storing just the pointer, without any dynamic allocation.
Definition
any-ptr.hpp:10
alpaqa::any_ptr::is_const
bool is_const
Definition
any-ptr.hpp:32
alpaqa::any_ptr::any_ptr
any_ptr(T *ptr)
Definition
any-ptr.hpp:14
alpaqa::any_ptr::any_ptr
any_ptr()=default
alpaqa::any_ptr::ptr_type
const std::type_info * ptr_type
Definition
any-ptr.hpp:31
alpaqa::any_ptr::cast
T * cast() const
Definition
any-ptr.hpp:19
alpaqa::any_ptr::ptr
void * ptr
Definition
any-ptr.hpp:30
alpaqa
Definition
anderson.hpp:10
alpaqa::inf
constexpr const auto inf
Definition
config.hpp:112
Generated on Fri May 3 2024 for alpaqa by
1.9.8