xref: /aoo4110/main/tools/inc/tools/unqid.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 _UNQID_HXX
24 #define _UNQID_HXX
25 
26 #include "tools/toolsdllapi.h"
27 #include <tools/unqidx.hxx>
28 
29 // ---------------
30 // - ImpUniqueId -
31 // ---------------
32 
33 struct ImpUniqueId
34 {
35     sal_uIntPtr    nId;
36     sal_uInt16   nRefCount;
ReleaseImpUniqueId37     void     Release()
38              {
39                 nRefCount--;
40                 if( 0 == nRefCount )
41                     delete this;
42              }
43 };
44 
45 // ------------
46 // - UniqueId -
47 // ------------
48 
49 class UniqueIdContainer;
50 class UniqueItemId
51 {
52     friend class UniqueIdContainer;
53     ImpUniqueId*    pId;
54 
UniqueItemId(ImpUniqueId * pIdP)55                     UniqueItemId( ImpUniqueId * pIdP )
56                         { pId = pIdP; pId->nRefCount++; }
57 public:
UniqueItemId()58                     UniqueItemId() { pId = NULL; }
UniqueItemId(const UniqueItemId & rId)59                     UniqueItemId( const UniqueItemId & rId )
60                         { pId = rId.pId; if( pId ) pId->nRefCount++; }
~UniqueItemId()61                     ~UniqueItemId()
62                         { if( pId ) pId->Release(); }
operator =(const UniqueItemId & rId)63     UniqueItemId&   operator = ( const UniqueItemId & rId )
64                         {
65                             if( rId.pId ) rId.pId->nRefCount++;
66                             if( pId ) pId->Release();
67                             pId = rId.pId;
68                             return *this;
69                         }
GetId() const70     sal_uIntPtr           GetId() const { return pId ? pId->nId : 0; }
71 };
72 
73 // ---------------------
74 // - UniqueIdContainer -
75 // ---------------------
76 
77 class TOOLS_DLLPUBLIC UniqueIdContainer : private UniqueIndex
78 {
79     sal_uInt16              nCollectCount;
80 
81 public: // Irgend etwas mit protected falsch
82     void                Clear( sal_Bool bAll );
83     UniqueItemId        CreateIdProt( sal_uIntPtr nId );
84 
85 public:
UniqueIdContainer(sal_uIntPtr _nStartIndex,sal_uIntPtr _nInitSize=16,sal_uIntPtr _nReSize=16)86                         UniqueIdContainer( sal_uIntPtr _nStartIndex,
87                                            sal_uIntPtr _nInitSize = 16,
88                                            sal_uIntPtr _nReSize = 16 )
89 							: UniqueIndex( _nStartIndex, _nInitSize, _nReSize )
90 							, nCollectCount( 0 )
91 							{}
92                         UniqueIdContainer( const UniqueIdContainer& );
93 
~UniqueIdContainer()94                         ~UniqueIdContainer()
95                             { Clear( sal_True ); }
96     UniqueIdContainer&  operator = ( const UniqueIdContainer & );
97 
IsIndexValid(sal_uIntPtr nIndex) const98     sal_Bool                IsIndexValid( sal_uIntPtr nIndex ) const
99                             { return UniqueIndex::IsIndexValid( nIndex ); }
100 
101     UniqueItemId        CreateId();
102     static UniqueItemId CreateFreeId( sal_uIntPtr nId ); // freies Id
103 };
104 
105 #endif // _UNQID_HXX
106