xref: /aoo4110/main/tools/inc/tools/unqidx.hxx (revision b1cdbd2c)
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 #ifndef _UNQIDX_HXX
24 #define _UNQIDX_HXX
25 
26 #include "tools/toolsdllapi.h"
27 #include <tools/solar.h>
28 #include <tools/contnr.hxx>
29 
30 // ---------------
31 // - UniqueIndex -
32 // ---------------
33 
34 #define UNIQUEINDEX_ENTRY_NOTFOUND   CONTAINER_ENTRY_NOTFOUND
35 
36 class TOOLS_DLLPUBLIC UniqueIndex : private Container
37 {
38 private:
39     sal_uIntPtr           nReSize;
40     sal_uIntPtr           nStartIndex;
41     sal_uIntPtr           nUniqIndex;
42     sal_uIntPtr           nCount;
43 
44 public:
45     				using Container::GetCurObject;
46 
47                     UniqueIndex( sal_uIntPtr nStartIndex = 0,
48                                  sal_uIntPtr nInitSize = 16,
49                                  sal_uIntPtr nReSize = 16 );
50                     UniqueIndex( const UniqueIndex& rIdx );
51 
52     sal_uIntPtr           Insert( sal_uIntPtr nIndex, void* p );
53     sal_uIntPtr           Insert( void* p );
54     void*           Remove( sal_uIntPtr nIndex );
55     void*           Replace( sal_uIntPtr nIndex, void* p );
56     void*           Get( sal_uIntPtr nIndex ) const;
57 
58     void            Clear();
Count() const59     sal_uIntPtr           Count() const { return nCount; }
60 
61     sal_uIntPtr           GetCurIndex() const;
62     sal_uIntPtr           GetIndex( const void* p ) const;
63     sal_Bool            IsIndexValid( sal_uIntPtr nIndex ) const;
64 
65     void*           Seek( sal_uIntPtr nIndex );
66     void*           Seek( void* p );
67     void*           First();
68     void*           Last();
69     void*           Next();
70     void*           Prev();
71 
GetStartIndex() const72     sal_uIntPtr           GetStartIndex() const { return nStartIndex; }
GetCurMaxIndex() const73     sal_uIntPtr           GetCurMaxIndex() const
74                         { return (nStartIndex + Container::GetSize()); }
75 
76     UniqueIndex&    operator =( const UniqueIndex& rIdx );
77 
78     sal_Bool            operator ==( const UniqueIndex& rIdx ) const;
operator !=(const UniqueIndex & rIdx) const79     sal_Bool            operator !=( const UniqueIndex& rIdx ) const
80                         { return !(UniqueIndex::operator==( rIdx )); }
81 };
82 
Clear()83 inline void UniqueIndex::Clear()
84 {
85     Container::Clear();
86     nCount     = 0;
87     nUniqIndex = 0;
88 }
89 
90 // -----------------------
91 // - DECLARE_UNIQUEINDEX -
92 // -----------------------
93 
94 #define DECLARE_UNIQUEINDEX( ClassName, Type )                          \
95 class ClassName : private UniqueIndex                                   \
96 {                                                                       \
97 public:                                                                 \
98     			using UniqueIndex::Clear;										\
99                 using UniqueIndex::Count;                                       \
100                 using UniqueIndex::GetCurIndex;                             \
101                 using UniqueIndex::IsIndexValid;                                \
102                 using UniqueIndex::GetStartIndex;                               \
103                 using UniqueIndex::GetCurMaxIndex;                          \
104                                                                         \
105                 ClassName( sal_uIntPtr _nStartIndex = 0,                      \
106                            sal_uIntPtr _nInitSize = 16, sal_uIntPtr _nReSize = 16 ):\
107                     UniqueIndex( _nStartIndex, _nInitSize, _nReSize ) {}\
108                 ClassName( const ClassName& rClassName ) :              \
109                     UniqueIndex( rClassName ) {}                        \
110                                                                         \
111     sal_uIntPtr       Insert( sal_uIntPtr nIndex, Type p )                          \
112                     { return UniqueIndex::Insert( nIndex, (void*)p ); } \
113     sal_uIntPtr       Insert( Type p )                                        \
114                     { return UniqueIndex::Insert( (void*)p ); }         \
115     Type        Remove( sal_uIntPtr nIndex )                                  \
116                     { return (Type)UniqueIndex::Remove( nIndex ); }     \
117     Type        Replace( sal_uIntPtr nIndex, Type p )                         \
118                     { return (Type)UniqueIndex::Replace( nIndex,        \
119                                                          (void*)p ); }  \
120     Type        Get( sal_uIntPtr nIndex ) const                               \
121                     { return (Type)UniqueIndex::Get( nIndex ); }        \
122                                                                         \
123     Type        GetCurObject() const                                    \
124                     { return (Type)UniqueIndex::GetCurObject(); }       \
125     sal_uIntPtr       GetIndex( const Type p ) const                          \
126                     { return UniqueIndex::GetIndex( (const void*)p ); } \
127                                                                         \
128     Type        Seek( sal_uIntPtr nKey )                                      \
129                     { return (Type)UniqueIndex::Seek( nKey ); }         \
130     Type        Seek( Type p )                                          \
131                     { return (Type)UniqueIndex::Seek( (void*)p ); }     \
132     Type        First()  { return (Type)UniqueIndex::First(); }         \
133     Type        Last()   { return (Type)UniqueIndex::Last(); }          \
134     Type        Next()   { return (Type)UniqueIndex::Next(); }          \
135     Type        Prev()   { return (Type)UniqueIndex::Prev(); }          \
136                                                                         \
137     ClassName&  operator =( const ClassName& rClassName )               \
138                     { UniqueIndex::operator =( rClassName );            \
139                       return *this; }                                   \
140                                                                         \
141     sal_Bool        operator ==( const ClassName& rIdx ) const              \
142                     { return UniqueIndex::operator ==( rIdx ); }        \
143     sal_Bool        operator !=( const ClassName& rIdx ) const              \
144                     { return UniqueIndex::operator !=( rIdx ); }        \
145 };
146 
147 #endif // _UNQIDX_HXX
148