diff options
-rw-r--r-- | cells-impl.hpp | 34 | ||||
-rw-r--r-- | cells.hpp | 12 |
2 files changed, 43 insertions, 3 deletions
diff --git a/cells-impl.hpp b/cells-impl.hpp index 86d7c98..deae2fd 100644 --- a/cells-impl.hpp +++ b/cells-impl.hpp @@ -58,6 +58,29 @@ namespace cells { static thread_local dynvar<std::forward_list<observer*>> current_dependencies; static thread_local dynvar<transaction> current_transaction; + inline observer::observer() : self(std::make_shared<observer*>(this)) { }; + + inline observer::observer(observer const& other) : self(std::make_shared<observer*>(this)) { + reset_dependents(other.dependents); + reset_dependencies(other.dependencies); + } + + inline observer& observer::operator =(observer const& other) { + reset_dependents(other.dependents); + reset_dependencies(other.dependencies); + return *this; + } + + inline void observer::reset_dependents(std::list<std::weak_ptr<observer*>> const& new_dependents) { + for (auto const& dependent : dependents) { + std::shared_ptr<observer*> sdependent = dependent.lock(); + if (sdependent) { + remove_dependent(*sdependent); + } + } + dependents = new_dependents; + } + inline void observer::clear_dependencies() { for (auto const& dep : dependencies) { std::shared_ptr<observer*> sdep = dep.lock(); @@ -79,6 +102,17 @@ namespace cells { }); } + inline void observer::reset_dependencies(std::forward_list<std::weak_ptr<observer*>> const& newdeps) { + clear_dependencies(); + dependencies = newdeps; + for (auto const& dep : newdeps) { + shared_ptr<observer*> p = dep.lock(); + if (p) { + (*p)->add_dependent(this); + } + } + } + inline void observer::reset_dependencies(std::forward_list<observer*> const& newdeps) { clear_dependencies(); for (auto const& dep : newdeps) { @@ -29,22 +29,28 @@ namespace cells { class observer { private: - std::shared_ptr<observer*> self; + std::shared_ptr<observer*> const self; std::list<std::weak_ptr<observer*>> dependents; std::forward_list<std::weak_ptr<observer*>> dependencies; void clear_dependencies(); void mark_dependents(); + void reset_dependencies(std::forward_list<std::weak_ptr<observer*>> const&); + void reset_dependents(std::list<std::weak_ptr<observer*>> const&); + protected: void mark(); public: - observer() : self(std::make_shared<observer*>(this)) { }; + observer(); + observer(observer const& other); + + observer& operator =(observer const& other); void add_dependent(observer* dependent); void remove_dependent(observer* dependent); - void reset_dependencies(std::forward_list<observer*> const& newdeps); + void reset_dependencies(std::forward_list<observer*> const&); virtual void update() = 0; |