LibreOffice Module svl (master) 1
IndexedStyleSheets.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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 */
9
10
12#include <svl/style.hxx>
13
14#include <stdexcept>
15#include <algorithm>
16#include <utility>
17
18
19namespace {
20const size_t NUMBER_OF_FAMILIES = 7;
21size_t family_to_index(SfxStyleFamily family)
22{
23 switch (family) {
25 return 0;
27 return 1;
29 return 2;
31 return 3;
33 return 4;
35 return 5;
37 return 6;
38 default: break;
39 }
40 assert(false); // only for compiler warning. all cases are handled in the switch
41 return 0;
42}
43}
44
45namespace svl {
46
48{
49 for (size_t i = 0; i < NUMBER_OF_FAMILIES; i++) {
50 mStyleSheetPositionsByFamily.emplace_back();
51 }
52;}
53
54void IndexedStyleSheets::Register(const SfxStyleSheetBase& style, sal_Int32 pos)
55{
56 mPositionsByName.insert(std::make_pair(style.GetName(), pos));
57 size_t position = family_to_index(style.GetFamily());
59 size_t positionForFamilyAll = family_to_index(SfxStyleFamily::All);
60 mStyleSheetPositionsByFamily.at(positionForFamilyAll).push_back(pos);
61}
62
63void
65{
66 mPositionsByName.clear();
68 for (size_t i = 0; i < NUMBER_OF_FAMILIES; i++) {
69 mStyleSheetPositionsByFamily.emplace_back();
70 }
71
72 sal_Int32 i = 0;
73 for (const auto& rxStyleSheet : mStyleSheets) {
74 SfxStyleSheetBase* p = rxStyleSheet.get();
75 Register(*p, i);
76 ++i;
77 }
78}
79
81{
82 return mStyleSheets.size();
83}
84
85void
87{
88 if (!HasStyleSheet(style)) {
89 mStyleSheets.push_back(style);
90 // since we just added an element to the vector, we can safely do -1 as it will always be >= 1
91 Register(*style, mStyleSheets.size()-1);
92 }
93}
94
95bool
97{
98 std::pair<MapType::const_iterator, MapType::const_iterator> range = mPositionsByName.equal_range(style->GetName());
99 for (MapType::const_iterator it = range.first; it != range.second; ++it)
100 {
101 sal_Int32 pos = it->second;
102 if (mStyleSheets.at(pos) == style)
103 {
104 mStyleSheets.erase(mStyleSheets.begin() + pos);
105 Reindex();
106 return true;
107 }
108 }
109 return false;
110}
111
112std::vector<sal_Int32> IndexedStyleSheets::FindPositionsByName(const OUString& name) const
113{
114 std::vector<sal_Int32> r;
115 std::pair<MapType::const_iterator, MapType::const_iterator> range = mPositionsByName.equal_range(name);
116 for (MapType::const_iterator it = range.first; it != range.second; ++it) {
117 r.push_back(it->second);
118 }
119 return r;
120}
121
122std::vector<sal_Int32> IndexedStyleSheets::FindPositionsByNameAndPredicate(const OUString& name,
123 StyleSheetPredicate& predicate, SearchBehavior behavior) const
124{
125 std::vector<sal_Int32> r;
126 auto range = mPositionsByName.equal_range(name);
127 for (auto it = range.first; it != range.second; ++it) {
128 sal_Int32 pos = it->second;
129 SfxStyleSheetBase *ssheet = mStyleSheets.at(pos).get();
130 if (predicate.Check(*ssheet)) {
131 r.push_back(pos);
132 if (behavior == SearchBehavior::ReturnFirst) {
133 break;
134 }
135 }
136 }
137 return r;
138}
139
140
141sal_Int32
143{
144 return std::count_if(mStyleSheets.begin(), mStyleSheets.end(),
145 [&predicate](const rtl::Reference<SfxStyleSheetBase>& rxStyleSheet) {
146 const SfxStyleSheetBase *ssheet = rxStyleSheet.get();
147 return predicate.Check(*ssheet);
148 });
149}
150
153 sal_Int32 n,
154 StyleSheetPredicate& predicate,
155 sal_Int32 startAt)
156{
157 SfxStyleSheetBase* retval = nullptr;
158 sal_Int32 matching = 0;
159 for (VectorType::const_iterator it = mStyleSheets.begin()+startAt; it != mStyleSheets.end(); ++it) {
160 SfxStyleSheetBase *ssheet = it->get();
161 if (predicate.Check(*ssheet)) {
162 if (matching == n) {
163 retval = it->get();
164 break;
165 }
166 ++matching;
167 }
168 }
169 return retval;
170}
171
173{
174 VectorType::const_iterator it = std::find(mStyleSheets.begin(), mStyleSheets.end(), &style);
175 if (it == mStyleSheets.end()) {
176 throw std::runtime_error("IndexedStyleSheets::FindStylePosition Looked for style not in index");
177 }
178 return std::distance(mStyleSheets.begin(), it);
179}
180
181void
183{
184 for (const auto& rxStyleSheet : mStyleSheets) {
185 disposer.Dispose(rxStyleSheet);
186 }
187 mStyleSheets.clear();
188 mPositionsByName.clear();
189}
190
192{
193}
194
195bool
197{
198 std::pair<MapType::const_iterator, MapType::const_iterator> range = mPositionsByName.equal_range(style->GetName());
199 for (MapType::const_iterator it = range.first; it != range.second; ++it)
200 {
201 if (mStyleSheets.at(it->second) == style)
202 return true;
203 }
204 return false;
205}
206
209{
210 if( pos < static_cast<sal_Int32>(mStyleSheets.size()) )
211 return mStyleSheets.at(pos).get();
212 return nullptr;
213}
214
215void
217{
218 for (const auto& rxStyleSheet : mStyleSheets) {
219 callback.DoIt(*rxStyleSheet);
220 }
221}
222
223std::vector<sal_Int32>
225{
226 std::vector<sal_Int32> r;
227 for (VectorType::const_iterator it = mStyleSheets.begin(); it != mStyleSheets.end(); ++it) {
228 if (predicate.Check(**it)) {
229 r.push_back(std::distance(mStyleSheets.begin(), it));
230 }
231 }
232 return r;
233}
234
235const std::vector<sal_Int32>&
237{
238 size_t position = family_to_index(e);
240}
241
242} /* namespace svl */
243
244/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const OUString & GetName() const
Definition: style.cxx:156
SfxStyleFamily GetFamily() const
Definition: style.hxx:163
bool RemoveStyleSheet(const rtl::Reference< SfxStyleSheetBase > &style)
Removes a style sheet.
void AddStyleSheet(const rtl::Reference< SfxStyleSheetBase > &style)
Adds a style sheet.
sal_Int32 FindStyleSheetPosition(const SfxStyleSheetBase &style) const
Find the position of a provided style.
SfxStyleSheetBase * GetStyleSheetByPosition(sal_Int32 pos)
Return the stylesheet by its position.
void ApplyToAllStyleSheets(StyleSheetCallback &callback) const
Execute a callback on all style sheets.
MapType mPositionsByName
A map which stores the positions of style sheets by their name.
VectorType mStyleSheets
Vector with the stylesheets to allow for index-based access.
void Clear(StyleSheetDisposer &cleanup)
Clear the contents of the index.
const std::vector< sal_Int32 > & GetStyleSheetPositionsByFamily(SfxStyleFamily) const
Get the positions of the style sheets which belong to a certain family.
SfxStyleSheetBase * GetNthStyleSheetThatMatchesPredicate(sal_Int32 n, StyleSheetPredicate &predicate, sal_Int32 startAt=0)
Warning: counting for n starts at 0, i.e., the 0th style sheet is the first that is found.
sal_Int32 GetNumberOfStyleSheets() const
Obtain the number of style sheets which are held.
std::vector< sal_Int32 > FindPositionsByName(const OUString &name) const
Obtain the positions of all styles which have a given name.
std::vector< std::vector< sal_Int32 > > mStyleSheetPositionsByFamily
void Register(const SfxStyleSheetBase &style, sal_Int32 pos)
Register the position of a styleName in the index.
std::vector< sal_Int32 > FindPositionsByPredicate(StyleSheetPredicate &predicate) const
Obtain the positions of all styles which fulfill a certain condition.
sal_Int32 GetNumberOfStyleSheetsWithPredicate(StyleSheetPredicate &predicate) const
Obtain the number of style sheets for which a certain condition holds.
std::vector< sal_Int32 > FindPositionsByNameAndPredicate(const OUString &name, StyleSheetPredicate &predicate, SearchBehavior behavior=SearchBehavior::ReturnAll) const
Obtain the positions of all styles which have a certain name and fulfill a certain condition.
bool HasStyleSheet(const rtl::Reference< SfxStyleSheetBase > &style) const
Check whether a specified style sheet is stored.
const char * name
void * p
sal_Int64 n
def position(n=-1)
int i
Function object to apply a method on all style sheets.
virtual void DoIt(const SfxStyleSheetBase &styleSheet)=0
Function object for cleanup-Strategy for IndexedSfxStyleSheets::Clear().
virtual void Dispose(rtl::Reference< SfxStyleSheetBase > styleSheet)=0
Function object to check whether a style sheet a fulfills specific criteria.
virtual bool Check(const SfxStyleSheetBase &styleSheet)=0
SfxStyleFamily
Definition: style.hxx:42
size_t pos