www.mooseframework.org
ActionFactory.h
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #ifndef ACTIONFACTORY_H
16 #define ACTIONFACTORY_H
17 
18 #include <vector>
19 #include <map>
20 #include <set>
21 
22 #include "Action.h" // Technically required for std::shared_ptr<Action>(Action*) constructor
23 #include "InputParameters.h"
24 #include "FileLineInfo.h"
25 
29 #define stringifyName(name) #name
30 #define registerAction(tplt, action) \
31  action_factory.reg<tplt>(stringifyName(tplt), action, __FILE__, __LINE__)
32 
33 #define registerSyntax(action, action_syntax) \
34  syntax.registerActionSyntax(action, action_syntax, "", __FILE__, __LINE__)
35 #define registerSyntaxTask(action, action_syntax, task) \
36  syntax.registerActionSyntax(action, action_syntax, task, __FILE__, __LINE__)
37 #define registerTask(name, is_required) syntax.registerTaskName(name, is_required)
38 #define registerMooseObjectTask(name, moose_system, is_required) \
39  syntax.registerTaskName(name, stringifyName(moose_system), is_required)
40 #define appendMooseObjectTask(name, moose_system) \
41  syntax.appendTaskName(name, stringifyName(moose_system))
42 #define addTaskDependency(action, depends_on) syntax.addDependency(action, depends_on)
43 
44 // Forward Declaration
45 class MooseApp;
46 
50 typedef std::shared_ptr<Action> (*buildActionPtr)(InputParameters parameters);
51 
56 
60 template <class T>
61 std::shared_ptr<Action>
63 {
64  return std::shared_ptr<Action>(new T(parameters));
65 }
66 
71 {
72 public:
73  ActionFactory(MooseApp & app);
74 
75  virtual ~ActionFactory();
76 
77  template <typename T>
78  void reg(const std::string & name,
79  const std::string & task,
80  const std::string & file = "",
81  int line = -1)
82  {
83  BuildInfo build_info;
84  build_info._build_pointer = &buildAction<T>;
85  build_info._params_pointer = &validParams<T>;
86  build_info._task = task;
87  build_info._unique_id = _unique_id++;
88  _name_to_build_info.insert(std::make_pair(name, build_info));
89  _task_to_action_map.insert(std::make_pair(task, name));
90  _name_to_line.addInfo(name, task, file, line);
91  }
92 
99  FileLineInfo getLineInfo(const std::string & name, const std::string & task) const;
100 
101  std::string getTaskName(const std::string & action);
102 
103  std::shared_ptr<Action>
104  create(const std::string & action, const std::string & action_name, InputParameters parameters);
105 
106  InputParameters getValidParams(const std::string & name);
107 
108  class BuildInfo
109  {
110  public:
113  std::string _task;
114  unsigned int _unique_id;
115  };
116 
118  typedef std::multimap<std::string, BuildInfo>::iterator iterator;
119  typedef std::multimap<std::string, BuildInfo>::const_iterator const_iterator;
120 
121  iterator begin();
122  const_iterator begin() const;
123 
124  iterator end();
125  const_iterator end() const;
126 
127  std::pair<std::multimap<std::string, std::string>::const_iterator,
128  std::multimap<std::string, std::string>::const_iterator>
129  getActionsByTask(const std::string & task) const;
130 
131  std::set<std::string> getTasksByAction(const std::string & action) const;
132 
133 protected:
135 
136  std::multimap<std::string, BuildInfo> _name_to_build_info;
137 
139  std::multimap<std::string, std::string> _task_to_action_map;
140 
141  // TODO: I don't think we need this anymore
142  static unsigned int _unique_id;
143 };
144 
145 #endif /* ACTIONFACTORY_H */
void reg(const std::string &name, const std::string &task, const std::string &file="", int line=-1)
Definition: ActionFactory.h:78
InputParameters getValidParams(const std::string &name)
Definition: ActionFactory.C:74
InputParameters & parameters()
Get the parameters of the object.
Definition: MooseApp.h:81
iterator end()
Base class for MOOSE-based applications.
Definition: MooseApp.h:58
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
virtual ~ActionFactory()
Definition: ActionFactory.C:23
std::shared_ptr< Action >(* buildActionPtr)(InputParameters parameters)
Typedef for function to build objects.
Definition: ActionFactory.h:50
void addInfo(const std::string &key0, const std::string &file, int line)
Associate a key with file/line info.
Definition: FileLineInfo.C:40
std::multimap< std::string, BuildInfo >::iterator iterator
Typedef for registered Action iterator.
std::shared_ptr< Action > buildAction(InputParameters parameters)
Build an object of type T.
Definition: ActionFactory.h:62
buildActionPtr _build_pointer
MooseApp & _app
FileLineInfoMap _name_to_line
iterator begin()
ActionFactory(MooseApp &app)
Definition: ActionFactory.C:21
InputParameters(* paramsActionPtr)()
Typedef for validParams.
Definition: ActionFactory.h:55
Specialized factory for generic Action System objects.
Definition: ActionFactory.h:70
std::multimap< std::string, BuildInfo >::const_iterator const_iterator
Holds file and line information.
Definition: FileLineInfo.h:24
std::multimap< std::string, BuildInfo > _name_to_build_info
std::string getTaskName(const std::string &action)
Definition: ActionFactory.C:95
std::multimap< std::string, std::string > _task_to_action_map
paramsActionPtr _params_pointer
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::shared_ptr< Action > create(const std::string &action, const std::string &action_name, InputParameters parameters)
Definition: ActionFactory.C:26
static unsigned int _unique_id
Unique ID for identifying multiple registrations.
A mapping between a series of keys to a FileLineInfo.
Definition: FileLineInfo.h:46
FileLineInfo getLineInfo(const std::string &name, const std::string &task) const
Gets file and line information where an action was registered.
std::set< std::string > getTasksByAction(const std::string &action) const