From d47ec4e15b21eff6049a8dd7484077d3c0ebd8b3 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: Mon, 23 Jul 2018 14:10:45 +0900 Subject: [PATCH] [coco] Introduce factory method for Module (#756) Several memory allocations are necessary to construct a module object currently, which may throw an exception. This commit extracts all of memory allocations from constructor. To extract memory allocations from constructor, this commit introduces tow major changes over Module class design: - Rewrite Module as pure virtual interface - Introduce factory method Signed-off-by: Jonghyun Park --- contrib/coco/core/include/coco/IR/Module.h | 15 +++++++++------ contrib/coco/core/src/IR/Module.cpp | 23 +++++++++++++++++++++-- contrib/coco/core/src/IR/Module.test.cpp | 14 +++++++------- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/contrib/coco/core/include/coco/IR/Module.h b/contrib/coco/core/include/coco/IR/Module.h index 0fede3a..f51a626 100644 --- a/contrib/coco/core/include/coco/IR/Module.h +++ b/contrib/coco/core/include/coco/IR/Module.h @@ -11,21 +11,24 @@ namespace coco /** * @breif Top-level element of coco IR which represents a neural network */ -class Module final +class Module { public: - Module(); + Module() = default; public: Module(const Module &) = delete; Module(Module &&) = delete; public: - EntityManager *entity(void) { return _entity.get(); } - const EntityManager *entity(void) const { return _entity.get(); } + virtual ~Module() = default; -private: - std::unique_ptr _entity; +public: + virtual EntityManager *entity(void) = 0; + virtual const EntityManager *entity(void) const = 0; + +public: + static std::unique_ptr create(void); }; } // namespace coco diff --git a/contrib/coco/core/src/IR/Module.cpp b/contrib/coco/core/src/IR/Module.cpp index 388c11b..35eb99e 100644 --- a/contrib/coco/core/src/IR/Module.cpp +++ b/contrib/coco/core/src/IR/Module.cpp @@ -12,12 +12,31 @@ struct EntityManagerImpl final : public coco::EntityManager } // namespace +namespace +{ + +class ModuleImpl final : public coco::Module +{ +public: + std::unique_ptr _entity; + +public: + coco::EntityManager *entity(void) override { return _entity.get(); } + const coco::EntityManager *entity(void) const override { return _entity.get(); } +}; + +} // namespace + namespace coco { -Module::Module() +std::unique_ptr Module::create(void) { - _entity = nncc::foundation::make_unique<::EntityManagerImpl>(); + auto m = nncc::foundation::make_unique<::ModuleImpl>(); + + m->_entity = nncc::foundation::make_unique<::EntityManagerImpl>(); + + return std::move(m); } } // namespace coco diff --git a/contrib/coco/core/src/IR/Module.test.cpp b/contrib/coco/core/src/IR/Module.test.cpp index 680ebf5..e1edace 100644 --- a/contrib/coco/core/src/IR/Module.test.cpp +++ b/contrib/coco/core/src/IR/Module.test.cpp @@ -2,15 +2,15 @@ #include -TEST(IR_MODULE, default_constructable) +TEST(IR_MODULE, create) { - // coco::Module should be default constructable. - auto m = new coco::Module{}; + auto m = coco::Module::create(); - ASSERT_NE(m, nullptr); + ASSERT_NE(m.get(), nullptr); - ASSERT_NE(m->entity(), nullptr); - ASSERT_NE(const_cast(m)->entity(), nullptr); + coco::Module *mutable_m = m.get(); + const coco::Module *immutable_m = m.get(); - delete m; + ASSERT_NE(mutable_m->entity(), nullptr); + ASSERT_NE(immutable_m->entity(), nullptr); } -- 2.7.4