Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_Any.H
Go to the documentation of this file.
1#ifndef AMREX_ANY_H_
2#define AMREX_ANY_H_
3#include <AMReX_Config.H>
4
5#include <concepts>
6#include <memory>
7#include <typeinfo>
8#include <type_traits>
9
10namespace amrex {
11
17class Any
18{
19public:
20
21 Any () = default;
22 ~Any () = default;
23
24 Any (Any const& rhs) = delete;
25 Any& operator= (Any const& rhs) = delete;
26
27 Any (Any && rhs) = default;
28 Any& operator= (Any && rhs) = default;
29
31 template <typename MF>
32 requires (!std::same_as<Any,std::remove_cvref_t<MF>>)
33 Any (MF && mf) // NOLINT(bugprone-forwarding-reference-overload)
34 : m_ptr(std::make_unique<innards<MF> >(std::forward<MF>(mf)))
35 {}
36
38 template <typename MF>
39 requires (!std::same_as<Any,std::remove_cvref_t<MF>>)
40 Any& operator= (MF && mf)
41 {
42 m_ptr = std::make_unique<innards<MF> >(std::forward<MF>(mf));
43 return *this;
44 }
45
47 [[nodiscard]] const std::type_info& Type () const {
48 if (m_ptr) {
49 return m_ptr->Type();
50 } else {
51 return typeid(void);
52 }
53 }
54
56 template <typename MF>
57 [[nodiscard]] MF& get () {
58 if (auto p0 = dynamic_cast<innards<MF>*>(m_ptr.get())) {
59 return p0->m_mf;
60 } else {
61 return dynamic_cast<innards<MF&>&>(*m_ptr).m_mf;
62 }
63 }
64
66 template <typename MF>
67 [[nodiscard]] MF const& get () const {
68 if (auto p0 = dynamic_cast<innards<MF>*>(m_ptr.get())) {
69 return p0->m_mf;
70 } else if (auto p1 = dynamic_cast<innards<MF&>*>(m_ptr.get())) {
71 return p1->m_mf;
72 } else {
73 return dynamic_cast<innards<MF const&> const&>(*m_ptr).m_mf;
74 }
75 }
76
79 template <typename MF>
80 [[nodiscard]] bool is () const { return Type() == typeid(MF); }
81
83 [[nodiscard]] bool hasValue () const { return m_ptr != nullptr; }
84
85private:
86
88
89 struct innards_base // NOLINT(cppcoreguidelines-special-member-functions)
90 {
91 [[nodiscard]] virtual const std::type_info& Type () const = 0;
92 virtual ~innards_base () = default;
93 };
94
95 template <typename MF>
96 struct innards final : innards_base // NOLINT(cppcoreguidelines-special-member-functions)
97 {
98 innards (MF && mf)
99 : m_mf(std::move(mf))
100 {}
101
102 ~innards () final = default;
103
104 [[nodiscard]] const std::type_info& Type () const final {
105 return typeid(MF);
106 }
107
108 MF m_mf;
109 };
110
112
113 std::unique_ptr<innards_base> m_ptr;
114};
115
116}
117
118#endif
Definition AMReX_Any.H:18
bool is() const
Definition AMReX_Any.H:80
const std::type_info & Type() const
Returns the contained type.
Definition AMReX_Any.H:47
~Any()=default
Any()=default
bool hasValue() const
Returns whether the object contains a value.
Definition AMReX_Any.H:83
MF const & get() const
Returns a const reference to the contained object.
Definition AMReX_Any.H:67
Any & operator=(Any const &rhs)=delete
MF & get()
Returns a reference to the contained object.
Definition AMReX_Any.H:57
Any(Any const &rhs)=delete
Any(Any &&rhs)=default
Definition AMReX_Amr.cpp:50