This repository has been archived by the owner on Jan 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell.h
211 lines (163 loc) · 3.7 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#pragma once
#include "common.h"
#include "formula.h"
#include <optional>
#include <functional>
#include <unordered_set>
class Sheet;
enum ImplType
{
EMPTY,
TEXT,
FORMULA
};
class Cell : public CellInterface
{
public:
using CellPtr = std::unique_ptr<Cell>;
Cell(SheetInterface& sheet, std::string text)
:sheet_(sheet)
{
cell_value_ = std::make_unique<CellEmptyImpl>();
Set(text);
};
~Cell();
void Set(std::string text);
void Clear();
Value GetValue() const override;
std::string GetText() const override;
std::vector<Position> GetReferencedCells() const override;
bool hasCircularDependency(const Cell* main_cell, const Position& pos) const;
void InvalidateCache();
ImplType GetType() const;
private:
class CellImpl
{
public:
virtual CellInterface::Value ImplGetValue() const = 0;
virtual std::string ImplGetText() const = 0;
virtual std::vector<Position> GetReferencedCells() const = 0;
virtual ImplType GetType() const = 0;
virtual bool isCacheValid() const;
virtual void ResetCache();
virtual ~CellImpl() = default;
};
class CellEmptyImpl : public CellImpl
{
public:
CellEmptyImpl();
CellInterface::Value ImplGetValue() const override;
std::string ImplGetText() const override;
std::vector<Position> GetReferencedCells() const override {
return {};
}
ImplType GetType() const override
{
return ImplType::EMPTY;
}
private:
std::string empty_;
};
class CellTextImpl : public CellImpl
{
public:
CellTextImpl(std::string& text)
:text_value_(std::move(text))
{
if (text_value_[0] == ESCAPE_SIGN)
{
has_apostroph_ = true;
}
}
CellInterface::Value ImplGetValue() const override
{
if (has_apostroph_)
{
std::string sub_str = text_value_;
sub_str.erase(0, 1);
return sub_str;
}
return text_value_;
}
std::string ImplGetText() const override
{
return text_value_;
}
std::vector<Position> GetReferencedCells() const override
{
return {};
}
ImplType GetType() const override
{
return ImplType::TEXT;
}
private:
std::string text_value_;
bool has_apostroph_ = false;
};
class CellFormulaImpl : public CellImpl
{
public:
CellFormulaImpl(std::string& expr, SheetInterface& sheet)
:formula_(ParseFormula(expr)),
sheet_(sheet){}
CellInterface::Value ImplGetValue() const override
{
if (!cache_value_)
{
for (const auto& pos : formula_.get()->GetReferencedCells())
{
if (dynamic_cast<Cell*>(sheet_.GetCell(pos))->GetType() == ImplType::TEXT)
{
cache_value_ = FormulaError(FormulaError::Category::Value);
return *cache_value_;
}
}
const auto val = formula_->Evaluate(sheet_);
if (std::holds_alternative<double>(val))
{
double res = std::get<double>(val);
if (std::isinf(res))
{
cache_value_ = FormulaError(FormulaError::Category::Div0);
}
else
{
cache_value_ = res;
}
}
else if (std::holds_alternative<FormulaError>(val))
{
cache_value_ = std::get<FormulaError>(val);
}
}
return *cache_value_;
}
std::string ImplGetText() const override
{
return FORMULA_SIGN + formula_.get()->GetExpression();
}
std::vector<Position> GetReferencedCells() const override
{
return formula_.get()->GetReferencedCells();
}
bool isCacheValid() const override
{
return cache_value_.has_value();
}
void ResetCache() override
{
cache_value_.reset();
}
ImplType GetType() const override
{
return ImplType::FORMULA;
}
private:
std::unique_ptr<FormulaInterface> formula_;
SheetInterface& sheet_;
mutable std::optional<CellInterface::Value> cache_value_;
};
SheetInterface& sheet_;
std::unique_ptr<CellImpl> cell_value_;
};