added observer to repository and updated tests

This commit is contained in:
Sascha Kühl
2025-12-18 16:13:36 +01:00
parent 6307850721
commit 5e2d2ddde5
11 changed files with 286 additions and 66 deletions
+32
View File
@@ -2,12 +2,28 @@
#define OBJECT_INFO_HPP
#include "matador/object/basic_object_info.hpp"
#include "matador/object/observer.hpp"
#include <functional>
namespace matador::object {
class repository_node;
template<typename Type>
class observer_ptr {
public:
explicit observer_ptr(std::unique_ptr<observer<Type>> &&observer)
: observer(std::move(observer)) {}
operator bool() const { return observer != nullptr; }
observer<Type> *get() const { return observer.get(); }
observer<Type> &operator*() const { return *observer; }
observer<Type> *operator->() const { return observer.get(); }
private:
std::unique_ptr<observer<Type>> observer;
};
template<typename Type>
class object_info final : public basic_object_info {
public:
@@ -27,9 +43,25 @@ public:
const Type &prototype() const { return prototype_; }
std::unique_ptr<Type> create() const { return creator_(); }
void on_attach() const override {
for (auto &observer : observers_) {
observer->on_attach(*node_, prototype_);
}
}
void on_detach() const override {
for (auto &observer : observers_) {
observer->on_detach(*node_, prototype_);
}
}
void register_observer(observer_ptr<Type> observer) {
observers_.push_back(std::move(observer));
}
private:
Type prototype_;
create_func creator_{[]{ return std::make_unique<Type>(); }};
std::vector<observer_ptr<Type>> observers_;
};
template<typename Type>