-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.h
117 lines (91 loc) · 2.58 KB
/
common.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
#pragma once
#include <iosfwd>
#include <memory>
#include <stdexcept>
#include <string>
#include <string_view>
#include <variant>
#include <vector>
struct Position {
int row = 0;
int col = 0;
bool operator==(Position rhs) const;
bool operator<(Position rhs) const;
bool IsValid() const;
std::string ToString() const;
static Position FromString(std::string_view str);
static constexpr int MAX_ROWS = 16384;
static constexpr int MAX_COLS = 16384;
static const Position NONE;
};
struct Size {
int rows = 0;
int cols = 0;
bool operator==(Size rhs) const;
};
class FormulaError {
public:
enum class Category {
Ref,
Value,
Div0,
};
FormulaError(Category category)
: category_(category) {}
Category GetCategory() const {
return category_;
}
bool operator==(FormulaError rhs) const {
return category_ == rhs.category_;
}
std::string_view ToString() const {
switch (category_) {
case Category::Div0:
return "#DIV0!";
case Category::Value:
return "#VALUE!";
case Category::Ref:
return "#REF!";
default:
break;
}
return "#UNKNOWN ERROR!";
}
private:
Category category_;
};
std::ostream& operator<<(std::ostream& output, FormulaError fe);
class InvalidPositionException : public std::out_of_range {
public:
using std::out_of_range::out_of_range;
};
class FormulaException : public std::runtime_error {
public:
using std::runtime_error::runtime_error;
};
class CircularDependencyException : public std::runtime_error {
public:
using std::runtime_error::runtime_error;
};
class CellInterface {
public:
using Value = std::variant<std::string, double, FormulaError>;
virtual ~CellInterface() = default;
virtual Value GetValue() const = 0;
virtual std::string GetText() const = 0;
virtual std::vector<Position> GetReferencedCells() const = 0;
};
inline constexpr char FORMULA_SIGN = '=';
inline constexpr char ESCAPE_SIGN = '\'';
class SheetInterface {
public:
virtual ~SheetInterface() = default;
virtual void SetCell(Position pos, const std::string& text) = 0;
virtual const CellInterface* GetCell(Position pos) const = 0;
virtual CellInterface* GetCell(Position pos) = 0;
virtual void ClearCell(Position pos) = 0;
virtual Size GetPrintableSize() const = 0;
virtual void PrintValues(std::ostream& output) const = 0;
virtual void PrintTexts(std::ostream& output) const = 0;
};
std::unique_ptr<SheetInterface> CreateSheet();