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 ()
 

Protected Member Functions

const_iterator FindField (const std::string &field_name) const
 Returns a const iterator to the field. More...
 
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. More...
 
void CheckForDoubleRegistration (const std::string &field_name, T *field) const
 Check for double-registration of a field. 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 27 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 31 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 30 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 37 of file MFEMContainers.h.

37 { 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 112 of file MFEMContainers.h.

112 { 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 150 of file MFEMContainers.h.

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

151  {
152  if (!Has(field_name))
153  {
154  MFEM_ABORT("The field '" << field_name << "' has not been registered.");
155  }
156  }
bool Has(const std::string &field_name) const
Predicate to check if a field is registered with name field_name.

◆ CheckFieldIsRegistrable()

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

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

Definition at line 129 of file MFEMContainers.h.

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

130  {
131  if (!field)
132  {
133  MFEM_ABORT("Cannot register NULL field with name '" << field_name << "'.");
134  }
135 
136  CheckForDoubleRegistration(field_name, field);
137  }
void CheckForDoubleRegistration(const std::string &field_name, T *field) const
Check for double-registration of a field.

◆ CheckForDoubleRegistration()

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

Check for double-registration of a field.

A double-registered field may result in undefined behavior.

Definition at line 141 of file MFEMContainers.h.

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

142  {
143  if (Has(field_name) && Get(field_name) == field)
144  {
145  MFEM_ABORT("The field '" << field_name << "' is already registered.");
146  }
147  }
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 57 of file MFEMContainers.h.

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

57 { _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 186 of file MFEMContainers.h.

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

186 { _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 116 of file MFEMContainers.h.

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

116 { 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 159 of file MFEMContainers.h.

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

160  {
161  auto owned_ptr = iterator->second;
162 
163  if (!owned_ptr)
164  {
165  MFEM_ABORT("The field '" << iterator->first << "' is NULL.");
166  }
167 
168  return owned_ptr;
169  }

◆ 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 173 of file MFEMContainers.h.

174  {
175  auto derived_ptr = dynamic_cast<TDerived *>(ptr);
176 
177  if (!derived_ptr)
178  {
179  MFEM_ABORT("The dynamic cast performed on the field pointer failed.");
180  }
181 
182  return derived_ptr;
183  }

◆ 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 123 of file MFEMContainers.h.

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

124  {
125  return _field_map.find(field_name);
126  }

◆ Get() [1/3]

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

◆ 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 89 of file MFEMContainers.h.

90  {
91  auto ptr = Get(field_name);
92 
93  return EnsurePointerCastIsNonNull<TDerived>(ptr);
94  }
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 97 of file MFEMContainers.h.

98  {
99  std::vector<T *> values;
100 
101  for (const auto & key : keys)
102  {
103  values.push_back(Get(key));
104  }
105 
106  values.shrink_to_fit();
107  return values;
108  }
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

Returns a reference to a field.

Definition at line 76 of file MFEMContainers.h.

Referenced by Moose::MFEM::EquationSystem::AddEssentialBC(), Moose::MFEM::EquationSystem::AddIntegratedBC(), Moose::MFEM::EquationSystem::AddKernel(), Moose::MFEM::TimeDependentEquationSystem::AddKernel(), Moose::MFEM::EquationSystem::ApplyEssentialBCs(), Moose::MFEM::EquationSystem::BuildLinearForms(), and MultiAppMFEMCopyTransfer::transfer().

77  {
78  return *GetShared(field_name);
79  }
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...

◆ 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 66 of file MFEMContainers.h.

Referenced by Moose::MFEM::EquationSystem::BuildBilinearForms(), Moose::MFEM::TimeDependentEquationSystem::BuildBilinearForms(), Moose::MFEM::EquationSystem::BuildLinearForms(), Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::Get(), Moose::MFEM::NamedFieldsMap< mfem::ParNonlinearForm >::GetRef(), and Moose::MFEM::EquationSystem::Init().

67  {
68  CheckFieldIsRegistered(field_name);
69 
70  auto it = FindField(field_name);
71 
72  return EnsureFieldPointerIsNonNull(it);
73  }
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 47 of file MFEMContainers.h.

48  {
49  CheckFieldIsRegistrable(field_name, field.get());
50 
51  Deregister(field_name);
52 
53  _field_map[field_name] = std::move(field);
54  }
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.
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

Definition at line 119 of file MFEMContainers.h.

119 { 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: