#ifndef __COCO_IR_READ_H__
#define __COCO_IR_READ_H__
-#include "coco/IR/ReadHook.h"
+#include "coco/IR/Bag.h"
+#include "coco/IR/BagInfo.forward.h"
-#include <cassert>
+#include "coco/ADT/PtrLink.h"
namespace coco
{
class Read final
{
public:
- Read(const PtrLink<Bag, BagInfo> *bag_link, Bag::Reader *read)
- : _hook{bag_link, read}, _bag{nullptr}
+ Read(const PtrLink<Bag, BagInfo> *bag_link, Bag::Reader *r)
{
- // DO NOTHING
+ // Initialize link and reader
+ link(bag_link);
+ reader(r);
}
public:
Bag *bag(void) const { return _bag; }
+ void bag(Bag *bag);
public:
- void bag(Bag *bag)
- {
- if (_bag)
- {
- _hook.onRelease(_bag);
- _bag = nullptr;
- }
-
- assert(_bag == nullptr);
+ Bag::Reader *reader(void) const { return _reader; }
+ void reader(Bag::Reader *r) { _reader = r; }
- if (bag)
- {
- _bag = bag;
- _hook.onTake(_bag);
- }
+public:
+ void link(const PtrLink<Bag, BagInfo> *l) { _link = l; }
- assert(_bag == bag);
- }
+private:
+ const PtrLink<Bag, BagInfo> *_link;
private:
- ReadHook _hook;
- Bag *_bag;
+ Bag *_bag = nullptr;
+ Bag::Reader *_reader = nullptr;
};
} // namespace coco
+++ /dev/null
-#ifndef __COCO_IR_READ_HOOK_H__
-#define __COCO_IR_READ_HOOK_H__
-
-#include "coco/IR/Bag.h"
-#include "coco/IR/BagInfo.forward.h"
-
-#include "coco/ADT/PtrLink.h"
-
-namespace coco
-{
-
-class ReadHook final
-{
-public:
- ReadHook(const PtrLink<Bag, BagInfo> *bag_link, Bag::Reader *read)
- : _bag_link{bag_link}, _read{read}
- {
- // DO NOTHING
- }
-
-private:
- BagInfo *info(Bag *bag) const;
-
-public:
- void onTake(Bag *bag);
- void onRelease(Bag *bag);
-
-private:
- const PtrLink<Bag, BagInfo> *const _bag_link;
- Bag::Reader *const _read;
-};
-
-} // namespace coco
-
-#endif // __COCO_IR_READ_HOOK_H__
--- /dev/null
+#include "coco/IR/Read.h"
+#include "coco/IR/BagInfo.h"
+
+#include <cassert>
+
+namespace coco
+{
+
+void Read::bag(Bag *bag)
+{
+ if (_bag)
+ {
+ if (_link && _reader)
+ {
+ auto info = _link->find(_bag);
+ assert(info != nullptr);
+ info->reads()->erase(_reader);
+ }
+ _bag = nullptr;
+ }
+
+ assert(_bag == nullptr);
+
+ if (bag)
+ {
+ _bag = bag;
+ if (_link && _reader)
+ {
+ auto info = _link->find(_bag);
+ assert(info != nullptr);
+ info->reads()->insert(_reader);
+ }
+ }
+
+ assert(_bag == bag);
+}
+
+} // namespace coco
+++ /dev/null
-#include "coco/IR/ReadHook.h"
-#include "coco/IR/BagInfo.h"
-
-#include <cassert>
-
-namespace coco
-{
-
-BagInfo *ReadHook::info(Bag *bag) const
-{
- auto info = _bag_link->find(bag);
- assert(info != nullptr);
- return info;
-}
-
-void ReadHook::onTake(Bag *bag) { info(bag)->reads()->insert(_read); }
-void ReadHook::onRelease(Bag *bag) { info(bag)->reads()->erase(_read); }
-
-} // namespace coco
+++ /dev/null
-#include "coco/IR/ReadHook.h"
-
-#include "coco/IR/BagInfo.h"
-#include "coco/IR/BagManager.h"
-
-#include "Read.mock.h"
-
-#include <gtest/gtest.h>
-
-namespace
-{
-class ReadHookTest : public ::testing::Test
-{
-protected:
- coco::PtrLink<coco::Bag, coco::BagInfo> bag_link;
-
- coco::BagManager bag_mgr{&bag_link};
-};
-} // namespace
-
-TEST_F(ReadHookTest, TakeAndRelease)
-{
- auto bag = bag_mgr.create(16);
-
- ::mock::Read read;
-
- coco::ReadHook hook{&bag_link, &read};
-
- hook.onTake(bag);
- {
- auto reads = bag->reads();
-
- ASSERT_EQ(reads.size(), 1);
- ASSERT_NE(reads.find(&read), reads.end());
- }
-
- hook.onRelease(bag);
- {
- auto reads = bag->reads();
-
- ASSERT_EQ(reads.size(), 0);
- }
-}