[coco] Add 'PtrLink' class (#698)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Wed, 18 Jul 2018 02:46:29 +0000 (11:46 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Wed, 18 Jul 2018 02:46:29 +0000 (11:46 +0900)
This commit adds 'PtrLink' class which manages a link relation between
two object pointers.

This commit also adds a related test, which shows the basic usage of
this class.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/coco/core/include/coco/ADT/PtrLink.h [new file with mode: 0644]
contrib/coco/core/src/ADT/PtrLink.test.cpp [new file with mode: 0644]

diff --git a/contrib/coco/core/include/coco/ADT/PtrLink.h b/contrib/coco/core/include/coco/ADT/PtrLink.h
new file mode 100644 (file)
index 0000000..cb5077a
--- /dev/null
@@ -0,0 +1,53 @@
+#ifndef __COCO_ADT_PTR_LINK_H__
+#define __COCO_ADT_PTR_LINK_H__
+
+#include <map>
+
+#include <cassert>
+
+namespace coco
+{
+
+template<typename From, typename Into> class PtrLink
+{
+public:
+  Into *find(const From *from) const
+  {
+    assert(from != nullptr);
+
+    auto it = _content.find(from);
+    if (it == _content.end())
+    {
+      return nullptr;
+    }
+
+    assert(it->second != nullptr);
+    return it->second;
+  }
+
+public:
+  void set(const From *from, Into *into)
+  {
+    assert(from != nullptr);
+    assert(into != nullptr);
+    assert(_content.find(from) == _content.end());
+
+    _content[from] = into;
+  }
+
+public:
+  void unset(const From *from)
+  {
+    assert(from != nullptr);
+    assert(_content.find(from) != _content.end());
+
+    _content.erase(from);
+  }
+
+private:
+  std::map<const From *, Into *> _content;
+};
+
+} // namespace coco
+
+#endif // __COCO_ADT_PTR_LINK_H__
diff --git a/contrib/coco/core/src/ADT/PtrLink.test.cpp b/contrib/coco/core/src/ADT/PtrLink.test.cpp
new file mode 100644 (file)
index 0000000..2618d48
--- /dev/null
@@ -0,0 +1,69 @@
+#include "coco/ADT/PtrLink.h"
+
+#include <gtest/gtest.h>
+
+namespace
+{
+
+class Parent;
+class Child;
+
+class Parent
+{
+public:
+  Parent(coco::PtrLink<Child, Parent> *link) : _link{link}
+  {
+    // DO NOTHING
+  }
+
+public:
+  void link(Child *child)
+  {
+    _link->set(child, this);
+  }
+
+public:
+  void unlink(Child *child)
+  {
+    assert(_link->find(child) == this);
+    _link->unset(child);
+  }
+
+private:
+  coco::PtrLink<Child, Parent> *_link;
+};
+
+class Child
+{
+public:
+  Child(const coco::PtrLink<Child, Parent> *link) : _link{link}
+  {
+    // DO NOTHING
+  }
+
+public:
+  Parent *parent(void) const { return _link->find(this); }
+
+private:
+  const coco::PtrLink<Child, Parent> *_link;
+};
+
+} // namespace
+
+TEST(ADT_PTR_LINK, usecase)
+{
+  // Note that Child has no setter over parent. To update Child's parent, one shuold use
+  // Parent's link and unlink method.
+  //
+  // This restriction prevents incorrect update of parent-child relation.
+  coco::PtrLink<::Child, ::Parent> link;
+
+  ::Parent parent{&link};
+  ::Child child{&link};
+
+  ASSERT_EQ(child.parent(), nullptr);
+  parent.link(&child);
+  ASSERT_EQ(child.parent(), &parent);
+  parent.unlink(&child);
+  ASSERT_EQ(child.parent(), nullptr);
+}