[coco] Safe downcast from Object to KernelObject (#798)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 26 Jul 2018 01:57:24 +0000 (10:57 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Thu, 26 Jul 2018 01:57:24 +0000 (10:57 +0900)
This commit introduces 'asKernel' method which allows users to safely
downcast 'Object' as 'KernelObject'.

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

index f2681f0..e72b867 100644 (file)
@@ -23,6 +23,10 @@ public:
   virtual ~KernelObject() = default;
 
 public:
+  KernelObject *asKernel(void) override { return this; }
+  const KernelObject *asKernel(void) const override { return this; }
+
+public:
   const nncc::core::ADT::kernel::Shape &shape(void) const { return _shape; }
 
 private:
index ad9a6f3..bb148a9 100644 (file)
@@ -4,6 +4,7 @@
 #include "coco/IR/Bag.h"
 
 #include "coco/IR/FeatureObject.forward.h"
+#include "coco/IR/KernelObject.forward.h"
 
 namespace coco
 {
@@ -32,6 +33,9 @@ public:
 public:
   virtual FeatureObject *asFeature(void) { return nullptr; }
   virtual const FeatureObject *asFeature(void) const { return nullptr; }
+
+  virtual KernelObject *asKernel(void) { return nullptr; }
+  virtual const KernelObject *asKernel(void) const { return nullptr; }
 };
 
 } // namespace coco
index b96125e..a543b9f 100644 (file)
@@ -14,6 +14,18 @@ TEST(IR_KERNEL_OBJECT, ctor_should_set_size)
   ASSERT_EQ(o.shape().width(), shape.width());
 }
 
+TEST(IR_KERNEL_OBJECT, asKernel)
+{
+  const nncc::core::ADT::kernel::Shape shape{1, 1, 3, 3};
+  coco::KernelObject o{shape};
+
+  coco::Object *mutable_object = &o;
+  const coco::Object *immutable_object = &o;
+
+  ASSERT_NE(mutable_object->asKernel(), nullptr);
+  ASSERT_EQ(mutable_object->asKernel(), immutable_object->asKernel());
+}
+
 TEST(IR_KERNEL_OBJECT, at)
 {
   const uint32_t N = 1;