From a36f92951f8c554f280237eb8a64fca40c9d062b Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Wed, 25 Jul 2018 12:11:14 +0900 Subject: [PATCH] [coco] Support Object downcast (#792) This commit adds 'asFeature' method in Object class which allows users to safely downcast 'Object' as 'FeatureObject'. Signed-off-by: Jonghyun Park --- contrib/coco/core/include/coco/IR/FeatureObject.h | 4 ++++ contrib/coco/core/include/coco/IR/Object.h | 6 ++++++ contrib/coco/core/src/IR/FeatureObject.test.cpp | 12 ++++++++++++ 3 files changed, 22 insertions(+) diff --git a/contrib/coco/core/include/coco/IR/FeatureObject.h b/contrib/coco/core/include/coco/IR/FeatureObject.h index ed28f2d..741f45f 100644 --- a/contrib/coco/core/include/coco/IR/FeatureObject.h +++ b/contrib/coco/core/include/coco/IR/FeatureObject.h @@ -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: diff --git a/contrib/coco/core/include/coco/IR/Object.h b/contrib/coco/core/include/coco/IR/Object.h index 521dc50..997a58e 100644 --- a/contrib/coco/core/include/coco/IR/Object.h +++ b/contrib/coco/core/include/coco/IR/Object.h @@ -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 diff --git a/contrib/coco/core/src/IR/FeatureObject.test.cpp b/contrib/coco/core/src/IR/FeatureObject.test.cpp index 2819ba9..38dfd50 100644 --- a/contrib/coco/core/src/IR/FeatureObject.test.cpp +++ b/contrib/coco/core/src/IR/FeatureObject.test.cpp @@ -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()); +} -- 2.7.4