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 _BOOTSTRP_GENINFO_HXX
24 #define _BOOTSTRP_GENINFO_HXX
25
26 #include "tools/toolsdllapi.h"
27
28 #include <tools/string.hxx>
29 #include <tools/list.hxx>
30
31 // forwards
32 class GenericInformationList;
33
34 //
35 // class GenericInformation
36 //
37
38 /******************************************************************************
39 Purpose: holds generic informations and subinformations in a simple format
40 ******************************************************************************/
41
42 class TOOLS_DLLPUBLIC GenericInformation : public ByteString // the key is stored in base class
43 {
44 friend class GenericInformationList; // can be child or/and parent
45 private:
46 ByteString sValue; // holds value of data
47 ByteString sComment;
48
49 GenericInformationList *pInfoList; // holds subinformations
50 GenericInformationList *pParent; // holds a pointer to parent list
51
52 // methods
ListDeleted()53 void ListDeleted() { pParent = NULL; } // allowed to be accessed
54 // from friend class
55 // GenericInformationList
56
57 public:
58 GenericInformation( const ByteString &rKey, const ByteString &rValue,
59 GenericInformationList *pParentList = NULL,
60 GenericInformationList *pSubInfos = NULL );
61 GenericInformation( const GenericInformation& rInf, sal_Bool bCopySubs = sal_True);
62
63 ~GenericInformation();
64
GetValue()65 ByteString &GetValue() { return sValue; }
SetValue(const ByteString & rValue)66 void SetValue( const ByteString &rValue ) { sValue = rValue; }
67
GetComment()68 ByteString &GetComment() { return sComment; }
SetComment(const ByteString & rComment)69 void SetComment( const ByteString &rComment ) { sComment = rComment; }
70
71 // this methods used to handle sub informations
72 sal_Bool InsertSubInfo( GenericInformation *pInfo );
73 // siehe GenericInformationList
74 sal_Bool InsertSubInfo( const ByteString &rPathKey, const ByteString &rValue,
75 sal_Bool bSearchByPath = sal_False, sal_Bool bNewPath = sal_False);
76 void RemoveSubInfo( GenericInformation *pInfo, sal_Bool bDelete = sal_False );
77 // void RemoveSelf( sal_Bool bDelete = sal_False ); // loescht sich selbst aus der Parentliste
78 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
79 // bei bDelete = sal_True werden auch alle Sublisten UND DIE INFO SELBST geloescht.
80 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
81
82 GenericInformation *GetSubInfo( ByteString &rKey, sal_Bool bSearchByPath = sal_False,
83 sal_Bool bCreatePath = sal_False );
84 // path can be something like this: src370/drives/o:
85
SetSubList(GenericInformationList * pSubList)86 void SetSubList( GenericInformationList *pSubList )
87 { pInfoList = pSubList; }
GetSubList()88 GenericInformationList *GetSubList() { return pInfoList; }
89 };
90
91 /* diese Klasse bietet einen SemaphoreLock zum lesen (und nicht Schreiben)
92 * oder schreiben (und nicht lesen)
93 */
94 class GenericLockInformation : public GenericInformation
95 {
96 public:
GenericLockInformation(const ByteString & rKey,const ByteString & rValue,GenericInformationList * pParentList=NULL,GenericInformationList * pSubInfos=NULL)97 GenericLockInformation( const ByteString &rKey, const ByteString &rValue,
98 GenericInformationList *pParentList = NULL,
99 GenericInformationList *pSubInfos = NULL )
100 : GenericInformation( rKey, rValue, pParentList, pSubInfos),
101 aLockState( read ), nLockKey( 0 ) {};
102 //~GenericLockInformation();
103
104 /* bietet einen Lockmechanismus fuer exclusive Zugriffe
105 *
106 * -"writeonly" wird angesprochen, wenn von der Wurzel ausgehend
107 * ein Item veraendert werden soll. In der Zeit kann die Liste nicht
108 * gelesen werden, womit keine Inconsistenzen entstehen koennen.
109 *
110 * -"read" wird zum Normalen lesen der Infos benutzt, 90% der Betriebszeit.
111 * waerenddessen kann nicht geschrieben werden -> writeonly Lock.
112 * Ist fuer den atomaren(nicht unterbrochenen) Lesezugriff gedacht
113 *
114 * -"readonly" wird zum niederschreiben des Teilbaums benutzt, was schon mal
115 * 10 Minuten dauern kann. In der Zeit kann kein Writeonlylock gesetzt
116 * werden, aber ein rescedule. Damit koennen andere Aktionen asynchron ausgefuert
117 * werden, aber die Datensicherheit bleibt gewahrt
118 *
119 * Zustandsaenderung: writeonly <-> read <-> readonly
120 *
121 * nLockKey ist zum verschluesseln des LockZugriffs mit einem 32bit Wort vorgesehen.
122 * ist der Schluessel nicht null, so kann nur mit dem Schluessel in
123 * die Baumstruktur geschrieben werden.
124 * ist der Schluessel nLockKey Null, dann kann jeder Schreiben und die Locks loesen
125 */
126 enum LockState{ writeonly, read, readonly };
127
128 /* der Schreibschutz darf nur aktiviert werden, wenn
129 * der Status auf Lesen steht
130 */
SetWriteLock(sal_uInt32 nKey=0)131 sal_Bool SetWriteLock(sal_uInt32 nKey = 0) { return ((read==aLockState) &&
132 (aLockState=writeonly, nLockKey=nKey, sal_True)); }
133 /* Schreibschutz darf nur geloest werden, wenn
134 * der Schreibschutz drin ist, und
135 * entweder der LockKey Null ist(Generalschluessel) oder der Key zum LockKey passt
136 */
ReleaseWriteLock(sal_uInt32 nKey=0)137 sal_Bool ReleaseWriteLock(sal_uInt32 nKey = 0) { return ((writeonly==aLockState) &&
138 (!nLockKey||nKey==nLockKey) &&
139 (aLockState=read, nLockKey=0, sal_True)); } // setzt den zustand auf "read"
SetReadLock(sal_uInt32 nKey=0)140 sal_Bool SetReadLock(sal_uInt32 nKey = 0) { return ((read==aLockState) &&
141 (aLockState=readonly, nLockKey=nKey, sal_True)); }
ReleaseReadLock(sal_uInt32 nKey=0)142 sal_Bool ReleaseReadLock(sal_uInt32 nKey = 0) { return ((readonly==aLockState) &&
143 (!nLockKey||nKey==nLockKey) &&
144 (aLockState=read, nLockKey=0, sal_True)); } // setzt den zustand auf "read"
145
GetLockState() const146 LockState GetLockState() const { return aLockState; }
IsWriteLocked() const147 sal_Bool IsWriteLocked() const { return (writeonly==aLockState); }
IsReadLocked() const148 sal_Bool IsReadLocked() const { return (readonly==aLockState); }
IsNotLocked() const149 sal_Bool IsNotLocked() const { return (read==aLockState); }
IsLocker(sal_uInt32 nKey)150 sal_Bool IsLocker( sal_uInt32 nKey ) { return (nKey==nLockKey || !nLockKey); }
151
152 /* wenn der Schreibschutz aktiviert wurde,
153 * und bei vorhandenem Schreibschutz die Keys stimmen
154 * rufe die Parentmethode auf */
InsertSubInfo(GenericInformation * pInfo,sal_uInt32 nKey=0)155 sal_Bool InsertSubInfo( GenericInformation *pInfo, sal_uInt32 nKey = 0 ) {
156 return ((writeonly==aLockState) &&
157 (!nLockKey || nKey==nLockKey) &&
158 (GenericInformation::InsertSubInfo( pInfo ), sal_True)); }
159
InsertSubInfo(const ByteString & rPathKey,const ByteString & rValue,sal_uInt32 nKey=0,sal_Bool bSearchByPath=sal_False,sal_Bool bNewPath=sal_False)160 sal_Bool InsertSubInfo( const ByteString &rPathKey, const ByteString &rValue, sal_uInt32 nKey = 0,
161 sal_Bool bSearchByPath = sal_False, sal_Bool bNewPath = sal_False) {
162 return ((writeonly==aLockState) &&
163 (!nLockKey || nKey==nLockKey) &&
164 (GenericInformation::InsertSubInfo( rPathKey, rValue, bSearchByPath, bNewPath ), sal_True)); }
165 /* 29.jan.98: erweiterung um lesemoeglichkeit vom Lockclienten */
GetSubInfo(ByteString & rKey,sal_Bool bSearchByPath=sal_False,sal_Bool bCreatePath=sal_False,sal_uInt32 nKey=0)166 GenericInformation *GetSubInfo( ByteString &rKey, sal_Bool bSearchByPath = sal_False,
167 sal_Bool bCreatePath = sal_False, sal_uInt32 nKey = 0 ) {
168 if (writeonly==aLockState && nLockKey && nKey!=nLockKey )
169 return NULL;
170 return GenericInformation::GetSubInfo(rKey, bSearchByPath, bCreatePath); }
171
172 // TYPEINFO();
173 private:
174
175 LockState aLockState;
176 sal_uInt32 nLockKey;
177 };
178
179 //
180 // class GenericInformationList
181 //
182
183 /******************************************************************************
184 Purpose: holds set of generic informations in a sorted list
185 ******************************************************************************/
186
187 DECLARE_LIST( GenericInformationList_Impl, GenericInformation * )
188
189 class TOOLS_DLLPUBLIC GenericInformationList : public GenericInformationList_Impl
190 {
191 private:
192 GenericInformation *pOwner; // holds parent of this list
193
194 protected:
195 // methods
196 sal_uIntPtr InsertSorted( GenericInformation *pInfo, sal_Bool bOverwrite,
197 sal_uIntPtr nStart, sal_uIntPtr nEnd );
198 GenericInformation *Search( sal_uIntPtr &rPos, ByteString sKey,
199 sal_uIntPtr nStart, sal_uIntPtr nEnd );
200
201 public:
202 GenericInformationList( GenericInformation *pParent = NULL );
203 GenericInformationList(const GenericInformationList& rList, GenericInformation *pParent = NULL);
204 ~GenericInformationList();
205
206 // this methods used to handle the informations using binary search
207 GenericInformation *GetInfo( ByteString &rKey, sal_Bool bSearchByPath = sal_False,
208 sal_Bool bCreatePath = sal_False );
209 /* path can be something like this: src370/drives/o:
210 * bCreatePath will create the neccecary paths to the GI */
211
212 sal_Bool InsertInfo( GenericInformation *pInfo, sal_Bool bOverwrite = sal_True );
213 /* legt eine GenericInformation im Baum an mit Key-Value
214 * wenn bNewPath gesetzt, wird der nichtexistente Teil des Pfades neu kreiert
215 * wenn bNewPath nicht gesetzt ist und ein Teil des Pfades nicht vorhanden ist,
216 * gibt die Methode sal_False zurueck.*/
217 sal_Bool InsertInfo( const ByteString &rPathKey, const ByteString &rValue,
218 sal_Bool bSearchByPath = sal_False, sal_Bool bNewPath = sal_False);
219 void RemoveInfo( GenericInformation *pInfo, sal_Bool bDelete = sal_False );
220
221 GenericInformation* SetOwner( GenericInformation *pNewOwner );
222
223 };
224
225 #endif
226
227