1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: accessiblestatesethelper.cxx,v $
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 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_unotools.hxx"
35 #include "unotools/accessiblestatesethelper.hxx"
37 #include <tools/debug.hxx>
43 // defines how many states the bitfield can contain
44 // it has the size of 64 because I use a uInt64
45 #define BITFIELDSIZE 64
47 using namespace ::utl
;
48 using namespace ::rtl
;
49 using namespace ::com::sun::star
;
50 using namespace ::com::sun::star::accessibility
;
52 class AccessibleStateSetHelperImpl
55 AccessibleStateSetHelperImpl();
56 AccessibleStateSetHelperImpl(const AccessibleStateSetHelperImpl
& rImpl
);
57 ~AccessibleStateSetHelperImpl();
60 throw (uno::RuntimeException
);
61 sal_Bool
Contains (sal_Int16 aState
)
62 throw (uno::RuntimeException
);
63 uno::Sequence
<sal_Int16
> GetStates()
64 throw (uno::RuntimeException
);
65 void AddState(sal_Int16 aState
)
66 throw (uno::RuntimeException
);
67 void RemoveState(sal_Int16 aState
)
68 throw (uno::RuntimeException
);
69 sal_Bool
Compare(const AccessibleStateSetHelperImpl
* pComparativeValue
,
70 AccessibleStateSetHelperImpl
* pOldStates
,
71 AccessibleStateSetHelperImpl
* pNewStates
)
72 throw (uno::RuntimeException
);
74 inline void AddStates( const sal_Int64 _nStates
) SAL_THROW( ( ) );
78 ::std::bitset
<BITFIELDSIZE
> maStates
; //Bitfield
83 AccessibleStateSetHelperImpl::AccessibleStateSetHelperImpl()
88 AccessibleStateSetHelperImpl::AccessibleStateSetHelperImpl(const AccessibleStateSetHelperImpl
& rImpl
)
89 : maStates(rImpl
.maStates
)
93 AccessibleStateSetHelperImpl::~AccessibleStateSetHelperImpl()
97 inline sal_Bool
AccessibleStateSetHelperImpl::IsEmpty ()
98 throw (uno::RuntimeException
)
101 return maStates
.none();
103 return maStates
== 0;
106 inline sal_Bool
AccessibleStateSetHelperImpl::Contains (sal_Int16 aState
)
107 throw (uno::RuntimeException
)
109 DBG_ASSERT(aState
< BITFIELDSIZE
, "the statesset is too small");
111 return maStates
.test(aState
);
113 sal_uInt64
aTempBitSet(1);
114 aTempBitSet
<<= aState
;
115 return ((aTempBitSet
& maStates
) != 0);
118 inline uno::Sequence
<sal_Int16
> AccessibleStateSetHelperImpl::GetStates()
119 throw (uno::RuntimeException
)
121 uno::Sequence
<sal_Int16
> aRet(BITFIELDSIZE
);
122 sal_Int16
* pSeq
= aRet
.getArray();
123 sal_Int16
nStateCount(0);
124 for (sal_Int16 i
= 0; i
< BITFIELDSIZE
; ++i
)
131 aRet
.realloc(nStateCount
);
135 inline void AccessibleStateSetHelperImpl::AddStates( const sal_Int64 _nStates
) SAL_THROW( ( ) )
137 maStates
|= _nStates
;
140 inline void AccessibleStateSetHelperImpl::AddState(sal_Int16 aState
)
141 throw (uno::RuntimeException
)
143 DBG_ASSERT(aState
< BITFIELDSIZE
, "the statesset is too small");
145 maStates
.set(aState
);
147 sal_uInt64
aTempBitSet(1);
148 aTempBitSet
<<= aState
;
149 maStates
|= aTempBitSet
;
152 inline void AccessibleStateSetHelperImpl::RemoveState(sal_Int16 aState
)
153 throw (uno::RuntimeException
)
155 DBG_ASSERT(aState
< BITFIELDSIZE
, "the statesset is too small");
157 maStates
.set(aState
, 0);
159 sal_uInt64
aTempBitSet(1);
160 aTempBitSet
<<= aState
;
161 aTempBitSet
= ~aTempBitSet
;
162 maStates
&= aTempBitSet
;
165 inline sal_Bool
AccessibleStateSetHelperImpl::Compare(
166 const AccessibleStateSetHelperImpl
* pComparativeValue
,
167 AccessibleStateSetHelperImpl
* pOldStates
,
168 AccessibleStateSetHelperImpl
* pNewStates
)
169 throw (uno::RuntimeException
)
171 sal_Bool
bResult(sal_False
);
172 if (pComparativeValue
&& pOldStates
&& pNewStates
)
174 if (maStates
== pComparativeValue
->maStates
)
179 std::bitset
<BITFIELDSIZE
> aTempBitSet(maStates
);
181 sal_uInt64
aTempBitSet(maStates
);
182 aTempBitSet
^= pComparativeValue
->maStates
;
183 pOldStates
->maStates
= aTempBitSet
;
184 pOldStates
->maStates
&= maStates
;
185 pNewStates
->maStates
= aTempBitSet
;
186 pNewStates
->maStates
&= pComparativeValue
->maStates
;
193 //===== internal ============================================================
195 AccessibleStateSetHelper::AccessibleStateSetHelper ()
198 mpHelperImpl
= new AccessibleStateSetHelperImpl();
201 AccessibleStateSetHelper::AccessibleStateSetHelper ( const sal_Int64 _nInitialStates
)
204 mpHelperImpl
= new AccessibleStateSetHelperImpl();
205 mpHelperImpl
->AddStates( _nInitialStates
);
208 AccessibleStateSetHelper::AccessibleStateSetHelper (const AccessibleStateSetHelper
& rHelper
)
209 : cppu::WeakImplHelper1
<XAccessibleStateSet
>()
212 if (rHelper
.mpHelperImpl
)
213 mpHelperImpl
= new AccessibleStateSetHelperImpl(*rHelper
.mpHelperImpl
);
215 mpHelperImpl
= new AccessibleStateSetHelperImpl();
218 AccessibleStateSetHelper::~AccessibleStateSetHelper(void)
223 //===== XAccessibleStateSet ==============================================
225 /** Checks whether the current state set is empty.
228 Returns <TRUE/> if there is no state in this state set and
229 <FALSE/> if there is at least one state set in it.
231 sal_Bool SAL_CALL
AccessibleStateSetHelper::isEmpty ()
232 throw (uno::RuntimeException
)
234 ::vos::OGuard
aGuard (maMutex
);
235 return mpHelperImpl
->IsEmpty();
238 /** Checks if the given state is a member of the state set of this
242 The state for which to check membership. This has to be one of
243 the constants of <type>AccessibleStateType</type>.
246 Returns <TRUE/> if the given state is a memeber of this object's
247 state set and <FALSE/> otherwise.
249 sal_Bool SAL_CALL
AccessibleStateSetHelper::contains (sal_Int16 aState
)
250 throw (uno::RuntimeException
)
252 ::vos::OGuard
aGuard (maMutex
);
253 return mpHelperImpl
->Contains(aState
);
256 /** Checks if all of the given states are in this object's state
260 This sequence of states is interpreted as set and every of its
261 members, duplicates are ignored, is checked for membership in
262 this object's state set. Each state has to be one of the
263 constants of <type>AccessibleStateType</type>.
266 Returns <TRUE/> if all states of the given state set are members
267 of this object's state set. <FALSE/> is returned if at least
268 one of the states in the given state is not a member of this
271 sal_Bool SAL_CALL
AccessibleStateSetHelper::containsAll
272 (const uno::Sequence
<sal_Int16
>& rStateSet
)
273 throw (uno::RuntimeException
)
275 ::vos::OGuard
aGuard (maMutex
);
276 sal_Int32
nCount(rStateSet
.getLength());
277 const sal_Int16
* pStates
= rStateSet
.getConstArray();
279 sal_Bool
bFound(sal_True
);
282 bFound
= mpHelperImpl
->Contains(pStates
[i
]);
288 uno::Sequence
<sal_Int16
> SAL_CALL
AccessibleStateSetHelper::getStates()
289 throw (uno::RuntimeException
)
291 ::vos::OGuard
aGuard(maMutex
);
292 return mpHelperImpl
->GetStates();
295 void AccessibleStateSetHelper::AddState(sal_Int16 aState
)
296 throw (uno::RuntimeException
)
298 ::vos::OGuard
aGuard (maMutex
);
299 mpHelperImpl
->AddState(aState
);
302 void AccessibleStateSetHelper::RemoveState(sal_Int16 aState
)
303 throw (uno::RuntimeException
)
305 ::vos::OGuard
aGuard (maMutex
);
306 mpHelperImpl
->RemoveState(aState
);
309 sal_Bool
AccessibleStateSetHelper::Compare(
310 const AccessibleStateSetHelper
& rComparativeValue
,
311 AccessibleStateSetHelper
& rOldStates
,
312 AccessibleStateSetHelper
& rNewStates
)
313 throw (uno::RuntimeException
)
315 ::vos::OGuard
aGuard (maMutex
);
316 return mpHelperImpl
->Compare(rComparativeValue
.mpHelperImpl
,
317 rOldStates
.mpHelperImpl
, rNewStates
.mpHelperImpl
);
320 //===== XTypeProvider =======================================================
322 uno::Sequence
< ::com::sun::star::uno::Type
>
323 AccessibleStateSetHelper::getTypes (void)
324 throw (::com::sun::star::uno::RuntimeException
)
326 const ::com::sun::star::uno::Type aTypeList
[] = {
327 ::getCppuType((const uno::Reference
<
328 XAccessibleStateSet
>*)0),
329 ::getCppuType((const uno::Reference
<
330 lang::XTypeProvider
>*)0)
332 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Type
>
333 aTypeSequence (aTypeList
, 2);
334 return aTypeSequence
;
337 uno::Sequence
<sal_Int8
> SAL_CALL
338 AccessibleStateSetHelper::getImplementationId (void)
339 throw (::com::sun::star::uno::RuntimeException
)
341 ::vos::OGuard
aGuard (maMutex
);
342 static uno::Sequence
<sal_Int8
> aId
;
343 if (aId
.getLength() == 0)
346 rtl_createUuid ((sal_uInt8
*)aId
.getArray(), 0, sal_True
);