LibreOffice Module sw (master) 1
frameformats.hxx
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 * 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
34class SwFrameFormat;
35class SwTableFormat;
36
37// Like o3tl::find_partialorder_ptrequals
38// We don't allow duplicated object entries!
39namespace sw
40{
41template <class value_type> class FrameFormats final : public SwFormatsBase
42{
43 struct ByPos
44 {
45 };
47 {
48 };
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
55 >
56 {
57 };
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>,
64 // function updating ByName index via modify
65 friend class ::SwFrameFormat;
66
67public:
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
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;
80
81private:
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>(); }
87 {
88 return m_vContainer.template get<ByTypeAndName>();
89 }
90
91public:
93 // frees all SwFrameFormat!
94 virtual ~FrameFormats() override { DeleteAndDestroyAll(); };
95
96 bool empty() const { return m_vContainer.empty(); }
97 size_t size() const { return m_vContainer.size(); }
98
99 // Only fails, if you try to insert the same object twice
100 std::pair<const_iterator, bool> push_back(const value_type& x)
101 {
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));
106 };
107
108 // This will try to remove the exact object!
109 bool erase(const value_type& x)
110 {
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())
114 {
115 assert(x == *ret);
116 x->m_ffList = nullptr;
117 GetByPos().erase(ret);
118 return true;
119 }
120 return false;
121 };
122 void erase(size_type index) { erase(begin() + index); };
123 void erase(const_iterator const& position)
124 {
125 (*position)->m_ffList = nullptr;
126 GetByPos().erase(begin() + (position - begin()));
127 }
128
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
133 {
134 auto it = GetByTypeAndName().find(std::make_tuple(x->GetName(), x->Which(), x));
135 return m_vContainer.template project<ByPos>(it);
136 };
137
138 const_name_iterator findByTypeAndName(sal_uInt16 type, const OUString& name) const
139 {
140 return GetByTypeAndName().find(std::make_tuple(name, type));
141 };
142 // search for formats by name
143 range_type findRangeByName(const OUString& rName) const
144 {
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 };
149 };
150 // So we can actually check for end()
152
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(); }
158
159 void dumpAsXml(xmlTextWriterPtr pWriter, const char* pName) const
160 {
161 (void)xmlTextWriterStartElement(pWriter, BAD_CAST(pName));
162 for (const auto pFormat : GetByPos())
163 pFormat->dumpAsXml(pWriter);
164 (void)xmlTextWriterEndElement(pWriter);
165 };
166
167 virtual size_t GetFormatCount() const override { return m_vContainer.size(); }
168 virtual SwFormat* GetFormat(size_t idx) const override
169 {
170 return const_cast<value_type&>(operator[](idx));
171 };
172 virtual void Rename(const SwFrameFormat& rFormat, const OUString& sNewName) override
173 {
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
185 };
186
189 bool ContainsFormat(const value_type& rpFormat) const { return rpFormat->m_ffList == this; };
190
192 bool IsAlive(const_value_type pFrameFormat) const
193 {
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();
197 };
198
199 void DeleteAndDestroyAll(bool keepDefault = false)
200 {
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();
210 };
211
212 bool newDefault(const value_type& x)
213 {
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;
218 };
219 void newDefault(const_iterator const& position)
220 {
221 if (position == begin())
222 return;
223 GetByPos().relocate(begin(), position);
224 };
225
226 // Override return type to reduce casting
227 value_type FindFrameFormatByName(const OUString& rName) const
228 {
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;
234 };
235};
238}
239
242
243/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const char * pName
Base class for various Writer styles.
Definition: format.hxx:47
const OUString & GetName() const
Definition: format.hxx:131
provides some methods for generic operations on lists that contain SwFormat* subclasses.
Definition: docary.hxx:44
Style of a layout element.
Definition: frmfmt.hxx:72
bool newDefault(const value_type &x)
const value_type & front() const
void DeleteAndDestroyAll(bool keepDefault=false)
void erase(size_type index)
void newDefault(const_iterator const &position)
std::pair< const_name_iterator, const_name_iterator > range_type
std::pair< const_iterator, bool > push_back(const value_type &x)
index_type::iterator iterator
void dumpAsXml(xmlTextWriterPtr pWriter, const char *pName) const
const_name_iterator typeAndNameEnd() const
void erase(const_iterator const &position)
const_name_iterator findByTypeAndName(sal_uInt16 type, const OUString &name) const
boost::multi_index_container< value_type, boost::multi_index::indexed_by< boost::multi_index::random_access< boost::multi_index::tag< ByPos > >, boost::multi_index::ordered_unique< boost::multi_index::tag< ByTypeAndName >, FrameFormatsKey > > > FrameFormatsContainer
index_type::const_iterator const_iterator
bool empty() const
const value_type & back() const
value_type FindFrameFormatByName(const OUString &rName) const
FrameFormatsContainer::size_type size_type
FrameFormatsContainer m_vContainer
FrameFormatsContainer::template index< ByPos >::type index_type
bool ContainsFormat(const value_type &rpFormat) const
fast check if given format is contained here @precond pFormat must not have been deleted
name_index_type & GetByTypeAndName()
const_iterator end() const
std::add_pointer< typenamestd::add_const< typenamestd::remove_pointer< value_type >::type >::type >::type const_value_type
virtual ~FrameFormats() override
range_type findRangeByName(const OUString &rName) const
const index_type & GetByPos() const
const_iterator begin() const
const_iterator find(const value_type &x) const
FrameFormatsContainer::template index< ByTypeAndName >::type name_index_type
index_type & GetByPos()
bool IsAlive(const_value_type pFrameFormat) const
not so fast check that given format is still alive (i.e. contained here)
const name_index_type & GetByTypeAndName() const
virtual void Rename(const SwFrameFormat &rFormat, const OUString &sNewName) override
virtual SwFormat * GetFormat(size_t idx) const override
size_t size() const
virtual size_t GetFormatCount() const override
bool erase(const value_type &x)
name_index_type::const_iterator const_name_iterator
const value_type & operator[](size_t index) const
name_index_type::iterator name_iterator
struct _xmlTextWriter * xmlTextWriterPtr
float x
const char * name
const sal_uInt16 idx[]
#define SAL_WARN_IF(condition, area, stream)
def position(n=-1)
index
Dialog to specify the properties of date form field.
FrameFormats< sw::SpzFrameFormat * > SpzFrameFormats
FrameFormats<::SwTableFormat * > TableFrameFormats
#define SW_DLLPUBLIC
Definition: swdllapi.h:28
#define SAL_MAX_UINT16
ResultType type