sc: filter: rtf: add method "AddFont"
[LibreOffice.git] / include / svl / IndexedStyleSheets.hxx
blob910acd9e2566110fd3bb4ea66ecee71b71e3453d
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 <vector>
22 namespace svl
24 /** Function object to check whether a style sheet a fulfills specific criteria.
25 * Derive from this class and override the Check() method.
27 struct StyleSheetPredicate
29 virtual bool Check(const SfxStyleSheetBase& styleSheet) = 0;
30 virtual ~StyleSheetPredicate() {}
33 /** Function object for cleanup-Strategy for IndexedSfxStyleSheets::Clear().
34 * Derive from it and do what is necessary to dispose of a style sheet in Dispose().
36 struct StyleSheetDisposer
38 virtual void Dispose(rtl::Reference<SfxStyleSheetBase> styleSheet) = 0;
39 virtual ~StyleSheetDisposer() {}
42 /** Function object to apply a method on all style sheets.
43 * Derive from it and do whatever you want to with the style sheet in the DoIt() method.
45 struct StyleSheetCallback
47 virtual void DoIt(const SfxStyleSheetBase& styleSheet) = 0;
48 virtual ~StyleSheetCallback() {}
51 /** This class holds SfxStyleSheets and allows for access via an id and a name.
53 * @warning
54 * The identification of style sheets happens by their name. If the name of a sheet changes,
55 * it will not be found again! Please call Reindex() after changing a style sheet's name.
57 * @internal
58 * This class was implemented to mitigate solve #fdo 30770.
59 * The issue describes an Excel file which takes several hours to open.
60 * An analysis revealed that the time is spent searching for style sheets with linear scans in an array.
61 * This class implements access to the style sheets via their name in (usually) constant time.
63 * The return type for most methods is a vector of unsigned integers which denote the position
64 * of the style sheets in the vector, and not of pointers to style sheets.
65 * You will need a non-const StyleSheetPool to obtain the actual style sheets.
68 * Index-based access is required in several code portions. Hence we have to store the style sheets
69 * in a vector as well as in a map.
71 class SVL_DLLPUBLIC IndexedStyleSheets final
73 public:
74 IndexedStyleSheets();
76 /** Destructor.
78 * @internal
79 * Is explicit because it has to know how to dispose of SfxStyleSheetBase objects.
81 ~IndexedStyleSheets();
83 /** Adds a style sheet.
85 * If the style sheet is already contained, this call has no effect.
87 void AddStyleSheet(const rtl::Reference<SfxStyleSheetBase>& style);
89 /** Removes a style sheet. */
90 bool RemoveStyleSheet(const rtl::Reference<SfxStyleSheetBase>& style);
92 /** Check whether a specified style sheet is stored. */
93 bool HasStyleSheet(const rtl::Reference<SfxStyleSheetBase>& style) const;
95 /** Obtain the number of style sheets which are held */
96 sal_Int32 GetNumberOfStyleSheets() const;
98 /** Obtain the number of style sheets for which a certain condition holds */
99 sal_Int32 GetNumberOfStyleSheetsWithPredicate(StyleSheetPredicate& predicate) const;
101 /** Return the stylesheet by its position.
102 * You can obtain the position by, e.g., FindStyleSheetPosition()
103 * @internal
104 * Method is not const because the returned style sheet is not const
106 SfxStyleSheetBase* GetStyleSheetByPosition(sal_Int32 pos);
108 /** Find the position of a provided style.
110 * @throws std::runtime_error if the style has not been found.
112 sal_Int32 FindStyleSheetPosition(const SfxStyleSheetBase& style) const;
114 /** Obtain the positions of all styles which have a given name
116 std::vector<sal_Int32> FindPositionsByName(const OUString& name) const;
118 enum class SearchBehavior
120 ReturnAll,
121 ReturnFirst
123 /** Obtain the positions of all styles which have a certain name and fulfill a certain condition.
125 * This method is fast because it can use the name-based index
127 std::vector<sal_Int32>
128 FindPositionsByNameAndPredicate(const OUString& name, StyleSheetPredicate& predicate,
129 SearchBehavior behavior = SearchBehavior::ReturnAll) const;
131 /** Obtain the positions of all styles which fulfill a certain condition.
133 * This method is slow because it cannot use the name-based index
135 std::vector<sal_Int32> FindPositionsByPredicate(StyleSheetPredicate& predicate) const;
137 /** Execute a callback on all style sheets */
138 void ApplyToAllStyleSheets(StyleSheetCallback& callback) const;
140 /** Clear the contents of the index.
141 * The StyleSheetDisposer::Dispose() method is called on each style sheet, e.g., if you want to broadcast
142 * changes.
144 void Clear(StyleSheetDisposer& cleanup);
146 void Reindex();
148 /** Warning: counting for n starts at 0, i.e., the 0th style sheet is the first that is found. */
149 SfxStyleSheetBase* GetNthStyleSheetThatMatchesPredicate(sal_Int32 n,
150 StyleSheetPredicate& predicate,
151 sal_Int32 startAt = 0);
153 /** Get the positions of the style sheets which belong to a certain family.
155 const std::vector<sal_Int32>& GetStyleSheetPositionsByFamily(SfxStyleFamily) const;
157 private:
158 /** Register the position of a styleName in the index */
159 void Register(const SfxStyleSheetBase& style, sal_Int32 pos);
161 typedef std::vector<rtl::Reference<SfxStyleSheetBase>> VectorType;
162 /** Vector with the stylesheets to allow for index-based access.
164 VectorType mStyleSheets;
166 /** The map type that is used to store the mapping from strings to ids in mStyleSheets
168 * @internal
169 * Must be an unordered map. A regular map is too slow for some files. */
170 typedef std::unordered_multimap<OUString, unsigned> MapType;
172 /** A map which stores the positions of style sheets by their name */
173 MapType mPositionsByName;
175 std::vector<std::vector<sal_Int32>> mStyleSheetPositionsByFamily;
178 } /* namespace svl */
180 #endif // INCLUDED_SVL_INDEXEDSTYLESHEETS_HXX
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */