OpenXLSX 1.9.1
Loading...
Searching...
No Matches
XLSharedStrings.hpp
Go to the documentation of this file.
1#ifndef OPENXLSX_XLSHAREDSTRINGS_HPP
2#define OPENXLSX_XLSHAREDSTRINGS_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#include <deque>
11#include <functional> // std::reference_wrapper
12#include <limits> // std::numeric_limits
13#include <ostream> // std::basic_ostream
14#include <shared_mutex> // std::shared_mutex
15#include <string>
16#include <ankerl/unordered_dense.h> // O(1) ankerl::unordered_dense string lookup
17
18// ===== OpenXLSX Includes ===== //
19#include "OpenXLSX-Exports.hpp"
20#include "XLStringArena.hpp"
21#include "XLXmlFile.hpp"
22#include <string_view>
23#include <vector>
24
25namespace OpenXLSX
26{
28 using is_transparent = void;
29 auto operator()(std::string_view str) const noexcept -> uint64_t {
30 return ankerl::unordered_dense::hash<std::string_view>{}(str);
31 }
32 };
33
34 // Alias for flat hash map to allow easy substitution in the future
35 template<typename Key, typename Value>
36 using FlatHashMap = ankerl::unordered_dense::map<Key, Value, StringViewHash, std::equal_to<>>;
37
38 constexpr size_t XLMaxSharedStrings = (std::numeric_limits<int32_t>::max)(); // pull request #261: wrapped max in parentheses to
39 // prevent expansion of windows.h "max" macro
40
43 std::vector<std::string_view> cache{};
45 std::unique_ptr<std::shared_mutex> mutex{std::make_unique<std::shared_mutex>()};
46
47 void clear() {
48 arena.clear();
49 cache.clear();
50 index.clear();
51 // mutex remains intact
52 }
53 };
54
55 class XLSharedStrings; // forward declaration
56 typedef std::reference_wrapper<const XLSharedStrings> XLSharedStringsRef;
57
58 extern const XLSharedStrings
59 XLSharedStringsDefaulted; // to be used for default initialization of all references of type XLSharedStrings
60
66 class OPENXLSX_EXPORT XLSharedStrings : public XLXmlFile
67 {
68 //---------- Friend Declarations ----------//
69 friend class XLDocument; // for access to protected function rewriteXmlFromCache
70
71 //----------------------------------------------------------------------------------------------------------------------
72 // Public Member Functions
73 //----------------------------------------------------------------------------------------------------------------------
74
75 public:
79 XLSharedStrings() = default;
80
88
93
98 XLSharedStrings(const XLSharedStrings& other) = default;
99
104 XLSharedStrings(XLSharedStrings&& other) noexcept = default;
105
111 XLSharedStrings& operator=(const XLSharedStrings& other) = default;
112
118 XLSharedStrings& operator=(XLSharedStrings&& other) noexcept = default;
119
124 int32_t stringCount() const
125 {
126 if (!m_state) return 0;
127 if (m_state->mutex) {
128 std::shared_lock<std::shared_mutex> lock(*m_state->mutex);
129 return static_cast<int32_t>(m_state->cache.size());
130 }
131 return static_cast<int32_t>(m_state->cache.size());
132 }
133
139 int32_t getStringIndex(std::string_view str) const;
140
146 bool stringExists(std::string_view str) const;
147
153 const char* getString(int32_t index) const;
154
160 std::string_view getStringView(int32_t index) const;
161
167 int32_t appendString(std::string_view str) const;
168
175 void reserveStrings(size_t n) const;
176
181 size_t memoryUsageBytes() const noexcept;
182
189 int32_t getOrCreateStringIndex(std::string_view str) const;
190
198 void clearString(int32_t index) const;
199
203 void print(std::basic_ostream<char>& ostr) const;
204
205 protected:
210 int32_t rewriteXmlFromCache();
211
218 void rebuild(const std::vector<int32_t>& indexMap, int32_t newStringCount);
219
220 private:
221 XLSharedStringsState* m_state{};
228 mutable bool m_domDirty{false};
229 };
230} // namespace OpenXLSX
231
232#ifdef _MSC_VER // conditionally enable MSVC specific pragmas to avoid other compilers warning about unknown pragmas
233# pragma warning(pop)
234#endif // _MSC_VER
235
236#endif // OPENXLSX_XLSHAREDSTRINGS_HPP
XLXmlData * xmlData
Definition XLDocument.cpp:1422
This class encapsulates the concept of an excel file. It is different from the XLWorkbook,...
Definition XLDocument.hpp:82
This class encapsulate the Excel concept of Shared Strings. In Excel, instead of havig individual str...
Definition XLSharedStrings.hpp:67
XLSharedStrings & operator=(XLSharedStrings &&other) noexcept=default
~XLSharedStrings()
Destructor.
XLSharedStrings(const XLSharedStrings &other)=default
XLSharedStrings(XLSharedStrings &&other) noexcept=default
int32_t stringCount() const
return the amount of shared string entries currently in the cache
Definition XLSharedStrings.hpp:124
XLSharedStrings & operator=(const XLSharedStrings &other)=default
String memory pool (Arena Allocator) with block recycling.
Definition XLStringArena.hpp:23
void clear() noexcept
Recycle all blocks for reuse without freeing heap memory.
Definition XLStringArena.hpp:76
The XLXmlData class encapsulates the properties and behaviour of the .xml files in an ....
Definition XLXmlData.hpp:29
The XLXmlFile class provides an interface for derived classes to use. It functions as an ancestor to ...
Definition XLXmlFile.hpp:42
Definition IZipArchive.hpp:18
const XLSharedStrings XLSharedStringsDefaulted
Definition XLSharedStrings.cpp:16
ankerl::unordered_dense::map< Key, Value, StringViewHash, std::equal_to<> > FlatHashMap
Definition XLSharedStrings.hpp:36
std::reference_wrapper< const XLSharedStrings > XLSharedStringsRef
Definition XLSharedStrings.hpp:56
constexpr size_t XLMaxSharedStrings
Definition XLSharedStrings.hpp:38
Definition XLCellIterator.hpp:121
Definition XLSharedStrings.hpp:27
void is_transparent
Definition XLSharedStrings.hpp:28
auto operator()(std::string_view str) const noexcept -> uint64_t
Definition XLSharedStrings.hpp:29
Definition XLSharedStrings.hpp:41
XLStringArena arena
Definition XLSharedStrings.hpp:42
void clear()
Definition XLSharedStrings.hpp:47
std::unique_ptr< std::shared_mutex > mutex
Definition XLSharedStrings.hpp:45
std::vector< std::string_view > cache
Definition XLSharedStrings.hpp:43
FlatHashMap< std::string_view, int32_t > index
Definition XLSharedStrings.hpp:44