+++ /dev/null
-#ifndef __COCO_IR_USE_HOOK_H__
-#define __COCO_IR_USE_HOOK_H__
-
-#include "coco/IR/Object.h"
-#include "coco/IR/ObjectInfo.forward.h"
-
-#include "coco/ADT/PtrLink.h"
-
-namespace coco
-{
-
-class UseHook final
-{
-public:
- UseHook(const PtrLink<Object, ObjectInfo> *obj_link, Object::Consumer *use)
- : _obj_link{obj_link}, _use{use}
- {
- // DO NOTHING
- }
-
-private:
- ObjectInfo *info(Object *o) const;
-
-public:
- void onTake(Object *o);
- void onRelease(Object *o);
-
-private:
- const PtrLink<Object, ObjectInfo> *const _obj_link;
- // TODO Rename field
- Object::Consumer *const _use;
-};
-
-} // namespace coco
-
-#endif // __COCO_IR_USE_HOOK_H__
#ifndef __COCO_IR_USE_SLOT_H__
#define __COCO_IR_USE_SLOT_H__
-#include "coco/IR/UseHook.h"
+#include "coco/IR/Object.h"
+#include "coco/IR/ObjectInfo.forward.h"
-#include <cassert>
+#include "coco/ADT/PtrLink.h"
namespace coco
{
{
public:
UseSlot(const PtrLink<Object, ObjectInfo> *obj_link, Object::Consumer *use)
- : _hook{obj_link, use}, _value{nullptr}
+ : _link{obj_link}, _value{nullptr}, _consumer{use}
{
// DO NOTHING
}
Object *value(void) const { return _value; }
public:
- void value(Object *value)
- {
- if (_value)
- {
- _hook.onRelease(_value);
- _value = nullptr;
- }
-
- assert(_value == nullptr);
-
- if (value)
- {
- _value = value;
- _hook.onTake(_value);
- }
+ void value(Object *value);
- assert(_value == value);
- }
+private:
+ const PtrLink<Object, ObjectInfo> *_link = nullptr;
private:
- UseHook _hook;
Object *_value;
+ Object::Consumer *_consumer = nullptr;
};
} // namespace coco
+++ /dev/null
-#include "coco/IR/UseHook.h"
-#include "coco/IR/ObjectInfo.h"
-
-#include <cassert>
-
-namespace coco
-{
-
-ObjectInfo *UseHook::info(Object *o) const
-{
- auto info = _obj_link->find(o);
- assert(info != nullptr);
- return info;
-}
-
-void UseHook::onTake(Object *o) { info(o)->user()->insert(_use); }
-void UseHook::onRelease(Object *o) { info(o)->user()->erase(_use); }
-
-} // namespace coco
+++ /dev/null
-#include "coco/IR/UseHook.h"
-#include "coco/IR/ObjectInfo.h"
-#include "coco/IR/ObjectManager.h"
-
-#include "coco/IR/FeatureObject.h"
-
-#include "Use.mock.h"
-
-#include <gtest/gtest.h>
-
-namespace
-{
-class UseHookTest : 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(UseHookTest, TakeAndRelease)
-{
- auto o = obj_mgr.create(nncc::core::ADT::feature::Shape{1, 1, 1});
-
- ::mock::Use use;
-
- coco::UseHook hook{&obj_link, &use};
-
- hook.onTake(o);
- hook.onRelease(o);
-}
--- /dev/null
+#include "coco/IR/UseSlot.h"
+#include "coco/IR/ObjectInfo.h"
+
+#include <cassert>
+
+namespace coco
+{
+
+void UseSlot::value(Object *value)
+{
+ if (_value)
+ {
+ if (_link)
+ {
+ auto info = _link->find(_value);
+ assert(info != nullptr);
+ info->user()->erase(_consumer);
+ }
+ _value = nullptr;
+ }
+
+ assert(_value == nullptr);
+
+ if (value)
+ {
+ _value = value;
+ if (_link)
+ {
+ auto info = _link->find(_value);
+ assert(info != nullptr);
+ info->user()->insert(_consumer);
+ }
+ }
+
+ assert(_value == value);
+}
+
+} // namespace coco