xref: /trunk/main/unotools/source/accessibility/accessiblestatesethelper.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_unotools.hxx"
26 
27 
28 #include "unotools/accessiblestatesethelper.hxx"
29 #include <rtl/uuid.h>
30 #include <tools/debug.hxx>
31 
32 #if 0
33 #include <bitset>
34 #endif
35 
36 // defines how many states the bitfield can contain
37 // it has the size of 64 because I use a uInt64
38 #define BITFIELDSIZE 64
39 
40 using namespace ::utl;
41 using namespace ::rtl;
42 using namespace ::com::sun::star;
43 using namespace ::com::sun::star::accessibility;
44 
45 class AccessibleStateSetHelperImpl
46 {
47 public:
48     AccessibleStateSetHelperImpl();
49     AccessibleStateSetHelperImpl(const AccessibleStateSetHelperImpl& rImpl);
50     ~AccessibleStateSetHelperImpl();
51 
52     sal_Bool IsEmpty ();
53     sal_Bool Contains (sal_Int16 aState);
54     uno::Sequence<sal_Int16> GetStates();
55     void AddState(sal_Int16 aState);
56     void RemoveState(sal_Int16 aState);
57     sal_Bool Compare(const AccessibleStateSetHelperImpl* pComparativeValue,
58                         AccessibleStateSetHelperImpl* pOldStates,
59                         AccessibleStateSetHelperImpl* pNewStates);
60 
61     inline void AddStates( const sal_Int64 _nStates ) SAL_THROW( ( ) );
62 
63 private:
64 #if 0
65     ::std::bitset<BITFIELDSIZE> maStates; //Bitfield
66 #endif
67     sal_uInt64 maStates;
68 };
69 
AccessibleStateSetHelperImpl()70 AccessibleStateSetHelperImpl::AccessibleStateSetHelperImpl()
71     : maStates(0)
72 {
73 }
74 
AccessibleStateSetHelperImpl(const AccessibleStateSetHelperImpl & rImpl)75 AccessibleStateSetHelperImpl::AccessibleStateSetHelperImpl(const AccessibleStateSetHelperImpl& rImpl)
76     : maStates(rImpl.maStates)
77 {
78 }
79 
~AccessibleStateSetHelperImpl()80 AccessibleStateSetHelperImpl::~AccessibleStateSetHelperImpl()
81 {
82 }
83 
IsEmpty()84 inline sal_Bool AccessibleStateSetHelperImpl::IsEmpty ()
85 {
86 #if 0
87     return maStates.none();
88 #endif
89     return maStates == 0;
90 }
91 
Contains(sal_Int16 aState)92 inline sal_Bool AccessibleStateSetHelperImpl::Contains (sal_Int16 aState)
93 {
94     DBG_ASSERT(aState < BITFIELDSIZE, "the statesset is too small");
95 #if 0
96     return maStates.test(aState);
97 #endif
98     sal_uInt64 aTempBitSet(1);
99     aTempBitSet <<= aState;
100     return ((aTempBitSet & maStates) != 0);
101 }
102 
GetStates()103 inline uno::Sequence<sal_Int16> AccessibleStateSetHelperImpl::GetStates()
104 {
105     uno::Sequence<sal_Int16> aRet(BITFIELDSIZE);
106     sal_Int16* pSeq = aRet.getArray();
107     sal_Int16 nStateCount(0);
108     for (sal_Int16 i = 0; i < BITFIELDSIZE; ++i)
109         if (Contains(i))
110         {
111             *pSeq = i;
112             ++pSeq;
113             ++nStateCount;
114         }
115     aRet.realloc(nStateCount);
116     return aRet;
117 }
118 
AddStates(const sal_Int64 _nStates)119 inline void AccessibleStateSetHelperImpl::AddStates( const sal_Int64 _nStates ) SAL_THROW( ( ) )
120 {
121     maStates |= _nStates;
122 }
123 
AddState(sal_Int16 aState)124 inline void AccessibleStateSetHelperImpl::AddState(sal_Int16 aState)
125 {
126     DBG_ASSERT(aState < BITFIELDSIZE, "the statesset is too small");
127 #if 0
128     maStates.set(aState);
129 #endif
130     sal_uInt64 aTempBitSet(1);
131     aTempBitSet <<= aState;
132     maStates |= aTempBitSet;
133 }
134 
RemoveState(sal_Int16 aState)135 inline void AccessibleStateSetHelperImpl::RemoveState(sal_Int16 aState)
136 {
137     DBG_ASSERT(aState < BITFIELDSIZE, "the statesset is too small");
138 #if 0
139     maStates.set(aState, 0);
140 #endif
141     sal_uInt64 aTempBitSet(1);
142     aTempBitSet <<= aState;
143     aTempBitSet = ~aTempBitSet;
144     maStates &= aTempBitSet;
145 }
146 
Compare(const AccessibleStateSetHelperImpl * pComparativeValue,AccessibleStateSetHelperImpl * pOldStates,AccessibleStateSetHelperImpl * pNewStates)147 inline sal_Bool AccessibleStateSetHelperImpl::Compare(
148     const AccessibleStateSetHelperImpl* pComparativeValue,
149         AccessibleStateSetHelperImpl* pOldStates,
150         AccessibleStateSetHelperImpl* pNewStates)
151 {
152     sal_Bool bResult(sal_False);
153     if (pComparativeValue && pOldStates && pNewStates)
154     {
155         if (maStates == pComparativeValue->maStates)
156             bResult = sal_True;
157         else
158         {
159 #if 0
160             std::bitset<BITFIELDSIZE> aTempBitSet(maStates);
161 #endif
162             sal_uInt64 aTempBitSet(maStates);
163             aTempBitSet ^= pComparativeValue->maStates;
164             pOldStates->maStates = aTempBitSet;
165             pOldStates->maStates &= maStates;
166             pNewStates->maStates = aTempBitSet;
167             pNewStates->maStates &= pComparativeValue->maStates;
168         }
169     }
170     return bResult;
171 }
172 
173 
174 //=====  internal  ============================================================
175 
AccessibleStateSetHelper()176 AccessibleStateSetHelper::AccessibleStateSetHelper ()
177     : mpHelperImpl(NULL)
178 {
179     mpHelperImpl = new AccessibleStateSetHelperImpl();
180 }
181 
AccessibleStateSetHelper(const sal_Int64 _nInitialStates)182 AccessibleStateSetHelper::AccessibleStateSetHelper ( const sal_Int64 _nInitialStates )
183     : mpHelperImpl(NULL)
184 {
185     mpHelperImpl = new AccessibleStateSetHelperImpl();
186     mpHelperImpl->AddStates( _nInitialStates );
187 }
188 
AccessibleStateSetHelper(const AccessibleStateSetHelper & rHelper)189 AccessibleStateSetHelper::AccessibleStateSetHelper (const AccessibleStateSetHelper& rHelper)
190     : cppu::WeakImplHelper1<XAccessibleStateSet>()
191     , mpHelperImpl(NULL)
192 {
193     if (rHelper.mpHelperImpl)
194         mpHelperImpl = new AccessibleStateSetHelperImpl(*rHelper.mpHelperImpl);
195     else
196         mpHelperImpl = new AccessibleStateSetHelperImpl();
197 }
198 
~AccessibleStateSetHelper(void)199 AccessibleStateSetHelper::~AccessibleStateSetHelper(void)
200 {
201     delete mpHelperImpl;
202 }
203 
204 //=====  XAccessibleStateSet  ==============================================
205 
206     /** Checks whether the current state set is empty.
207 
208         @return
209             Returns <TRUE/> if there is no state in this state set and
210             <FALSE/> if there is at least one state set in it.
211     */
isEmpty()212 sal_Bool SAL_CALL AccessibleStateSetHelper::isEmpty ()
213 {
214     ::vos::OGuard aGuard (maMutex);
215     return mpHelperImpl->IsEmpty();
216 }
217 
218     /** Checks if the given state is a member of the state set of this
219         object.
220 
221         @param aState
222             The state for which to check membership.  This has to be one of
223             the constants of <type>AccessibleStateType</type>.
224 
225         @return
226             Returns <TRUE/> if the given state is a member of this object's
227             state set and <FALSE/> otherwise.
228     */
contains(sal_Int16 aState)229 sal_Bool SAL_CALL AccessibleStateSetHelper::contains (sal_Int16 aState)
230 {
231     ::vos::OGuard aGuard (maMutex);
232     return mpHelperImpl->Contains(aState);
233 }
234 
235     /** Checks if all of the given states are in this object's state
236         set.
237 
238         @param aStateSet
239             This sequence of states is interpreted as set and every of its
240             members, duplicates are ignored, is checked for membership in
241             this object's state set.  Each state has to be one of the
242             constants of <type>AccessibleStateType</type>.
243 
244         @return
245             Returns <TRUE/> if all states of the given state set are members
246             of this object's state set.  <FALSE/> is returned if at least
247             one of the states in the given state is not a member of this
248             object's state set.
249     */
containsAll(const uno::Sequence<sal_Int16> & rStateSet)250 sal_Bool SAL_CALL AccessibleStateSetHelper::containsAll
251     (const uno::Sequence<sal_Int16>& rStateSet)
252 {
253     ::vos::OGuard aGuard (maMutex);
254     sal_Int32 nCount(rStateSet.getLength());
255     const sal_Int16* pStates = rStateSet.getConstArray();
256     sal_Int32 i = 0;
257     sal_Bool bFound(sal_True);
258     while (i < nCount)
259     {
260         bFound = mpHelperImpl->Contains(pStates[i]);
261         i++;
262     }
263     return bFound;
264 }
265 
getStates()266 uno::Sequence<sal_Int16> SAL_CALL AccessibleStateSetHelper::getStates()
267 {
268     ::vos::OGuard aGuard(maMutex);
269     return mpHelperImpl->GetStates();
270 }
271 
AddState(sal_Int16 aState)272 void AccessibleStateSetHelper::AddState(sal_Int16 aState)
273 {
274     ::vos::OGuard aGuard (maMutex);
275     mpHelperImpl->AddState(aState);
276 }
277 
RemoveState(sal_Int16 aState)278 void AccessibleStateSetHelper::RemoveState(sal_Int16 aState)
279 {
280     ::vos::OGuard aGuard (maMutex);
281     mpHelperImpl->RemoveState(aState);
282 }
283 
Compare(const AccessibleStateSetHelper & rComparativeValue,AccessibleStateSetHelper & rOldStates,AccessibleStateSetHelper & rNewStates)284 sal_Bool AccessibleStateSetHelper::Compare(
285     const AccessibleStateSetHelper& rComparativeValue,
286         AccessibleStateSetHelper& rOldStates,
287         AccessibleStateSetHelper& rNewStates)
288 {
289     ::vos::OGuard aGuard (maMutex);
290     return mpHelperImpl->Compare(rComparativeValue.mpHelperImpl,
291         rOldStates.mpHelperImpl, rNewStates.mpHelperImpl);
292 }
293 
294 //=====  XTypeProvider  =======================================================
295 
296 uno::Sequence< ::com::sun::star::uno::Type>
getTypes(void)297     AccessibleStateSetHelper::getTypes (void)
298 {
299     const ::com::sun::star::uno::Type aTypeList[] = {
300         ::getCppuType((const uno::Reference<
301             XAccessibleStateSet>*)0),
302         ::getCppuType((const uno::Reference<
303             lang::XTypeProvider>*)0)
304         };
305     ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type>
306         aTypeSequence (aTypeList, 2);
307     return aTypeSequence;
308 }
309 
310 uno::Sequence<sal_Int8> SAL_CALL
getImplementationId(void)311     AccessibleStateSetHelper::getImplementationId (void)
312 {
313     ::vos::OGuard aGuard (maMutex);
314     static uno::Sequence<sal_Int8> aId;
315     if (aId.getLength() == 0)
316     {
317         aId.realloc (16);
318         rtl_createUuid ((sal_uInt8 *)aId.getArray(), 0, sal_True);
319     }
320     return aId;
321 }
322