[loco] Introduce const Graph getters (#5974)
author박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Mon, 29 Jul 2019 11:13:41 +0000 (20:13 +0900)
committer오형석/On-Device Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Mon, 29 Jul 2019 11:13:41 +0000 (20:13 +0900)
Now, Graph provides const "nodes", "inputs", "outputs" methods.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
compiler/loco/include/loco/IR/Graph.h
compiler/loco/src/IR/Graph.test.cpp

index 269c362..a3a9956 100644 (file)
@@ -200,8 +200,11 @@ public:
 
 public:
   NodeContext *nodes(void) { return &_node_ctx; }
+  const NodeContext *nodes(void) const { return &_node_ctx; }
   InputContext *inputs(void) { return &_input_ctx; }
+  const InputContext *inputs(void) const { return &_input_ctx; }
   OutputContext *outputs(void) { return &_output_ctx; }
+  const OutputContext *outputs(void) const { return &_output_ctx; }
 
 private:
   NodeContext _node_ctx;
index 636a2bb..db74db1 100644 (file)
@@ -127,6 +127,24 @@ TEST(GraphTest, consturctor_with_param_node)
   ASSERT_THROW(g->nodes()->destroy(test_node), std::invalid_argument);
 }
 
+TEST(GraphTest, getters_over_const_instance)
+{
+  auto g = loco::make_graph();
+
+  auto pull = g->nodes()->create<loco::Pull>();
+  auto push = g->nodes()->create<loco::Push>();
+
+  g->inputs()->create()->node(pull);
+  g->outputs()->create()->node(push);
+
+  auto ptr = const_cast<const loco::Graph *>(g.get());
+
+  EXPECT_EQ(ptr->nodes()->size(), 2);
+  EXPECT_EQ(ptr->inputs()->size(), 1);
+  EXPECT_EQ(ptr->inputs()->at(0)->node(), pull);
+  EXPECT_EQ(ptr->outputs()->at(0)->node(), push);
+}
+
 TEST(GraphTest, graph_node_enumeration)
 {
   auto g = loco::make_graph();