LibreOffice Module svl (master) 1
whichranges.hxx
Go to the documentation of this file.
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.
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#pragma once
10
11#include <sal/config.h>
12#include <sal/types.h>
13#include <svl/svldllapi.h>
14#include <array>
15#include <memory>
16#include <cassert>
17
18typedef std::pair<sal_uInt16, sal_uInt16> WhichPair;
19
20namespace svl
21{
22namespace detail
23{
24constexpr bool validRange(sal_uInt16 wid1, sal_uInt16 wid2) { return wid1 != 0 && wid1 <= wid2; }
25
26constexpr bool validGap(sal_uInt16 wid1, sal_uInt16 wid2) { return wid2 > wid1; }
27
28template <sal_uInt16 WID1, sal_uInt16 WID2> constexpr bool validRanges()
29{
30 return validRange(WID1, WID2);
31}
32
33template <sal_uInt16 WID1, sal_uInt16 WID2, sal_uInt16 WID3, sal_uInt16... WIDs>
34constexpr bool validRanges()
35{
36 return validRange(WID1, WID2) && validGap(WID2, WID3) && validRanges<WID3, WIDs...>();
37}
38
39// The calculations in rangeSize cannot overflow, assuming
40// std::size_t is no smaller than sal_uInt16:
41constexpr std::size_t rangeSize(sal_uInt16 wid1, sal_uInt16 wid2)
42{
43 assert(validRange(wid1, wid2));
44 return wid2 - wid1 + 1;
45}
46}
47
48template <sal_uInt16... WIDs> struct Items_t
49{
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)
53 {
54 it->first = WID1;
55 it->second = WID2;
56 if constexpr (sizeof...(Rest) > 0)
57 fill<Rest...>(++it);
58 }
59 static constexpr Array make()
60 {
61 assert(svl::detail::validRanges<WIDs...>());
62 Array a{};
63 fill<WIDs...>(a.begin());
64 return a;
65 }
66 // This is passed to WhichRangesContainer so we can avoid needing to malloc()
67 // for compile-time data.
68 static constexpr Array value = make();
69};
70
71template <sal_uInt16... WIDs> inline static constexpr auto Items = Items_t<WIDs...>{};
72}
73
79{
80 using const_iterator = WhichPair const*;
81
82 WhichPair const* m_pairs = nullptr;
83 sal_Int32 m_size = 0;
86 bool m_bOwnRanges = false;
87
89
90 WhichRangesContainer(std::unique_ptr<WhichPair[]> wids, sal_Int32 nSize)
91 : m_pairs(wids.release())
92 , m_size(nSize)
93 , m_bOwnRanges(true)
94 {
95 }
96 template <sal_uInt16... WIDs>
98 : m_pairs(svl::Items_t<WIDs...>::value.data())
99 , m_size(svl::Items_t<WIDs...>::value.size())
100 , m_bOwnRanges(false)
101 {
102 }
103 WhichRangesContainer(const WhichPair* wids, sal_Int32 nSize);
104 WhichRangesContainer(sal_uInt16 nWhichStart, sal_uInt16 nWhichEnd);
105 WhichRangesContainer(WhichRangesContainer const& other) { operator=(other); }
108
109 WhichRangesContainer& operator=(WhichRangesContainer&& other);
110 WhichRangesContainer& operator=(WhichRangesContainer const& other);
111
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
118 {
119 assert(idx >= 0 && idx < size() && "index out of range");
120 return m_pairs[idx];
121 }
122 void reset();
123
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;
126};
127
128/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
Any value
const sal_uInt16 idx[]
uno_Any a
size
enumrange< T >::Iterator begin(enumrange< T >)
constexpr bool validRange(sal_uInt16 wid1, sal_uInt16 wid2)
Definition: whichranges.hxx:24
constexpr bool validRanges()
Definition: whichranges.hxx:28
constexpr bool validGap(sal_uInt16 wid1, sal_uInt16 wid2)
Definition: whichranges.hxx:26
constexpr std::size_t rangeSize(sal_uInt16 wid1, sal_uInt16 wid2)
Definition: whichranges.hxx:41
static constexpr auto Items
Definition: whichranges.hxx:71
Most of the time, the which ranges we point at are a compile-time literal.
Definition: whichranges.hxx:79
bool empty() const noexcept
sal_Int32 size() const noexcept
WhichRangesContainer(WhichRangesContainer const &other)
WhichRangesContainer(std::unique_ptr< WhichPair[]> wids, sal_Int32 nSize)
Definition: whichranges.hxx:90
const_iterator begin() const noexcept
WhichPair const * const_iterator
Definition: whichranges.hxx:80
WhichRangesContainer(svl::Items_t< WIDs... >)
Definition: whichranges.hxx:97
WhichPair const & operator[](sal_Int32 idx) const noexcept
WhichRangesContainer()=default
const_iterator end() const noexcept
static constexpr void fill(typename Array::iterator it)
Definition: whichranges.hxx:52
static constexpr Array value
Definition: whichranges.hxx:68
std::array< WhichPair, sizeof...(WIDs)/2 > Array
Definition: whichranges.hxx:50
static constexpr Array make()
Definition: whichranges.hxx:59
#define SVL_DLLPUBLIC
Definition: svldllapi.h:28
constexpr bool operator==(TypedWhichId< T > const &lhs, TypedWhichId< T > rhs)
Definition: typedwhich.hxx:43
#define SAL_WARN_UNUSED_RESULT
std::pair< sal_uInt16, sal_uInt16 > WhichPair
Definition: whichranges.hxx:18