https://mooseframework.inl.gov
MeshMetaDataInterface.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 #include "MooseError.h"
13 #include "RestartableData.h"
14 #include "MooseObject.h"
15 
16 #include <string>
17 
18 class MeshGenerator;
19 
27 {
28 public:
30  static constexpr auto SYSTEM = "MeshMetaData";
31 
33  static constexpr auto NAME = "<empty>";
34 
35 protected:
41  MeshMetaDataInterface(const MooseObject * moose_object);
42 
47  MeshMetaDataInterface(MooseApp & moose_app);
48 
53  template <typename T>
54  const T & getMeshProperty(const std::string & data_name, const std::string & prefix);
55  template <typename T>
56  const T & getMeshProperty(const std::string & data_name)
57  {
58  return getMeshProperty<T>(data_name, meshPropertyPrefix(data_name));
59  }
60 
64  bool hasMeshProperty(const std::string & data_name, const std::string & prefix) const;
68  template <typename T>
69  bool hasMeshProperty(const std::string & data_name, const std::string & prefix) const;
70 
74  bool hasMeshProperty(const std::string & data_name) const
75  {
76  return hasMeshProperty(data_name, meshPropertyPrefix(data_name));
77  }
81  template <typename T>
82  bool hasMeshProperty(const std::string & data_name) const
83  {
84  return hasMeshProperty<T>(data_name, meshPropertyPrefix(data_name));
85  }
86 
90  static std::string meshPropertyName(const std::string & data_name, const std::string & prefix);
91 
95  std::string meshPropertyName(const std::string & data_name) const
96  {
97  return meshPropertyName(data_name, meshPropertyPrefix(data_name));
98  }
99 
100 private:
107  virtual std::string meshPropertyPrefix(const std::string & data_name) const;
108 
110  const RestartableDataValue & getMeshPropertyInternal(const std::string & data_name,
111  const std::string & prefix) const;
112 
115 
118 
123  template <typename... Args>
124  [[noreturn]] void mooseErrorInternal(Args &&... args) const
125  {
126  if (_meta_data_object)
127  _meta_data_object->mooseError(std::forward<Args>(args)...);
128  mooseError(std::forward<Args>(args)...);
129  }
130 };
131 
132 template <typename T>
133 const T &
134 MeshMetaDataInterface::getMeshProperty(const std::string & data_name, const std::string & prefix)
135 
136 {
137  if (!hasMeshProperty(data_name, prefix))
138  mooseErrorInternal("Failed to get mesh property '", prefix, "/", data_name, "'");
139 
140  auto value = &getMeshPropertyInternal(data_name, prefix);
141  mooseAssert(value->declared(), "Value has not been declared");
142  const RestartableData<T> * T_value = dynamic_cast<const RestartableData<T> *>(value);
143  if (!T_value)
144  mooseErrorInternal("While retrieving mesh property '",
145  prefix,
146  "/",
147  data_name,
148  "' with type '",
149  MooseUtils::prettyCppType<T>(),
150  "',\nthe property exists with different type '",
151  value->type(),
152  "'");
153  return T_value->get();
154 }
155 
156 template <typename T>
157 bool
158 MeshMetaDataInterface::hasMeshProperty(const std::string & data_name,
159  const std::string & prefix) const
160 {
161  if (!hasMeshProperty(data_name, prefix))
162  return false;
163  const auto & value = getMeshPropertyInternal(data_name, prefix);
164  return dynamic_cast<const RestartableData<T> *>(&value) != nullptr;
165 }
const T & getMeshProperty(const std::string &data_name, const std::string &prefix)
Method for retrieving a property with the given type and name exists in the mesh meta-data store...
static constexpr auto NAME
The data name used when initializing the Restartable interface for non-MeshGenerator objects...
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
void mooseErrorInternal(Args &&... args) const
Helper for forwarding a mooseError to an object&#39;s mooseError if it is available (said error will prov...
bool hasMeshProperty(const std::string &data_name) const
Base class for MOOSE-based applications.
Definition: MooseApp.h:96
static std::string meshPropertyName(const std::string &data_name, const std::string &prefix)
MeshMetaDataInterface(const MooseObject *moose_object)
Interface for all objects reading MeshMetaData.
bool hasMeshProperty(const std::string &data_name) const
MooseApp & _meta_data_app
Reference to the application.
const MooseObject *const _meta_data_object
The MooseObject (if any); used for better error handling.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:28
Concrete definition of a parameter value for a specified type.
static constexpr auto SYSTEM
The system name used when initializing the Restartable interface.
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
The Interface used to retrieve mesh meta data (attributes) set by the MeshGenerator system...
const T & getMeshProperty(const std::string &data_name)
virtual std::string meshPropertyPrefix(const std::string &data_name) const
The default prefix to use for getting/seeing if mesh properties exist.
std::string meshPropertyName(const std::string &data_name) const
bool hasMeshProperty(const std::string &data_name, const std::string &prefix) const
MeshGenerators are objects that can modify or add to an existing mesh.
Definition: MeshGenerator.h:32
const RestartableDataValue & getMeshPropertyInternal(const std::string &data_name, const std::string &prefix) const
Helper for getting a mesh property.
Abstract definition of a RestartableData value.