Noise Modeler Library  0.1
Library for procedurally generating noise-based terrains and other content
 All Classes Files Functions Variables Enumerations Enumerator Groups Pages
inlinegenerator.hpp
1 #ifndef NM_INLINEGENERATOR_HPP
2 #define NM_INLINEGENERATOR_HPP
3 
4 #include <nmlib/codegeneration/idgenerator.hpp>
5 #include <nmlib/codegeneration/modulegenerator.hpp>
6 
7 #include <nmlib/model/inputlink.hpp>
8 #include <nmlib/model/outputlink.hpp>
9 #include <nmlib/model/signalvalue.hpp>
10 
11 #include <nmlib/util.hpp>
12 
13 namespace nm {
14 
24 struct Expression;
25 struct Assignment;
26 struct Variable;
27 struct Declaration;
28 struct FunctionCall;
29 
35 public:
36  struct InputRemap {
37  std::string externalName;
38  const InputLink* inputLink;
39  };
40  struct OutputRemap {
41  std::string externalName;
42  const OutputLink* outputLink;
43  };
44 
45  void generateFromLinks(const std::vector<InputRemap> &inputRemaps, const std::vector<OutputRemap> &outputRemaps, std::ostream &out);
46  void generateModule(const Module& module, const std::vector<InputRemap> &inputRemaps, const std::vector<OutputRemap> &outputRemaps, std::ostream &out);
47  std::string getUniqueId();
48  virtual std::unique_ptr<ModuleGenerator> getModuleGenerator(const Module &module);
49 
50 protected:
51  //syntax tree generation helpers
52  virtual void genTypeKeyword(const SignalType &signalType, std::ostream& out) = 0;
53  virtual void genDeclaration(const Declaration &variable, std::ostream &out);
54  virtual void genAssignment(const Assignment &assignment, std::ostream &out);
55  virtual void genVariable(const Variable &variable, std::ostream &out);
56  virtual void genValue(const SignalValue &value, std::ostream &out);
57  virtual void genFunctionCall(FunctionCall &functionCall, std::ostream &out);
58 
59 
60 private:
61  void generateOutputDeclarations(const std::vector<OutputRemap> &conversions, std::ostream& out);
62  void generateInputDeclarations(const Module &, std::ostream& out);
63  void generateInputAssignments(const std::vector<InputRemap> &conversions, std::ostream& out);
64  void generateOutputAssignments(const std::vector<OutputRemap> &conversions, std::ostream& out);
65 
66  IdGenerator m_idGenerator;
67 
68  friend struct Assignment;
69  friend struct Variable;
70  friend struct Expression;
71  friend struct Value;
72  friend struct Declaration;
73  friend struct FunctionCall;
74 };
75 
76 struct SyntaxNode {
77  virtual void gen(InlineGenerator &gen,std::ostream& out) = 0;
78  virtual ~SyntaxNode(){}
79 };
80 
81 struct Expression : public SyntaxNode {
82 };
83 
84 struct Variable : public Expression {
85  Variable(std::string id):m_id(id){}
86  virtual void gen(InlineGenerator &g, std::ostream &out) override {
87  g.genVariable(*this, out);
88  }
89  std::string m_id;
90 };
91 
92 struct Assignment : public Expression {
93  Assignment(std::unique_ptr<Variable> id, std::unique_ptr<Expression> value):
94  lhs(std::move(id)),
95  rhs(std::move(value))
96  {}
97  Assignment(std::string l, std::string r):
98  lhs{make_unique<Variable>(std::move(l))},
99  rhs{make_unique<Variable>(std::move(r))}
100  {}
101  virtual void gen(InlineGenerator &g, std::ostream &out) override {
102  g.genAssignment(*this, out);
103  }
104  std::unique_ptr<Variable> lhs;
105  std::unique_ptr<Expression> rhs;
106 };
107 
108 struct Declaration : public SyntaxNode {
109  Declaration(SignalType t, std::string s):
110  type{t},
111  id{make_unique<Variable>(std::move(s))}
112  {}
113  Declaration(SignalType t, std::unique_ptr<Variable> v):
114  type(t),
115  id(std::move(v))
116  {}
117  virtual void gen(InlineGenerator &g, std::ostream &out) override {
118  g.genDeclaration(*this, out);
119  }
120  SignalType type;
121  std::unique_ptr<Variable> id;
122 };
123 
124 struct Value : public Expression {
125  Value(std::unique_ptr<SignalValue> v): value(std::move(v)){}
126  virtual void gen(InlineGenerator &g, std::ostream &out) override {
127  g.genValue(*value, out);
128  }
129  std::unique_ptr<SignalValue> value;
130 };
131 
132 struct FunctionCall : public SyntaxNode {
133  template<typename T, typename U, typename V>
134  FunctionCall(T&& function, U&& ins, V&& outs):
135  functionName{std::forward<T>(function)},
136  inputs{std::forward<U>(ins)},
137  outputs{std::forward<V>(outs)}
138  {}
139  virtual void gen(InlineGenerator &g, std::ostream &out) override {
140  g.genFunctionCall(*this, out);
141  }
142  std::string functionName;
143  std::vector<Variable> inputs;
144  std::vector<Variable> outputs;
145 };
146 
147 } // namespace nm
148 
149 #endif // NM_INLINEGENERATOR_HPP
Definition: inlinegenerator.hpp:76
Definition: inlinegenerator.hpp:132
An instantiated ModuleType. A node in a function graph.
Definition: module.hpp:40
Definition: inlinegenerator.hpp:124
Definition: inlinegenerator.hpp:40
Definition: inlinegenerator.hpp:81
Definition: inlinegenerator.hpp:92
Utility headers.
Abstract base class for code generators.
Definition: inlinegenerator.hpp:34
Describes a the dimensionality of a signal Can be extended to include other type information such as ...
Definition: signaltype.hpp:13
Definition: inlinegenerator.hpp:84
A vector of floats that can be set as the unlinked value of InputLinks.
Definition: signalvalue.hpp:15
Definition: inlinegenerator.hpp:108
Definition: inlinegenerator.hpp:36
The IdGenerator class.
Definition: idgenerator.hpp:12