www.mooseframework.org
ActionFactory.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 #include <map>
14 #include <set>
15 
16 #include "Action.h" // Technically required for std::shared_ptr<Action>(Action*) constructor
17 #include "InputParameters.h"
18 #include "FileLineInfo.h"
19 
23 #define stringifyName(name) #name
24 #define registerAction(tplt, action) \
25  action_factory.reg<tplt>(stringifyName(tplt), action, __FILE__, __LINE__)
26 
27 #define registerSyntax(action, action_syntax) \
28  syntax.registerActionSyntax(action, action_syntax, "", __FILE__, __LINE__)
29 #define registerSyntaxTask(action, action_syntax, task) \
30  syntax.registerActionSyntax(action, action_syntax, task, __FILE__, __LINE__)
31 #define registerTask(name, is_required) syntax.registerTaskName(name, is_required)
32 #define registerMooseObjectTask(name, moose_system, is_required) \
33  syntax.registerTaskName(name, stringifyName(moose_system), is_required)
34 #define appendMooseObjectTask(name, moose_system) \
35  syntax.appendTaskName(name, stringifyName(moose_system))
36 #define addTaskDependency(action, depends_on) syntax.addDependency(action, depends_on)
37 
38 // Forward Declaration
39 class MooseApp;
40 
44 typedef std::shared_ptr<Action> (*buildActionPtr)(const InputParameters & parameters);
45 
50 
54 template <class T>
55 std::shared_ptr<Action>
57 {
58  return std::make_shared<T>(parameters);
59 }
60 
65 {
66 public:
68 
69  virtual ~ActionFactory();
70 
71  MooseApp & app() { return _app; }
72 
73  template <typename T>
74  void reg(const std::string & name,
75  const std::string & task,
76  const std::string & file = "",
77  int line = -1)
78  {
79  reg(name, task, &buildAction<T>, &validParams<T>, file, line);
80  }
81 
82  void reg(const std::string & name,
83  const std::string & task,
84  buildActionPtr obj_builder,
85  paramsActionPtr ref_params,
86  const std::string & file = "",
87  int line = -1);
88 
95  FileLineInfo getLineInfo(const std::string & name, const std::string & task) const;
96 
97  std::string getTaskName(const std::string & action);
98 
99  std::shared_ptr<Action>
100  create(const std::string & action, const std::string & action_name, InputParameters parameters);
101 
102  InputParameters getValidParams(const std::string & name);
103 
104  class BuildInfo
105  {
106  public:
109  std::string _task;
110  };
111 
113  typedef std::multimap<std::string, BuildInfo>::iterator iterator;
114  typedef std::multimap<std::string, BuildInfo>::const_iterator const_iterator;
115 
116  iterator begin();
117  const_iterator begin() const;
118 
119  iterator end();
120  const_iterator end() const;
121 
122  std::pair<std::multimap<std::string, std::string>::const_iterator,
123  std::multimap<std::string, std::string>::const_iterator>
124  getActionsByTask(const std::string & task) const;
125 
126  std::set<std::string> getTasksByAction(const std::string & action) const;
127 
128 protected:
130 
131  std::multimap<std::string, BuildInfo> _name_to_build_info;
132 
134  std::multimap<std::string, std::string> _task_to_action_map;
135 
137  std::set<std::pair<std::string, std::string>> _current_objs;
138 };
139 
void reg(const std::string &name, const std::string &task, const std::string &file="", int line=-1)
Definition: ActionFactory.h:74
InputParameters getValidParams(const std::string &name)
Definition: ActionFactory.C:70
InputParameters & parameters()
Get the parameters of the object.
Definition: MooseApp.h:86
std::pair< std::multimap< std::string, std::string >::const_iterator, std::multimap< std::string, std::string >::const_iterator > getActionsByTask(const std::string &task) const
std::set< std::string > getTasksByAction(const std::string &action) const
iterator end()
Base class for MOOSE-based applications.
Definition: MooseApp.h:59
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
FileLineInfo getLineInfo(const std::string &name, const std::string &task) const
Gets file and line information where an action was registered.
virtual ~ActionFactory()
Definition: ActionFactory.C:16
std::multimap< std::string, BuildInfo >::iterator iterator
Typedef for registered Action iterator.
buildActionPtr _build_pointer
MooseApp & _app
FileLineInfoMap _name_to_line
iterator begin()
ActionFactory(MooseApp &app)
Definition: ActionFactory.C:14
InputParameters(* paramsActionPtr)()
Typedef for validParams.
Definition: ActionFactory.h:49
std::set< std::pair< std::string, std::string > > _current_objs
set<objectname, task> used to track if an object previously added is being added again ...
Specialized factory for generic Action System objects.
Definition: ActionFactory.h:64
std::multimap< std::string, BuildInfo >::const_iterator const_iterator
Holds file and line information.
Definition: FileLineInfo.h:18
std::multimap< std::string, BuildInfo > _name_to_build_info
std::string getTaskName(const std::string &action)
Definition: ActionFactory.C:90
std::multimap< std::string, std::string > _task_to_action_map
paramsActionPtr _params_pointer
std::shared_ptr< Action > create(const std::string &action, const std::string &action_name, InputParameters parameters)
Definition: ActionFactory.C:41
std::shared_ptr< Action > buildAction(const InputParameters &parameters)
Build an object of type T.
Definition: ActionFactory.h:56
std::shared_ptr< Action >(*)(const InputParameters &parameters) buildActionPtr
Definition: Registry.h:171
MooseApp & app()
Definition: ActionFactory.h:71
A mapping between a series of keys to a FileLineInfo.
Definition: FileLineInfo.h:40