2020-08-23 06:55:16 +03:00
|
|
|
/*
|
2021-02-28 17:54:53 +03:00
|
|
|
* Copyright (c) 2020-2021, the SerenityOS developers.
|
2020-08-23 06:55:16 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-23 06:55:16 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-08-28 14:50:23 +03:00
|
|
|
#include "Cell.h"
|
|
|
|
#include "Forward.h"
|
2020-11-23 15:46:06 +03:00
|
|
|
#include "Readers/XSV.h"
|
2020-08-23 06:55:16 +03:00
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/HashTable.h>
|
|
|
|
#include <AK/String.h>
|
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
#include <AK/Traits.h>
|
|
|
|
#include <AK/Types.h>
|
|
|
|
#include <AK/WeakPtr.h>
|
|
|
|
#include <AK/Weakable.h>
|
|
|
|
#include <LibCore/Object.h>
|
|
|
|
#include <LibJS/Interpreter.h>
|
|
|
|
|
|
|
|
namespace Spreadsheet {
|
|
|
|
|
|
|
|
class Sheet : public Core::Object {
|
|
|
|
C_OBJECT(Sheet);
|
|
|
|
|
|
|
|
public:
|
2020-11-26 10:16:22 +03:00
|
|
|
constexpr static size_t default_row_count = 100;
|
|
|
|
constexpr static size_t default_column_count = 26;
|
|
|
|
|
2020-08-23 06:55:16 +03:00
|
|
|
~Sheet();
|
|
|
|
|
2021-02-23 16:36:07 +03:00
|
|
|
Optional<Position> parse_cell_name(const StringView&) const;
|
2020-11-27 13:25:14 +03:00
|
|
|
Optional<size_t> column_index(const StringView& column_name) const;
|
|
|
|
Optional<String> column_arithmetic(const StringView& column_name, int offset);
|
2020-08-23 06:55:16 +03:00
|
|
|
|
2020-11-02 19:50:15 +03:00
|
|
|
Cell* from_url(const URL&);
|
|
|
|
const Cell* from_url(const URL& url) const { return const_cast<Sheet*>(this)->from_url(url); }
|
2020-11-07 22:48:41 +03:00
|
|
|
Optional<Position> position_from_url(const URL& url) const;
|
|
|
|
|
|
|
|
/// Resolve 'offset' to an absolute position assuming 'base' is at 'offset_base'.
|
|
|
|
/// Effectively, "Walk the distance between 'offset' and 'offset_base' away from 'base'".
|
|
|
|
Position offset_relative_to(const Position& base, const Position& offset, const Position& offset_base) const;
|
2020-11-02 19:50:15 +03:00
|
|
|
|
2020-08-23 06:55:16 +03:00
|
|
|
JsonObject to_json() const;
|
2020-08-26 06:02:38 +03:00
|
|
|
static RefPtr<Sheet> from_json(const JsonObject&, Workbook&);
|
2020-08-23 06:55:16 +03:00
|
|
|
|
2020-11-23 15:46:06 +03:00
|
|
|
Vector<Vector<String>> to_xsv() const;
|
|
|
|
static RefPtr<Sheet> from_xsv(const Reader::XSV&, Workbook&);
|
|
|
|
|
2020-08-23 06:55:16 +03:00
|
|
|
const String& name() const { return m_name; }
|
|
|
|
void set_name(const StringView& name) { m_name = name; }
|
|
|
|
|
2020-08-24 18:35:20 +03:00
|
|
|
JsonObject gather_documentation() const;
|
|
|
|
|
2020-08-27 00:00:24 +03:00
|
|
|
const HashTable<Position>& selected_cells() const { return m_selected_cells; }
|
|
|
|
HashTable<Position>& selected_cells() { return m_selected_cells; }
|
2020-08-23 06:55:16 +03:00
|
|
|
const HashMap<Position, NonnullOwnPtr<Cell>>& cells() const { return m_cells; }
|
|
|
|
HashMap<Position, NonnullOwnPtr<Cell>>& cells() { return m_cells; }
|
|
|
|
|
|
|
|
Cell* at(const Position& position);
|
|
|
|
const Cell* at(const Position& position) const { return const_cast<Sheet*>(this)->at(position); }
|
|
|
|
|
|
|
|
const Cell* at(const StringView& name) const { return const_cast<Sheet*>(this)->at(name); }
|
|
|
|
Cell* at(const StringView&);
|
|
|
|
|
|
|
|
const Cell& ensure(const Position& position) const { return const_cast<Sheet*>(this)->ensure(position); }
|
|
|
|
Cell& ensure(const Position& position)
|
|
|
|
{
|
|
|
|
if (auto cell = at(position))
|
|
|
|
return *cell;
|
|
|
|
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
m_cells.set(position, make<Cell>(String::empty(), position, *this));
|
2020-08-23 06:55:16 +03:00
|
|
|
return *at(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t add_row();
|
|
|
|
String add_column();
|
|
|
|
|
|
|
|
size_t row_count() const { return m_rows; }
|
|
|
|
size_t column_count() const { return m_columns.size(); }
|
|
|
|
const Vector<String>& columns() const { return m_columns; }
|
2020-11-07 22:48:41 +03:00
|
|
|
const String& column(size_t index)
|
|
|
|
{
|
|
|
|
for (size_t i = column_count(); i < index; ++i)
|
|
|
|
add_column();
|
|
|
|
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(column_count() > index);
|
2020-11-07 22:48:41 +03:00
|
|
|
return m_columns[index];
|
|
|
|
}
|
2020-08-23 06:55:16 +03:00
|
|
|
const String& column(size_t index) const
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(column_count() > index);
|
2020-08-23 06:55:16 +03:00
|
|
|
return m_columns[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
void update();
|
|
|
|
void update(Cell&);
|
2020-12-22 12:22:59 +03:00
|
|
|
void disable_updates() { m_should_ignore_updates = true; }
|
|
|
|
void enable_updates()
|
|
|
|
{
|
|
|
|
m_should_ignore_updates = false;
|
|
|
|
if (m_update_requested) {
|
|
|
|
m_update_requested = false;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
2020-08-23 06:55:16 +03:00
|
|
|
|
2020-12-22 13:48:33 +03:00
|
|
|
struct ValueAndException {
|
|
|
|
JS::Value value;
|
|
|
|
JS::Exception* exception { nullptr };
|
|
|
|
};
|
|
|
|
ValueAndException evaluate(const StringView&, Cell* = nullptr);
|
2020-08-26 06:02:38 +03:00
|
|
|
JS::Interpreter& interpreter() const;
|
|
|
|
SheetGlobalObject& global_object() const { return *m_global_object; }
|
2020-08-23 06:55:16 +03:00
|
|
|
|
|
|
|
Cell*& current_evaluated_cell() { return m_current_cell_being_evaluated; }
|
|
|
|
bool has_been_visited(Cell* cell) const { return m_visited_cells_in_update.contains(cell); }
|
|
|
|
|
2020-10-31 16:22:13 +03:00
|
|
|
const Workbook& workbook() const { return m_workbook; }
|
|
|
|
|
2021-02-28 17:54:53 +03:00
|
|
|
enum class CopyOperation {
|
|
|
|
Copy,
|
|
|
|
Cut
|
|
|
|
};
|
|
|
|
|
|
|
|
void copy_cells(Vector<Position> from, Vector<Position> to, Optional<Position> resolve_relative_to = {}, CopyOperation copy_operation = CopyOperation::Copy);
|
2020-11-07 22:48:41 +03:00
|
|
|
|
2020-11-26 10:16:22 +03:00
|
|
|
/// Gives the bottom-right corner of the smallest bounding box containing all the written data.
|
|
|
|
Position written_data_bounds() const;
|
|
|
|
|
2020-11-27 13:25:14 +03:00
|
|
|
bool columns_are_standard() const;
|
|
|
|
|
2020-12-28 23:03:16 +03:00
|
|
|
String generate_inline_documentation_for(StringView function, size_t argument_index);
|
|
|
|
|
2020-08-23 06:55:16 +03:00
|
|
|
private:
|
2020-08-26 06:02:38 +03:00
|
|
|
explicit Sheet(Workbook&);
|
|
|
|
explicit Sheet(const StringView& name, Workbook&);
|
2020-08-23 06:55:16 +03:00
|
|
|
|
|
|
|
String m_name;
|
|
|
|
Vector<String> m_columns;
|
|
|
|
size_t m_rows { 0 };
|
|
|
|
HashMap<Position, NonnullOwnPtr<Cell>> m_cells;
|
2020-08-27 00:00:24 +03:00
|
|
|
HashTable<Position> m_selected_cells;
|
2020-08-23 06:55:16 +03:00
|
|
|
|
2020-08-26 06:02:38 +03:00
|
|
|
Workbook& m_workbook;
|
|
|
|
mutable SheetGlobalObject* m_global_object;
|
|
|
|
|
2020-08-23 06:55:16 +03:00
|
|
|
Cell* m_current_cell_being_evaluated { nullptr };
|
|
|
|
|
|
|
|
HashTable<Cell*> m_visited_cells_in_update;
|
2020-12-22 12:22:59 +03:00
|
|
|
bool m_should_ignore_updates { false };
|
|
|
|
bool m_update_requested { false };
|
2020-12-28 23:03:16 +03:00
|
|
|
mutable Optional<JsonObject> m_cached_documentation;
|
2020-08-23 06:55:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct Traits<Spreadsheet::Position> : public GenericTraits<Spreadsheet::Position> {
|
|
|
|
static constexpr bool is_trivial() { return false; }
|
|
|
|
static unsigned hash(const Spreadsheet::Position& p)
|
|
|
|
{
|
2021-02-23 16:36:07 +03:00
|
|
|
return p.hash();
|
2020-08-23 06:55:16 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|