https://mooseframework.inl.gov
AppFactory.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 <vector>
13 
14 #include "MooseApp.h"
15 #include "Capabilities.h"
16 
17 #ifdef MOOSE_UNIT_TEST
18 #include "gtest/gtest.h"
19 class GTEST_TEST_CLASS_NAME_(AppFactoryTest, manageAppParams);
20 class GTEST_TEST_CLASS_NAME_(AppFactoryTest, appCopyConstructParams);
21 class GTEST_TEST_CLASS_NAME_(AppFactoryTest, createNotRegistered);
22 #endif
23 
27 #define registerApp(name) AppFactory::instance().reg<name>(#name)
28 
33 {
34  virtual std::unique_ptr<MooseApp> build(const InputParameters & params) = 0;
35  virtual InputParameters buildParameters() = 0;
36  virtual ~AppFactoryBuildInfoBase() = default;
37 
38  std::size_t _app_creation_count = 0;
39 };
40 template <typename T>
42 {
43  virtual std::unique_ptr<MooseApp> build(const InputParameters & params) override
44  {
45  return std::make_unique<T>(params);
46  }
47  virtual InputParameters buildParameters() override { return T::validParams(); }
48 };
49 
50 using AppFactoryBuildInfoMap = std::map<std::string, std::unique_ptr<AppFactoryBuildInfoBase>>;
51 
56 {
57 public:
62  static AppFactory & instance();
63 
64  virtual ~AppFactory();
65 
67 
69  static const std::string main_app_name;
70 
76  static std::unique_ptr<MooseApp> create(const std::string & app_type,
77  const std::vector<std::string> & cli_args = {});
78 
82  static std::unique_ptr<MooseApp> create(std::unique_ptr<Parser> parser,
83  std::unique_ptr<CommandLine> command_line);
84 
96  std::unique_ptr<MooseApp> create(const std::string & app_type,
98  const std::string & name,
99  InputParameters parameters,
100  MPI_Comm COMM_WORLD_IN);
101  std::shared_ptr<MooseApp> createShared(const std::string & app_type,
102  const std::string & name,
103  InputParameters parameters,
104  MPI_Comm COMM_WORLD_IN);
106 
111  static std::shared_ptr<MooseApp> createAppShared(const std::string & default_app_type,
112  int argc,
113  char ** argv,
114  MPI_Comm comm_word = MPI_COMM_WORLD);
115 
120  template <typename T>
121  void reg(const std::string & name);
122 
128  InputParameters getValidParams(const std::string & name);
129 
138  const InputParameters & getAppParams(const InputParameters & params) const;
139 
145  {
146  friend class MooseApp;
147 #ifdef MOOSE_UNIT_TEST
148  FRIEND_TEST(::AppFactoryTest, manageAppParams);
149 #endif
152  };
153 
159  void clearAppParams(const InputParameters & params, const ClearAppParamsKey);
160 
164  const auto & registeredObjects() const { return _name_to_build_info; }
165 
169  bool isRegistered(const std::string & app_name) const
170  {
171  return _name_to_build_info.count(app_name);
172  }
173 
177  std::size_t createdAppCount(const std::string & app_type) const;
178 
184 
186  AppFactory(AppFactory const &) = delete;
187  Registry & operator=(AppFactory const &) = delete;
188 
189  AppFactory(AppFactory &&) = delete;
190  Registry & operator=(AppFactory &&) = delete;
192 
193 protected:
195 
196 private:
203 
215  std::size_t getAppParamsID(const InputParameters & params) const;
216 
219 
220 #ifdef MOOSE_UNIT_TEST
221  FRIEND_TEST(::AppFactoryTest, manageAppParams);
222  FRIEND_TEST(::AppFactoryTest, appCopyConstructParams);
223  FRIEND_TEST(::AppFactoryTest, createNotRegistered);
224 #endif
225 
227  std::map<std::size_t, std::unique_ptr<InputParameters>> _input_parameters;
228 };
229 
230 template <typename T>
231 void
232 AppFactory::reg(const std::string & name)
233 {
234  if (isRegistered(name))
235  return;
236 
237  _name_to_build_info[name] = std::make_unique<AppFactoryBuildInfo<T>>();
239  name, true, "MOOSE application " + name + " is available.");
240 }
bool isRegistered(const std::string &app_name) const
Returns a Boolean indicating whether an application type has been registered.
Definition: AppFactory.h:169
std::shared_ptr< MooseApp > createShared(const std::string &app_type, const std::string &name, InputParameters parameters, MPI_Comm COMM_WORLD_IN)
Definition: AppFactory.C:180
static const std::string main_app_name
The name for the "main" moose application.
Definition: AppFactory.h:69
static std::shared_ptr< MooseApp > createAppShared(const std::string &default_app_type, int argc, char **argv, MPI_Comm comm_word=MPI_COMM_WORLD)
Deprecated helper function for creating a MooseApp for Apps haven&#39;t adapted to the new Parser and Bui...
Definition: AppFactory.C:95
const InputParameters & storeAppParams(InputParameters &params)
Stores the given parameters within _input_parameters for app construction.
Definition: AppFactory.C:200
const InputParameters & getAppParams(const InputParameters &params) const
Definition: AppFactory.C:42
class GTEST_TEST_CLASS_NAME_(AppFactoryTest, manageAppParams)
Base class for MOOSE-based applications.
Definition: MooseApp.h:103
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
virtual std::unique_ptr< MooseApp > build(const InputParameters &params) override
Definition: AppFactory.h:43
InputParameters validParams()
static InputParameters validParams()
virtual ~AppFactory()
Definition: AppFactory.C:28
static std::unique_ptr< MooseApp > create(const std::string &app_type, const std::vector< std::string > &cli_args={})
Create an app with no input and command line arguments.
Definition: AppFactory.C:62
const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:103
const auto & registeredObjects() const
Returns a reference to the map from names to AppFactoryBuildInfo pointers.
Definition: AppFactory.h:164
virtual std::unique_ptr< MooseApp > build(const InputParameters &params)=0
Generic AppFactory class for building Application objects.
Definition: AppFactory.h:55
void clearAppParams(const InputParameters &params, const ClearAppParamsKey)
Clears the stored parameters for the given application parameteres.
Definition: AppFactory.C:51
AppFactoryBuildInfoMap _name_to_build_info
Definition: AppFactory.h:194
virtual InputParameters buildParameters()=0
ClearAppParamsKey(const ClearAppParamsKey &)
Definition: AppFactory.h:151
std::size_t createdAppCount(const std::string &app_type) const
Definition: AppFactory.C:189
FRIEND_TEST(::AppFactoryTest, manageAppParams)
std::size_t _app_creation_count
Definition: AppFactory.h:38
std::map< std::size_t, std::unique_ptr< InputParameters > > _input_parameters
Storage of input parameters used in applications (ID (from getAppParamsID()) -> params) ...
Definition: AppFactory.h:227
FRIEND_TEST(::AppFactoryTest, manageAppParams)
void reg(const std::string &name)
Register a new object.
Definition: AppFactory.h:232
AppFactory()
Private constructor for singleton pattern.
Definition: AppFactory.h:218
static AppFactory & instance()
Get the instance of the AppFactory.
Definition: AppFactory.C:18
static Capabilities & getCapabilityRegistry()
Definition: Capabilities.C:22
std::size_t getAppParamsID(const InputParameters &params) const
Get the ID for the InputParameters associated with an application, used in storing them in _input_par...
Definition: AppFactory.C:214
virtual InputParameters buildParameters() override
Definition: AppFactory.h:47
const AppFactoryBuildInfoMap & registeredObjectBuildInfos() const
Returns the map of object name to a function pointer for building said object&#39;s input parameters...
Definition: AppFactory.h:183
Registry & operator=(AppFactory const &)=delete
Class that is used as a parameter to clearAppParams() that allows only MooseApp to call clearAppParam...
Definition: AppFactory.h:144
InputParameters getValidParams(const std::string &name)
Get valid parameters for the object.
Definition: AppFactory.C:33
std::map< std::string, std::unique_ptr< AppFactoryBuildInfoBase > > AppFactoryBuildInfoMap
Definition: AppFactory.h:50
The registry is used as a global singleton to collect information on all available MooseObject and Ac...
Definition: Registry.h:161
Polymorphic data structure with parameter and object build access.
Definition: AppFactory.h:32
void add(const std::string &capability, CapabilityUtils::Type value, const std::string &doc)
register a new capability
Definition: Capabilities.C:33
virtual ~AppFactoryBuildInfoBase()=default