[coco] Introduce DefSlot<T> (#1048)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 16 Aug 2018 05:06:41 +0000 (14:06 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Thu, 16 Aug 2018 05:06:41 +0000 (14:06 +0900)
This commit introduces DefSlot template class which make it easy to
declare Object-defining entities such Instr.

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

diff --git a/contrib/coco/core/include/coco/IR/DefSlot.h b/contrib/coco/core/include/coco/IR/DefSlot.h
new file mode 100644 (file)
index 0000000..996b2e1
--- /dev/null
@@ -0,0 +1,47 @@
+#ifndef __COCO_IR_DEF_SLOT_H__
+#define __COCO_IR_DEF_SLOT_H__
+
+#include "coco/IR/DefHook.h"
+
+namespace coco
+{
+
+template <typename T> class DefSlot final
+{
+public:
+  DefSlot(const PtrLink<Object, ObjectInfo> *obj_link, Object::Def *use)
+      : _hook{obj_link, use}, _value{nullptr}
+  {
+  }
+
+public:
+  T *value(void) const { return _value; }
+
+public:
+  void value(T *value)
+  {
+    if (_value)
+    {
+      _hook.onRelease(_value);
+      _value = nullptr;
+    }
+
+    assert(_value == nullptr);
+
+    if (value)
+    {
+      _value = value;
+      _hook.onTake(_value);
+    }
+
+    assert(_value == value);
+  }
+
+private:
+  DefHook _hook;
+  T *_value;
+};
+
+} // namespace coco
+
+#endif // __COCO_IR_DEF_SLOT_H__
diff --git a/contrib/coco/core/src/IR/DefSlot.test.cpp b/contrib/coco/core/src/IR/DefSlot.test.cpp
new file mode 100644 (file)
index 0000000..7dd1864
--- /dev/null
@@ -0,0 +1,59 @@
+#include "coco/IR/DefSlot.h"
+#include "coco/IR/ObjectInfo.h"
+#include "coco/IR/ObjectManager.h"
+
+#include "coco/IR/FeatureObject.h"
+
+#include <gtest/gtest.h>
+
+namespace
+{
+namespace mock
+{
+struct Def final : public coco::Object::Def
+{
+};
+} // namespace mock
+} // namespace
+
+namespace
+{
+class DefSlotTest : public ::testing::Test
+{
+protected:
+  coco::PtrLink<coco::Bag, coco::BagInfo> bag_link;
+  coco::PtrLink<coco::Object, coco::ObjectInfo> obj_link;
+
+  coco::ObjectManager obj_mgr{&obj_link, &bag_link};
+};
+} // namespace
+
+TEST_F(DefSlotTest, constructor)
+{
+  auto o = obj_mgr.create(nncc::core::ADT::feature::Shape{1, 1, 1});
+
+  ::mock::Def def;
+
+  coco::DefSlot<coco::FeatureObject> slot{&obj_link, &def};
+
+  ASSERT_EQ(slot.value(), nullptr);
+}
+
+TEST_F(DefSlotTest, value)
+{
+  auto o = obj_mgr.create(nncc::core::ADT::feature::Shape{1, 1, 1});
+
+  ::mock::Def def;
+
+  coco::DefSlot<coco::FeatureObject> slot{&obj_link, &def};
+
+  slot.value(o);
+
+  ASSERT_EQ(slot.value(), o);
+
+  ASSERT_EQ(o->def(), &def);
+
+  slot.value(nullptr);
+
+  ASSERT_EQ(o->def(), nullptr);
+}