tdf#162912 i18npool: Updated CJK BreakIterator to use custom rules
[LibreOffice.git] / include / svl / IndexedStyleSheets.hxx
blob425c508ea6f3e091b383d757a41bf464fc55bc00
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 #ifndef INCLUDED_SVL_INDEXEDSTYLESHEETS_HXX
11 #define INCLUDED_SVL_INDEXEDSTYLESHEETS_HXX
13 #include <svl/style.hxx>
14 #include <rtl/ustring.hxx>
15 #include <rtl/ref.hxx>
17 #include <svl/svldllapi.h>
19 #include <unordered_map>
20 #include <array>
21 #include <vector>
23 namespace svl
25 /** Function object to check whether a style sheet a fulfills specific criteria.
26 * Derive from this class and override the Check() method.
28 struct StyleSheetPredicate
30 virtual bool Check(const SfxStyleSheetBase& styleSheet) = 0;
31 virtual ~StyleSheetPredicate() {}
34 /** Function object for cleanup-Strategy for IndexedSfxStyleSheets::Clear().
35 * Derive from it and do what is necessary to dispose of a style sheet in Dispose().
37 struct StyleSheetDisposer
39 virtual void Dispose(rtl::Reference<SfxStyleSheetBase> styleSheet) = 0;
40 virtual ~StyleSheetDisposer() {}
43 /** Function object to apply a method on all style sheets.
44 * Derive from it and do whatever you want to with the style sheet in the DoIt() method.
46 struct StyleSheetCallback
48 virtual void DoIt(const SfxStyleSheetBase& styleSheet) = 0;
49 virtual ~StyleSheetCallback() {}
52 /** This class holds SfxStyleSheets and allows for access via an id and a name.
54 * @warning
55 * The identification of style sheets happens by their name. If the name of a sheet changes,
56 * it will not be found again! Please call Reindex() after changing a style sheet's name.
58 * @internal
59 * This class was implemented to mitigate solve #fdo 30770.
60 * The issue describes an Excel file which takes several hours to open.
61 * An analysis revealed that the time is spent searching for style sheets with linear scans in an array.
62 * This class implements access to the style sheets via their name in (usually) constant time.
64 * The return type for most methods is a vector of unsigned integers which denote the position
65 * of the style sheets in the vector, and not of pointers to style sheets.
66 * You will need a non-const StyleSheetPool to obtain the actual style sheets.
69 * Index-based access is required in several code portions. Hence we have to store the style sheets
70 * in a vector as well as in a map.
72 class SVL_DLLPUBLIC IndexedStyleSheets final
74 public:
75 IndexedStyleSheets();
77 /** Destructor.
79 * @internal
80 * Is explicit because it has to know how to dispose of SfxStyleSheetBase objects.
82 ~IndexedStyleSheets();
84 /** Adds a style sheet.
86 * If the style sheet is already contained, this call has no effect.
88 void AddStyleSheet(const rtl::Reference<SfxStyleSheetBase>& style);
90 /** Removes a style sheet. */
91 bool RemoveStyleSheet(const rtl::Reference<SfxStyleSheetBase>& style);
93 /** Check whether a specified style sheet is stored. */
94 bool HasStyleSheet(const rtl::Reference<SfxStyleSheetBase>& style) const;
96 /** Obtain the number of style sheets which are held */
97 sal_Int32 GetNumberOfStyleSheets() const { return mStyleSheets.size(); }
99 /** Obtain the number of style sheets for which a certain condition holds */
100 sal_Int32 GetNumberOfStyleSheetsWithPredicate(StyleSheetPredicate& predicate) const;
102 /** Return the stylesheet by its position.
103 * You can obtain the position by, e.g., FindStyleSheetPosition()
104 * @internal
105 * Method is not const because the returned style sheet is not const
107 SfxStyleSheetBase* GetStyleSheetByPosition(sal_Int32 pos);
109 /** Find the position of a provided style.
111 * @throws std::runtime_error if the style has not been found.
113 sal_Int32 FindStyleSheetPosition(const SfxStyleSheetBase& style) const;
115 /** Obtain the positions of all styles which have a given name
117 std::vector<sal_Int32> FindPositionsByName(const OUString& name) const;
119 enum class SearchBehavior
121 ReturnAll,
122 ReturnFirst
124 /** Obtain the positions of all styles which have a certain name and fulfill a certain condition.
126 * This method is fast because it can use the name-based index
128 std::vector<sal_Int32>
129 FindPositionsByNameAndPredicate(const OUString& name, StyleSheetPredicate& predicate,
130 SearchBehavior behavior = SearchBehavior::ReturnAll) const;
132 /** Obtain the positions of all styles which fulfill a certain condition.
134 * This method is slow because it cannot use the name-based index
136 std::vector<sal_Int32> FindPositionsByPredicate(StyleSheetPredicate& predicate) const;
138 /** Execute a callback on all style sheets */
139 void ApplyToAllStyleSheets(StyleSheetCallback& callback) const;
141 /** Clear the contents of the index.
142 * The StyleSheetDisposer::Dispose() method is called on each style sheet, e.g., if you want to broadcast
143 * changes.
145 void Clear(StyleSheetDisposer& cleanup);
147 void Reindex();
148 void ReindexOnNameChange(const SfxStyleSheetBase& style, const OUString& rOldName,
149 const OUString& rNewName);
151 /** Warning: counting for n starts at 0, i.e., the 0th style sheet is the first that is found.
152 Returns a pointer if a stylesheet is found, and the position of the found stylesheet
154 std::pair<SfxStyleSheetBase*, sal_Int32>
155 GetNthStyleSheetThatMatchesPredicate(sal_Int32 n, StyleSheetPredicate& predicate,
156 sal_Int32 startAt = 0);
158 /** Get the positions of the style sheets which belong to a certain family.
160 const std::vector<SfxStyleSheetBase*>& GetStyleSheetsByFamily(SfxStyleFamily) const;
162 private:
163 /** Register the position of a styleName in the index */
164 void Register(SfxStyleSheetBase& style, sal_Int32 pos);
166 typedef std::vector<rtl::Reference<SfxStyleSheetBase>> VectorType;
167 /** Vector with the stylesheets to allow for index-based access.
169 VectorType mStyleSheets;
171 /** The map type that is used to store the mapping from strings to ids in mStyleSheets
173 * @internal
174 * Must be an unordered map. A regular map is too slow for some files. */
175 typedef std::unordered_multimap<OUString, unsigned> MapType;
177 /** A map which stores the positions of style sheets by their name */
178 MapType mPositionsByName;
180 static constexpr size_t NUMBER_OF_FAMILIES = 6;
182 std::array<std::vector<SfxStyleSheetBase*>, NUMBER_OF_FAMILIES> mStyleSheetsByFamily;
185 } /* namespace svl */
187 #endif // INCLUDED_SVL_INDEXEDSTYLESHEETS_HXX
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */