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 <memory>
6#include <typeinfo>
7#include <type_traits>
8
9namespace amrex {
10
16class Any
17{
18public:
19
20 Any () = default;
21 ~Any () = default;
22
23 Any (Any const& rhs) = delete;
24 Any& operator= (Any const& rhs) = delete;
25
26 Any (Any && rhs) = default;
27 Any& operator= (Any && rhs) = default;
28
30 template <typename MF,
31 std::enable_if_t<!std::is_same_v<Any,std::decay_t<MF>>,int> = 0>
32 Any (MF && mf) // NOLINT(bugprone-forwarding-reference-overload)
33 : m_ptr(std::make_unique<innards<MF> >(std::forward<MF>(mf)))
34 {}
35
37 template <typename MF,
38 std::enable_if_t<!std::is_same_v<Any,std::decay_t<MF>>,int> = 0>
39 Any& operator= (MF && mf) {
40 m_ptr = std::make_unique<innards<MF> >(std::forward<MF>(mf));
41 return *this;
42 }
43
45 [[nodiscard]] const std::type_info& Type () const {
46 if (m_ptr) {
47 return m_ptr->Type();
48 } else {
49 return typeid(void);
50 }
51 }
52
54 template <typename MF>
55 [[nodiscard]] MF& get () {
56 if (auto p0 = dynamic_cast<innards<MF>*>(m_ptr.get())) {
57 return p0->m_mf;
58 } else {
59 return dynamic_cast<innards<MF&>&>(*m_ptr).m_mf;
60 }
61 }
62
64 template <typename MF>
65 [[nodiscard]] MF const& get () const {
66 if (auto p0 = dynamic_cast<innards<MF>*>(m_ptr.get())) {
67 return p0->m_mf;
68 } else if (auto p1 = dynamic_cast<innards<MF&>*>(m_ptr.get())) {
69 return p1->m_mf;
70 } else {
71 return dynamic_cast<innards<MF const&> const&>(*m_ptr).m_mf;
72 }
73 }
74
75 template <typename MF>
76 [[nodiscard]] bool is () const { return Type() == typeid(MF); }
77
78 [[nodiscard]] bool hasValue () const { return m_ptr != nullptr; }
79
80private:
81
83
84 struct innards_base // NOLINT(cppcoreguidelines-special-member-functions)
85 {
86 [[nodiscard]] virtual const std::type_info& Type () const = 0;
87 virtual ~innards_base () = default;
88 };
89
90 template <typename MF>
91 struct innards final : innards_base // NOLINT(cppcoreguidelines-special-member-functions)
92 {
93 innards (MF && mf)
94 : m_mf(std::move(mf))
95 {}
96
97 ~innards () final = default;
98
99 [[nodiscard]] const std::type_info& Type () const final {
100 return typeid(MF);
101 }
102
103 MF m_mf;
104 };
105
107
108 std::unique_ptr<innards_base> m_ptr;
109};
110
111}
112
113#endif
Definition AMReX_Any.H:17
Any(MF &&mf)
Constructs by moving the given object.
Definition AMReX_Any.H:32
bool is() const
Definition AMReX_Any.H:76
const std::type_info & Type() const
Returns the contained type.
Definition AMReX_Any.H:45
~Any()=default
Any()=default
bool hasValue() const
Definition AMReX_Any.H:78
MF const & get() const
Returns a const reference to the contained object.
Definition AMReX_Any.H:65
std::unique_ptr< innards_base > m_ptr
Definition AMReX_Any.H:108
Any & operator=(Any const &rhs)=delete
MF & get()
Returns a reference to the contained object.
Definition AMReX_Any.H:55
Any(Any const &rhs)=delete
Any(Any &&rhs)=default
Definition AMReX_Amr.cpp:49