xref: /trunk/main/sw/source/filter/ww8/sortedarray.hxx (revision 1d2dbeb0)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil -*- */
25 
26 #ifndef WW_SORTEDARRAY_HXX
27 #define WW_SORTEDARRAY_HXX
28 
29 #include <algorithm>
30 #include <errhdl.hxx>       // ASSERT()
31 #include <tools/debug.hxx>
32 
33 //simple template that manages a static [] array by sorting at construction
34 
35 namespace ww
36 {
37     /** simple template that manages a static array
38 
39         The template sorts the array at construction in place.
40 
41         @author
42         <a href="mailto:cmc@openoffice.org">Caol&aacute;n McNamara</a>
43     */
44     template<class C> class SortedArray
45     {
46     private:
47         //The array e.g. of sprms.
48         C *mpWwSprmTab;
49         size_t mnNoElems;
50 
51         //No copying
52         SortedArray(const SortedArray&);
53         SortedArray& operator=(const SortedArray&);
54     public:
55         //Find an entry, return its address if found and 0 if not
search(C aSrch) const56         const C *search(C aSrch) const
57         {
58             std::pair<C *, C *> aPair =
59                 std::equal_range(mpWwSprmTab, mpWwSprmTab + mnNoElems, aSrch);
60             if (aPair.first != aPair.second)
61                 return aPair.first;
62             else
63                 return 0;
64         }
65 
SortedArray(C * pWwSprmTab,size_t nNoElems)66         SortedArray(C *pWwSprmTab, size_t nNoElems)
67             : mpWwSprmTab(pWwSprmTab), mnNoElems(nNoElems)
68         {
69             ASSERT(mnNoElems && pWwSprmTab, "WW8: empty Array: Don't do that");
70             std::sort(mpWwSprmTab, mpWwSprmTab + mnNoElems);
71 #if OSL_DEBUG_LEVEL > 1
72             bool bBroken=false;
73             rtl::OUString sError;
74             const C *pIter = mpWwSprmTab;
75             const C *pBeforeEnd = mpWwSprmTab + mnNoElems - 1;
76             while (pIter < pBeforeEnd)
77             {
78                 if (*pIter == *(pIter+1))
79                 {
80                     if (!bBroken)
81                     {
82                         sError = rtl::OUString::createFromAscii(
83                             "WW8: Duplicate in list, almost certainly don't "
84                             "want that!\n"
85                             "(You will not see this message again unless you "
86                             "restart)\n"
87                             "Extra entries are...\n");
88                         bBroken=true;
89                     }
90 
91                     size_t nSize = sizeof(C);
92                     const sal_uInt8 *pHack =
93                         reinterpret_cast<const sal_uInt8 *>(&(*pIter));
94                     for (size_t i=0; i < nSize; ++i)
95                     {
96                         sError += rtl::OUString::valueOf(
97                             static_cast<sal_Int32>(pHack[i]), 16);
98                         sError += rtl::OUString::valueOf(sal_Unicode(' '));
99                     }
100                     sError += rtl::OUString::valueOf(sal_Unicode('\n'));
101                     while (*pIter == *(pIter+1) && pIter < pBeforeEnd)
102                         ++pIter;
103                 }
104                 else
105                     ++pIter;
106             }
107             if (bBroken)
108             {
109                DBG_ERROR(rtl::OUStringToOString(sError, RTL_TEXTENCODING_ASCII_US));
110             }
111 #endif
112         }
113     };
114 }
115 #endif
116 
117 /* vi:set tabstop=4 shiftwidth=4 expandtab: */
118