added observer to repository and updated tests
This commit is contained in:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user