OpenXLSX 1.9.1
Loading...
Searching...
No Matches
XLFormula.hpp
Go to the documentation of this file.
1#ifndef OPENXLSX_XLFORMULA_HPP
2#define OPENXLSX_XLFORMULA_HPP
3
4#ifdef _MSC_VER // conditionally enable MSVC specific pragmas to avoid other compilers warning about unknown pragmas
5# pragma warning(push)
6# pragma warning(disable : 4251)
7# pragma warning(disable : 4275)
8#endif // _MSC_VER
9
10// ===== External Includes ===== //
11#include <iostream>
12#include <string>
13#include <variant>
14
15// ===== OpenXLSX Includes ===== //
16#include "OpenXLSX-Exports.hpp"
17#include "XLXmlParser.hpp"
18
19namespace OpenXLSX
20{
21 constexpr bool XLResetValue = true;
22 constexpr bool XLPreserveValue = false;
23
24 //---------- Forward Declarations ----------//
25 class XLFormulaProxy;
26 class XLCell;
27
36 class OPENXLSX_EXPORT XLFormula
37 {
38 //---------- Friend Declarations ----------//
39
40 friend bool operator==(const XLFormula& lhs, const XLFormula& rhs);
41 friend bool operator!=(const XLFormula& lhs, const XLFormula& rhs);
42 friend std::ostream& operator<<(std::ostream& os, const XLFormula& value);
43
44 public:
49
55 template<
56 typename T,
57 typename = std::enable_if_t<std::is_same_v<std::decay_t<T>, std::string> or std::is_same_v<std::decay_t<T>, std::string_view> ||
58 std::is_same_v<std::decay_t<T>, const char*> or std::is_same_v<std::decay_t<T>, char*>>>
59 explicit XLFormula(T formula)
60 {
61 // ===== If the argument is a const char *, use the argument directly; otherwise, assume it has a .c_str() function.
62 if constexpr (std::is_same_v<std::decay_t<T>, const char*> or std::is_same_v<std::decay_t<T>, char*>)
63 m_formulaString = formula;
64 else if constexpr (std::is_same_v<std::decay_t<T>, std::string_view>)
65 m_formulaString = std::string(formula);
66 else
67 m_formulaString = formula.c_str();
68 }
69
74 XLFormula(const XLFormula& other);
75
80 XLFormula(XLFormula&& other) noexcept;
81
86
93
99 XLFormula& operator=(XLFormula&& other) noexcept;
100
107 template<
108 typename T,
109 typename = std::enable_if_t<std::is_same_v<std::decay_t<T>, std::string> or std::is_same_v<std::decay_t<T>, std::string_view> ||
110 std::is_same_v<std::decay_t<T>, const char*> or std::is_same_v<std::decay_t<T>, char*>>>
112 {
113 XLFormula temp(formula);
114 std::swap(*this, temp);
115 return *this;
116 }
117
123 template<
124 typename T,
125 typename = std::enable_if_t<std::is_same_v<std::decay_t<T>, std::string> or std::is_same_v<std::decay_t<T>, std::string_view> ||
126 std::is_same_v<std::decay_t<T>, const char*> or std::is_same_v<std::decay_t<T>, char*>>>
127 void set(T formula)
128 { *this = formula; }
129
134 std::string get() const;
135
140 operator std::string() const; // NOLINT
141
146 XLFormula& clear();
147
148 private:
149 std::string m_formulaString;
150 };
151
156 class OPENXLSX_EXPORT XLFormulaProxy
157 {
158 friend class XLCell;
159 friend class XLFormula;
160
161 public:
166
172 XLFormulaProxy& operator=(const XLFormulaProxy& other);
173
180 template<typename T,
181 typename = std::enable_if_t<std::is_same_v<std::decay_t<T>, XLFormula> or std::is_same_v<std::decay_t<T>, std::string> ||
182 std::is_same_v<std::decay_t<T>, std::string_view> ||
183 std::is_same_v<std::decay_t<T>, const char*> or std::is_same_v<std::decay_t<T>, char*>>>
185 {
186 if constexpr (std::is_same_v<std::decay_t<T>, XLFormula>)
187 setFormulaString(formula.get().c_str());
188 else if constexpr (std::is_same_v<std::decay_t<T>, std::string>)
189 setFormulaString(formula.c_str());
190 else if constexpr (std::is_same_v<std::decay_t<T>, std::string_view>)
191 setFormulaString(std::string(formula).c_str());
192 else
193 setFormulaString(formula);
194
195 return *this;
196 }
197
203 template<
204 typename T,
205 typename = std::enable_if_t<std::is_same_v<std::decay_t<T>, std::string> or std::is_same_v<std::decay_t<T>, std::string_view> ||
206 std::is_same_v<std::decay_t<T>, const char*> or std::is_same_v<std::decay_t<T>, char*>>>
207 void set(T formula)
208 { *this = formula; }
209
214 std::string get() const;
215
220 XLFormulaProxy& clear();
221
226 operator std::string() const; // NOLINT
227
232 operator XLFormula() const; // NOLINT
233
234 private:
240 XLFormulaProxy(XLCell* cell, XMLNode* cellNode);
241
246 XLFormulaProxy(const XLFormulaProxy& other);
247
252 XLFormulaProxy(XLFormulaProxy&& other) noexcept;
253
259 XLFormulaProxy& operator=(XLFormulaProxy&& other) noexcept;
260
266 void setFormulaString(const char* formulaString, bool resetValue = XLResetValue);
267
273 XLFormula getFormula() const;
274
275 //---------- Private Member Variables ---------- //
276 XLCell* m_cell;
277 XMLNode* m_cellNode;
278 };
279} // namespace OpenXLSX
280
281namespace OpenXLSX
282{
289 inline bool operator==(const XLFormula& lhs, const XLFormula& rhs) { return lhs.m_formulaString == rhs.m_formulaString; }
290
297 inline bool operator!=(const XLFormula& lhs, const XLFormula& rhs) { return lhs.m_formulaString != rhs.m_formulaString; }
298
305 inline std::ostream& operator<<(std::ostream& os, const XLFormula& value) { return os << value.m_formulaString; }
306
313 inline std::ostream& operator<<(std::ostream& os, const XLFormulaProxy& value) { return os << value.get(); }
314} // namespace OpenXLSX
315
316#ifdef _MSC_VER // conditionally enable MSVC specific pragmas to avoid other compilers warning about unknown pragmas
317# pragma warning(pop)
318#endif // _MSC_VER
319
320#endif // OPENXLSX_XLFORMULA_HPP
Definition XLXmlParser.hpp:84
An implementation class encapsulating the properties and behaviours of a spreadsheet cell.
Definition XLCell.hpp:41
The XLFormulaProxy serves as a placeholder for XLFormula objects. This enable getting and setting for...
Definition XLFormula.hpp:157
std::string get() const
Get the formula as a std::string.
Definition XLFormula.cpp:234
~XLFormulaProxy()
Destructor.
XLFormulaProxy & operator=(T formula)
Templated assignment operator, taking a string-type argument.
Definition XLFormula.hpp:184
void set(T formula)
Templated setter, taking a string-type argument.
Definition XLFormula.hpp:207
The XLFormula class encapsulates the concept of an Excel formula. The class is essentially a wrapper ...
Definition XLFormula.hpp:37
XLFormula(T formula)
Constructor, taking a string-type argument.
Definition XLFormula.hpp:59
XLFormula(XLFormula &&other) noexcept
Move constructor.
XLFormula & operator=(T formula)
Templated assignment operator, taking a string-type object as an argument.
Definition XLFormula.hpp:111
~XLFormula()
Destructor.
XLFormula()
Constructor.
void set(T formula)
Templated setter function, taking a string-type object as an argument.
Definition XLFormula.hpp:127
XLFormula & operator=(const XLFormula &other)
Copy assignment operator.
XLFormula & operator=(XLFormula &&other) noexcept
Move assignment operator.
XLFormula(const XLFormula &other)
Copy constructor.
Definition IZipArchive.hpp:18
bool operator==(const XLCell &lhs, const XLCell &rhs)
Definition XLCell.hpp:304
bool operator!=(const XLCell &lhs, const XLCell &rhs)
Definition XLCell.hpp:311
constexpr bool XLResetValue
Definition XLFormula.hpp:21
constexpr bool XLPreserveValue
Definition XLFormula.hpp:22
std::ostream & operator<<(std::ostream &os, const XLCell &c)
ostream output of XLCell content
Definition XLCell.hpp:319