[coco] Support Object downcast (#792)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Wed, 25 Jul 2018 03:11:14 +0000 (12:11 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Wed, 25 Jul 2018 03:11:14 +0000 (12:11 +0900)
This commit adds 'asFeature' method in Object class which allows users
to safely downcast 'Object' as 'FeatureObject'.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/coco/core/include/coco/IR/FeatureObject.h
contrib/coco/core/include/coco/IR/Object.h
contrib/coco/core/src/IR/FeatureObject.test.cpp

index ed28f2d..741f45f 100644 (file)
@@ -20,6 +20,10 @@ public:
   virtual ~FeatureObject() = default;
 
 public:
+  FeatureObject *asFeature(void) override { return this; }
+  const FeatureObject *asFeature(void) const override { return this; }
+
+public:
   const nncc::core::ADT::feature::Shape &shape(void) const { return _shape; }
 
 private:
index 521dc50..997a58e 100644 (file)
@@ -3,6 +3,8 @@
 
 #include "coco/IR/Bag.h"
 
+#include "coco/IR/FeatureObject.forward.h"
+
 namespace coco
 {
 
@@ -23,6 +25,10 @@ private:
 public:
   coco::Bag *bag(void) const { return _bag; }
   void bag(coco::Bag *bag);
+
+public:
+  virtual FeatureObject *asFeature(void) { return nullptr; }
+  virtual const FeatureObject *asFeature(void) const { return nullptr; }
 };
 
 } // namespace coco
index 2819ba9..38dfd50 100644 (file)
@@ -9,3 +9,15 @@ TEST(IR_FEATURE_OBJECT, ctor_should_set_size)
 
   ASSERT_EQ(o.shape(), shape);
 }
+
+TEST(IR_FEATURE_OBJECT, asFeature)
+{
+  const nncc::core::ADT::feature::Shape shape{1, 3, 3};
+  coco::FeatureObject o{shape};
+
+  coco::Object *mutable_object = &o;
+  const coco::Object *immutable_object = &o;
+
+  ASSERT_NE(mutable_object->asFeature(), nullptr);
+  ASSERT_EQ(mutable_object->asFeature(), immutable_object->asFeature());
+}