merge the formfield patch from ooo-build
[ooovba.git] / binfilter / inc / bf_svtools / cntnrsrt.hxx
blob9747c38bca6c1a03a3a5184bcc50323a9607eb3c
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.3 $
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 #ifndef _CONTNR_HXX //autogen
34 #include <tools/contnr.hxx>
35 #endif
37 namespace binfilter {
39 #define DECLARE_CONTAINER_SORT_COMMON( ClassName, Type ) \
40 ClassName( const ClassName& ); \
41 ClassName& operator =( const ClassName& ); \
42 public: \
43 using Container::Count; \
45 ClassName( USHORT InitSize, USHORT ReSize ) : \
46 Container( CONTAINER_MAXBLOCKSIZE, InitSize, ReSize ) {} \
48 BOOL Insert( Type* pObj ); \
50 Type *Remove( ULONG nPos ) \
51 { return (Type *)Container::Remove( nPos ); } \
53 Type *Remove( Type* pObj ); \
55 void DeleteAndDestroy( ULONG nPos ) \
56 { \
57 Type *pObj = Remove( nPos ); \
58 if( pObj ) \
59 delete pObj; \
60 } \
62 void DeleteAndDestroy() \
63 { while( Count() ) DeleteAndDestroy( 0 ); } \
65 Type* GetObject( ULONG nPos ) const \
66 { return (Type *)Container::GetObject( nPos ); } \
68 Type* operator[]( ULONG nPos ) const \
69 { return GetObject(nPos); } \
71 BOOL Seek_Entry( const Type *pObj, ULONG* pPos ) const; \
73 ULONG GetPos( const Type* pObj ) const; \
76 #define DECLARE_CONTAINER_SORT( ClassName, Type ) \
77 class ClassName : private Container \
78 { \
79 DECLARE_CONTAINER_SORT_COMMON( ClassName, Type ) \
80 ~ClassName() {} \
81 }; \
84 #define DECLARE_CONTAINER_SORT_DEL( ClassName, Type ) \
85 class ClassName : private Container \
86 { \
87 DECLARE_CONTAINER_SORT_COMMON( ClassName, Type ) \
88 ~ClassName() { DeleteAndDestroy(); } \
89 }; \
92 #define IMPL_CONTAINER_SORT( ClassName, Type, SortFunc ) \
93 BOOL ClassName::Insert( Type *pObj ) \
94 { \
95 ULONG nPos; \
96 BOOL bExist = Seek_Entry( pObj, &nPos ); \
97 if( !bExist ) \
98 Container::Insert( pObj, nPos ); \
99 return !bExist; \
102 Type *ClassName::Remove( Type* pObj ) \
104 ULONG nPos; \
105 if( Seek_Entry( pObj, &nPos ) ) \
106 return Remove( nPos ); \
107 else \
108 return 0; \
111 ULONG ClassName::GetPos( const Type* pObj ) const \
113 ULONG nPos; \
114 if( Seek_Entry( pObj, &nPos ) ) \
115 return nPos; \
116 else \
117 return CONTAINER_ENTRY_NOTFOUND; \
120 BOOL ClassName::Seek_Entry( const Type* pObj, ULONG* pPos ) const \
122 register ULONG nO = Count(), \
123 nM, \
124 nU = 0; \
125 if( nO > 0 ) \
127 nO--; \
128 while( nU <= nO ) \
130 nM = nU + ( nO - nU ) / 2; \
131 int nCmp = SortFunc( *GetObject(nM), *pObj ); \
133 if( 0 == nCmp ) \
135 if( pPos ) *pPos = nM; \
136 return TRUE; \
138 else if( nCmp < 0 ) \
139 nU = nM + 1; \
140 else if( nM == 0 ) \
142 if( pPos ) *pPos = nU; \
143 return FALSE; \
145 else \
146 nO = nM - 1; \
149 if( pPos ) *pPos = nU; \
150 return FALSE; \
155 #endif