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
10#include <concepts>
11#include <memory>
12#include <typeinfo>
13#include <type_traits>
14
15namespace amrex {
16
26class Any
27{
28public:
29
30 Any () = default;
31 ~Any () = default;
32
33 Any (Any const& rhs) = delete;
34 Any& operator= (Any const& rhs) = delete;
35
36 Any (Any && rhs) = default;
37 Any& operator= (Any && rhs) = default;
38
41 template <typename MF>
42 requires (!std::same_as<Any,std::remove_cvref_t<MF>>)
43 Any (MF && mf) // NOLINT(bugprone-forwarding-reference-overload)
44 : m_ptr(std::make_unique<innards<MF> >(std::forward<MF>(mf)))
45 {}
46
49 template <typename MF>
50 requires (!std::same_as<Any,std::remove_cvref_t<MF>>)
51 Any& operator= (MF && mf)
52 {
53 m_ptr = std::make_unique<innards<MF> >(std::forward<MF>(mf));
54 return *this;
55 }
56
58 [[nodiscard]] const std::type_info& Type () const {
59 if (m_ptr) {
60 return m_ptr->Type();
61 } else {
62 return typeid(void);
63 }
64 }
65
67 template <typename MF>
68 [[nodiscard]] MF& get () {
69 if (auto p0 = dynamic_cast<innards<MF>*>(m_ptr.get())) {
70 return p0->m_mf;
71 } else {
72 return dynamic_cast<innards<MF&>&>(*m_ptr).m_mf;
73 }
74 }
75
77 template <typename MF>
78 [[nodiscard]] MF const& get () const {
79 if (auto p0 = dynamic_cast<innards<MF>*>(m_ptr.get())) {
80 return p0->m_mf;
81 } else if (auto p1 = dynamic_cast<innards<MF&>*>(m_ptr.get())) {
82 return p1->m_mf;
83 } else {
84 return dynamic_cast<innards<MF const&> const&>(*m_ptr).m_mf;
85 }
86 }
87
90 template <typename MF>
91 [[nodiscard]] bool is () const { return Type() == typeid(MF); }
92
94 [[nodiscard]] bool hasValue () const { return m_ptr != nullptr; }
95
96private:
97
99
100 struct innards_base // NOLINT(cppcoreguidelines-special-member-functions)
101 {
102 [[nodiscard]] virtual const std::type_info& Type () const = 0;
103 virtual ~innards_base () = default;
104 };
105
106 template <typename MF>
107 struct innards final : innards_base // NOLINT(cppcoreguidelines-special-member-functions)
108 {
109 innards (MF && mf)
110 : m_mf(std::move(mf))
111 {}
112
113 ~innards () final = default;
114
115 [[nodiscard]] const std::type_info& Type () const final {
116 return typeid(MF);
117 }
118
119 MF m_mf;
120 };
121
123
124 std::unique_ptr<innards_base> m_ptr;
125};
126
127}
128
129#endif
Type-erased container similar to std::any, but supports move-only types.
Definition AMReX_Any.H:27
bool is() const
Definition AMReX_Any.H:91
const std::type_info & Type() const
Returns the contained type.
Definition AMReX_Any.H:58
~Any()=default
Any()=default
bool hasValue() const
Returns whether the object contains a value.
Definition AMReX_Any.H:94
MF const & get() const
Returns a const reference to the contained object.
Definition AMReX_Any.H:78
Any & operator=(Any const &rhs)=delete
MF & get()
Returns a reference to the contained object.
Definition AMReX_Any.H:68
Any(Any const &rhs)=delete
Any(Any &&rhs)=default
Definition AMReX_Amr.cpp:50