From 53de92ca887e4386d8d6acb462be4e4a7978a90b Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Sun, 9 Dec 2012 09:34:27 +0100 Subject: Initial check-in. --- cells.hpp | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 cells.hpp (limited to 'cells.hpp') diff --git a/cells.hpp b/cells.hpp new file mode 100644 index 0000000..27b3633 --- /dev/null +++ b/cells.hpp @@ -0,0 +1,87 @@ +// Copyright 2012, Matthias Andreas Benkard. + +#pragma once + +#ifndef CELLS_HPP +#define CELLS_HPP + +#include +#include +#include +#include + +namespace cells { + + class observer : public virtual std::enable_shared_from_this { + private: + std::forward_list> dependents; + std::forward_list> dependencies; + + void clear_dependencies(); + void mark_dependents(); + + protected: + void mark(); + + public: + void add_dependent(std::shared_ptr dependent); + void remove_dependent(observer* dependent); + void reset_dependencies(std::forward_list> const& newdeps); + + virtual void update() = 0; + + virtual ~observer(); + }; + + + template + class cell : public observer { + private: + T current_value; + + protected: + cell(); + virtual void update(); + virtual T recompute(T) = 0; + virtual T init() = 0; + + public: + T& get(); + T& operator *() { return get(); } + //T operator ()() { return get(); } + operator T() { return get(); } + + virtual ~cell(); + }; + + + template + class formula_cell : public cell { + //friend class std::shared_ptr>; + //friend std::shared_ptr> std::make_shared>(); + private: + std::function formula; + std::function alt_formula; + + protected: + formula_cell() { } + virtual T recompute(T); + virtual T init(); + + public: + //static std::shared_ptr> make() { return std::make_shared>(); } + static std::shared_ptr> make() { return std::shared_ptr>(new formula_cell()); } + + void reset(T value); + void reset(std::function); + void reset(std::function, std::function); + + virtual ~formula_cell(); + }; + + void with_transaction(std::function); +} + +#endif //CELLS_HPP + +#include "cells-impl.hpp" -- cgit v1.2.3