Avoid potential negative array index access to cached text.
[LibreOffice.git] / sc / inc / mtvelements.hxx
blob9a7b525fb82a37ee3d388a39f02019b1b7ae192b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #pragma once
12 #include "formulacell.hxx"
13 #include <svl/broadcast.hxx>
14 #include <svl/sharedstring.hxx>
15 #include <editeng/editobj.hxx>
16 #include "calcmacros.hxx"
17 #include "postit.hxx"
18 #include "SparklineCell.hxx"
19 #include "celltextattr.hxx"
21 #if DEBUG_COLUMN_STORAGE
22 #ifdef NDEBUG
23 #undef NDEBUG
24 #endif
25 #define MDDS_MULTI_TYPE_VECTOR_DEBUG 1
26 #endif
28 #include <mdds/multi_type_vector/macro.hpp>
29 #include <mdds/multi_type_vector/soa/main.hpp>
30 #include <mdds/multi_type_vector/block_funcs.hpp>
32 #include <unordered_map>
33 #include <memory>
34 #include <mutex>
36 class ScDocument;
37 class ScColumn;
38 struct ScRefCellValue;
40 namespace sc {
42 /// Custom element type IDs for multi_type_vector.
44 const mdds::mtv::element_t element_type_broadcaster = mdds::mtv::element_type_user_start;
45 const mdds::mtv::element_t element_type_celltextattr = mdds::mtv::element_type_user_start + 1;
47 const mdds::mtv::element_t element_type_string = mdds::mtv::element_type_user_start + 2;
48 const mdds::mtv::element_t element_type_edittext = mdds::mtv::element_type_user_start + 3;
49 const mdds::mtv::element_t element_type_formula = mdds::mtv::element_type_user_start + 4;
51 const mdds::mtv::element_t element_type_cellnote = mdds::mtv::element_type_user_start + 5;
52 const mdds::mtv::element_t element_type_sparkline = mdds::mtv::element_type_user_start + 6;
54 /// Mapped standard element types (for convenience).
55 const mdds::mtv::element_t element_type_numeric = mdds::mtv::element_type_double;
56 const mdds::mtv::element_t element_type_empty = mdds::mtv::element_type_empty;
57 const mdds::mtv::element_t element_type_uint16 = mdds::mtv::element_type_uint16;
59 /// Custom element blocks.
61 typedef mdds::mtv::noncopyable_managed_element_block<element_type_sparkline, sc::SparklineCell> sparkline_block;
62 typedef mdds::mtv::noncopyable_managed_element_block<element_type_cellnote, ScPostIt> cellnote_block;
63 typedef mdds::mtv::noncopyable_managed_element_block<element_type_broadcaster, SvtBroadcaster> broadcaster_block;
64 typedef mdds::mtv::default_element_block<element_type_celltextattr, CellTextAttr> celltextattr_block;
65 typedef mdds::mtv::default_element_block<element_type_string, svl::SharedString> string_block;
66 typedef mdds::mtv::noncopyable_managed_element_block<element_type_edittext, EditTextObject> edittext_block;
67 typedef mdds::mtv::noncopyable_managed_element_block<element_type_formula, ScFormulaCell> formula_block;
69 /// Mapped standard element blocks (for convenience).
70 typedef mdds::mtv::double_element_block numeric_block;
71 typedef mdds::mtv::uint16_element_block uint16_block;
73 } // end sc namespace
75 /// CAUTION! The following defines must be in the same namespace as the respective type.
76 /// For example sc types like sc::CellTextAttr, ScFormulaCell in global namespace.
77 namespace sc {
78 MDDS_MTV_DEFINE_ELEMENT_CALLBACKS(CellTextAttr, element_type_celltextattr, CellTextAttr(), celltextattr_block)
79 MDDS_MTV_DEFINE_ELEMENT_CALLBACKS_PTR(SparklineCell, sc::element_type_sparkline, nullptr, sc::sparkline_block)
82 /// These need to be in global namespace just like their respective types are.
83 MDDS_MTV_DEFINE_ELEMENT_CALLBACKS_PTR(ScPostIt, sc::element_type_cellnote, nullptr, sc::cellnote_block)
84 MDDS_MTV_DEFINE_ELEMENT_CALLBACKS_PTR(SvtBroadcaster, sc::element_type_broadcaster, nullptr, sc::broadcaster_block)
85 MDDS_MTV_DEFINE_ELEMENT_CALLBACKS_PTR(ScFormulaCell, sc::element_type_formula, nullptr, sc::formula_block)
86 MDDS_MTV_DEFINE_ELEMENT_CALLBACKS_PTR(EditTextObject, sc::element_type_edittext, nullptr, sc::edittext_block)
88 namespace svl {
89 MDDS_MTV_DEFINE_ELEMENT_CALLBACKS(SharedString, sc::element_type_string, SharedString(), sc::string_block)
92 namespace sc {
94 class CellStoreEvent
96 ScColumn* mpCol;
97 public:
98 CellStoreEvent();
99 CellStoreEvent(ScColumn* pCol);
101 void element_block_acquired(const mdds::mtv::base_element_block* block);
102 void element_block_released(const mdds::mtv::base_element_block* block);
104 /** Stop processing events. */
105 void stop();
107 void swap(CellStoreEvent& other);
109 const ScColumn* getColumn() const;
112 struct SparklineTraits : public mdds::mtv::default_traits
114 using block_funcs = mdds::mtv::element_block_funcs<sc::sparkline_block>;
117 struct CellNoteTraits : public mdds::mtv::default_traits
119 using event_func = CellStoreEvent;
120 using block_funcs = mdds::mtv::element_block_funcs<sc::cellnote_block>;
123 struct BroadcasterTraits : public mdds::mtv::default_traits
125 using block_funcs = mdds::mtv::element_block_funcs<sc::broadcaster_block>;
128 struct CellTextAttrTraits : public mdds::mtv::default_traits
130 using block_funcs = mdds::mtv::element_block_funcs<sc::celltextattr_block>;
133 struct CellStoreTraits : public mdds::mtv::default_traits
135 using event_func = CellStoreEvent;
136 using block_funcs = mdds::mtv::element_block_funcs<
137 numeric_block, sc::string_block, sc::edittext_block, sc::formula_block>;
140 /// Sparkline container
141 typedef mdds::mtv::soa::multi_type_vector<SparklineTraits> SparklineStoreType;
143 /// Cell note container
144 typedef mdds::mtv::soa::multi_type_vector<CellNoteTraits> CellNoteStoreType;
146 /// Broadcaster storage container
147 typedef mdds::mtv::soa::multi_type_vector<BroadcasterTraits> BroadcasterStoreType;
149 /// Cell text attribute container.
150 typedef mdds::mtv::soa::multi_type_vector<CellTextAttrTraits> CellTextAttrStoreType;
152 /// Cell container
153 typedef mdds::mtv::soa::multi_type_vector<CellStoreTraits> CellStoreType;
156 * Store position data for column array storage.
158 struct ColumnBlockPosition
160 CellNoteStoreType::iterator miCellNotePos;
161 SparklineStoreType::iterator miSparklinePos;
162 BroadcasterStoreType::iterator miBroadcasterPos;
163 CellTextAttrStoreType::iterator miCellTextAttrPos;
164 CellStoreType::iterator miCellPos;
166 ColumnBlockPosition(): miCellPos() {}
169 struct ColumnBlockConstPosition
171 CellNoteStoreType::const_iterator miCellNotePos;
172 SparklineStoreType::const_iterator miSparklinePos;
173 CellTextAttrStoreType::const_iterator miCellTextAttrPos;
174 CellStoreType::const_iterator miCellPos;
176 ColumnBlockConstPosition(): miCellPos() {}
179 class ColumnBlockPositionSet
181 typedef std::unordered_map<SCCOL, ColumnBlockPosition> ColumnsType;
182 typedef std::unordered_map<SCTAB, ColumnsType> TablesType;
184 ScDocument& mrDoc;
185 TablesType maTables;
186 std::mutex maMtxTables;
188 public:
189 ColumnBlockPositionSet(ScDocument& rDoc);
191 ColumnBlockPosition* getBlockPosition(SCTAB nTab, SCCOL nCol);
193 void clear();
197 * Set of column block positions only for one table.
199 class TableColumnBlockPositionSet
201 struct Impl;
202 std::unique_ptr<Impl> mpImpl;
204 public:
205 TableColumnBlockPositionSet( ScDocument& rDoc, SCTAB nTab );
206 TableColumnBlockPositionSet(TableColumnBlockPositionSet&& rOther) noexcept;
207 ~TableColumnBlockPositionSet();
209 ColumnBlockPosition* getBlockPosition( SCCOL nCol );
210 void invalidate(); // discards cached positions
213 ScRefCellValue toRefCell( const sc::CellStoreType::const_iterator& itPos, size_t nOffset );
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */