Debian Codegraph @

sw/inc/frameformats.hxx

raw symbols SCIP
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 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19 #pragma once
20
21 #include "docary.hxx"
22 #include "frmfmt.hxx"
23 #include "swtblfmt.hxx"
24 #include <boost/multi_index_container.hpp>
25 #include <boost/multi_index/composite_key.hpp>
26 #include <boost/multi_index/identity.hpp>
27 #include <boost/multi_index/mem_fun.hpp>
28 #include <boost/multi_index/ordered_index.hpp>
29 #include <boost/multi_index/random_access_index.hpp>
30 #include <boost/multi_index/tag.hpp>
31 #include <libxml/xmlstring.h>
32 #include <libxml/xmlwriter.h>
33
34 // Like o3tl::find_partialorder_ptrequals
35 // We don't allow duplicated object entries!
36 namespace sw
37 {
38 template <class value_type> class FrameFormats final : public SwFormatsBase
39 {
40 struct ByPos
41 {
42 };
43 struct ByTypeAndName
44 {
45 };
46 struct FrameFormatsKey
47 : boost::multi_index::composite_key<
48 value_type,
49 boost::multi_index::const_mem_fun<SwFormat, const UIName&, &SwFormat::GetName>,
50 boost::multi_index::const_mem_fun<SwFormat, sal_uInt16, &SwFormat::Which>,
51 boost::multi_index::identity<value_type> // the actual object pointer
52 >
53 {
54 };
55 typedef boost::multi_index_container<
56 value_type, boost::multi_index::indexed_by<
57 boost::multi_index::random_access<boost::multi_index::tag<ByPos>>,
58 boost::multi_index::ordered_unique<boost::multi_index::tag<ByTypeAndName>,
59 FrameFormatsKey>>>
60 FrameFormatsContainer;
61 // function updating ByName index via modify
62 friend class ::SwFrameFormat;
63
64 public:
65 // getting from T* to T const* ...
66 typedef typename std::add_pointer<
67 typename std::add_const<typename std::remove_pointer<value_type>::type>::type>::type
68 const_value_type;
69 typedef typename FrameFormatsContainer::size_type size_type;
70 typedef typename FrameFormatsContainer::template index<ByPos>::type index_type;
71 typedef typename index_type::iterator iterator;
72 typedef typename index_type::const_iterator const_iterator;
73 typedef typename FrameFormatsContainer::template index<ByTypeAndName>::type name_index_type;
74 typedef typename name_index_type::iterator name_iterator;
75 typedef typename name_index_type::const_iterator const_name_iterator;
76 typedef typename std::pair<const_name_iterator, const_name_iterator> range_type;
77
78 private:
79 FrameFormatsContainer m_vContainer;
80 index_type& GetByPos() { return m_vContainer.template get<ByPos>(); }
81 name_index_type& GetByTypeAndName() { return m_vContainer.template get<ByTypeAndName>(); }
82 const index_type& GetByPos() const { return m_vContainer.template get<ByPos>(); }
83 const name_index_type& GetByTypeAndName() const
84 {
85 return m_vContainer.template get<ByTypeAndName>();
86 }
87
88 public:
89 FrameFormats(){};
90 // frees all SwFrameFormat!
91 virtual ~FrameFormats() override { DeleteAndDestroyAll(); };
92
93 bool empty() const { return m_vContainer.empty(); }
94 size_t size() const { return m_vContainer.size(); }
95
96 // Only fails, if you try to insert the same object twice
97 std::pair<const_iterator, bool> push_back(const value_type& x)
98 {
99 SAL_WARN_IF(x->m_ffList != nullptr, "sw.core", "Inserting already assigned item");
100 assert(x->m_ffList == nullptr);
101 x->m_ffList = this;
102 return GetByPos().push_back(const_cast<value_type>(x));
103 };
104
105 // This will try to remove the exact object!
106 bool erase(const value_type& x)
107 {
108 const_iterator const ret = find(x);
109 SAL_WARN_IF(x->m_ffList != this, "sw.core", "Removing invalid / unassigned item");
110 if (ret != end())
111 {
112 assert(x == *ret);
113 x->m_ffList = nullptr;
114 GetByPos().erase(ret);
115 return true;
116 }
117 return false;
118 };
119 void erase(size_type index) { erase(begin() + index); };
120 void erase(const_iterator const& position)
121 {
122 (*position)->m_ffList = nullptr;
123 GetByPos().erase(begin() + (position - begin()));
124 }
125
126 // Get the iterator of the exact object (includes pointer!),
127 // e.g for position with std::distance.
128 // There is also ContainsFormat, if you don't need the position.
129 const_iterator find(const value_type& x) const
130 {
131 auto it = GetByTypeAndName().find(std::make_tuple(x->GetName(), x->Which(), x));
132 return m_vContainer.template project<ByPos>(it);
133 };
134
135 const_name_iterator findByTypeAndName(sal_uInt16 type, const UIName& name) const
136 {
137 return GetByTypeAndName().find(std::make_tuple(name, type));
138 };
139 // search for formats by name
140 range_type findRangeByName(const UIName& rName) const
141 {
142 auto& idx = GetByTypeAndName();
143 auto it = idx.lower_bound(std::make_tuple(rName, sal_uInt16(0)));
144 auto itEnd = idx.upper_bound(std::make_tuple(rName, SAL_MAX_UINT16));
145 return { it, itEnd };
146 };
147 // So we can actually check for end()
148 const_name_iterator typeAndNameEnd() const { return GetByTypeAndName().end(); }
149
150 const value_type& operator[](size_t index) const { return GetByPos().operator[](index); }
151 const value_type& front() const { return GetByPos().front(); }
152 const value_type& back() const { return GetByPos().back(); }
153 const_iterator begin() const { return GetByPos().begin(); }
154 const_iterator end() const { return GetByPos().end(); }
155
156 void dumpAsXml(xmlTextWriterPtr pWriter, const char* pName) const
157 {
158 (void)xmlTextWriterStartElement(pWriter, BAD_CAST(pName));
159 for (const auto pFormat : GetByPos())
160 pFormat->dumpAsXml(pWriter);
161 (void)xmlTextWriterEndElement(pWriter);
162 };
163
164 virtual size_t GetFormatCount() const override { return m_vContainer.size(); }
165 virtual SwFormat* GetFormat(size_t idx) const override
166 {
167 return const_cast<value_type&>(operator[](idx));
168 };
169 virtual void Rename(const SwFrameFormat& rFormat, const UIName& sNewName) override
170 {
171 assert(dynamic_cast<value_type>(const_cast<SwFrameFormat*>(&rFormat)));
172 iterator it = find(static_cast<value_type>(const_cast<SwFrameFormat*>(&rFormat)));
173 assert(end() != it);
174 const auto sOldName = rFormat.GetName();
175 auto fRenamer
176 = [sNewName](SwFormat* pFormat) { pFormat->SwFormat::SetFormatName(sNewName, false); };
177 auto fRenamerUndo
178 = [sOldName](SwFormat* pFormat) { pFormat->SwFormat::SetFormatName(sOldName, false); };
179 bool const renamed = GetByPos().modify(it, fRenamer, fRenamerUndo);
180 assert(renamed);
181 (void)renamed; // unused in NDEBUG
182 };
183
184 /// fast check if given format is contained here
185 /// @precond pFormat must not have been deleted
186 bool ContainsFormat(const value_type& rpFormat) const { return rpFormat->m_ffList == this; };
187
188 /// not so fast check that given format is still alive (i.e. contained here)
189 bool IsAlive(const_value_type pFrameFormat) const
190 {
191 auto pThisNonConst
192 = const_cast<typename std::remove_const<sw::FrameFormats<value_type>>::type*>(this);
193 return pThisNonConst->find(const_cast<value_type>(pFrameFormat)) != pThisNonConst->end();
194 };
195
196 void DeleteAndDestroyAll(bool keepDefault = false)
197 {
198 if (empty())
199 return;
200 const int offset = keepDefault ? 1 : 0;
201 for (const_iterator it = begin() + offset; it != end(); ++it)
202 delete *it;
203 if (offset)
204 GetByPos().erase(begin() + offset, end());
205 else
206 m_vContainer.clear();
207 };
208
209 bool newDefault(const value_type& x)
210 {
211 std::pair<iterator, bool> res = GetByPos().push_front(const_cast<value_type&>(x));
212 if (!res.second)
213 newDefault(res.first);
214 return res.second;
215 };
216 void newDefault(const_iterator const& position)
217 {
218 if (position == begin())
219 return;
220 GetByPos().relocate(begin(), position);
221 };
222
223 // Override return type to reduce casting
224 value_type FindFrameFormatByName(const UIName& rName) const
225 {
226 auto& idx = GetByTypeAndName();
227 auto it = idx.lower_bound(std::make_tuple(rName, sal_uInt16(0)));
228 if (it != idx.end() && (*it)->GetName() == rName)
229 return *it;
230 return nullptr;
231 };
232 };
233 typedef FrameFormats<::SwTableFormat*> TableFrameFormats;
234 typedef FrameFormats<sw::SpzFrameFormat*> SpzFrameFormats;
235 }
236
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */