https://mooseframework.inl.gov
Public Types | Public Member Functions | Protected Member Functions | Private Attributes | List of all members
Moose::MFEM::NamedFieldsMap< T > Class Template Reference

Lightweight adaptor over an std::map from strings to pointer to T. More...

#include <MFEMContainers.h>

Public Types

using MapType = std::map< std::string, std::shared_ptr< T > >
 
using const_iterator = typename MapType::const_iterator
 

Public Member Functions

 NamedFieldsMap ()=default
 Default initializer. More...
 
 ~NamedFieldsMap ()
 Destructor. More...
 
template<class FieldType , class... FieldArgs>
void Register (const std::string &field_name, FieldArgs &&... args)
 Construct new field with name field_name and register. More...
 
void Register (const std::string &field_name, std::shared_ptr< T > field)
 Register association between field and field_name. More...
 
void Deregister (const std::string &field_name)
 Unregister association between a field and the field_name. More...
 
bool Has (const std::string &field_name) const
 Predicate to check if a field is registered with name field_name. More...
 
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. More...
 
T & GetRef (const std::string &field_name) const
 Returns a reference to a field. More...
 
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. More...
 
template<typename TDerived >
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. More...
 
std::vector< T * > Get (const std::vector< std::string > &keys) const
 Returns a vector containing all values for supplied keys. More...
 
const_iterator begin () const
 Returns a begin const iterator to the registered fields. More...
 
const_iterator end () const
 Returns an end const iterator to the registered fields. More...
 
int size ()
 Returns the number of elements in the map. More...
 

Protected Member Functions

const_iterator FindField (const std::string &field_name) const
 Returns a const iterator to the field. More...
 
void CheckFieldIsRegistrable ([[maybe_unused]] const std::string &field_name, [[maybe_unused]] T *field) const
 Check that the field pointer is valid and the field has not already been registered. More...
 
void CheckFieldIsRegistered (const std::string &field_name) const
 Check that a field exists in the map. More...
 
std::shared_ptr< T > EnsureFieldPointerIsNonNull (const_iterator &iterator) const
 Ensure that a returned shared pointer is valid. More...
 
template<typename TDerived >
TDerived * EnsurePointerCastIsNonNull (T *ptr) const
 Ensure that a dynamic cast is successful. More...
 
void DeregisterAll ()
 Clear all associations between names and fields. More...
 

Private Attributes

MapType _field_map {}
 

Detailed Description

template<typename T>
class Moose::MFEM::NamedFieldsMap< T >

Lightweight adaptor over an std::map from strings to pointer to T.

Definition at line 35 of file MFEMContainers.h.

Member Typedef Documentation

◆ const_iterator

template<typename T>
using Moose::MFEM::NamedFieldsMap< T >::const_iterator = typename MapType::const_iterator

Definition at line 39 of file MFEMContainers.h.

◆ MapType

template<typename T>
using Moose::MFEM::NamedFieldsMap< T >::MapType = std::map<std::string, std::shared_ptr<T> >

Definition at line 38 of file MFEMContainers.h.

Constructor & Destructor Documentation

◆ NamedFieldsMap()

template<typename T>
Moose::MFEM::NamedFieldsMap< T >::NamedFieldsMap ( )
default

Default initializer.

◆ ~NamedFieldsMap()

template<typename T>
Moose::MFEM::NamedFieldsMap< T >::~NamedFieldsMap ( )
inline

Destructor.

Definition at line 45 of file MFEMContainers.h.

45 { DeregisterAll(); }
void DeregisterAll()
Clear all associations between names and fields.

Member Function Documentation

◆ begin()

template<typename T>
const_iterator Moose::MFEM::NamedFieldsMap< T >::begin ( ) const
inline

Returns a begin const iterator to the registered fields.

Definition at line 120 of file MFEMContainers.h.

120 { return _field_map.begin(); }

◆ CheckFieldIsRegistered()

template<typename T>
void Moose::MFEM::NamedFieldsMap< T >::CheckFieldIsRegistered ( const std::string &  field_name) const
inlineprotected

Check that a field exists in the map.

Definition at line 146 of file MFEMContainers.h.

Referenced by Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::GetShared().

147  {
148  if (!Has(field_name))
149  mooseError("The field '" + field_name + "' has not been registered.");
150  }
bool Has(const std::string &field_name) const
Predicate to check if a field is registered with name field_name.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311

◆ CheckFieldIsRegistrable()

