LibreOffice Module sw (master)  1
sortedarray.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 
20 #ifndef INCLUDED_SW_SOURCE_FILTER_WW8_SORTEDARRAY_HXX
21 #define INCLUDED_SW_SOURCE_FILTER_WW8_SORTEDARRAY_HXX
22 
23 #include <osl/diagnose.h>
24 #include <algorithm>
25 
26 //simple template that manages a static [] array by sorting at construction
27 
28 namespace ww
29 {
34  template<class C> class SortedArray
35  {
36  private:
37  //The array e.g. of sprms.
39  size_t mnNoElems;
40 
41  SortedArray(const SortedArray&) = delete;
42  SortedArray& operator=(const SortedArray&) = delete;
43  public:
44  //Find an entry, return its address if found and 0 if not
45  const C *search(C aSrch) const
46  {
47  std::pair<C *, C *> aPair =
48  std::equal_range(mpWwSprmTab, mpWwSprmTab + mnNoElems, aSrch);
49  if (aPair.first != aPair.second)
50  return aPair.first;
51  else
52  return 0;
53  }
54 
55  SortedArray(C *pWwSprmTab, size_t nNoElems)
56  : mpWwSprmTab(pWwSprmTab), mnNoElems(nNoElems)
57  {
58  OSL_ENSURE(mnNoElems && pWwSprmTab, "WW8: empty Array: Don't do that");
59  std::sort(mpWwSprmTab, mpWwSprmTab + mnNoElems);
60 #if OSL_DEBUG_LEVEL > 1
61  bool bBroken=false;
62  OUString sError;
63  const C *pIter = mpWwSprmTab;
64  const C *pBeforeEnd = mpWwSprmTab + mnNoElems - 1;
65  while (pIter < pBeforeEnd)
66  {
67  if (pIter->nId == (pIter+1)->nId)
68  {
69  if (!bBroken)
70  {
71  sError =
72  "WW8: Duplicate in list, almost certainly don't "
73  "want that!\n"
74  "(You will not see this message again unless you "
75  "restart)\n"
76  "Extra entries are...\n";
77  bBroken=true;
78  }
79 
80  size_t nSize = sizeof(C);
81  const sal_uInt8 *pHack =
82  reinterpret_cast<const sal_uInt8 *>(&(*pIter));
83  for (size_t i=0; i < nSize; ++i)
84  {
85  sError += OUString::number(
86  static_cast<sal_Int32>(pHack[i]), 16);
87  sError += OUString(' ');
88  }
89  sError += OUString('\n');
90  while (pIter->nId == (pIter+1)->nId && pIter < pBeforeEnd)
91  ++pIter;
92  }
93  else
94  ++pIter;
95  }
96  if (bBroken)
97  {
98  SAL_WARN( "sw", sError );
99  }
100 #endif
101  }
102  };
103 }
104 #endif
105 
106 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition: wwstyles.hxx:25
sal_Int32 * pHack
SortedArray(const SortedArray &)=delete
#define C
SortedArray & operator=(const SortedArray &)=delete
const C * search(C aSrch) const
Definition: sortedarray.hxx:45
int i
SortedArray(C *pWwSprmTab, size_t nNoElems)
Definition: sortedarray.hxx:55
unsigned char sal_uInt8
simple template that manages a static array
Definition: sortedarray.hxx:34
#define SAL_WARN(area, stream)