-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spreadsheet.cpp
77 lines (71 loc) · 1.66 KB
/
Spreadsheet.cpp
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
//
// Created by Frederik Desmet on 29/04/2022.
//
#include <utility>
#include <stdexcept>
#include <algorithm>
#include "ISpreadsheet.h"
#include "Spreadsheet.h"
ISpreadsheet::Spreadsheet::Spreadsheet(const SpreadsheetApplication& theApp, size_t width, size_t height)
: _id(_counter++), _theApp(theApp), _width(width), _height(height)
{
_cells = new SpreadsheetCell* [_width];
for (size_t i = 0; i < _width; i++)
{
_cells[i] = new SpreadsheetCell[_height];
}
}
ISpreadsheet::Spreadsheet::~Spreadsheet()
{
for (size_t i = 0; i < _width; i++)
{
delete[] _cells[i];
}
delete[] _cells;
_cells = nullptr;
}
ISpreadsheet::Spreadsheet::Spreadsheet(const Spreadsheet& src)
: Spreadsheet(src._theApp, src._width, src._height)
{
for (size_t i = 0; i < _width; i++)
{
for (size_t j = 0; j < _height; j++)
{
_cells[i][j] = src._cells[i][j];
}
}
}
void ISpreadsheet::Spreadsheet::verifyCoordinates(size_t x, size_t y) const
{
if (x >= _width || y >= _height)
throw std::out_of_range("");
}
void ISpreadsheet::Spreadsheet::setCellAt(size_t x, size_t y, const SpreadsheetCell& cell)
{
verifyCoordinates(x, y);
_cells[x][y] = cell;
}
SpreadsheetCell& ISpreadsheet::Spreadsheet::getCellAt(size_t x, size_t y)
{
verifyCoordinates(x, y);
return _cells[x][y];
}
void ISpreadsheet::Spreadsheet::swap(Spreadsheet& other) noexcept
{
using std::swap;
swap(_width, other._width);
swap(_height, other._height);
swap(_cells, other._cells);
}
ISpreadsheet::Spreadsheet& ISpreadsheet::Spreadsheet::operator=(const Spreadsheet& rhs)
{
if (this == &rhs)
return *this;
Spreadsheet temp(rhs);
swap(temp);
return *this;
}
size_t ISpreadsheet::Spreadsheet::getId() const
{
return _id;
}