wxMaxima
Loading...
Searching...
No Matches
CellList.h
Go to the documentation of this file.
1// -*- mode: c++; c-file-style: "linux"; c-basic-offset: 2; indent-tabs-mode: nil -*-
2//
3// Copyright (C) 2020 Kuba Ober <kuba@bertec.com>
4//
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15//
16// You should have received a copy of the GNU General Public License
17// along with this program; if not, write to the Free Software
18// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19//
20// SPDX-License-Identifier: GPL-2.0+
21
30#ifndef WXMAXIMA_CELLLIST_H
31#define WXMAXIMA_CELLLIST_H
32
33#include "CellPtr.h"
34#include <memory>
35#include <utility>
36
38{
39protected:
40 std::unique_ptr<Cell> m_head;
41 Cell *m_tail = {};
42 Cell *m_lastAppended = {};
43
45 void base_Append(std::unique_ptr<Cell> &&cells);
46
50 Cell *base_DynamicAppend(std::unique_ptr<Cell> &&cells,
51 Cell *(*caster)(Cell *));
52
54 void ClearLastAppended() { m_lastAppended = {}; }
55};
56
58template <typename T = Cell> class CellListBuilder : CellListBuilderBase
59{
60 static Cell *DynamicCast(Cell *cell) { return dynamic_cast<T *>(cell); }
61
62public:
64
66 explicit operator bool() const { return static_cast<bool>(m_head); }
67
69 operator std::unique_ptr<T>() && { return TakeHead(); }
70
79 {
80 T *ptr = dynamic_cast<T *>(m_head.get());
81 if (ptr) {
82 (void) m_head.release();
83 m_tail = {};
84 m_lastAppended = {};
85 } else
86 m_head.reset();
87
88 wxASSERT(!m_head && !m_tail && !m_lastAppended); //-V614
89 return ptr;
90 }
91
93 std::unique_ptr<T> TakeHead()
94 {
95 m_tail = {};
96 m_lastAppended = {};
97 auto retval = dynamic_unique_ptr_cast<T>(std::move(m_head));
98
99 wxASSERT(!m_tail && !m_lastAppended);
100 return retval;
101 }
102
104 T *GetTail() const { return dynamic_cast<T*>(m_tail); }
105
107 T *GetLastAppended() const { return dynamic_cast<T*>(m_lastAppended); }
108
110 // cppcheck-suppress deallocret
111 T *Append(T *cells)
112 {
113 base_Append(std::unique_ptr<Cell>(cells));
114 return cells;
115 }
116
128 {
129 return static_cast<T *>(
130 base_DynamicAppend(std::move(std::unique_ptr<Cell>(cells)), DynamicCast));
131 }
132
136 T *DynamicAppend(std::unique_ptr<Cell> &&cells)
137 {
138 return static_cast<T *>(base_DynamicAppend(std::move(cells), DynamicCast));
139 }
140
142 T *Append(std::unique_ptr<T> &&cells)
143 {
144 auto *const retval = cells.get();
145 base_Append(std::move(cells));
146 return retval;
147 }
148};
149
151{
152public:
155 static void Check(const Cell *cell);
156 static void Check(const GroupCell *c);
157
164 static std::unique_ptr<Cell> SetNext(Cell *cell, std::unique_ptr<Cell> &&next);
165
170 static void DeleteList(Cell *afterMe);
171
177 static void AppendCell(Cell *cell, std::unique_ptr<Cell> &&tail);
178
179 template <typename T>
180 static void AppendCell(const std::unique_ptr<T> &cell, std::unique_ptr<Cell> &&tail)
181 { AppendCell(cell.get(), std::move(tail)); }
182
184 {
188 };
189
198 static SplicedIn SpliceInAfter(Cell *where, std::unique_ptr<Cell> &&head, Cell *last = nullptr);
199
200 struct TornOut {
205 std::unique_ptr<Cell> cellOwner;
208 std::unique_ptr<Cell> tailOwner;
209 };
210
218 static TornOut TearOut(Cell *first, Cell *last);
219};
220
221#endif
Implementation of an observing weak Cell pointer.
Definition: CellList.h:38
void ClearLastAppended()
Clears the pointer to the last appended cell. Useful when tree building.
Definition: CellList.h:54
Cell * base_DynamicAppend(std::unique_ptr< Cell > &&cells, Cell *(*caster)(Cell *))
Appends one or more cells if they are of the correct type, otherwise deletes them.
Definition: CellList.cpp:40
void base_Append(std::unique_ptr< Cell > &&cells)
Appends one or more cells.
Definition: CellList.cpp:26
Manages building a list of cells, keeping the head and tail of the list.
Definition: CellList.h:59
T * DynamicAppend(std::unique_ptr< Cell > &&cells)
Appends one or more cells if they are all of the correct type, otherwise deletes them.
Definition: CellList.h:136
T * GetLastAppended() const
Provides the most cell passed to the most recent Append call.
Definition: CellList.h:107
T * DynamicAppend(Cell *cells)
Appends one or more cells if they are all of the correct type, otherwise deletes them.
Definition: CellList.h:127
T * Append(T *cells)
Appends one or more cells.
Definition: CellList.h:111
T * Append(std::unique_ptr< T > &&cells)
Appends one or more cells.
Definition: CellList.h:142
T * GetTail() const
Provides the last cell in the list (if any).
Definition: CellList.h:104
T * ReleaseHead()
Releases the ownership of the list head of the list to the caller.
Definition: CellList.h:78
std::unique_ptr< T > TakeHead()
Passes on the ownership of the list head.
Definition: CellList.h:93
Definition: CellList.h:151
static void DeleteList(Cell *afterMe)
Deletes the list of cells anchored at the given cell.
Definition: CellList.cpp:84
static void AppendCell(Cell *cell, std::unique_ptr< Cell > &&tail)
Appends a cell to the end of the cell list that starts with a given cell.
Definition: CellList.cpp:92
static SplicedIn SpliceInAfter(Cell *where, std::unique_ptr< Cell > &&head, Cell *last=nullptr)
Splices a given list of cells after the given cell.
Definition: CellList.cpp:121
static TornOut TearOut(Cell *first, Cell *last)
Tears out a cell range and returns the list thus formed.
Definition: CellList.cpp:154
static std::unique_ptr< Cell > SetNext(Cell *cell, std::unique_ptr< Cell > &&next)
Replaces the successor of a given cell, and returns the old one (if any).
Definition: CellList.cpp:61
static void Check(const Cell *cell)
Checks the integrity of the list pointers of the given cell in relation to its neighbors.
Definition: CellList.cpp:52
A weak non-owning pointer that becomes null whenever the observed object is destroyed.
Definition: CellPtr.h:480
The base class all cell types the worksheet can consist of are derived from.
Definition: Cell.h:142
A cell grouping input (and, if there is one, also the output) cell to a foldable item.
Definition: GroupCell.h:74
Definition: CellList.h:184
Cell * lastSpliced
The last cell in the in the list of spliced-in cells - copied from the last parameter,...
Definition: CellList.h:187
Definition: CellList.h:200
CellPtr< Cell > cell
The first in the torn-out list of cells, or null if the tearing out had failed.
Definition: CellList.h:202
std::unique_ptr< Cell > tailOwner
The owner of the tail beyond the last cell, or null if the cell did have a predecessor in the list.
Definition: CellList.h:208
std::unique_ptr< Cell > cellOwner
The owner of the torn-out cell list, or null if the cell had no predecessor in the list.
Definition: CellList.h:205