[coco] Add a static_assert for type of Child (#830)
author이한종/동작제어Lab(SR)/Engineer/삼성전자 <hanjoung.lee@samsung.com>
Thu, 26 Jul 2018 08:30:56 +0000 (17:30 +0900)
committer박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 26 Jul 2018 08:30:56 +0000 (17:30 +0900)
This commit limits the type of template parameter `Child` to be
subclass of `DLinkedList::Node`.

Signed-off-by: Hanjoung Lee <hanjoung.lee@samsung.com>
contrib/coco/core/include/coco/ADT/DLinkedList.h

index ea46633..d2af781 100644 (file)
@@ -4,6 +4,7 @@
 #include "coco/ADT/PtrLink.h"
 
 #include <cassert>
+#include <type_traits>
 
 namespace coco
 {
@@ -134,6 +135,8 @@ template<typename Child, typename Parent> struct DLinkedList
   public:
     Node(const PtrLink<Child, Parent> *link) : _link{link}
     {
+      static_assert(std::is_base_of<Node, Child>::value, "Type `Child` must be subclass of `Node`.");
+
       _prev = nullptr;
       _next = nullptr;
     }
@@ -160,7 +163,6 @@ template<typename Child, typename Parent> struct DLinkedList
       assert(_prev == nullptr);
       assert(_next == nullptr);
 
-      // REQUIRE Child should inheir Node class
       auto curr = reinterpret_cast<Child *>(this);
 
       // Update the link of the current node
@@ -188,7 +190,6 @@ template<typename Child, typename Parent> struct DLinkedList
       assert(_prev == nullptr);
       assert(_next == nullptr);
 
-      // REQUIRE Child should inherit Node class
       auto curr = reinterpret_cast<Child *>(this);
 
       // Update the link of the current node
@@ -211,7 +212,6 @@ template<typename Child, typename Parent> struct DLinkedList
   public:
     void detach(void)
     {
-      // REQUIRE Child SHOULD inherit Node class
       auto curr = reinterpret_cast<Child *>(this);
 
       // Update parent-child relation