@@ -18,7 +18,8 @@ Block::Block(const std::string &id, const std::string &opcode) :
1818/* ! Calls the compile function. */
1919void Block::compile (Compiler *compiler)
2020{
21- return impl->compileFunction (compiler);
21+ if (impl->compileFunction )
22+ return impl->compileFunction (compiler);
2223}
2324
2425/* ! Returns the opcode. */
@@ -66,16 +67,18 @@ std::shared_ptr<Block> Block::next() const
6667/* ! Returns the ID of the next block. */
6768std::string Block::nextId () const
6869{
69- if (impl->next )
70- return impl->next ->id ();
71- else
72- return impl->nextId ;
70+ return impl->nextId ;
7371}
7472
7573/* ! Sets the next block. */
7674void Block::setNext (std::shared_ptr<Block> block)
7775{
7876 impl->next = block;
77+
78+ if (block)
79+ impl->nextId = block->id ();
80+ else
81+ impl->nextId = " " ;
7982}
8083
8184/* ! Sets the next block by ID. */
@@ -94,16 +97,18 @@ std::shared_ptr<Block> Block::parent() const
9497/* ! Returns the ID of the parent block. */
9598std::string Block::parentId () const
9699{
97- if (impl->parent )
98- return impl->parent ->id ();
99- else
100- return impl->parentId ;
100+ return impl->parentId ;
101101}
102102
103103/* ! Sets the parent block. */
104104void Block::setParent (std::shared_ptr<Block> block)
105105{
106106 impl->parent = block;
107+
108+ if (block)
109+ impl->parentId = block->id ();
110+ else
111+ impl->parentId = " " ;
107112}
108113
109114/* ! Sets the parent block by ID. */
@@ -122,13 +127,21 @@ std::vector<std::shared_ptr<Input>> Block::inputs() const
122127/* ! Adds an input and returns its index. */
123128int Block::addInput (std::shared_ptr<Input> input)
124129{
130+ auto it = std::find (impl->inputs .begin (), impl->inputs .end (), input);
131+
132+ if (it != impl->inputs .end ())
133+ return it - impl->inputs .begin ();
134+
125135 impl->inputs .push_back (input);
126136 return impl->inputs .size () - 1 ;
127137}
128138
129139/* ! Returns the input at index. */
130140std::shared_ptr<Input> Block::inputAt (int index) const
131141{
142+ if (index < 0 || index >= impl->inputs .size ())
143+ return nullptr ;
144+
132145 return impl->inputs [index];
133146}
134147
@@ -169,13 +182,21 @@ std::vector<std::shared_ptr<Field>> Block::fields() const
169182/* ! Adds a field and returns its index. */
170183int Block::addField (std::shared_ptr<Field> field)
171184{
185+ auto it = std::find (impl->fields .begin (), impl->fields .end (), field);
186+
187+ if (it != impl->fields .end ())
188+ return it - impl->fields .begin ();
189+
172190 impl->fields .push_back (field);
173191 return impl->fields .size () - 1 ;
174192}
175193
176194/* ! Returns the field at index. */
177195std::shared_ptr<Field> Block::fieldAt (int index) const
178196{
197+ if (index < 0 || index >= impl->fields .size ())
198+ return nullptr ;
199+
179200 return impl->fields [index];
180201}
181202
@@ -222,15 +243,7 @@ void Block::setShadow(bool newShadow)
222243/* ! Returns true if this is a top level block. */
223244bool Block::topLevel () const
224245{
225- // TODO: Return true if parentId() == ""
226- // and remove the setter
227- return impl->topLevel ;
228- }
229-
230- /* ! Toggles whether this block is a top level block. */
231- void Block::setTopLevel (bool newTopLevel)
232- {
233- impl->topLevel = newTopLevel;
246+ return (impl->parentId == " " && !impl->parent );
234247}
235248
236249/* ! Sets the Engine. */
@@ -239,8 +252,20 @@ void Block::setEngine(IEngine *newEngine)
239252 impl->engine = newEngine;
240253}
241254
255+ /* ! Returns the Engine. */
256+ IEngine *Block::engine () const
257+ {
258+ return impl->engine ;
259+ }
260+
242261/* ! Sets the Target. */
243262void Block::setTarget (Target *newTarget)
244263{
245264 impl->target = newTarget;
246265}
266+
267+ /* ! Returns the Target. */
268+ Target *Block::target () const
269+ {
270+ return impl->target ;
271+ }
0 commit comments