[coco] Class static head (#1498)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Fri, 14 Sep 2018 10:57:09 +0000 (19:57 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Fri, 14 Sep 2018 10:57:09 +0000 (19:57 +0900)
This commit replaces pure virtual head method with class static version
in order to support detach from destructor.

Note that destructor SHOULD NOT invoke virtual method.

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

index 92c1743..d506675 100644 (file)
@@ -130,6 +130,9 @@ template <typename Child, typename Parent> struct DLinkedList
     Child *_tail;
   };
 
+  // NOTE Client SHOULD implement this static method
+  static Head *head(Parent *);
+
   class Node
   {
   public:
@@ -159,14 +162,12 @@ template <typename Child, typename Parent> struct DLinkedList
     Child *prev(void) const { return _prev; }
     Child *next(void) const { return _next; }
 
-  protected:
-    virtual Head *head(void) const = 0;
-
   public:
     void insertBefore(Node *next)
     {
       assert(next != nullptr);
-      assert(next->head() != nullptr);
+      assert(next->parent() != nullptr);
+      assert(head(next->parent()) != nullptr);
 
       assert(_prev == nullptr);
       assert(_next == nullptr);
@@ -183,7 +184,7 @@ template <typename Child, typename Parent> struct DLinkedList
 
       // Update parent-child relation
       assert(parent() == nullptr);
-      next->head()->enlist(curr());
+      head(next->parent())->enlist(curr());
       assert(parent() == next->parent());
     }
 
@@ -191,7 +192,8 @@ template <typename Child, typename Parent> struct DLinkedList
     void insertAfter(Node *prev)
     {
       assert(prev != nullptr);
-      assert(prev->head() != nullptr);
+      assert(prev->parent() != nullptr);
+      assert(head(prev->parent()) != nullptr);
 
       assert(_prev == nullptr);
       assert(_next == nullptr);
@@ -209,7 +211,7 @@ template <typename Child, typename Parent> struct DLinkedList
 
       // Update parent-child relation
       assert(parent() == nullptr);
-      prev->head()->enlist(curr());
+      head(prev->parent())->enlist(curr());
       assert(parent() == prev->parent());
     };
 
@@ -218,8 +220,8 @@ template <typename Child, typename Parent> struct DLinkedList
     {
       // Update parent-child relation
       assert(parent() != nullptr);
-      assert(head() != nullptr);
-      head()->delist(curr());
+      assert(head(parent()) != nullptr);
+      head(parent())->delist(curr());
       assert(parent() == nullptr);
 
       // Update the link of sibling nodes
index e17ddb7..af1eed0 100644 (file)
@@ -38,9 +38,6 @@ public:
 
 private:
   DLinkedList<Instr, Block>::Head _instr;
-
-public:
-  BlockList *head(void) const override;
 };
 
 } // namespace coco
index 42ef65a..30df370 100644 (file)
@@ -39,9 +39,6 @@ public:
   virtual ~Instr() = default;
 
 public:
-  InstrList *head(void) const override;
-
-public:
 #define INSTR(Name)                                \
   virtual Name *as##Name(void) { return nullptr; } \
   virtual const Name *as##Name(void) const { return nullptr; }
index caaffdc..c09ddd8 100644 (file)
@@ -33,13 +33,17 @@ public:
   {
     // DO NOTHING
   }
-
-private:
-  ChildList *head(void) const override { return parent()->children(); }
 };
 
 } // namespace
 
+namespace coco
+{
+
+template <> ChildList *DLinkedList<Child, Parent>::head(Parent *p) { return p->children(); }
+
+} // namespace coco
+
 TEST(ADT_DLINKED_LINK, insert_two_elements)
 {
   auto link = new coco::PtrLink<::Child, ::Parent>{};
index ec11d8d..f52facf 100644 (file)
@@ -4,6 +4,6 @@
 namespace coco
 {
 
-BlockList *Block::head(void) const { return parent()->block(); }
+template <> BlockList *DLinkedList<Block, Module>::head(Module *m) { return m->block(); }
 
 } // namespace coco
index 60e7b8e..6cd5cd7 100644 (file)
@@ -6,7 +6,7 @@
 namespace coco
 {
 
-InstrList *Instr::head(void) const { return parent()->instr(); }
+template <> InstrList *DLinkedList<Instr, Block>::head(Block *b) { return b->instr(); }
 
 } // namespace coco