1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
11 #include <sal/config.h>
12 #include <sal/types.h>
13 #include <svl/svldllapi.h>
18 typedef std::pair
<sal_uInt16
, sal_uInt16
> WhichPair
;
24 constexpr bool validRange(sal_uInt16 wid1
, sal_uInt16 wid2
) { return wid1
!= 0 && wid1
<= wid2
; }
26 constexpr bool validGap(sal_uInt16 wid1
, sal_uInt16 wid2
) { return wid2
> wid1
; }
28 template <sal_uInt16 WID1
, sal_uInt16 WID2
> constexpr bool validRanges()
30 return validRange(WID1
, WID2
);
33 template <sal_uInt16 WID1
, sal_uInt16 WID2
, sal_uInt16 WID3
, sal_uInt16
... WIDs
>
34 constexpr bool validRanges()
36 return validRange(WID1
, WID2
) && validGap(WID2
, WID3
) && validRanges
<WID3
, WIDs
...>();
39 // The calculations in rangeSize cannot overflow, assuming
40 // std::size_t is no smaller than sal_uInt16:
41 constexpr std::size_t rangeSize(sal_uInt16 wid1
, sal_uInt16 wid2
)
43 assert(validRange(wid1
, wid2
));
44 return wid2
- wid1
+ 1;
48 template <sal_uInt16
... WIDs
> struct Items_t
50 using Array
= std::array
<WhichPair
, sizeof...(WIDs
) / 2>;
51 template <sal_uInt16 WID1
, sal_uInt16 WID2
, sal_uInt16
... Rest
>
52 static constexpr void fill(typename
Array::iterator it
)
56 if constexpr (sizeof...(Rest
) > 0)
59 static constexpr Array
make()
61 assert(svl::detail::validRanges
<WIDs
...>());
63 fill
<WIDs
...>(a
.begin());
66 // This is passed to WhichRangesContainer so we can avoid needing to malloc()
67 // for compile-time data.
68 static constexpr Array value
= make();
71 template <sal_uInt16
... WIDs
> inline static constexpr auto Items
= Items_t
<WIDs
...>{};
75 * Most of the time, the which ranges we point at are a compile-time literal.
76 * So we take advantage of that, and avoid the cost of allocating our own array and copying into it.
78 struct SVL_DLLPUBLIC WhichRangesContainer
80 using const_iterator
= WhichPair
const*;
82 WhichPair
const* m_pairs
= nullptr;
84 /** if true, we allocated and need to delete the pairs, if not, we are pointing
85 * at a global const literal */
86 bool m_bOwnRanges
= false;
88 WhichRangesContainer() = default;
90 WhichRangesContainer(std::unique_ptr
<WhichPair
[]> wids
, sal_Int32 nSize
)
91 : m_pairs(wids
.release())
96 template <sal_uInt16
... WIDs
>
97 WhichRangesContainer(svl::Items_t
<WIDs
...>)
98 : m_pairs(svl::Items_t
<WIDs
...>::value
.data())
99 , m_size(svl::Items_t
<WIDs
...>::value
.size())
100 , m_bOwnRanges(false)
103 WhichRangesContainer(const WhichPair
* wids
, sal_Int32 nSize
);
104 WhichRangesContainer(sal_uInt16 nWhichStart
, sal_uInt16 nWhichEnd
);
105 WhichRangesContainer(WhichRangesContainer
const& other
) { operator=(other
); }
106 WhichRangesContainer(WhichRangesContainer
&& other
);
107 ~WhichRangesContainer();
109 WhichRangesContainer
& operator=(WhichRangesContainer
&& other
);
110 WhichRangesContainer
& operator=(WhichRangesContainer
const& other
);
112 bool operator==(WhichRangesContainer
const& other
) const;
113 const_iterator
begin() const noexcept
{ return m_pairs
; }
114 const_iterator
end() const noexcept
{ return begin() + size(); }
115 bool empty() const noexcept
{ return m_size
== 0; }
116 sal_Int32
size() const noexcept
{ return m_size
; }
117 WhichPair
const& operator[](sal_Int32 idx
) const noexcept
119 assert(idx
>= 0 && idx
< size() && "index out of range");
124 // Adds a range to which ranges, keeping the ranges in valid state (sorted, non-overlapping)
125 SAL_WARN_UNUSED_RESULT WhichRangesContainer
MergeRange(sal_uInt16 nFrom
, sal_uInt16 nTo
) const;
128 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */