aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Andreas Benkard <code@mail.matthias.benkard.de>2012-12-09 12:24:10 +0100
committerMatthias Andreas Benkard <code@mail.matthias.benkard.de>2012-12-09 12:27:58 +0100
commitac3830ae0c369fe9ab9f403f94215d073b9330aa (patch)
treede3039ed3e60299589baf31fefeac3dd05956a80
parent3e5637b0aad29d8a5e93ad4fdff4074f8dbc9409 (diff)
Implement copy construction and assignment for observers.
-rw-r--r--cells-impl.hpp34
-rw-r--r--cells.hpp12
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) {
diff --git a/cells.hpp b/cells.hpp
index 4799e69..2aa17e7 100644
--- a/cells.hpp
+++ b/cells.hpp
@@ -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;