-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell.h
125 lines (97 loc) · 3.3 KB
/
cell.h
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#pragma once
#include "common.h"
#include "formula.h"
#include <set>
#include <iostream>
#include <optional>
class Cell : public CellInterface {
public:
Cell(SheetInterface& sheet);
~Cell();
void Set(std::string text);
void Clear();
Value GetValue() const override;
std::string GetText() const override;
std::vector<Position> GetReferencedCells() const override;
std::vector<Position> GetDependentCells() const override;
void AddDependentCell(Position pos) override;
private:
class Impl {
public:
virtual ~Impl() = default;
virtual void Set(std::string text) = 0;
virtual Value GetValue(const SheetInterface& sheet) const = 0;
virtual std::string GetText() const = 0;
virtual std::vector<Position> GetReferencedCells() const = 0;
};
class EmptyImpl : public Impl {
public:
void Set(std::string text) override {
(void)text;
}
Value GetValue([[maybe_unused]] const SheetInterface& sheet) const override {
return 0.0;
}
std::string GetText() const override {
return "";
}
std::vector<Position> GetReferencedCells() const {
return {};
}
};
class TextImpl : public Impl {
public:
void Set(std::string text) override {
text_ = text;
}
Value GetValue([[maybe_unused]] const SheetInterface& sheet) const override {
if (text_.size() && text_.at(0) == '\''){
return text_.substr(1, text_.size() - 1);
}
return text_;
}
std::string GetText() const override {
return text_;
}
std::vector<Position> GetReferencedCells() const {
return {};
}
private:
std::string text_;
};
class FormulaImpl : public Impl {
public:
void Set(std::string text) override {
try {
formula_ = ParseFormula(text);
} catch (...) {
throw FormulaException("Parsing Error");
}
}
Value GetValue(const SheetInterface& sheet) const override {
auto res = formula_.get()->Evaluate(sheet);
if (std::holds_alternative<double>(res)){
return std::get<double>(res);
}
return std::get<FormulaError>(res);
}
std::string GetText() const override {
return "=" + formula_.get()->GetExpression();
}
std::vector<Position> GetReferencedCells() const {
return formula_.get()->GetReferencedCells();
}
private:
std::unique_ptr<FormulaInterface> formula_;
};
std::unique_ptr<Impl> impl_;
SheetInterface& sheet_;
mutable std::optional<Value> cached_value_;
// Ячейки, которые зависят от текущей ячейка
std::vector<Position> dependent_cells_;
// Ячейки, от которых зависит текущая ячейка
std::vector<Position> referenced_cells_;
// Очищает кэши в текущей ячейке
// и во всех зависимых
void ClearCache_() override;
};