Noise Modeler Library  0.1
Library for procedurally generating noise-based terrains and other content
 All Classes Files Functions Variables Enumerations Enumerator Groups Pages
module.hpp
1 #ifndef NM_MODULE_H
2 #define NM_MODULE_H
3 
4 #include <nmlib/util/signals.hpp>
5 #include <nmlib/util/userdataprovider.hpp>
6 
7 #include <vector>
8 #include <string>
9 #include <memory>
10 #include <functional>
11 #include <set>
12 
13 namespace nm {
14 
15 class InputLink;
16 class OutputLink;
17 class ModuleType;
18 class ModuleInput;
19 class ModuleOutput;
20 
40 class Module : public UserDataProvider
41 {
42 public:
47  explicit Module(const ModuleType &type, std::string name, std::string description = "");
48  virtual ~Module();
49  const ModuleType& getType() const {return m_type;}
50  const ModuleType& getType() {return m_type;}
51  const std::string getName() const {return m_name;}
52  void setName(std::string name);
53  const std::string getDescription() const {return m_description;}
54  void setDescription(std::string description);
55 
56 
57  //inputs
58  InputLink *getInput(std::string name);
59  const InputLink *getInput(std::string name) const;
60  InputLink *getInput(unsigned int i);
61  const InputLink *getInput(unsigned int i) const;
62  unsigned int getInputSize() const;
63  std::vector<InputLink*> getInputs();
64 
65  //outputs
66  OutputLink *getOutput(std::string name);
67  const OutputLink *getOutput(std::string name) const;
68  OutputLink *getOutput(unsigned int i);
69  const OutputLink *getOutput(unsigned int i) const;
70  unsigned int getOutputSize() const;
71  std::vector<OutputLink*> getOutputs();
72 
76  void disconnect();
77 
78  //module graph convenience functions
79  void traverseChildren(std::function<void (const Module &)> callback) const;
80  void traverseParents(std::function<void(const Module&)> callback) const;
81  //for now, these two includes this node as well
82  void traverseDescendants(std::function<void(const Module&)> callback) const;
83  void traverseDescendants(std::function<void(Module&)> callback);
84  void traverseAncestors(std::function<void(const Module&)> callback) const;
85 
89  int getDepth() const;
93  int getHeight() const;
94 
95  //signals
97  signal<void (Module&, const std::string&)> nameChanged;
99  signal<void (Module&, const std::string&)> descriptionChanged;
101  signal<void (Module&)> destroying;
103  signal<void (Module&, InputLink&)> addedInputLink;
105  signal<void (Module&, const ModuleInput&)> removedInputLink;
107  signal<void (Module&, OutputLink&)> addedOutputLink;
109  signal<void (Module&, const ModuleOutput&)> removedOutputLink;
111  signal<void (Module&)> dependenciesChanged;
112 
113  //static methods
114  static std::vector<Module *> getDependenciesSorted(const std::vector<OutputLink *> &outputs,
115  const std::set<InputLink *> &ignoreInputs = {});
116  static std::vector<const Module *> getDependenciesSorted(const std::vector<const OutputLink *> &outputs,
117  const std::set<const InputLink *> &ignoreInputs = {});
118  static void topologicallyTraverseDependencies(const std::vector<OutputLink*> &outputs, std::function<void(Module&)> visitor,
119  const std::set<InputLink*> &ignoreInputs = {});
120  static void topologicallyTraverseDependencies(const std::vector<const OutputLink*> &outputs, std::function<void(const Module&)> visitor,
121  const std::set<const InputLink*> &ignoreInputs = {});
122 
123 private:
124  bool removeInput(const ModuleInput& moduleInput);
125  bool removeOutput(const ModuleOutput& moduleOutput);
126  void createInputLink(const ModuleInput& moduleInput);
127  void createOutputLink(const ModuleOutput& moduleInput);
128  const ModuleType& m_type;
129  std::string m_name, m_description;
130  std::vector<std::unique_ptr<InputLink>> m_inputs;
131  std::vector<std::unique_ptr<OutputLink>> m_outputs;
132 
133  //we need some connections to the moduletype to make sure links are added and removed as needed
134  boost::signals2::scoped_connection m_moduleInputAddedCon, m_moduleInputRemovedCon;
135  boost::signals2::scoped_connection m_moduleOutputAddedCon, m_moduleOutputRemovedCon;
136 };
137 
138 } // namespace nm
139 
140 #endif // NM_MODULE_H
Base class for stuff that needs to provide user data in form of a void* pointer.
Definition: userdataprovider.hpp:10
signal< void(Module &)> destroying
This signal is emitted before the Module is destroyed.
Definition: module.hpp:101
An instantiated ModuleType. A node in a function graph.
Definition: module.hpp:40
Module(const ModuleType &type, std::string name, std::string description="")
Constructor.
Definition: module.cpp:15
void disconnect()
disconnects module by unlinking all InputLinks and OutputLinks of this module.
Definition: module.cpp:175
Describes a recipe for a module and its inputs and outputs.
Definition: moduletype.hpp:37
int getDepth() const
Definition: module.cpp:250
signal< void(Module &, OutputLink &)> addedOutputLink
This signal is emitted after an output has been added.
Definition: module.hpp:107
signal< void(Module &, const ModuleOutput &)> removedOutputLink
This signal is emitted after an output has been removed.
Definition: module.hpp:109
signal< void(Module &)> dependenciesChanged
This signal is emitted if changes have been made to the graph that may influence this Module's output...
Definition: module.hpp:111
signal< void(Module &, InputLink &)> addedInputLink
This signal is emitted after an input has been added.
Definition: module.hpp:103
signal< void(Module &, const std::string &)> nameChanged
This signal is emitted when the name of the Module is changed.
Definition: module.hpp:97
signal< void(Module &, const ModuleInput &)> removedInputLink
This signal is emitted after an input has been removed.
Definition: module.hpp:105
int getHeight() const
Definition: module.cpp:260
signal< void(Module &, const std::string &)> descriptionChanged
This signal is emitted when the description of the Module is changed.
Definition: module.hpp:99