Noise Modeler Library  0.1
Library for procedurally generating noise-based terrains and other content
 All Classes Files Functions Variables Enumerations Enumerator Groups Pages
moduletype.hpp
1 #ifndef NM_MODULETYPE_HPP
2 #define NM_MODULETYPE_HPP
3 
4 #include <nmlib/model/signaltype.hpp>
5 
6 #include <nmlib/util/noncopyable.hpp>
7 #include <nmlib/util/userdataprovider.hpp>
8 #include <nmlib/util/signals.hpp>
9 
10 #include <vector>
11 #include <string>
12 #include <memory>
13 
14 namespace nm {
15 
16 class Module;
17 class ModuleOutput;
18 class ModuleInput;
19 class Graph;
20 class OutputLink;
21 class SignalValue;
22 
38 {
39 public:
43  enum class Category { //couldn't call it moduletypetype, cause that would be silly
45  Primitive,
46 
48  Composite,
49 
51  GraphInput,
52 
55  };
56 
62  explicit ModuleType(std::string name, std::string description);
63 
69  explicit ModuleType(std::string name, Category category = Category::Composite, std::string description = "");
70  ~ModuleType();
71 
75  std::string getName() const { return m_name; }
76  void setName(std::string name);
77 
78  std::string getDescription() const { return m_description; }
79  void setDescription(std::string description);
83  bool isBuiltin() const { return !isComposite(); } //subject to change (some builtins may become composites in the future
84 
88  bool isComposite() const { return m_category == Category::Composite; }
89 
93  bool isPrimitive() const { return m_category == Category::Primitive; } //consider if graphinput and graphoutput should be primitive as well?
94 
101  bool isRemovable() const { return m_removable; }
102 
106  bool isGraphInput() const { return m_category == Category::GraphInput; }
107 
111  bool isGraphOutput() const { return m_category == Category::GraphOutput; }
112 
116  void setRemovable(bool removable) { m_removable = removable; }
117 
118  unsigned int numInputs() const { return m_inputs.size(); }
119  const ModuleInput *getInput(std::string name) const;
120  ModuleInput *getInput(std::string name);
121  const ModuleInput *getInput(unsigned int index) const { return m_inputs.at(index).get(); }
122  ModuleInput *getInput(unsigned int index) { return m_inputs.at(index).get(); }
123 
125  void eachModuleInput(std::function<void(const ModuleInput&)> f) const;
126 
128  void eachModuleInput(std::function<void(ModuleInput&)> f);
129  //mutable stuff
130 
138  ModuleInput *addInput(std::string name, SignalType signalType);
139 
147  ModuleInput *addInput(std::string name, SignalValue defaultValue);
148 
157  bool removeInput(ModuleInput *moduleInput);
158 
159  unsigned int numOutputs() const { return m_outputs.size(); }
160  const ModuleOutput *getOutput(std::string name) const;
161  ModuleOutput *getOutput(std::string name);
162  const ModuleOutput *getOutput(unsigned int index) const { return m_outputs.at(index).get(); }
163  ModuleOutput *getOutput(unsigned int index) { return m_outputs.at(index).get(); }
164 
166  void eachModuleOutput(std::function<void(const ModuleOutput&)> f) const;
167 
169  void eachModuleOutput(std::function<void(ModuleOutput&)> f);
170 
178  ModuleOutput *addOutput(std::string name, SignalType signalType);
187  bool removeOutput(ModuleOutput *moduleOutput);
188 
194  Graph* getGraph() { return m_graph.get(); }
195 
197  const Graph* getGraph() const { return m_graph.get(); }
198 
203 
205  const Module* getInputModule() const;
206 
211 
213  const Module* getOutputModule() const;
214 
220  ModuleOutput *exportInternalOutput(OutputLink &outputLink, std::string externalName);
221 
222  //TODO enum and/or constant params
223 
224  //signals
226  signal<void(ModuleType&, const std::string&)> nameChanged;
228  signal<void(ModuleType&, const std::string&)> descriptionChanged;
230  signal<void(ModuleInput&)> inputAdded;
232  signal<void(ModuleInput&)> removingInput;
234  signal<void(ModuleType&)> inputRemoved;
236  signal<void(ModuleOutput&)> outputAdded;
238  signal<void(ModuleOutput&)> removingOutput;
240  signal<void(ModuleType&)> outputRemoved;
242  signal<void(ModuleType&)> destroying;
243  //TODO more signals
244 // signal<void(ModuleType&)> nameChanged;
245 
246 
247  //implementation
248 private:
249  std::string m_name, m_description;
250  bool m_removable;
251  const Category m_category; // these are special types of nodes that there can only be one of in each graph
252  // they also can't be copied
253 
254  std::vector<std::unique_ptr<ModuleInput>> m_inputs;
255  std::vector<std::unique_ptr<ModuleOutput>> m_outputs;
256 
257  std::unique_ptr<Graph> m_graph; //if this is a composite module, it will have a graph of internal nodes, this is it.
258  std::unique_ptr<ModuleType> m_inputModuleType; //if this is a composite module, it will have a special module type for inputs this is were its type will be stored
259  std::unique_ptr<ModuleType> m_outputModuleType; //if this is a composite module, it will have a special module type for outputs
260 };
261 
262 } // namespace nm
263 
264 #endif // NM_MODULETYPE_HPP
Base class for stuff that needs to provide user data in form of a void* pointer.
Definition: userdataprovider.hpp:10
Module * getInputModule()
Accessor for the input module of a composite module type.
Definition: moduletype.cpp:169
signal< void(ModuleOutput &)> outputAdded
This signal is emitted after an output has been added.
Definition: moduletype.hpp:236
signal< void(ModuleType &, const std::string &)> nameChanged
This signal is emitted when the name of the moduleType changes.
Definition: moduletype.hpp:226
An instantiated ModuleType. A node in a function graph.
Definition: module.hpp:40
std::string getName() const
A unique identifier for the module type.
Definition: moduletype.hpp:75
bool isPrimitive() const
A primitive module type, is the opposite of a composite type.
Definition: moduletype.hpp:93
A graph of Modules.
Definition: graph.hpp:16
bool isRemovable() const
Some modules, like the ones representing inputs and outputs, may not be removed from their graphs...
Definition: moduletype.hpp:101
signal< void(ModuleType &)> destroying
This signal is emitted before the type is destroyed.
Definition: moduletype.hpp:242
signal< void(ModuleType &)> outputRemoved
This signal is emitted after an output has been removed.
Definition: moduletype.hpp:240
bool isBuiltin() const
A built-in module type is part of nmlib, and not created by the user.
Definition: moduletype.hpp:83
ModuleOutput * exportInternalOutput(OutputLink &outputLink, std::string externalName)
Convenience method for exposing a part of the module graph as a new external output.
Definition: moduletype.cpp:189
bool isGraphInput() const
A graph input module type represents the inputs of a composite module type.
Definition: moduletype.hpp:106
signal< void(ModuleType &)> inputRemoved
This signal is emitted after an input has been removed.
Definition: moduletype.hpp:234
void eachModuleInput(std::function< void(const ModuleInput &)> f) const
Iterate over the inputs using the provided callback.
Definition: moduletype.cpp:64
Describes a recipe for a module and its inputs and outputs.
Definition: moduletype.hpp:37
Describes one of a ModuleType's outputs (name, SignalType)
Definition: moduleoutput.hpp:19
bool isComposite() const
A composite module type is built from a graph of modules of other types.
Definition: moduletype.hpp:88
Graph * getGraph()
Accessor for the graph of a composite module type.
Definition: moduletype.hpp:194
Category
Module type category.
Definition: moduletype.hpp:43
const Graph * getGraph() const
Accessor for the graph of a composite module type.
Definition: moduletype.hpp:197
bool removeInput(ModuleInput *moduleInput)
Removes an input from this module type.
Definition: moduletype.cpp:95
void eachModuleOutput(std::function< void(const ModuleOutput &)> f) const
Iterate over the outputs using the provided callback.
Definition: moduletype.cpp:127
ModuleOutput * addOutput(std::string name, SignalType signalType)
Add a new output to the module type.
Definition: moduletype.cpp:141
signal< void(ModuleOutput &)> removingOutput
This signal is emitted before an output is removed.
Definition: moduletype.hpp:238
signal< void(ModuleType &, const std::string &)> descriptionChanged
This signal is emitted when the description of the moduleType changes.
Definition: moduletype.hpp:228
Module * getOutputModule()
Accessor for the output module of a composite module type.
Definition: moduletype.cpp:178
bool removeOutput(ModuleOutput *moduleOutput)
Removes an output from this module type.
Definition: moduletype.cpp:153
signal< void(ModuleInput &)> removingInput
This signal is emitted before an input is removed.
Definition: moduletype.hpp:232
A super-class for non-copyable classes.
Definition: noncopyable.hpp:10
void setRemovable(bool removable)
Change whether modules of this type may be removed from graphs or not.
Definition: moduletype.hpp:116
ModuleInput * addInput(std::string name, SignalType signalType)
Add a new input to the module type.
Definition: moduletype.cpp:78
bool isGraphOutput() const
A graph output module type represents the outputs of a composite module type.
Definition: moduletype.hpp:111
ModuleType(std::string name, std::string description)
Simple Constructor for composite module types.
Definition: moduletype.cpp:12
signal< void(ModuleInput &)> inputAdded
This signal is emitted after an input has been added.
Definition: moduletype.hpp:230