Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / include / svl / whichranges.hxx
blob744a0f2edaf562e851d48ca36433f007e5379c4e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
8 */
9 #pragma once
11 #include <sal/config.h>
12 #include <sal/types.h>
13 #include <svl/svldllapi.h>
14 #include <array>
15 #include <memory>
16 #include <cassert>
18 typedef std::pair<sal_uInt16, sal_uInt16> WhichPair;
20 namespace svl
22 namespace detail
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)
54 it->first = WID1;
55 it->second = WID2;
56 if constexpr (sizeof...(Rest) > 0)
57 fill<Rest...>(++it);
59 static constexpr Array make()
61 assert(svl::detail::validRanges<WIDs...>());
62 Array a{};
63 fill<WIDs...>(a.begin());
64 return a;
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...>{};
74 /**
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;
83 sal_Int32 m_size = 0;
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())
92 , m_size(nSize)
93 , m_bOwnRanges(true)
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");
120 return m_pairs[idx];
122 void reset();
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: */