/src/libreoffice/sc/inc/cellvalue.hxx
Line | Count | Source |
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 | | |
10 | | #pragma once |
11 | | |
12 | | #include "global.hxx" |
13 | | #include <svl/sharedstring.hxx> |
14 | | #include <variant> |
15 | | |
16 | | class ScDocument; |
17 | | class ScFormulaCell; |
18 | | class EditTextObject; |
19 | | class ScColumn; |
20 | | struct ScRefCellValue; |
21 | | |
22 | | namespace sc { |
23 | | struct ColumnBlockPosition; |
24 | | } |
25 | | namespace svl { class SharedStringPool; } |
26 | | |
27 | | /** |
28 | | * Store arbitrary cell value of any kind. It only stores cell value and |
29 | | * nothing else. It creates a copy of the original cell value, and manages |
30 | | * the life cycle of the copied value. |
31 | | */ |
32 | | struct ScCellValue |
33 | | { |
34 | | private: |
35 | | /// std::monostate is there to indicate CellType::NONE |
36 | | std::variant<std::monostate, double, svl::SharedString, EditTextObject*, ScFormulaCell*> maData; |
37 | | |
38 | | void reset_to_empty(); |
39 | | public: |
40 | | |
41 | | SC_DLLPUBLIC ScCellValue(); |
42 | | ScCellValue( const ScRefCellValue& rCell ); |
43 | | ScCellValue( double fValue ); |
44 | | ScCellValue( const svl::SharedString& rString ); |
45 | | ScCellValue( std::unique_ptr<EditTextObject> ); |
46 | | ScCellValue( const ScCellValue& r ); |
47 | | ScCellValue(ScCellValue&& r) noexcept; |
48 | | SC_DLLPUBLIC ~ScCellValue(); |
49 | | |
50 | | SC_DLLPUBLIC void clear() noexcept; |
51 | | |
52 | | SC_DLLPUBLIC void set( double fValue ); |
53 | | SC_DLLPUBLIC void set( const svl::SharedString& rStr ); |
54 | | void set( const EditTextObject& rEditText ); |
55 | | SC_DLLPUBLIC void set( std::unique_ptr<EditTextObject> ); |
56 | | SC_DLLPUBLIC void set( ScFormulaCell* pFormula ); |
57 | | |
58 | | SC_DLLPUBLIC CellType getType() const; |
59 | 90.2k | double getDouble() const { return std::get<double>(maData); } |
60 | 241k | ScFormulaCell* getFormula() const { return std::get<ScFormulaCell*>(maData); } |
61 | 2.12M | const svl::SharedString* getSharedString() const { return &std::get<svl::SharedString>(maData); } |
62 | 339 | EditTextObject* getEditText() const { return std::get<EditTextObject*>(maData); } |
63 | 0 | EditTextObject* releaseEditText() { auto p = getEditText(); maData = static_cast<EditTextObject*>(nullptr); return p; } |
64 | 0 | ScFormulaCell* releaseFormula() { auto p = getFormula(); maData = static_cast<ScFormulaCell*>(nullptr); return p; } |
65 | | |
66 | | /** |
67 | | * Take cell value from specified position in specified document. |
68 | | */ |
69 | | void assign( const ScDocument& rDoc, const ScAddress& rPos ); |
70 | | |
71 | | void assign(const ScCellValue& rOther, ScDocument& rDestDoc, ScCloneFlags nCloneFlags = ScCloneFlags::Default); |
72 | | |
73 | | /** |
74 | | * Set cell value at specified position in specified document. |
75 | | */ |
76 | | void commit( ScDocument& rDoc, const ScAddress& rPos ) const; |
77 | | |
78 | | void commit( ScColumn& rColumn, SCROW nRow ) const; |
79 | | |
80 | | /** |
81 | | * Set cell value at specified position in specified document. But unlike |
82 | | * commit(), this method sets the original value to the document without |
83 | | * copying. After this call, the value gets cleared. |
84 | | */ |
85 | | void release( ScDocument& rDoc, const ScAddress& rPos ); |
86 | | |
87 | | void release( ScColumn& rColumn, SCROW nRow, sc::StartListeningType eListenType = sc::SingleCellListening ); |
88 | | |
89 | | OUString getString( const ScDocument& rDoc ) const; |
90 | | |
91 | | SC_DLLPUBLIC bool isEmpty() const; |
92 | | |
93 | | bool equalsWithoutFormat( const ScCellValue& r ) const; |
94 | | |
95 | | ScCellValue& operator= ( const ScCellValue& r ); |
96 | | ScCellValue& operator=(ScCellValue&& r) noexcept; |
97 | | ScCellValue& operator= ( const ScRefCellValue& r ); |
98 | | |
99 | | void swap( ScCellValue& r ); |
100 | | }; |
101 | | |
102 | | /** |
103 | | * This is very similar to ScCellValue, except that it references the |
104 | | * original value instead of copying it. As such, don't hold an instance of |
105 | | * this class any longer than necessary, and absolutely not after the |
106 | | * original cell has been destroyed. |
107 | | */ |
108 | | struct SAL_DLLPUBLIC_RTTI ScRefCellValue |
109 | | { |
110 | | private: |
111 | | CellType meType; |
112 | | union { |
113 | | double mfValue; |
114 | | const svl::SharedString* mpString; |
115 | | const EditTextObject* mpEditText; |
116 | | ScFormulaCell* mpFormula; |
117 | | }; |
118 | | public: |
119 | | |
120 | | SC_DLLPUBLIC ScRefCellValue(); |
121 | | SC_DLLPUBLIC ScRefCellValue( double fValue ); |
122 | | ScRefCellValue( const svl::SharedString* pString ); |
123 | | ScRefCellValue( const EditTextObject* pEditText ); |
124 | | ScRefCellValue( ScFormulaCell* pFormula ); |
125 | | |
126 | | /** |
127 | | * Take cell value from specified position in specified document. |
128 | | */ |
129 | | SC_DLLPUBLIC ScRefCellValue( ScDocument& rDoc, const ScAddress& rPos ); |
130 | | SC_DLLPUBLIC ScRefCellValue( ScDocument& rDoc, const ScAddress& rPos, sc::ColumnBlockPosition& rBlockPos ); |
131 | | |
132 | | bool operator==(const ScRefCellValue&) const; |
133 | | |
134 | | void clear(); |
135 | | |
136 | 159M | CellType getType() const { return meType; } |
137 | 258k | double getDouble() const { assert(meType == CELLTYPE_VALUE); return mfValue; } |
138 | 522k | const svl::SharedString* getSharedString() const { assert(meType == CELLTYPE_STRING); return mpString; } |
139 | 106k | const EditTextObject* getEditText() const { assert(meType == CELLTYPE_EDIT); return mpEditText; } |
140 | 4.81M | ScFormulaCell* getFormula() const { assert(meType == CELLTYPE_FORMULA); return mpFormula; } |
141 | | |
142 | | /** |
143 | | * Take cell value from specified position in specified document. |
144 | | */ |
145 | | SC_DLLPUBLIC void assign( ScDocument& rDoc, const ScAddress& rPos ); |
146 | | void assign( ScDocument& rDoc, const ScAddress& rPos, sc::ColumnBlockPosition& rBlockPos ); |
147 | | |
148 | | /** |
149 | | * Set cell value at specified position in specified document. |
150 | | */ |
151 | | void commit( ScDocument& rDoc, const ScAddress& rPos ) const; |
152 | | |
153 | | bool hasString() const; |
154 | | |
155 | | SC_DLLPUBLIC bool hasNumeric() const; |
156 | | |
157 | | bool hasError() const; |
158 | | |
159 | | double getValue(); |
160 | | |
161 | | /** |
162 | | * Retrieve a numeric value without modifying the states of any objects in |
163 | | * the referenced document store. |
164 | | */ |
165 | | double getRawValue() const; |
166 | | |
167 | | /** |
168 | | * Retrieve string value. |
169 | | * |
170 | | * Note that this method is NOT thread-safe. |
171 | | * |
172 | | * @param rDoc |
173 | | * Needed to resolve EditCells' field contents, obtain a |
174 | | * ScFieldEditEngine from that document. |
175 | | */ |
176 | | SC_DLLPUBLIC OUString getString( const ScDocument& rDoc ) const; |
177 | | SC_DLLPUBLIC svl::SharedString getSharedString( const ScDocument& rDoc, svl::SharedStringPool& rStrPool ) const; |
178 | | |
179 | | /** |
180 | | * Retrieve a string value without modifying the states of any objects in |
181 | | * the referenced document store. |
182 | | * |
183 | | * This method is thread-safe. |
184 | | */ |
185 | | OUString getRawString( const ScDocument& rDoc ) const; |
186 | | |
187 | | SC_DLLPUBLIC bool isEmpty() const; |
188 | | |
189 | | bool hasEmptyValue(); |
190 | | |
191 | | bool equalsWithoutFormat( const ScRefCellValue& r ) const; |
192 | | }; |
193 | | |
194 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |