OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
XLSharedStrings.hpp
Go to the documentation of this file.
1/*
2 * OpenXLSX-NX - Modern High-Performance C++ Excel Library
3 *
4 * Original work Copyright (c) 2018-2025, Kenneth Troldal Balslev and OpenXLSX contributors
5 * Modified work Copyright (c) 2026, Curry Tang and OpenXLSX-NX contributors
6 *
7 * Distributed under the BSD 3-Clause License.
8 * See LICENSE.md in the project root or https://opensource.org/licenses/BSD-3-Clause
9 */
10
11#ifndef OPENXLSX_XLSHAREDSTRINGS_HPP
12#define OPENXLSX_XLSHAREDSTRINGS_HPP
13
14#ifdef _MSC_VER // conditionally enable MSVC specific pragmas to avoid other compilers warning about unknown pragmas
15# pragma warning(push)
16# pragma warning(disable : 4251)
17# pragma warning(disable : 4275)
18#endif // _MSC_VER
19
20#include <deque>
21#include <functional> // std::reference_wrapper
22#include <limits> // std::numeric_limits
23#include <ostream> // std::basic_ostream
24#include <shared_mutex> // std::shared_mutex
25#include <string>
26#include <ankerl/unordered_dense.h> // O(1) ankerl::unordered_dense string lookup
27
28// ===== OpenXLSX Includes ===== //
29#include "OpenXLSX-Exports.hpp"
30#include "XLStringArena.hpp"
31#include "XLXmlData.hpp"
32#include "XLXmlFile.hpp"
33#include <string_view>
34#include <vector>
35
36namespace OpenXLSX
37{
39 using is_transparent = void;
40 auto operator()(std::string_view str) const noexcept -> uint64_t {
41 return ankerl::unordered_dense::hash<std::string_view>{}(str);
42 }
43 };
44
45 // Alias for flat hash map to allow easy substitution in the future
46 template<typename Key, typename Value>
47 using FlatHashMap = ankerl::unordered_dense::map<Key, Value, StringViewHash, std::equal_to<>>;
48
49 constexpr size_t XLMaxSharedStrings = (std::numeric_limits<int32_t>::max)(); // pull request #261: wrapped max in parentheses to
50 // prevent expansion of windows.h "max" macro
51
54 std::vector<std::string_view> cache{};
56 std::unique_ptr<std::shared_mutex> mutex{std::make_unique<std::shared_mutex>()};
57
58 void clear() {
59 arena.clear();
60 cache.clear();
61 index.clear();
62 // mutex remains intact
63 }
64 };
65
66 class XLSharedStrings; // forward declaration
67 typedef std::reference_wrapper<const XLSharedStrings> XLSharedStringsRef;
68
69 extern const XLSharedStrings
70 XLSharedStringsDefaulted; // to be used for default initialization of all references of type XLSharedStrings
71
77 class OPENXLSX_EXPORT XLSharedStrings : public XLXmlFile
78 {
79 //---------- Friend Declarations ----------//
80 friend class XLDocument; // for access to protected function rewriteXmlFromCache
81
82 //----------------------------------------------------------------------------------------------------------------------
83 // Public Member Functions
84 //----------------------------------------------------------------------------------------------------------------------
85
86 public:
90 XLSharedStrings() = default;
91
99
104
109 XLSharedStrings(const XLSharedStrings& other) = default;
110
115 XLSharedStrings(XLSharedStrings&& other) noexcept = default;
116
122 XLSharedStrings& operator=(const XLSharedStrings& other) = default;
123
129 XLSharedStrings& operator=(XLSharedStrings&& other) noexcept = default;
130
135 int32_t stringCount() const
136 {
137 if (!m_state) return 0;
138 if (m_state->mutex) {
139 std::shared_lock<std::shared_mutex> lock(*m_state->mutex);
140 return static_cast<int32_t>(m_state->cache.size());
141 }
142 return static_cast<int32_t>(m_state->cache.size());
143 }
144
150 int32_t getStringIndex(std::string_view str) const;
151
157 bool stringExists(std::string_view str) const;
158
164 const char* getString(int32_t index) const;
165
171 std::string_view getStringView(int32_t index) const;
172
178 int32_t appendString(std::string_view str) const;
179
186 void reserveStrings(size_t n) const;
187
192 size_t memoryUsageBytes() const noexcept;
193
200 int32_t getOrCreateStringIndex(std::string_view str) const;
201
209 void clearString(int32_t index) const;
210
214 void print(std::basic_ostream<char>& ostr) const;
215
216 protected:
221 int32_t rewriteXmlFromCache();
222
228 XLAllocatedMemory generateRawAllocatedSstXml() const;
229
236 void rebuild(const std::vector<int32_t>& indexMap, int32_t newStringCount);
237
238 private:
239 XLSharedStringsState* m_state{};
240
246 mutable bool m_domDirty{false};
247 };
248} // namespace OpenXLSX
249
250#ifdef _MSC_VER // conditionally enable MSVC specific pragmas to avoid other compilers warning about unknown pragmas
251# pragma warning(pop)
252#endif // _MSC_VER
253
254#endif // OPENXLSX_XLSHAREDSTRINGS_HPP
This class encapsulate the Excel concept of Shared Strings. In Excel, instead of havig individual str...
Definition XLSharedStrings.hpp:78
XLSharedStrings & operator=(XLSharedStrings &&other) noexcept=default
~XLSharedStrings()
Destructor.
XLSharedStrings(const XLSharedStrings &other)=default
friend class XLDocument
Definition XLSharedStrings.hpp:80
XLSharedStrings(XLSharedStrings &&other) noexcept=default
int32_t stringCount() const
return the amount of shared string entries currently in the cache
Definition XLSharedStrings.hpp:135
XLSharedStrings & operator=(const XLSharedStrings &other)=default
String memory pool (Arena Allocator) with block recycling.
Definition XLStringArena.hpp:33
The XLXmlData class encapsulates the properties and behaviour of the .xml files in an ....
Definition XLXmlData.hpp:79
std::string xmlData(XLXmlSavingDeclaration savingDeclaration=XLXmlSavingDeclaration{}) const
Method for getting the XML data represented by the object.
Definition XLXmlFile.cpp:49
XLXmlFile()=default
Default constructor.
Definition IXLCellProvider.hpp:24
ankerl::unordered_dense::map< Key, Value, StringViewHash, std::equal_to<> > FlatHashMap
Definition XLSharedStrings.hpp:47
const XLSharedStrings XLSharedStringsDefaulted
Definition XLSharedStrings.cpp:27
std::reference_wrapper< const XLSharedStrings > XLSharedStringsRef
Definition XLSharedStrings.hpp:67
constexpr size_t XLMaxSharedStrings
Definition XLSharedStrings.hpp:49
Definition XLCellIterator.hpp:131
Definition XLSharedStrings.hpp:38
void is_transparent
Definition XLSharedStrings.hpp:39
auto operator()(std::string_view str) const noexcept -> uint64_t
Definition XLSharedStrings.hpp:40
Auto-managed malloc memory for Zero-Copy serialization.
Definition XLXmlData.hpp:37
Definition XLSharedStrings.hpp:52
XLStringArena arena
Definition XLSharedStrings.hpp:53
void clear()
Definition XLSharedStrings.hpp:58
std::unique_ptr< std::shared_mutex > mutex
Definition XLSharedStrings.hpp:56
std::vector< std::string_view > cache
Definition XLSharedStrings.hpp:54
FlatHashMap< std::string_view, int32_t > index
Definition XLSharedStrings.hpp:55