Avoid potential negative array index access to cached text.
[LibreOffice.git] / sc / inc / cellvalue.hxx
blob6db9dee493cf08d847933b399098daea2cce4466
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 "global.hxx"
13 #include <svl/sharedstring.hxx>
14 #include <variant>
16 class ScDocument;
17 class ScFormulaCell;
18 class EditTextObject;
19 class ScColumn;
20 struct ScRefCellValue;
22 namespace sc {
23 struct ColumnBlockPosition;
26 /**
27 * Store arbitrary cell value of any kind. It only stores cell value and
28 * nothing else. It creates a copy of the original cell value, and manages
29 * the life cycle of the copied value.
31 struct SC_DLLPUBLIC ScCellValue
33 private:
34 /// std::monostate is there to indicate CellType::NONE
35 std::variant<std::monostate, double, svl::SharedString, EditTextObject*, ScFormulaCell*> maData;
37 void reset_to_empty();
38 public:
40 ScCellValue();
41 ScCellValue( const ScRefCellValue& rCell );
42 ScCellValue( double fValue );
43 ScCellValue( const svl::SharedString& rString );
44 ScCellValue( std::unique_ptr<EditTextObject> );
45 ScCellValue( const ScCellValue& r );
46 ScCellValue(ScCellValue&& r) noexcept;
47 ~ScCellValue();
49 void clear() noexcept;
51 void set( double fValue );
52 void set( const svl::SharedString& rStr );
53 void set( const EditTextObject& rEditText );
54 void set( std::unique_ptr<EditTextObject> );
55 void set( ScFormulaCell* pFormula );
57 CellType getType() const;
58 double getDouble() const { return std::get<double>(maData); }
59 ScFormulaCell* getFormula() const { return std::get<ScFormulaCell*>(maData); }
60 const svl::SharedString* getSharedString() const { return &std::get<svl::SharedString>(maData); }
61 EditTextObject* getEditText() const { return std::get<EditTextObject*>(maData); }
62 EditTextObject* releaseEditText() { auto p = getEditText(); maData = static_cast<EditTextObject*>(nullptr); return p; }
63 ScFormulaCell* releaseFormula() { auto p = getFormula(); maData = static_cast<ScFormulaCell*>(nullptr); return p; }
65 /**
66 * Take cell value from specified position in specified document.
68 void assign( const ScDocument& rDoc, const ScAddress& rPos );
70 void assign(const ScCellValue& rOther, ScDocument& rDestDoc, ScCloneFlags nCloneFlags = ScCloneFlags::Default);
72 /**
73 * Set cell value at specified position in specified document.
75 void commit( ScDocument& rDoc, const ScAddress& rPos ) const;
77 void commit( ScColumn& rColumn, SCROW nRow ) const;
79 /**
80 * Set cell value at specified position in specified document. But unlike
81 * commit(), this method sets the original value to the document without
82 * copying. After this call, the value gets cleared.
84 void release( ScDocument& rDoc, const ScAddress& rPos );
86 void release( ScColumn& rColumn, SCROW nRow, sc::StartListeningType eListenType = sc::SingleCellListening );
88 OUString getString( const ScDocument& rDoc ) const;
90 bool isEmpty() const;
92 bool equalsWithoutFormat( const ScCellValue& r ) const;
94 ScCellValue& operator= ( const ScCellValue& r );
95 ScCellValue& operator=(ScCellValue&& r) noexcept;
96 ScCellValue& operator= ( const ScRefCellValue& r );
98 void swap( ScCellValue& r );
102 * This is very similar to ScCellValue, except that it references the
103 * original value instead of copying it. As such, don't hold an instance of
104 * this class any longer than necessary, and absolutely not after the
105 * original cell has been destroyed.
107 struct SC_DLLPUBLIC ScRefCellValue
109 private:
110 CellType meType;
111 union {
112 double mfValue;
113 const svl::SharedString* mpString;
114 const EditTextObject* mpEditText;
115 ScFormulaCell* mpFormula;
117 public:
119 ScRefCellValue();
120 ScRefCellValue( double fValue );
121 ScRefCellValue( const svl::SharedString* pString );
122 ScRefCellValue( const EditTextObject* pEditText );
123 ScRefCellValue( ScFormulaCell* pFormula );
126 * Take cell value from specified position in specified document.
128 ScRefCellValue( ScDocument& rDoc, const ScAddress& rPos );
129 ScRefCellValue( ScDocument& rDoc, const ScAddress& rPos, sc::ColumnBlockPosition& rBlockPos );
131 void clear();
133 CellType getType() const { return meType; }
134 double getDouble() const { assert(meType == CELLTYPE_VALUE); return mfValue; }
135 const svl::SharedString* getSharedString() const { assert(meType == CELLTYPE_STRING); return mpString; }
136 const EditTextObject* getEditText() const { assert(meType == CELLTYPE_EDIT); return mpEditText; }
137 ScFormulaCell* getFormula() const { assert(meType == CELLTYPE_FORMULA); return mpFormula; }
140 * Take cell value from specified position in specified document.
142 void assign( ScDocument& rDoc, const ScAddress& rPos );
143 void assign( ScDocument& rDoc, const ScAddress& rPos, sc::ColumnBlockPosition& rBlockPos );
146 * Set cell value at specified position in specified document.
148 void commit( ScDocument& rDoc, const ScAddress& rPos ) const;
150 bool hasString() const;
152 bool hasNumeric() const;
154 bool hasError() const;
156 double getValue();
159 * Retrieve a numeric value without modifying the states of any objects in
160 * the referenced document store.
162 double getRawValue() const;
165 * Retrieve string value.
167 * Note that this method is NOT thread-safe.
169 * @param pDoc
170 * Needed to resolve EditCells' field contents, obtain a
171 * ScFieldEditEngine from that document. May be NULL if there is
172 * no ScDocument in the calling context but then the document
173 * specific fields can not be resolved. See
174 * ScEditUtil::GetString().
176 OUString getString( const ScDocument* pDoc ) const;
179 * Retrieve a string value without modifying the states of any objects in
180 * the referenced document store.
182 * This method is thread-safe.
184 OUString getRawString( const ScDocument& rDoc ) const;
186 bool isEmpty() const;
188 bool hasEmptyValue();
190 bool equalsWithoutFormat( const ScRefCellValue& r ) const;
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */