Line data Source code
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 <algorithm>
24 :
25 : //simple template that manages a static [] array by sorting at construction
26 :
27 : namespace ww
28 : {
29 : /** simple template that manages a static array
30 :
31 : The template sorts the array at construction in place.
32 :
33 : @author
34 : <a href="mailto:cmc@openoffice.org">Caolán McNamara</a>
35 : */
36 : template<class C> class SortedArray
37 : {
38 : private:
39 : //The array e.g. of sprms.
40 : C *mpWwSprmTab;
41 : size_t mnNoElems;
42 :
43 : //No copying
44 : SortedArray(const SortedArray&);
45 : SortedArray& operator=(const SortedArray&);
46 : public:
47 : //Find an entry, return its address if found and 0 if not
48 86162 : const C *search(C aSrch) const
49 : {
50 : std::pair<C *, C *> aPair =
51 86162 : std::equal_range(mpWwSprmTab, mpWwSprmTab + mnNoElems, aSrch);
52 86162 : if (aPair.first != aPair.second)
53 81476 : return aPair.first;
54 : else
55 4686 : return 0;
56 : }
57 :
58 7 : SortedArray(C *pWwSprmTab, size_t nNoElems)
59 7 : : mpWwSprmTab(pWwSprmTab), mnNoElems(nNoElems)
60 : {
61 : OSL_ENSURE(mnNoElems && pWwSprmTab, "WW8: empty Array: Don't do that");
62 7 : std::sort(mpWwSprmTab, mpWwSprmTab + mnNoElems);
63 : #if OSL_DEBUG_LEVEL > 1
64 : bool bBroken=false;
65 : OUString sError;
66 : const C *pIter = mpWwSprmTab;
67 : const C *pBeforeEnd = mpWwSprmTab + mnNoElems - 1;
68 : while (pIter < pBeforeEnd)
69 : {
70 : if (*pIter == *(pIter+1))
71 : {
72 : if (!bBroken)
73 : {
74 : sError =
75 : "WW8: Duplicate in list, almost certainly don't "
76 : "want that!\n"
77 : "(You will not see this message again unless you "
78 : "restart)\n"
79 : "Extra entries are...\n";
80 : bBroken=true;
81 : }
82 :
83 : size_t nSize = sizeof(C);
84 : const sal_uInt8 *pHack =
85 : reinterpret_cast<const sal_uInt8 *>(&(*pIter));
86 : for (size_t i=0; i < nSize; ++i)
87 : {
88 : sError += OUString::number(
89 : static_cast<sal_Int32>(pHack[i]), 16);
90 : sError += OUString(' ');
91 : }
92 : sError += OUString('\n');
93 : while (*pIter == *(pIter+1) && pIter < pBeforeEnd)
94 : ++pIter;
95 : }
96 : else
97 : ++pIter;
98 : }
99 : if (bBroken)
100 : {
101 : OSL_FAIL( OUStringToOString( sError, RTL_TEXTENCODING_ASCII_US ).getStr() );
102 : }
103 : #endif
104 7 : }
105 : };
106 : }
107 : #endif
108 :
109 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|