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/.
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 .
22 #include <boost/multi_index_container.hpp>
23 #include <boost/multi_index/composite_key.hpp>
24 #include <boost/multi_index/identity.hpp>
25 #include <boost/multi_index/mem_fun.hpp>
26 #include <boost/multi_index/ordered_index.hpp>
27 #include <boost/multi_index/random_access_index.hpp>
29 // Like o3tl::find_partialorder_ptrequals
30 // We don't allow duplicated object entries!
31 struct char_formats_name_key
32 : boost::multi_index::composite_key
<
34 boost::multi_index::const_mem_fun
<SwFormat
, const OUString
&, &SwFormat::GetName
>,
35 boost::multi_index::identity
<SwCharFormat
*> // the actual object pointer
40 typedef boost::multi_index_container
<
42 boost::multi_index::indexed_by
<boost::multi_index::random_access
<>,
43 boost::multi_index::ordered_unique
<char_formats_name_key
>>>
46 class SW_DLLPUBLIC SwCharFormats final
: public SwFormatsBase
48 // function updating ByName index via modify
49 friend void SwFormat::SetFormatName(const OUString
&, bool);
52 typedef SwCharFormatsBase::nth_index
<0>::type ByPos
;
53 typedef SwCharFormatsBase::nth_index
<1>::type ByName
;
54 typedef ByPos::iterator iterator
;
57 SwCharFormatsBase m_Array
;
62 typedef ByPos::const_iterator const_iterator
;
63 typedef SwCharFormatsBase::size_type size_type
;
64 typedef SwCharFormatsBase::value_type value_type
;
67 // frees all SwCharFormat!
68 virtual ~SwCharFormats() override
;
70 bool empty() const { return m_Array
.empty(); }
71 size_t size() const { return m_Array
.size(); }
73 // Only fails, if you try to insert the same object twice
74 void insert(SwCharFormat
* x
);
76 // This will try to remove the exact object!
77 void erase(const_iterator
const& position
);
79 // Get the iterator of the exact object (includes pointer!),
80 // e.g for position with std::distance.
81 // There is also ContainsFormat, if you don't need the position.
82 const_iterator
find(const SwCharFormat
* x
) const;
83 size_t GetPos(const SwCharFormat
* p
) const;
85 // search for formats by name
86 ByName::const_iterator
findByName(const OUString
& name
) const;
88 SwCharFormat
* operator[](size_t index_
) const { return m_PosIndex
.operator[](index_
); }
89 const_iterator
begin() const { return m_PosIndex
.begin(); }
90 const_iterator
end() const { return m_PosIndex
.end(); }
92 void dumpAsXml(xmlTextWriterPtr pWriter
) const;
94 virtual size_t GetFormatCount() const override
{ return m_Array
.size(); }
95 virtual SwCharFormat
* GetFormat(size_t idx
) const override
{ return operator[](idx
); }
97 /// fast check if given format is contained here
98 /// @precond pFormat must not have been deleted
99 bool ContainsFormat(const SwCharFormat
* pFormat
) const;
101 void DeleteAndDestroyAll(bool keepDefault
= false);
103 // Override return type to reduce casting
104 virtual SwCharFormat
* FindFormatByName(const OUString
& rName
) const override
;
106 /** Need to call this when the format name changes */
107 void SetFormatNameAndReindex(SwCharFormat
* v
, const OUString
& sNewName
);
110 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */