update dev300-m57
[ooovba.git] / svtools / inc / cntnrsrt.hxx
blob190673cd13ce2991a0a3c3a364b2d3a39168182e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: cntnrsrt.hxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #ifndef _CNTRSRT_HXX
31 #define _CNTRSRT_HXX
33 #if 0
34 ***********************************************************************
36 * Hier folgt die Beschreibung fuer die exportierten Makros:
38 * DECLARE_CONTAINER_SORT( ClassName, Type )
39 * IMPL_CONTAINER_SORT( ClassName, Type, SortFunc )
41 * Definiert eine von Container abgeleitete Klasse "ClassName",
42 * in der die Elemente des Typs "Type" sortiert enthalten sind.
43 * Dazu muss einer Funktion "SortFunc" definiert sein, die als
44 * Paramter zwei "const Type&" erwartet und 0 zurueckgibt, wenn
45 * beide gleich sind, -1 wenn der erste Paramter kleiner ist als
46 * der zweite und +1 wenn der erste Paramter groesser ist als
47 * der zweite.
49 * Die Zugriffs-Methoden entsprechen in etwa denen der Container-
50 * Klasse, mit Ausnahme von Insert, DeleteAndDestroy und Seek_Entry,
51 * der den SV-Pointer-Arrays entsprechen.
53 * DECLARE_CONTAINER_SORT_DEL( ClassName, Type )
54 * IMPL_CONTAINER_SORT( ClassName, Type, SortFunc )
56 * Wie DECLARE_CONTAINER_SORT, nur dass beim Aufruf des Destruktors
57 * alle im Conatiner vorhandenen Objekte geloescht werden.
59 #endif
61 #include <tools/contnr.hxx>
63 #define DECLARE_CONTAINER_SORT_COMMON( ClassName, Type ) \
64 ClassName( const ClassName& ); \
65 ClassName& operator =( const ClassName& ); \
66 public: \
67 using Container::Count; \
69 ClassName( USHORT InitSize, USHORT ReSize ) : \
70 Container( CONTAINER_MAXBLOCKSIZE, InitSize, ReSize ) {} \
72 BOOL Insert( Type* pObj ); \
74 Type *Remove( ULONG nPos ) \
75 { return (Type *)Container::Remove( nPos ); } \
77 Type *Remove( Type* pObj ); \
79 void DeleteAndDestroy( ULONG nPos ) \
80 { \
81 Type *pObj = Remove( nPos ); \
82 if( pObj ) \
83 delete pObj; \
84 } \
86 void DeleteAndDestroy() \
87 { while( Count() ) DeleteAndDestroy( 0 ); } \
89 Type* GetObject( ULONG nPos ) const \
90 { return (Type *)Container::GetObject( nPos ); } \
92 Type* operator[]( ULONG nPos ) const \
93 { return GetObject(nPos); } \
95 BOOL Seek_Entry( const Type *pObj, ULONG* pPos ) const; \
97 ULONG GetPos( const Type* pObj ) const; \
100 #define DECLARE_CONTAINER_SORT( ClassName, Type ) \
101 class ClassName : private Container \
103 DECLARE_CONTAINER_SORT_COMMON( ClassName, Type ) \
104 ~ClassName() {} \
105 }; \
108 #define DECLARE_CONTAINER_SORT_DEL( ClassName, Type ) \
109 class ClassName : private Container \
111 DECLARE_CONTAINER_SORT_COMMON( ClassName, Type ) \
112 ~ClassName() { DeleteAndDestroy(); } \
113 }; \
116 #define IMPL_CONTAINER_SORT( ClassName, Type, SortFunc ) \
117 BOOL ClassName::Insert( Type *pObj ) \
119 ULONG nPos; \
120 BOOL bExist = Seek_Entry( pObj, &nPos ); \
121 if( !bExist ) \
122 Container::Insert( pObj, nPos ); \
123 return !bExist; \
126 Type *ClassName::Remove( Type* pObj ) \
128 ULONG nPos; \
129 if( Seek_Entry( pObj, &nPos ) ) \
130 return Remove( nPos ); \
131 else \
132 return 0; \
135 ULONG ClassName::GetPos( const Type* pObj ) const \
137 ULONG nPos; \
138 if( Seek_Entry( pObj, &nPos ) ) \
139 return nPos; \
140 else \
141 return CONTAINER_ENTRY_NOTFOUND; \
144 BOOL ClassName::Seek_Entry( const Type* pObj, ULONG* pPos ) const \
146 register ULONG nO = Count(), \
147 nM, \
148 nU = 0; \
149 if( nO > 0 ) \
151 nO--; \
152 while( nU <= nO ) \
154 nM = nU + ( nO - nU ) / 2; \
155 int nCmp = SortFunc( *GetObject(nM), *pObj ); \
157 if( 0 == nCmp ) \
159 if( pPos ) *pPos = nM; \
160 return TRUE; \
162 else if( nCmp < 0 ) \
163 nU = nM + 1; \
164 else if( nM == 0 ) \
166 if( pPos ) *pPos = nU; \
167 return FALSE; \
169 else \
170 nO = nM - 1; \
173 if( pPos ) *pPos = nU; \
174 return FALSE; \
177 #endif