[coco] Maintain ReadSet instead of ReaderSet (#1435)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Tue, 11 Sep 2018 02:24:40 +0000 (11:24 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Tue, 11 Sep 2018 02:24:40 +0000 (11:24 +0900)
* [coco] Maintain ReadSet instead of ReaderSet

This commit revises BagInfo to maintain a set of Read instead of Reader
as a step to support Bag substitution.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
* Do not check _reader on update

contrib/coco/core/include/coco/IR/BagInfo.h
contrib/coco/core/include/coco/IR/Read.forward.h [new file with mode: 0644]
contrib/coco/core/include/coco/IR/ReadSet.h [new file with mode: 0644]
contrib/coco/core/src/IR/Bag.cpp
contrib/coco/core/src/IR/Read.cpp

index aaf4a0b..5ac23f9 100644 (file)
@@ -3,6 +3,7 @@
 
 #include "coco/IR/Bag.h"
 #include "coco/IR/DepSet.h"
+#include "coco/IR/ReadSet.h"
 
 #include <set>
 
@@ -59,8 +60,8 @@ public:
   const DepSet *deps(void) const { return &_deps; }
 
 public:
-  Bag::ReaderSet *reads(void) { return &_reads; }
-  const Bag::ReaderSet *reads(void) const { return &_reads; }
+  ReadSet *reads(void) { return &_reads; }
+  const ReadSet *reads(void) const { return &_reads; }
 
 public:
   Bag::UpdaterSet *updates(void) { return &_updates; }
@@ -74,7 +75,7 @@ private:
   /** @brief Links to dependent Object(s) */
   DepSet _deps;
   /** @brief Direct reads (not through Object) */
-  Bag::ReaderSet _reads;
+  ReadSet _reads;
   /** @brief Direct updates (not through Object) */
   Bag::UpdaterSet _updates;
 
diff --git a/contrib/coco/core/include/coco/IR/Read.forward.h b/contrib/coco/core/include/coco/IR/Read.forward.h
new file mode 100644 (file)
index 0000000..dbdb190
--- /dev/null
@@ -0,0 +1,53 @@
+#ifndef __COCO_IR_READ_H__
+#define __COCO_IR_READ_H__
+
+#include "coco/IR/ReadHook.h"
+
+#include <cassert>
+
+namespace coco
+{
+
+/**
+ * @brief A Read represents an edge between a Bag and its Reader
+ */
+class Read final
+{
+public:
+  Read(const PtrLink<Bag, BagInfo> *bag_link, Bag::Reader *read)
+      : _hook{bag_link, read}, _bag{nullptr}
+  {
+    // DO NOTHING
+  }
+
+public:
+  Bag *bag(void) const { return _bag; }
+
+public:
+  void bag(Bag *bag)
+  {
+    if (_bag)
+    {
+      _hook.onRelease(_bag);
+      _bag = nullptr;
+    }
+
+    assert(_bag == nullptr);
+
+    if (bag)
+    {
+      _bag = bag;
+      _hook.onTake(_bag);
+    }
+
+    assert(_bag == bag);
+  }
+
+private:
+  ReadHook _hook;
+  Bag *_bag;
+};
+
+} // namespace coco
+
+#endif // __COCO_IR_READ_H__
diff --git a/contrib/coco/core/include/coco/IR/ReadSet.h b/contrib/coco/core/include/coco/IR/ReadSet.h
new file mode 100644 (file)
index 0000000..021f8e8
--- /dev/null
@@ -0,0 +1,15 @@
+#ifndef __COCO_IR_READ_SET_H__
+#define __COCO_IR_READ_SET_H__
+
+#include "coco/IR/Read.h"
+
+#include <set>
+
+namespace coco
+{
+
+using ReadSet = std::set<Read *>;
+
+} // namespace coco
+
+#endif // __COCO_IR_READ_SET_H__
index 366912c..2149455 100644 (file)
@@ -52,7 +52,9 @@ Bag::ReaderSet Bag::reads(void) const
 
   for (auto read : *_info->reads())
   {
-    res.insert(read);
+    auto reader = read->reader();
+    assert(reader != nullptr);
+    res.insert(reader);
   }
 
   return res;
index 3144fc8..9596630 100644 (file)
@@ -10,11 +10,11 @@ void Read::bag(Bag *bag)
 {
   if (_bag)
   {
-    if (_link && _reader)
+    if (_link)
     {
       auto info = _link->find(_bag);
       assert(info != nullptr);
-      info->reads()->erase(_reader);
+      info->reads()->erase(this);
     }
     _bag = nullptr;
   }
@@ -24,11 +24,11 @@ void Read::bag(Bag *bag)
   if (bag)
   {
     _bag = bag;
-    if (_link && _reader)
+    if (_link)
     {
       auto info = _link->find(_bag);
       assert(info != nullptr);
-      info->reads()->insert(_reader);
+      info->reads()->insert(this);
     }
   }