18 #include "libmesh/ignore_warnings.h" 20 #include "libmesh/restore_warnings.h" 30 using MapType = std::map<std::string, std::shared_ptr<T>>;
40 template <
class FieldType,
class... FieldArgs>
41 void Register(
const std::string & field_name, FieldArgs &&... args)
43 Register(field_name, std::make_shared<FieldType>(std::forward<FieldArgs>(args)...));
47 void Register(
const std::string & field_name, std::shared_ptr<T> field)
60 [[nodiscard]]
inline bool Has(
const std::string & field_name)
const 66 [[nodiscard]]
inline std::shared_ptr<T>
GetShared(
const std::string & field_name)
const 76 [[nodiscard]]
inline T &
GetRef(
const std::string & field_name)
const 82 [[nodiscard]]
inline T *
Get(
const std::string & field_name)
const 88 template <
typename TDerived>
89 [[nodiscard]]
inline TDerived *
Get(
const std::string & field_name)
const 91 auto ptr =
Get(field_name);
93 return EnsurePointerCastIsNonNull<TDerived>(ptr);
97 [[nodiscard]] std::vector<T *>
Get(
const std::vector<std::string> & keys)
const 99 std::vector<T *> values;
101 for (
const auto & key : keys)
103 values.push_back(
Get(key));
106 values.shrink_to_fit();
133 MFEM_ABORT(
"Cannot register NULL field with name '" << field_name <<
"'.");
143 if (
Has(field_name) &&
Get(field_name) == field)
145 MFEM_ABORT(
"The field '" << field_name <<
"' is already registered.");
152 if (!
Has(field_name))
154 MFEM_ABORT(
"The field '" << field_name <<
"' has not been registered.");
161 auto owned_ptr = iterator->second;
165 MFEM_ABORT(
"The field '" << iterator->first <<
"' is NULL.");
172 template <
typename TDerived>
175 auto derived_ptr =
dynamic_cast<TDerived *
>(ptr);
179 MFEM_ABORT(
"The dynamic cast performed on the field pointer failed.");
195 return std::string(
"d") +
name + std::string(
"_dt");
std::string name(const ElemQuality q)
bool Has(const std::string &field_name) const
Predicate to check if a field is registered with name field_name.
typename MapType::const_iterator const_iterator
void CheckFieldIsRegistered(const std::string &field_name) const
Check that a field exists in the map.
const_iterator FindField(const std::string &field_name) const
Returns a const iterator to the field.
std::shared_ptr< T > EnsureFieldPointerIsNonNull(const_iterator &iterator) const
Ensure that a returned shared pointer is valid.
Lightweight adaptor over an std::map from strings to pointer to T.
const_iterator end() const
Returns an end const iterator to the registered fields.
std::string GetTimeDerivativeName(std::string name)
void CheckForDoubleRegistration(const std::string &field_name, T *field) const
Check for double-registration of a field.
std::map< std::string, std::shared_ptr< mfem::ParNonlinearForm > > MapType
~NamedFieldsMap()
Destructor.
TDerived * EnsurePointerCastIsNonNull(T *ptr) const
Ensure that a dynamic cast is successful.
T * Get(const std::string &field_name) const
Returns a non-owning pointer to the field. This is guaranteed to return a non-null pointer...
const_iterator begin() const
Returns a begin const iterator to the registered fields.
std::shared_ptr< T > GetShared(const std::string &field_name) const
Returns a shared pointer to the field. This is guaranteed to return a non-null shared pointer...
void CheckFieldIsRegistrable(const std::string &field_name, T *field) const
Check that the field pointer is valid and the field has not already been registered.
NamedFieldsMap()=default
Default initializer.
void Register(const std::string &field_name, std::shared_ptr< T > field)
Register association between field and field_name.
TDerived * Get(const std::string &field_name) const
Returns a non-owning pointer to the field where TDerived is a derived class of class T...
void Deregister(const std::string &field_name)
Unregister association between a field and the field_name.
void Register(const std::string &field_name, FieldArgs &&... args)
Construct new field with name field_name and register.
T & GetRef(const std::string &field_name) const
Returns a reference to a field.
void DeregisterAll()
Clear all associations between names and fields.
std::vector< T * > Get(const std::vector< std::string > &keys) const
Returns a vector containing all values for supplied keys.