[coco] Use 'Dep' inside 'Object' (#1406)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Fri, 7 Sep 2018 07:38:51 +0000 (16:38 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Fri, 7 Sep 2018 07:38:51 +0000 (16:38 +0900)
This commit replaces Bag * inside Object with 'Dep'.

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

index cde9132..308e3f7 100644 (file)
@@ -2,6 +2,7 @@
 #define __COCO_IR_OBJECT_H__
 
 #include "coco/IR/Bag.h"
+#include "coco/IR/Dep.h"
 #include "coco/IR/ObjectInfo.forward.h"
 
 #include "coco/IR/FeatureObject.forward.h"
@@ -42,12 +43,9 @@ protected:
   virtual void get(ObjectInfo **) const = 0;
   void setBagLink(const PtrLink<Bag, BagInfo> *);
 
-private:
-  coco::Bag *_bag;
-
 public:
-  coco::Bag *bag(void) const { return _bag; }
-  void bag(coco::Bag *bag);
+  coco::Bag *bag(void) const { return _dep.bag(); }
+  void bag(coco::Bag *bag) { _dep.bag(bag); }
 
 public:
   virtual FeatureObject *asFeature(void) { return nullptr; }
@@ -58,14 +56,13 @@ public:
 
 private:
   ObjectInfo *info(void) const;
-  BagInfo *info(const Bag *) const;
 
 public:
   Def *def(void) const;
   const User *user(void) const;
 
 private:
-  const PtrLink<Bag, BagInfo> *_bag_link;
+  Dep _dep;
 };
 
 } // namespace coco
index 838c06c..2fca0cc 100644 (file)
@@ -8,38 +8,13 @@
 namespace coco
 {
 
-Object::Object() : _bag{nullptr}
+Object::Object()
 {
-  // DO NOTHING
+  // Register self to Dep
+  _dep.object(this);
 }
 
-void Object::setBagLink(const PtrLink<Bag, BagInfo> *bag_link) { _bag_link = bag_link; }
-
-void Object::bag(coco::Bag *bag)
-{
-  if (_bag != nullptr)
-  {
-    auto bag_info = info(_bag);
-    assert(bag_info != nullptr);
-    assert(bag_info->object()->find(this) != bag_info->object()->end());
-
-    bag_info->object()->erase(this);
-
-    _bag = nullptr;
-  }
-
-  assert(_bag == nullptr);
-
-  if (bag != nullptr)
-  {
-    // TODO Update bag-object relation
-    _bag = bag;
-
-    auto bag_info = info(_bag);
-    assert(bag_info != nullptr);
-    bag_info->object()->insert(this);
-  }
-}
+void Object::setBagLink(const PtrLink<Bag, BagInfo> *bag_link) { _dep.link(bag_link); }
 
 ObjectInfo *Object::info(void) const
 {
@@ -48,12 +23,6 @@ ObjectInfo *Object::info(void) const
   return res;
 }
 
-BagInfo *Object::info(const Bag *bag) const
-{
-  assert(_bag_link != nullptr);
-  return _bag_link->find(bag);
-}
-
 Object::Def *Object::def(void) const { return info()->def(); }
 const Object::User *Object::user(void) const { return info()->user(); }
 
index 2305bbe..6a236c8 100644 (file)
@@ -91,3 +91,21 @@ TEST_F(ObjectTest, bag_update)
 
   ASSERT_EQ(bag->object()->size(), 0);
 }
+
+TEST_F(ObjectTest, destructor)
+{
+  auto bag = bag_mgr.create(1);
+
+  // Destruct Object after proper initialization
+  {
+    ::mock::Object obj;
+
+    obj.info = make_unique<coco::ObjectInfo>();
+    obj.link(&link);
+
+    obj.bag(bag);
+  }
+
+  // Object SHOULD be unlinked from Bag on destruction
+  ASSERT_EQ(bag->object()->size(), 0);
+}