1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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>
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
{
28 virtual bool Check(const SfxStyleSheetBase
& styleSheet
) = 0;
29 virtual ~StyleSheetPredicate() {}
32 /** Function object for cleanup-Strategy for IndexedSfxStyleSheets::Clear().
33 * Derive from it and do what is necessary to dispose of a style sheet in Dispose().
35 struct StyleSheetDisposer
{
36 virtual void Dispose(rtl::Reference
<SfxStyleSheetBase
> styleSheet
) = 0;
37 virtual ~StyleSheetDisposer() {}
40 /** Function object to apply a method on all style sheets.
41 * Derive from it and do whatever you want to with the style sheet in the DoIt() method.
43 struct StyleSheetCallback
{
44 virtual void DoIt(const SfxStyleSheetBase
& styleSheet
) = 0;
45 virtual ~StyleSheetCallback() {}
48 /** This class holds SfxStyleSheets and allows for access via an id and a name.
51 * The identification of style sheets happens by their name. If the name of a sheet changes,
52 * it will not be found again! Please call Reindex() after changing a style sheet's name.
55 * This class was implemented to mitigate solve #fdo 30770.
56 * The issue describes an Excel file which takes several hours to open.
57 * An analysis revealed that the time is spent searching for style sheets with linear scans in an array.
58 * This class implements access to the style sheets via their name in (usually) constant time.
60 * The return type for most methods is a vector of unsigned integers which denote the position
61 * of the style sheets in the vector, and not of pointers to style sheets.
62 * You will need a non-const StyleSheetPool to obtain the actual style sheets.
65 * Index-based access is required in several code portions. Hence we have to store the style sheets
66 * in a vector as well as in a map.
68 class SVL_DLLPUBLIC IndexedStyleSheets final
76 * Is explicit because it has to know how to dispose of SfxStyleSheetBase objects.
78 ~IndexedStyleSheets();
80 /** Adds a style sheet.
82 * If the style sheet is already contained, this call has no effect.
85 AddStyleSheet(const rtl::Reference
< SfxStyleSheetBase
>& style
);
87 /** Removes a style sheet. */
89 RemoveStyleSheet(const rtl::Reference
< SfxStyleSheetBase
>& style
);
91 /** Check whether a specified style sheet is stored. */
93 HasStyleSheet(const rtl::Reference
< SfxStyleSheetBase
>& style
) const;
95 /** Obtain the number of style sheets which are held */
97 GetNumberOfStyleSheets() const;
99 /** Obtain the number of style sheets for which a certain condition holds */
101 GetNumberOfStyleSheetsWithPredicate(StyleSheetPredicate
& predicate
) const;
103 /** Return the stylesheet by its position.
104 * You can obtain the position by, e.g., FindStyleSheetPosition()
106 * Method is not const because the returned style sheet is not const
109 GetStyleSheetByPosition(unsigned pos
);
111 /** Find the position of a provided style.
113 * @throws std::runtime_error if the style has not been found.
116 FindStyleSheetPosition(const SfxStyleSheetBase
& style
) const;
118 /** Obtain the positions of all styles which have a given name
120 std::vector
<unsigned>
121 FindPositionsByName(const OUString
& name
) const;
123 enum class SearchBehavior
{ ReturnAll
, 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
<unsigned>
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
<unsigned>
137 FindPositionsByPredicate(StyleSheetPredicate
& predicate
) const;
139 /** Execute a callback on all style sheets */
141 ApplyToAllStyleSheets(StyleSheetCallback
& callback
) const;
143 /** Clear the contents of the index.
144 * The StyleSheetDisposer::Dispose() method is called on each style sheet, e.g., if you want to broadcast
148 Clear(StyleSheetDisposer
& cleanup
);
153 /** Warning: counting for n starts at 0, i.e., the 0th style sheet is the first that is found. */
155 GetNthStyleSheetThatMatchesPredicate(unsigned n
, StyleSheetPredicate
& predicate
,
156 unsigned startAt
= 0);
158 /** Get the positions of the style sheets which belong to a certain family.
160 const std::vector
<unsigned>&
161 GetStyleSheetPositionsByFamily(SfxStyleFamily
) const;
164 /** Register the position of a styleName in the index */
166 Register(const SfxStyleSheetBase
& style
, unsigned pos
);
168 typedef std::vector
<rtl::Reference
<SfxStyleSheetBase
> > VectorType
;
169 /** Vector with the stylesheets to allow for index-based access.
171 VectorType mStyleSheets
;
173 /** The map type that is used to store the mapping from strings to ids in mStyleSheets
176 * Must be an unordered map. A regular map is too slow for some files. */
177 typedef std::unordered_multimap
<OUString
, unsigned> MapType
;
179 /** A map which stores the positions of style sheets by their name */
180 MapType mPositionsByName
;
182 std::vector
<std::vector
<unsigned> > mStyleSheetPositionsByFamily
;
185 } /* namespace svl */
187 #endif // INCLUDED_SVL_INDEXEDSTYLESHEETS_HXX
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */