From: 박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 Date: Thu, 26 Jul 2018 01:57:24 +0000 (+0900) Subject: [coco] Safe downcast from Object to KernelObject (#798) X-Git-Tag: nncc_backup~2299 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cc59204f8af0e6d1c5ea1c62bb4977c324c131cf;p=platform%2Fcore%2Fml%2Fnnfw.git [coco] Safe downcast from Object to KernelObject (#798) This commit introduces 'asKernel' method which allows users to safely downcast 'Object' as 'KernelObject'. Signed-off-by: Jonghyun Park --- diff --git a/contrib/coco/core/include/coco/IR/KernelObject.h b/contrib/coco/core/include/coco/IR/KernelObject.h index f2681f0..e72b867 100644 --- a/contrib/coco/core/include/coco/IR/KernelObject.h +++ b/contrib/coco/core/include/coco/IR/KernelObject.h @@ -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: diff --git a/contrib/coco/core/include/coco/IR/Object.h b/contrib/coco/core/include/coco/IR/Object.h index ad9a6f3..bb148a9 100644 --- a/contrib/coco/core/include/coco/IR/Object.h +++ b/contrib/coco/core/include/coco/IR/Object.h @@ -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 diff --git a/contrib/coco/core/src/IR/KernelObject.test.cpp b/contrib/coco/core/src/IR/KernelObject.test.cpp index b96125e..a543b9f 100644 --- a/contrib/coco/core/src/IR/KernelObject.test.cpp +++ b/contrib/coco/core/src/IR/KernelObject.test.cpp @@ -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;