blob: 4799e69b86af3dd84ed3e7acfba7d1d16bfd4574 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
// Copyright 2012, Matthias Andreas Benkard.
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program. If not, see
// <http://www.gnu.org/licenses/>.
#pragma once
#ifndef CELLS_HPP
#define CELLS_HPP
#include <functional>
#include <memory>
#include <forward_list>
#include <list>
#include <algorithm>
namespace cells {
class observer {
private:
std::shared_ptr<observer*> self;
std::list<std::weak_ptr<observer*>> dependents;
std::forward_list<std::weak_ptr<observer*>> dependencies;
void clear_dependencies();
void mark_dependents();
protected:
void mark();
public:
observer() : self(std::make_shared<observer*>(this)) { };
void add_dependent(observer* dependent);
void remove_dependent(observer* dependent);
void reset_dependencies(std::forward_list<observer*> const& newdeps);
virtual void update() = 0;
virtual ~observer();
};
template <typename T>
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 <typename T>
class formula_cell : public cell<T> {
//friend class std::shared_ptr<formula_cell<T>>;
//friend std::shared_ptr<formula_cell<T>> std::make_shared<formula_cell<T>>();
private:
std::function<T ()> formula;
std::function<T (T old)> alt_formula;
protected:
virtual T recompute(T);
virtual T init();
public:
formula_cell() { }
void reset(T value);
void reset(std::function<T ()>);
void reset(std::function<T ()>, std::function<T (T old)>);
virtual ~formula_cell();
};
static void with_transaction(std::function<void ()>);
}
#endif //CELLS_HPP
#include "cells-impl.hpp"
|