#ifndef __COCO_IR_UPDATE_H__
#define __COCO_IR_UPDATE_H__
-#include "coco/IR/UpdateHook.h"
+#include "coco/IR/Bag.h"
+#include "coco/IR/BagInfo.forward.h"
-#include <cassert>
+#include "coco/ADT/PtrLink.h"
namespace coco
{
class Update final
{
public:
- Update(const PtrLink<Bag, BagInfo> *bag_link, Bag::Updater *update)
- : _hook{bag_link, update}, _bag{nullptr}
+ Update(const PtrLink<Bag, BagInfo> *bag_link, Bag::Updater *u)
{
- // DO NOTHING
+ link(bag_link);
+ updater(u);
}
public:
- Bag *bag(void) const { return _bag; }
+ void link(const PtrLink<Bag, BagInfo> *l) { _link = l; }
public:
- void bag(Bag *bag)
- {
- if (_bag)
- {
- _hook.onRelease(_bag);
- _bag = nullptr;
- }
-
- assert(_bag == nullptr);
+ Bag *bag(void) const { return _bag; }
+ void bag(Bag *bag);
- if (bag)
- {
- _bag = bag;
- _hook.onTake(_bag);
- }
+public:
+ Bag::Updater *updater(void) const { return _updater; }
+ void updater(Bag::Updater *u) { _updater = u; }
- assert(_bag == bag);
- }
+private:
+ const PtrLink<Bag, BagInfo> *_link;
private:
- UpdateHook _hook;
- Bag *_bag;
+ Bag *_bag = nullptr;
+ Bag::Updater *_updater = nullptr;
};
} // namespace coco
+++ /dev/null
-#ifndef __COCO_IR_UPDATE_HOOK_H__
-#define __COCO_IR_UPDATE_HOOK_H__
-
-#include "coco/IR/Bag.h"
-#include "coco/IR/BagInfo.forward.h"
-
-#include "coco/ADT/PtrLink.h"
-
-namespace coco
-{
-
-class UpdateHook final
-{
-public:
- UpdateHook(const PtrLink<Bag, BagInfo> *bag_link, Bag::Updater *update)
- : _bag_link{bag_link}, _update{update}
- {
- // 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::Updater *const _update;
-};
-
-} // namespace coco
-
-#endif // __COCO_IR_UPDATE_HOOK_H__
--- /dev/null
+#include "coco/IR/Update.h"
+#include "coco/IR/BagInfo.h"
+
+#include <cassert>
+
+namespace coco
+{
+
+void Update::bag(Bag *bag)
+{
+ if (_bag)
+ {
+ if (_link && _updater)
+ {
+ auto info = _link->find(_bag);
+ assert(info != nullptr);
+ info->updates()->erase(_updater);
+ }
+ _bag = nullptr;
+ }
+
+ assert(_bag == nullptr);
+
+ if (bag)
+ {
+ _bag = bag;
+ if (_link && _updater)
+ {
+ auto info = _link->find(_bag);
+ assert(info != nullptr);
+ info->updates()->insert(_updater);
+ }
+ }
+
+ assert(_bag == bag);
+}
+
+} // namespace coco
+++ /dev/null
-#include "coco/IR/UpdateHook.h"
-#include "coco/IR/BagInfo.h"
-
-#include <cassert>
-
-namespace coco
-{
-
-BagInfo *UpdateHook::info(Bag *bag) const
-{
- auto info = _bag_link->find(bag);
- assert(info != nullptr);
- return info;
-}
-
-void UpdateHook::onTake(Bag *bag) { info(bag)->updates()->insert(_update); }
-void UpdateHook::onRelease(Bag *bag) { info(bag)->updates()->erase(_update); }
-
-} // namespace coco
+++ /dev/null
-#include "coco/IR/UpdateHook.h"
-
-#include "coco/IR/BagInfo.h"
-#include "coco/IR/BagManager.h"
-
-#include "Update.mock.h"
-
-#include <gtest/gtest.h>
-
-namespace
-{
-class UpdateHookTest : public ::testing::Test
-{
-protected:
- coco::PtrLink<coco::Bag, coco::BagInfo> bag_link;
-
- coco::BagManager bag_mgr{&bag_link};
-};
-} // namespace
-
-TEST_F(UpdateHookTest, TakeAndRelease)
-{
- auto bag = bag_mgr.create(16);
-
- ::mock::Update update;
-
- coco::UpdateHook hook{&bag_link, &update};
-
- hook.onTake(bag);
- {
- auto updates = bag->updates();
-
- ASSERT_EQ(updates.size(), 1);
- ASSERT_NE(updates.find(&update), updates.end());
- }
-
- hook.onRelease(bag);
- {
- auto updates = bag->updates();
-
- ASSERT_EQ(updates.size(), 0);
- }
-}