ofz: Use-of-uninitialized-value
[LibreOffice.git] / sc / inc / sharedformula.hxx
blob77567ea95dbf75cacbbf624586fa0899d92ee00f
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 "mtvelements.hxx"
15 #include <vector>
17 #define USE_FORMULA_GROUP_LISTENER 1
19 namespace sc {
21 class StartListeningContext;
23 class SharedFormulaUtil
25 public:
27 /**
28 * Group formula cells stored in the passed container. The formula cells
29 * in the container are assumed to be all <b>non-shared</b>.
31 template<typename Iter>
32 static void groupFormulaCells(const Iter& itBeg, const Iter& itEnd)
34 Iter it = itBeg;
35 ScFormulaCell* pPrev = *it;
36 ScFormulaCell* pCur = nullptr;
37 for (++it; it != itEnd; ++it, pPrev = pCur)
39 pCur = *it;
40 ScFormulaCell::CompareState eState = pCur->CompareByTokenArray(*pPrev);
41 if (eState == ScFormulaCell::NotEqual)
42 continue;
44 ScFormulaCellGroupRef xGroup = pPrev->GetCellGroup();
45 if (xGroup)
47 // Extend the group.
48 ++xGroup->mnLength;
49 pCur->SetCellGroup(xGroup);
50 continue;
53 // Create a new group.
54 xGroup = pPrev->CreateCellGroup(2, eState == ScFormulaCell::EqualInvariant);
55 pCur->SetCellGroup(xGroup);
59 /** Get shared formula top cell from position, if any, else nullptr. */
60 static const ScFormulaCell* getSharedTopFormulaCell(const CellStoreType::position_type& aPos);
62 /**
63 * Split existing shared formula range at specified position. The cell at
64 * specified position becomes the top cell of the lower shared formula
65 * range after this call. This method does nothing if the cell at
66 * specified position is not a formula cell.
68 * @param aPos position of cell to examine.
69 * @param pCxt context to be used, if any, may be nullptr.
71 * @return TRUE if there indeed was a split, else FALSE (e.g. split
72 * position was top or bottom cell or no formula group).
74 static bool splitFormulaCellGroup(const CellStoreType::position_type& aPos, sc::EndListeningContext* pCxt);
76 /**
77 * Split existing shared formula ranges at specified row positions.
79 * @param rCells cell storage container
80 * @param rBounds row positions at which to split existing shared formula
81 * ranges. Note that this method will directly modify this
82 * parameter to sort and remove duplicates.
84 * @return TRUE if there indeed was a split, else FALSE (e.g. split
85 * positions were only top or bottom cells or no formula group).
87 static bool splitFormulaCellGroups(const ScDocument& rDoc, CellStoreType& rCells, std::vector<SCROW>& rBounds);
89 /**
90 * See if two specified adjacent formula cells can be merged, and if they
91 * can, merge them into the same group.
93 * @param rPos position object of the first cell
94 * @param rCell1 first cell
95 * @param rCell2 second cell located immediately below the first cell.
97 * @return true if the cells are merged, false otherwise. If the two
98 * cells already belong to the same group, it returns false.
100 static bool joinFormulaCells(
101 const CellStoreType::position_type& rPos, ScFormulaCell& rCell1, ScFormulaCell& rCell2 );
103 * Merge with an existing formula group (if any) located immediately above
104 * if the cell at specified position is a formula cell, and its formula
105 * tokens are identical to that of the above formula group.
107 * @param aPos position of cell to examine.
109 * @return true if the cells are merged, false otherwise. If the two
110 * cells already belong to the same group, it returns false.
112 static bool joinFormulaCellAbove( const CellStoreType::position_type& aPos );
115 * Turn a shared formula cell into a non-shared one, and split it off from
116 * the adjacent formula cell groups.
118 * @param aPos position of cell to examine
119 * @param rCell formula cell instance
121 static void unshareFormulaCell(const CellStoreType::position_type& aPos, ScFormulaCell& rCell);
124 * Make specified formula cells non-shared ones, and split them off from
125 * their respective adjacent formula cell groups.
127 * @param rCells cell storage container
128 * @param rRows row positions at which to unshare formula cells.
130 static void unshareFormulaCells(const ScDocument& rDoc, CellStoreType& rCells, std::vector<SCROW>& rRows);
133 * Have all formula cells belonging to a group start listening to their
134 * references.
136 * @param rCxt context object.
137 * @param ppSharedTop memory position of the pointer of the topmost
138 * formula cell instance in the cell storage. The
139 * caller is responsible for ensuring that it is indeed
140 * the topmost cell of a shared formula group.
142 static void startListeningAsGroup( StartListeningContext& rCxt, ScFormulaCell** ppSharedTop );
147 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */