[coco] Direct Parent-Child relation update in DLinkedList (#1491)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Fri, 14 Sep 2018 07:48:31 +0000 (16:48 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Fri, 14 Sep 2018 07:48:31 +0000 (16:48 +0900)
This commit revises DLinkedLists to directly updates parent-child
relations instead of using PtrLink.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/coco/core/include/coco/ADT/DLinkedList.h

index 4bfd804..81ac344 100644 (file)
@@ -15,7 +15,8 @@ template <typename Child, typename Parent> struct DLinkedList
   class Head
   {
   public:
-    Head(Parent *parent, PtrLink<Child, Parent> *link) : _parent{parent}, _link{link}
+    // TODO _link is no longer used
+    Head(Parent *parent, PtrLink<Child, Parent> *link) : _parent{parent}
     {
       _head = nullptr;
       _tail = nullptr;
@@ -72,7 +73,7 @@ template <typename Child, typename Parent> struct DLinkedList
       }
 
       // Update parent-child relation
-      _link->set(child, _parent);
+      child->parent(_parent);
     }
 
   public:
@@ -92,7 +93,7 @@ template <typename Child, typename Parent> struct DLinkedList
       }
 
       // Update parent-child relation
-      _link->unset(child);
+      child->parent(nullptr);
     }
 
   public:
@@ -123,7 +124,6 @@ template <typename Child, typename Parent> struct DLinkedList
 
   private:
     Parent *const _parent;
-    PtrLink<Child, Parent> *const _link;
 
   private:
     Child *_head;
@@ -133,6 +133,9 @@ template <typename Child, typename Parent> struct DLinkedList
   class Node
   {
   public:
+    friend class Head;
+
+  public:
     Node()
     {
       static_assert(std::is_base_of<Node, Child>::value,
@@ -146,16 +149,11 @@ template <typename Child, typename Parent> struct DLinkedList
     virtual ~Node() = default;
 
   protected:
+    // NOTE This method will be deprecated
     virtual void get(const PtrLink<Child, Parent> **out) const = 0;
 
   public:
-    Parent *parent(void) const
-    {
-      const PtrLink<Child, Parent> *link = nullptr;
-      get(&link);
-      assert(link != nullptr);
-      return link->find(curr());
-    }
+    Parent *parent(void) const { return _parent; }
 
   private:
     Child *curr(void) { return reinterpret_cast<Child *>(this); }
@@ -245,6 +243,12 @@ template <typename Child, typename Parent> struct DLinkedList
     }
 
   private:
+    // WARN Do NOT invoke this method outside Head::enlist
+    void parent(Parent *p) { _parent = p; }
+
+  private:
+    // WARN Do NOT modify this field inside Node.
+    Parent *_parent = nullptr;
     Child *_prev;
     Child *_next;
   };