android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / inc / frameformats.hxx
blobca0d5025fbe2de0ee4d95fea5c7af3a881cf2e56
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #pragma once
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>
34 class SwFrameFormat;
35 class SwTableFormat;
37 // Like o3tl::find_partialorder_ptrequals
38 // We don't allow duplicated object entries!
39 namespace sw
41 template <class value_type> class FrameFormats final : public SwFormatsBase
43 struct ByPos
46 struct ByTypeAndName
49 struct FrameFormatsKey
50 : boost::multi_index::composite_key<
51 value_type,
52 boost::multi_index::const_mem_fun<SwFormat, const OUString&, &SwFormat::GetName>,
53 boost::multi_index::const_mem_fun<SwFormat, sal_uInt16, &SwFormat::Which>,
54 boost::multi_index::identity<value_type> // the actual object pointer
58 typedef boost::multi_index_container<
59 value_type, boost::multi_index::indexed_by<
60 boost::multi_index::random_access<boost::multi_index::tag<ByPos>>,
61 boost::multi_index::ordered_unique<boost::multi_index::tag<ByTypeAndName>,
62 FrameFormatsKey>>>
63 FrameFormatsContainer;
64 // function updating ByName index via modify
65 friend class ::SwFrameFormat;
67 public:
68 // getting from T* to T const* ...
69 typedef typename std::add_pointer<
70 typename std::add_const<typename std::remove_pointer<value_type>::type>::type>::type
71 const_value_type;
72 typedef typename FrameFormatsContainer::size_type size_type;
73 typedef typename FrameFormatsContainer::template index<ByPos>::type index_type;
74 typedef typename index_type::iterator iterator;
75 typedef typename index_type::const_iterator const_iterator;
76 typedef typename FrameFormatsContainer::template index<ByTypeAndName>::type name_index_type;
77 typedef typename name_index_type::iterator name_iterator;
78 typedef typename name_index_type::const_iterator const_name_iterator;
79 typedef typename std::pair<const_name_iterator, const_name_iterator> range_type;
81 private:
82 FrameFormatsContainer m_vContainer;
83 index_type& GetByPos() { return m_vContainer.template get<ByPos>(); }
84 name_index_type& GetByTypeAndName() { return m_vContainer.template get<ByTypeAndName>(); }
85 const index_type& GetByPos() const { return m_vContainer.template get<ByPos>(); }
86 const name_index_type& GetByTypeAndName() const
88 return m_vContainer.template get<ByTypeAndName>();
91 public:
92 FrameFormats(){};
93 // frees all SwFrameFormat!
94 virtual ~FrameFormats() override { DeleteAndDestroyAll(); };
96 bool empty() const { return m_vContainer.empty(); }
97 size_t size() const { return m_vContainer.size(); }
99 // Only fails, if you try to insert the same object twice
100 std::pair<const_iterator, bool> push_back(const value_type& x)
102 SAL_WARN_IF(x->m_ffList != nullptr, "sw.core", "Inserting already assigned item");
103 assert(x->m_ffList == nullptr);
104 x->m_ffList = this;
105 return GetByPos().push_back(const_cast<value_type>(x));
108 // This will try to remove the exact object!
109 bool erase(const value_type& x)
111 const_iterator const ret = find(x);
112 SAL_WARN_IF(x->m_ffList != this, "sw.core", "Removing invalid / unassigned item");
113 if (ret != end())
115 assert(x == *ret);
116 x->m_ffList = nullptr;
117 GetByPos().erase(ret);
118 return true;
120 return false;
122 void erase(size_type index) { erase(begin() + index); };
123 void erase(const_iterator const& position)
125 (*position)->m_ffList = nullptr;
126 GetByPos().erase(begin() + (position - begin()));
129 // Get the iterator of the exact object (includes pointer!),
130 // e.g for position with std::distance.
131 // There is also ContainsFormat, if you don't need the position.
132 const_iterator find(const value_type& x) const
134 auto it = GetByTypeAndName().find(std::make_tuple(x->GetName(), x->Which(), x));
135 return m_vContainer.template project<ByPos>(it);
138 const_name_iterator findByTypeAndName(sal_uInt16 type, const OUString& name) const
140 return GetByTypeAndName().find(std::make_tuple(name, type));
142 // search for formats by name
143 range_type findRangeByName(const OUString& rName) const
145 auto& idx = GetByTypeAndName();
146 auto it = idx.lower_bound(std::make_tuple(rName, sal_uInt16(0)));
147 auto itEnd = idx.upper_bound(std::make_tuple(rName, SAL_MAX_UINT16));
148 return { it, itEnd };
150 // So we can actually check for end()
151 const_name_iterator typeAndNameEnd() const { return GetByTypeAndName().end(); }
153 const value_type& operator[](size_t index) const { return GetByPos().operator[](index); }
154 const value_type& front() const { return GetByPos().front(); }
155 const value_type& back() const { return GetByPos().back(); }
156 const_iterator begin() const { return GetByPos().begin(); }
157 const_iterator end() const { return GetByPos().end(); }
159 void dumpAsXml(xmlTextWriterPtr pWriter, const char* pName) const
161 (void)xmlTextWriterStartElement(pWriter, BAD_CAST(pName));
162 for (const auto pFormat : GetByPos())
163 pFormat->dumpAsXml(pWriter);
164 (void)xmlTextWriterEndElement(pWriter);
167 virtual size_t GetFormatCount() const override { return m_vContainer.size(); }
168 virtual SwFormat* GetFormat(size_t idx) const override
170 return const_cast<value_type&>(operator[](idx));
172 virtual void Rename(const SwFrameFormat& rFormat, const OUString& sNewName) override
174 assert(dynamic_cast<value_type>(const_cast<SwFrameFormat*>(&rFormat)));
175 iterator it = find(static_cast<value_type>(const_cast<SwFrameFormat*>(&rFormat)));
176 assert(end() != it);
177 const auto sOldName = rFormat.GetName();
178 auto fRenamer
179 = [sNewName](SwFormat* pFormat) { pFormat->SwFormat::SetFormatName(sNewName, false); };
180 auto fRenamerUndo
181 = [sOldName](SwFormat* pFormat) { pFormat->SwFormat::SetFormatName(sOldName, false); };
182 bool const renamed = GetByPos().modify(it, fRenamer, fRenamerUndo);
183 assert(renamed);
184 (void)renamed; // unused in NDEBUG
187 /// fast check if given format is contained here
188 /// @precond pFormat must not have been deleted
189 bool ContainsFormat(const value_type& rpFormat) const { return rpFormat->m_ffList == this; };
191 /// not so fast check that given format is still alive (i.e. contained here)
192 bool IsAlive(const_value_type pFrameFormat) const
194 auto pThisNonConst
195 = const_cast<typename std::remove_const<sw::FrameFormats<value_type>>::type*>(this);
196 return pThisNonConst->find(const_cast<value_type>(pFrameFormat)) != pThisNonConst->end();
199 void DeleteAndDestroyAll(bool keepDefault = false)
201 if (empty())
202 return;
203 const int offset = keepDefault ? 1 : 0;
204 for (const_iterator it = begin() + offset; it != end(); ++it)
205 delete *it;
206 if (offset)
207 GetByPos().erase(begin() + offset, end());
208 else
209 m_vContainer.clear();
212 bool newDefault(const value_type& x)
214 std::pair<iterator, bool> res = GetByPos().push_front(const_cast<value_type&>(x));
215 if (!res.second)
216 newDefault(res.first);
217 return res.second;
219 void newDefault(const_iterator const& position)
221 if (position == begin())
222 return;
223 GetByPos().relocate(begin(), position);
226 // Override return type to reduce casting
227 value_type FindFrameFormatByName(const OUString& rName) const
229 auto& idx = GetByTypeAndName();
230 auto it = idx.lower_bound(std::make_tuple(rName, sal_uInt16(0)));
231 if (it != idx.end() && (*it)->GetName() == rName)
232 return *it;
233 return nullptr;
236 typedef FrameFormats<::SwTableFormat*> TableFrameFormats;
237 typedef FrameFormats<sw::SpzFrameFormat*> SpzFrameFormats;
240 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */