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 
16 #ifdef MOOSE_UNIT_TEST
17 #include "gtest/gtest.h"
18 class GTEST_TEST_CLASS_NAME_(AppFactoryTest, manageAppParams);
19 class GTEST_TEST_CLASS_NAME_(AppFactoryTest, appCopyConstructParams);
20 class GTEST_TEST_CLASS_NAME_(AppFactoryTest, createNotRegistered);
21 #endif
22 
26 #define registerApp(name) AppFactory::instance().reg<name>(#name)
27 
32 {
33  virtual std::unique_ptr<MooseApp> build(const InputParameters & params) = 0;
34  virtual InputParameters buildParameters() = 0;
35  virtual ~AppFactoryBuildInfoBase() = default;
36 
37  std::size_t _app_creation_count = 0;
38 };
39 template <typename T>
41 {
42  virtual std::unique_ptr<MooseApp> build(const InputParameters & params) override
43  {
44  return std::make_unique<T>(params);
45  }
46  virtual InputParameters buildParameters() override { return T::validParams(); }
47 };
48 
49 using AppFactoryBuildInfoMap = std::map<std::string, std::unique_ptr<AppFactoryBuildInfoBase>>;
50 
55 {
56 public:
61  static AppFactory & instance();
62 
63  virtual ~AppFactory();
64 
66 
68  static const std::string main_app_name;
69 
75  static std::unique_ptr<MooseApp> create(const std::string & app_type,
76  const std::vector<std::string> & cli_args = {});
77 
81  static std::unique_ptr<MooseApp> create(std::unique_ptr<Parser> parser,
82  std::unique_ptr<CommandLine> command_line);
83 
95  std::unique_ptr<MooseApp> create(const std::string & app_type,
97  const std::string & name,
98  InputParameters parameters,
99  MPI_Comm COMM_WORLD_IN);
100  std::shared_ptr<MooseApp> createShared(const std::string & app_type,
101  const std::string & name,
102  InputParameters parameters,
103  MPI_Comm COMM_WORLD_IN);
105 
110  static std::shared_ptr<MooseApp> createAppShared(const std::string & default_app_type,
111  int argc,
112  char ** argv,
113  MPI_Comm comm_word = MPI_COMM_WORLD);
114 
119  template <typename T>
120  void reg(const std::string & name);
121 
127  InputParameters getValidParams(const std::string & name);
128 
137  const InputParameters & getAppParams(const InputParameters & params) const;
138 
144  {
145  friend class MooseApp;
146 #ifdef MOOSE_UNIT_TEST
147  FRIEND_TEST(::AppFactoryTest, manageAppParams);
148 #endif
151  };
152 
158  void clearAppParams(const InputParameters & params, const ClearAppParamsKey);
159 
163  const auto & registeredObjects() const { return _name_to_build_info; }
164 
168  bool isRegistered(const std::string & app_name) const
169  {
170  return _name_to_build_info.count(app_name);
171  }
172 
176  std::size_t createdAppCount(const std::string & app_type) const;
177 
183 
185  AppFactory(AppFactory const &) = delete;
186  Registry & operator=(AppFactory const &) = delete;
187 
188  AppFactory(AppFactory &&) = delete;
189  Registry & operator=(AppFactory &&) = delete;
191 
192 protected:
194 
195 private:
202 
214  std::size_t getAppParamsID(const InputParameters & params) const;
215 
219  void registerAppCapability(const std::string & app_name);
220 
223 
224 #ifdef MOOSE_UNIT_TEST
225  FRIEND_TEST(::AppFactoryTest, manageAppParams);
226  FRIEND_TEST(::AppFactoryTest, appCopyConstructParams);
227  FRIEND_TEST(::AppFactoryTest, createNotRegistered);
228 #endif
229 
231  std::map<std::size_t, std::unique_ptr<InputParameters>> _input_parameters;
232 };
233 
234 template <typename T>
235 void
236 AppFactory::reg(const std::string & name)
237 {
238  if (isRegistered(name))
239  return;
240 
241  _name_to_build_info[name] = std::make_unique<AppFactoryBuildInfo<T>>();
242 
244 }
bool isRegistered(const std::string &app_name) const
Returns a Boolean indicating whether an application type has been registered.
Definition: AppFactory.h:168
std::shared_ptr< MooseApp > createShared(const std::string &app_type, const std::string &name, InputParameters parameters, MPI_Comm COMM_WORLD_IN)
Definition: AppFactory.C:182
static const std::string main_app_name
The name for the "main" moose application.
Definition: AppFactory.h:68
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:97
const InputParameters & storeAppParams(InputParameters &params)
Stores the given parameters within _input_parameters for app construction.
Definition: AppFactory.C:202
const InputParameters & getAppParams(const InputParameters &params) const
Definition: AppFactory.C:44
class GTEST_TEST_CLASS_NAME_(AppFactoryTest, manageAppParams)
Base class for MOOSE-based applications.
Definition: MooseApp.h:108
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:42
static InputParameters validParams()
virtual ~AppFactory()
Definition: AppFactory.C:30
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:64
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:163
virtual std::unique_ptr< MooseApp > build(const InputParameters &params)=0
Generic AppFactory class for building Application objects.
Definition: AppFactory.h:54
void clearAppParams(const InputParameters &params, const ClearAppParamsKey)
Clears the stored parameters for the given application parameteres.
Definition: AppFactory.C:53
AppFactoryBuildInfoMap _name_to_build_info
Definition: AppFactory.h:193
virtual InputParameters buildParameters()=0
ClearAppParamsKey(const ClearAppParamsKey &)
Definition: AppFactory.h:150
std::size_t createdAppCount(const std::string &app_type) const
Definition: AppFactory.C:191
FRIEND_TEST(::AppFactoryTest, manageAppParams)
std::size_t _app_creation_count
Definition: AppFactory.h:37
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:231
FRIEND_TEST(::AppFactoryTest, manageAppParams)
void reg(const std::string &name)
Register a new object.
Definition: AppFactory.h:236
AppFactory()
Private constructor for singleton pattern.
Definition: AppFactory.h:222
static AppFactory & instance()
Get the instance of the AppFactory.
Definition: AppFactory.C:20
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:216
virtual InputParameters buildParameters() override
Definition: AppFactory.h:46
void registerAppCapability(const std::string &app_name)
Register an application name as a capability.
Definition: AppFactory.C:225
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:182
Registry & operator=(AppFactory const &)=delete
Class that is used as a parameter to clearAppParams() that allows only MooseApp to call clearAppParam...
Definition: AppFactory.h:143
InputParameters getValidParams(const std::string &name)
Get valid parameters for the object.
Definition: AppFactory.C:35
std::map< std::string, std::unique_ptr< AppFactoryBuildInfoBase > > AppFactoryBuildInfoMap
Definition: AppFactory.h:49
The registry is used as a global singleton to collect information on all available MooseObject and Ac...
Definition: Registry.h:171
Polymorphic data structure with parameter and object build access.
Definition: AppFactory.h:31
InputParameters validParams()
virtual ~AppFactoryBuildInfoBase()=default