LibreOffice Module sc (master)  1
rangenam.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #pragma once
21 
22 #include "global.hxx"
23 #include "address.hxx"
24 #include <formula/grammar.hxx>
25 #include "scdllapi.h"
26 #include "calcmacros.hxx"
27 
28 #include <memory>
29 #include <map>
30 #include <vector>
31 
32 class ScDocument;
33 class ScTokenArray;
34 
35 namespace sc {
36 
37 struct RefUpdateContext;
38 struct RefUpdateInsertTabContext;
39 struct RefUpdateDeleteTabContext;
40 struct RefUpdateMoveTabContext;
41 class CompileFormulaContext;
42 
43 }
44 
46 {
47 public:
48  enum class Type //specialization to typed_flags outside of class
49  {
50  Name = 0x0000,
51  Database = 0x0001,
52  Criteria = 0x0002,
53  PrintArea = 0x0004,
54  ColHeader = 0x0008,
55  RowHeader = 0x0010,
56  AbsArea = 0x0020,
57  RefArea = 0x0040,
58  AbsPos = 0x0080
59  };
60 
61  enum class IsNameValidType
62  {
63  NAME_VALID,
66  };
67 
68 private:
69  OUString aName;
70  OUString aUpperName; // #i62977# for faster searching (aName is never modified after ctor)
71  OUString maNewName;
72  std::unique_ptr<ScTokenArray>
77  formula::FormulaGrammar::Grammar eTempGrammar; // needed for unresolved XML compiles
78  sal_uInt16 nIndex;
79  bool bModified; // is set/cleared by UpdateReference
80 
81  void CompileRangeData( const OUString& rSymbol, bool bSetError );
82  void InitCode();
83 public:
84 
86  const OUString& rName,
87  const OUString& rSymbol,
88  const ScAddress& rAdr = ScAddress(),
92  const OUString& rName,
93  const ScTokenArray& rArr,
94  const ScAddress& rAdr = ScAddress(),
95  Type nType = Type::Name );
97  const OUString& rName,
98  const ScAddress& rTarget );
99  // rTarget is ABSPOS jump label
100 
101  /* Exact copy, not recompiled, no other index (!), nothing... except if
102  * pDocument or pPos are passed, those values are assigned instead of the
103  * copies. */
104  ScRangeData( const ScRangeData& rScRangeData, ScDocument* pDocument = nullptr, const ScAddress* pPos = nullptr );
105 
107 
108  bool operator== (const ScRangeData& rData) const;
109 
110  void GetName( OUString& rName ) const { rName = maNewName.isEmpty() ? aName : maNewName; }
111  const OUString& GetName() const { return maNewName.isEmpty() ? aName : maNewName; }
112  const OUString& GetUpperName() const { return aUpperName; }
113  const ScAddress& GetPos() const { return aPos; }
114  // The index has to be unique. If index=0 a new index value is assigned.
115  void SetIndex( sal_uInt16 nInd ) { nIndex = nInd; }
116  sal_uInt16 GetIndex() const { return nIndex; }
118  void SetNewName( const OUString& rNewName ) { maNewName = rNewName; }
119  ScTokenArray* GetCode() { return pCode.get(); }
120  SC_DLLPUBLIC void SetCode( const ScTokenArray& );
121  const ScTokenArray* GetCode() const { return pCode.get(); }
123  bool HasReferences() const;
124  void AddType( Type nType );
125  Type GetType() const { return eType; }
126  bool HasType( Type nType ) const;
127  sal_uInt32 GetUnoType() const;
130  void UpdateSymbol( OUStringBuffer& rBuffer, const ScAddress& );
131 
136  void UpdateReference( sc::RefUpdateContext& rCxt, SCTAB nLocalTab );
137  bool IsModified() const { return bModified; }
138 
140 
141  void UpdateTranspose( const ScRange& rSource, const ScAddress& rDest );
142  void UpdateGrow( const ScRange& rArea, SCCOL nGrowX, SCROW nGrowY );
143 
144  SC_DLLPUBLIC bool IsReference( ScRange& rRef ) const;
145  bool IsReference( ScRange& rRef, const ScAddress& rPos ) const;
146  SC_DLLPUBLIC bool IsValidReference( ScRange& rRef ) const;
147  bool IsRangeAtBlock( const ScRange& ) const;
148 
149  void UpdateInsertTab( sc::RefUpdateInsertTabContext& rCxt, SCTAB nLocalTab );
150  void UpdateDeleteTab( sc::RefUpdateDeleteTabContext& rCxt, SCTAB nLocalTab );
151  void UpdateMoveTab( sc::RefUpdateMoveTabContext& rCxt, SCTAB nLocalTab );
152 
153  void ValidateTabRefs();
154 
155  static void MakeValidName( const ScDocument& rDoc, OUString& rName );
156 
157  SC_DLLPUBLIC static IsNameValidType IsNameValid( const OUString& rName, const ScDocument& rDoc );
158 
159  bool HasPossibleAddressConflict() const;
160 
162 
163 #if DEBUG_FORMULA_COMPILER
164  void Dump() const;
165 #endif
166 };
167 namespace o3tl
168 {
169  template<> struct typed_flags<ScRangeData::Type> : is_typed_flags<ScRangeData::Type, 0xff> {};
170 }
171 
172 
174 {
175  eType = eType|nType;
176 }
177 
178 inline bool ScRangeData::HasType( Type nType ) const
179 {
180  return ( ( eType & nType ) == nType );
181 }
182 
183 extern "C" int ScRangeData_QsortNameCompare( const void*, const void* );
184 
186 {
187 private:
188  typedef std::vector<ScRangeData*> IndexDataType;
189  typedef ::std::map<OUString, std::unique_ptr<ScRangeData>> DataType;
190  DataType m_Data;
191  IndexDataType maIndexToData;
192  // Use for optimization, true if any of the contained names resolves
193  // as a valid cell address (e.g. 'day1' with 16k columns).
194  mutable bool mHasPossibleAddressConflict : 1;
196 
197  void checkHasPossibleAddressConflict() const;
198 
199 public:
201  typedef ::std::map<SCTAB, const ScRangeName*> TabNameCopyMap;
202 
203  typedef DataType::const_iterator const_iterator;
204  typedef DataType::iterator iterator;
205 
206  ScRangeName();
208 
209  SC_DLLPUBLIC const ScRangeData* findByRange(const ScRange& rRange) const;
210  SC_DLLPUBLIC ScRangeData* findByUpperName(const OUString& rName);
211  SC_DLLPUBLIC const ScRangeData* findByUpperName(const OUString& rName) const;
212  SC_DLLPUBLIC ScRangeData* findByIndex(sal_uInt16 i) const;
213  void UpdateReference( sc::RefUpdateContext& rCxt, SCTAB nLocalTab = -1 );
214  void UpdateInsertTab( sc::RefUpdateInsertTabContext& rCxt, SCTAB nLocalTab = -1 );
215  void UpdateDeleteTab( sc::RefUpdateDeleteTabContext& rCxt, SCTAB nLocalTab = -1 );
216  void UpdateMoveTab( sc::RefUpdateMoveTabContext& rCxt, SCTAB nLocalTab = -1 );
217  void UpdateTranspose(const ScRange& rSource, const ScAddress& rDest);
218  void UpdateGrow(const ScRange& rArea, SCCOL nGrowX, SCROW nGrowY);
219 
224 
242  void CopyUsedNames( const SCTAB nLocalTab, const SCTAB nOldTab, const SCTAB nNewTab,
243  const ScDocument& rOldDoc, ScDocument& rNewDoc, const bool bGlobalNamesToLocal ) const;
244 
245  SC_DLLPUBLIC const_iterator begin() const { return m_Data.begin(); }
246  SC_DLLPUBLIC const_iterator end() const { return m_Data.end(); }
247  SC_DLLPUBLIC size_t size() const { return m_Data.size(); }
248  SC_DLLPUBLIC size_t index_size() const { return maIndexToData.size(); }
249  bool empty() const { return m_Data.empty(); }
250 
263  SC_DLLPUBLIC bool insert( ScRangeData* p, bool bReuseFreeIndex = true );
264 
265  void erase(const ScRangeData& r);
266  void erase(const OUString& rName);
267 
273  void erase(const_iterator itr);
274 
275  void clear();
276  bool operator== (const ScRangeName& r) const;
277 
279  {
283  }
284 };
285 
286 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type
sal_uInt32 GetUnoType() const
Definition: rangenam.cxx:525
void UpdateDeleteTab(sc::RefUpdateDeleteTabContext &rCxt, SCTAB nLocalTab=-1)
Definition: rangenam.cxx:746
void CompileUnresolvedXML(sc::CompileFormulaContext &rCxt)
Compile those names that couldn't be resolved during loading and inserting because they may have refe...
Definition: rangenam.cxx:778
SC_DLLPUBLIC size_t size() const
Definition: rangenam.hxx:247
SC_DLLPUBLIC void SetCode(const ScTokenArray &)
Definition: rangenam.cxx:621
bool hasPossibleAddressConflict() const
Definition: rangenam.hxx:278
SC_DLLPUBLIC OUString GetSymbol(const formula::FormulaGrammar::Grammar eGrammar=formula::FormulaGrammar::GRAM_DEFAULT) const
Definition: rangenam.cxx:243
const OUString & GetUpperName() const
Definition: rangenam.hxx:112
sal_uInt16 GetIndex() const
Definition: rangenam.hxx:116
Context for reference update during shifting, moving or copying of cell ranges.
void UpdateTranspose(const ScRange &rSource, const ScAddress &rDest)
Definition: rangenam.cxx:275
bool operator==(const ScRangeName &r) const
Definition: rangenam.cxx:891
static void MakeValidName(const ScDocument &rDoc, OUString &rName)
Definition: rangenam.cxx:423
bool HasPossibleAddressConflict() const
Definition: rangenam.cxx:500
DataType::iterator iterator
Definition: rangenam.hxx:204
void clear()
Definition: rangenam.cxx:869
void CompileUnresolvedXML(sc::CompileFormulaContext &rCxt)
Definition: rangenam.cxx:177
IndexDataType maIndexToData
Definition: rangenam.hxx:191
::std::map< SCTAB, const ScRangeName * > TabNameCopyMap
Map that stores non-managed pointers to ScRangeName instances.
Definition: rangenam.hxx:201
SC_DLLPUBLIC const_iterator end() const
Definition: rangenam.hxx:246
DataType::const_iterator const_iterator
Definition: rangenam.hxx:203
formula::FormulaGrammar::Grammar eTempGrammar
Definition: rangenam.hxx:77
bool empty() const
Definition: rangenam.hxx:249
DataType m_Data
Definition: rangenam.hxx:190
void UpdateGrow(const ScRange &rArea, SCCOL nGrowX, SCROW nGrowY)
Definition: rangenam.cxx:309
void CopyUsedNames(const SCTAB nLocalTab, const SCTAB nOldTab, const SCTAB nNewTab, const ScDocument &rOldDoc, ScDocument &rNewDoc, const bool bGlobalNamesToLocal) const
Copy names while copying a sheet if they reference the sheet to be copied.
Definition: rangenam.cxx:786
void UpdateGrow(const ScRange &rArea, SCCOL nGrowX, SCROW nGrowY)
Definition: rangenam.cxx:770
SC_DLLPUBLIC void GuessPosition()
Definition: rangenam.cxx:205
void GetName(OUString &rName) const
Definition: rangenam.hxx:110
OUString aName
Definition: rangenam.hxx:69
void UpdateInsertTab(sc::RefUpdateInsertTabContext &rCxt, SCTAB nLocalTab)
Definition: rangenam.cxx:394
void UpdateReference(sc::RefUpdateContext &rCxt, SCTAB nLocalTab=-1)
Definition: rangenam.cxx:726
bool HasReferences() const
Definition: rangenam.cxx:520
std::unique_ptr< ScTokenArray > pCode
Definition: rangenam.hxx:73
void UpdateInsertTab(sc::RefUpdateInsertTabContext &rCxt, SCTAB nLocalTab=-1)
Definition: rangenam.cxx:738
bool IsRangeAtBlock(const ScRange &) const
Definition: rangenam.cxx:361
SC_DLLPUBLIC ScRangeData * findByUpperName(const OUString &rName)
Definition: rangenam.cxx:704
SC_DLLPUBLIC const ScRangeData * findByRange(const ScRange &rRange) const
Definition: rangenam.cxx:697
std::vector< ScRangeData * > IndexDataType
Definition: rangenam.hxx:188
void UpdateTranspose(const ScRange &rSource, const ScAddress &rDest)
Definition: rangenam.cxx:762
sal_Int16 SCCOL
Definition: types.hxx:21
SC_DLLPUBLIC size_t index_size() const
Definition: rangenam.hxx:248
CAUTION! The following defines must be in the same namespace as the respective type.
static SC_DLLPUBLIC IsNameValidType IsNameValid(const OUString &rName, const ScDocument &rDoc)
Definition: rangenam.cxx:468
const OUString & GetName() const
Definition: rangenam.hxx:111
OUString aUpperName
Definition: rangenam.hxx:70
void AddType(Type nType)
Definition: rangenam.hxx:173
void UpdateMoveTab(sc::RefUpdateMoveTabContext &rCxt, SCTAB nLocalTab)
Definition: rangenam.cxx:414
ScAddress aPos
Definition: rangenam.hxx:74
SC_DLLPUBLIC ScRangeData * findByIndex(sal_uInt16 i) const
Definition: rangenam.cxx:716
void erase(const ScRangeData &r)
Definition: rangenam.cxx:846
void checkHasPossibleAddressConflict() const
Definition: rangenam.cxx:877
bool operator==(const ScRangeData &rData) const
Definition: rangenam.cxx:341
bool HasType(Type nType) const
Definition: rangenam.hxx:178
void CompileRangeData(const OUString &rSymbol, bool bSetError)
Definition: rangenam.cxx:140
FormulaError
void UpdateMoveTab(sc::RefUpdateMoveTabContext &rCxt, SCTAB nLocalTab=-1)
Definition: rangenam.cxx:754
int ScRangeData_QsortNameCompare(const void *, const void *)
Definition: rangenam.cxx:644
ScDocument & rDoc
Definition: rangenam.hxx:76
SC_DLLPUBLIC const_iterator begin() const
Definition: rangenam.hxx:245
sal_Int32 SCROW
Definition: types.hxx:17
void SetIndex(sal_uInt16 nInd)
Definition: rangenam.hxx:115
void UpdateDeleteTab(sc::RefUpdateDeleteTabContext &rCxt, SCTAB nLocalTab)
Definition: rangenam.cxx:404
const ScAddress & GetPos() const
Definition: rangenam.hxx:113
const ScTokenArray * GetCode() const
Definition: rangenam.hxx:121
sal_uInt16 nIndex
Definition: rangenam.hxx:78
bool mHasPossibleAddressConflictDirty
Definition: rangenam.hxx:195
void SetNewName(const OUString &rNewName)
Does not change the name, but sets maNewName for formula update after dialog.
Definition: rangenam.hxx:118
bool mHasPossibleAddressConflict
Definition: rangenam.hxx:194
QPRO_FUNC_TYPE nType
Definition: qproform.cxx:398
SC_DLLPUBLIC FormulaError GetErrCode() const
Definition: rangenam.cxx:515
bool IsModified() const
Definition: rangenam.hxx:137
SC_DLLPUBLIC bool IsValidReference(ScRange &rRef) const
Definition: rangenam.cxx:386
SC_DLLPUBLIC bool IsReference(ScRange &rRef) const
Definition: rangenam.cxx:370
Type eType
Definition: rangenam.hxx:75
Type GetType() const
Definition: rangenam.hxx:125
void InitCode()
Definition: rangenam.cxx:628
bool bModified
Definition: rangenam.hxx:79
void UpdateSymbol(OUStringBuffer &rBuffer, const ScAddress &)
Definition: rangenam.cxx:259
ScTokenArray * GetCode()
Definition: rangenam.hxx:119
::std::map< OUString, std::unique_ptr< ScRangeData > > DataType
Definition: rangenam.hxx:189
void ValidateTabRefs()
Definition: rangenam.cxx:535
#define SC_DLLPUBLIC
Definition: scdllapi.h:27
SC_DLLPUBLIC ScRangeData(ScDocument &rDoc, const OUString &rName, const OUString &rSymbol, const ScAddress &rAdr=ScAddress(), Type nType=Type::Name, const formula::FormulaGrammar::Grammar eGrammar=formula::FormulaGrammar::GRAM_DEFAULT)
Definition: rangenam.cxx:44
SC_DLLPUBLIC bool insert(ScRangeData *p, bool bReuseFreeIndex=true)
Insert object into set.
Definition: rangenam.cxx:802
SC_DLLPUBLIC ~ScRangeData()
Definition: rangenam.cxx:136
sal_Int16 SCTAB
Definition: types.hxx:22
void UpdateReference(sc::RefUpdateContext &rCxt, SCTAB nLocalTab)
Definition: rangenam.cxx:267
OUString maNewName
used for formulas after changing names in the dialog
Definition: rangenam.hxx:71