template<typename T>
void Moose::MFEM::NamedFieldsMap< T >::CheckFieldIsRegistrable ( [[maybe_unused] ] const std::string &  field_name,
[[maybe_unused] ] T *  field 
) const
inlineprotected

Check that the field pointer is valid and the field has not already been registered.

Definition at line 137 of file MFEMContainers.h.

Referenced by Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::Register().

139  {
140  mooseAssert(field, "Cannot register NULL field with name '" + field_name + "'.");
141  mooseAssert(!Has(field_name) || Get(field_name) != field,
142  "The field '" + field_name + "' is already registered.");
143  }
bool Has(const std::string &field_name) const
Predicate to check if a field is registered with name field_name.
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...

◆ Deregister()

template<typename T>
void Moose::MFEM::NamedFieldsMap< T >::Deregister ( const std::string &  field_name)
inline

Unregister association between a field and the field_name.

Definition at line 65 of file MFEMContainers.h.

Referenced by Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::Register().

65 { _field_map.erase(field_name); }

◆ DeregisterAll()

template<typename T>
void Moose::MFEM::NamedFieldsMap< T >::DeregisterAll ( )
inlineprotected

Clear all associations between names and fields.

Definition at line 170 of file MFEMContainers.h.

Referenced by Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::~NamedFieldsMap().

170 { _field_map.clear(); }

◆ end()

template<typename T>
const_iterator Moose::MFEM::NamedFieldsMap< T >::end ( ) const
inline

Returns an end const iterator to the registered fields.

Definition at line 124 of file MFEMContainers.h.

Referenced by Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::Has().

124 { return _field_map.end(); }

◆ EnsureFieldPointerIsNonNull()

template<typename T>
std::shared_ptr<T> Moose::MFEM::NamedFieldsMap< T >::EnsureFieldPointerIsNonNull ( const_iterator iterator) const
inlineprotected

Ensure that a returned shared pointer is valid.

Definition at line 153 of file MFEMContainers.h.

Referenced by Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::GetShared().

154  {
155  auto owned_ptr = iterator->second;
156  mooseAssert(owned_ptr, "The field '" + iterator->first + "' is NULL.");
157  return owned_ptr;
158  }

◆ EnsurePointerCastIsNonNull()

template<typename T>
template<typename TDerived >
TDerived* Moose::MFEM::NamedFieldsMap< T >::EnsurePointerCastIsNonNull ( T *  ptr) const
inlineprotected

Ensure that a dynamic cast is successful.

Definition at line 162 of file MFEMContainers.h.

163  {
164  auto derived_ptr = dynamic_cast<TDerived *>(ptr);
165  mooseAssert(derived_ptr, "The dynamic cast performed on the field pointer failed.");
166  return derived_ptr;
167  }

◆ FindField()

template<typename T>
const_iterator Moose::MFEM::NamedFieldsMap< T >::FindField ( const std::string &  field_name) const
inlineprotected

Returns a const iterator to the field.

Definition at line 131 of file MFEMContainers.h.

Referenced by Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::GetShared(), and Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::Has().

132  {
133  return _field_map.find(field_name);
134  }

◆ Get() [1/3]

template<typename T>
T* Moose::MFEM::NamedFieldsMap< T >::Get ( const std::string &  field_name) const
inline

Returns a non-owning pointer to the field. This is guaranteed to return a non-null pointer.

Definition at line 90 of file MFEMContainers.h.

Referenced by Moose::MFEM::TimeDependentEquationSystem::AddKernel(), Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::CheckFieldIsRegistrable(), Moose::MFEM::TimeDependentEquationSystem::EliminateCoupledVariables(), Moose::MFEM::EquationSystem::EliminateCoupledVariables(), Moose::MFEM::EigenproblemEquationSystem::FormEigenproblemMatrix(), Moose::MFEM::EquationSystem::FormJacobianMatrix(), Moose::MFEM::ComplexEquationSystem::FormSystemMatrix(), Moose::MFEM::EquationSystem::FormSystemMatrix(), Moose::MFEM::ComplexEquationSystem::FormSystemOperator(), Moose::MFEM::EquationSystem::FormSystemOperator(), Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::Get(), MFEMProblem::getMeshDisplacementGridFunction(), MFEMProblem::getMFEMVariableMesh(), Moose::MFEM::ComplexEquationSystem::Init(), Moose::MFEM::EquationSystem::Init(), Moose::MFEM::EigenproblemESProblemOperator::RecoverEigenproblemSolution(), Moose::MFEM::ComplexEquationSystemProblemOperator::SetGridFunctions(), Moose::MFEM::ProblemOperatorBase::SetGridFunctions(), Moose::MFEM::ComplexEquationSystem::SetTrialVariablesFromTrueVectors(), and Moose::MFEM::EquationSystem::SetTrialVariablesFromTrueVectors().

91  {
92  return GetShared(field_name).get();
93  }
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...

◆ Get() [2/3]

template<typename T>
template<typename TDerived >
TDerived* Moose::MFEM::NamedFieldsMap< T >::Get ( const std::string &  field_name) const
inline

Returns a non-owning pointer to the field where TDerived is a derived class of class T.

Definition at line 97 of file MFEMContainers.h.

98  {
99  auto ptr = Get(field_name);
100 
101  return EnsurePointerCastIsNonNull<TDerived>(ptr);
102  }
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...

◆ Get() [3/3]

template<typename T>
std::vector<T *> Moose::MFEM::NamedFieldsMap< T >::Get ( const std::vector< std::string > &  keys) const
inline

Returns a vector containing all values for supplied keys.

Definition at line 105 of file MFEMContainers.h.

106  {
107  std::vector<T *> values;
108 
109  for (const auto & key : keys)
110  {
111  values.push_back(Get(key));
112  }
113 
114  values.shrink_to_fit();
115  return values;
116  }
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...

◆ GetRef()

template<typename T>
T& Moose::MFEM::NamedFieldsMap< T >::GetRef ( const std::string &  field_name) const
inline

◆ GetShared()

template<typename T>
std::shared_ptr<T> Moose::MFEM::NamedFieldsMap< T >::GetShared ( const std::string &  field_name) const
inline

Returns a shared pointer to the field. This is guaranteed to return a non-null shared pointer.

Definition at line 74 of file MFEMContainers.h.

Referenced by Moose::MFEM::TimeDependentEquationSystem::BuildBilinearForms(), Moose::MFEM::ComplexEquationSystem::BuildBilinearForms(), Moose::MFEM::EquationSystem::BuildBilinearForms(), Moose::MFEM::ComplexEquationSystem::BuildLinearForms(), Moose::MFEM::EquationSystem::BuildLinearForms(), Moose::MFEM::TimeDependentEquationSystem::BuildNonlinearForms(), Moose::MFEM::EquationSystem::BuildNonlinearForms(), Moose::MFEM::EquationSystem::ComputeNonlinearResidual(), Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::Get(), MFEMProblem::getComplexGridFunction(), MFEMProblem::getGridFunction(), Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::GetRef(), Moose::MFEM::ComplexEquationSystem::Init(), Moose::MFEM::EquationSystem::Init(), and MFEMVariable::MFEMVariable().

75  {
76  CheckFieldIsRegistered(field_name);
77 
78  auto it = FindField(field_name);
79 
80  return EnsureFieldPointerIsNonNull(it);
81  }
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.

◆ Has()

template<typename T>
bool Moose::MFEM::NamedFieldsMap< T >::Has ( const std::string &  field_name) const
inline

◆ Register() [1/2]

template<typename T>
template<class FieldType , class... FieldArgs>
void Moose::MFEM::NamedFieldsMap< T >::Register ( const std::string &  field_name,
FieldArgs &&...  args 
)
inline

◆ Register() [2/2]

template<typename T>
void Moose::MFEM::NamedFieldsMap< T >::Register ( const std::string &  field_name,
std::shared_ptr< T >  field 
)
inline

Register association between field and field_name.

Definition at line 55 of file MFEMContainers.h.

56  {
57  CheckFieldIsRegistrable(field_name, field.get());
58 
59  Deregister(field_name);
60 
61  _field_map[field_name] = std::move(field);
62  }
void CheckFieldIsRegistrable([[maybe_unused]] const std::string &field_name, [[maybe_unused]] T *field) const
Check that the field pointer is valid and the field has not already been registered.
void Deregister(const std::string &field_name)
Unregister association between a field and the field_name.

◆ size()

template<typename T>
int Moose::MFEM::NamedFieldsMap< T >::size ( )
inline

Returns the number of elements in the map.

Definition at line 127 of file MFEMContainers.h.

Referenced by dataStore(), and Moose::MFEM::ComplexEquationSystem::Init().

127 { return _field_map.size(); }

Member Data Documentation

◆ _field_map

template<typename T>
MapType Moose::MFEM::NamedFieldsMap< T >::_field_map {}
private

The documentation for this class was generated from the following file: