1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <unotools/accessiblestatesethelper.hxx>
21 #include <tools/debug.hxx>
22 #include <comphelper/servicehelper.hxx>
24 // defines how many states the bitfield can contain
25 // it has the size of 64 because I use a uInt64
26 #define BITFIELDSIZE 64
28 using namespace ::utl
;
29 using namespace ::com::sun::star
;
30 using namespace ::com::sun::star::accessibility
;
32 class AccessibleStateSetHelperImpl
35 AccessibleStateSetHelperImpl();
36 AccessibleStateSetHelperImpl(const AccessibleStateSetHelperImpl
& rImpl
);
37 ~AccessibleStateSetHelperImpl();
40 throw (uno::RuntimeException
);
41 bool Contains (sal_Int16 aState
) const
42 throw (uno::RuntimeException
);
43 uno::Sequence
<sal_Int16
> GetStates() const
44 throw (uno::RuntimeException
);
45 void AddState(sal_Int16 aState
)
46 throw (uno::RuntimeException
);
47 void RemoveState(sal_Int16 aState
)
48 throw (uno::RuntimeException
);
50 inline void AddStates( const sal_Int64 _nStates
);
56 AccessibleStateSetHelperImpl::AccessibleStateSetHelperImpl()
61 AccessibleStateSetHelperImpl::AccessibleStateSetHelperImpl(const AccessibleStateSetHelperImpl
& rImpl
)
62 : maStates(rImpl
.maStates
)
66 AccessibleStateSetHelperImpl::~AccessibleStateSetHelperImpl()
70 inline bool AccessibleStateSetHelperImpl::IsEmpty () const
71 throw (uno::RuntimeException
)
76 inline bool AccessibleStateSetHelperImpl::Contains (sal_Int16 aState
) const
77 throw (uno::RuntimeException
)
79 DBG_ASSERT(aState
< BITFIELDSIZE
, "the statesset is too small");
80 sal_uInt64
aTempBitSet(1);
81 aTempBitSet
<<= aState
;
82 return ((aTempBitSet
& maStates
) != 0);
85 inline uno::Sequence
<sal_Int16
> AccessibleStateSetHelperImpl::GetStates() const
86 throw (uno::RuntimeException
)
88 uno::Sequence
<sal_Int16
> aRet(BITFIELDSIZE
);
89 sal_Int16
* pSeq
= aRet
.getArray();
90 sal_Int16
nStateCount(0);
91 for (sal_Int16 i
= 0; i
< BITFIELDSIZE
; ++i
)
98 aRet
.realloc(nStateCount
);
102 inline void AccessibleStateSetHelperImpl::AddStates( const sal_Int64 _nStates
)
104 maStates
|= _nStates
;
107 inline void AccessibleStateSetHelperImpl::AddState(sal_Int16 aState
)
108 throw (uno::RuntimeException
)
110 DBG_ASSERT(aState
< BITFIELDSIZE
, "the statesset is too small");
111 sal_uInt64
aTempBitSet(1);
112 aTempBitSet
<<= aState
;
113 maStates
|= aTempBitSet
;
116 inline void AccessibleStateSetHelperImpl::RemoveState(sal_Int16 aState
)
117 throw (uno::RuntimeException
)
119 DBG_ASSERT(aState
< BITFIELDSIZE
, "the statesset is too small");
120 sal_uInt64
aTempBitSet(1);
121 aTempBitSet
<<= aState
;
122 aTempBitSet
= ~aTempBitSet
;
123 maStates
&= aTempBitSet
;
126 //===== internal ============================================================
128 AccessibleStateSetHelper::AccessibleStateSetHelper ()
129 : mpHelperImpl(new AccessibleStateSetHelperImpl
)
133 AccessibleStateSetHelper::AccessibleStateSetHelper ( const sal_Int64 _nInitialStates
)
134 : mpHelperImpl(new AccessibleStateSetHelperImpl
)
136 mpHelperImpl
->AddStates( _nInitialStates
);
139 AccessibleStateSetHelper::AccessibleStateSetHelper (const AccessibleStateSetHelper
& rHelper
)
140 : cppu::WeakImplHelper1
<XAccessibleStateSet
>()
142 if (rHelper
.mpHelperImpl
)
143 mpHelperImpl
.reset(new AccessibleStateSetHelperImpl(*rHelper
.mpHelperImpl
));
145 mpHelperImpl
.reset(new AccessibleStateSetHelperImpl());
148 AccessibleStateSetHelper::~AccessibleStateSetHelper()
152 //===== XAccessibleStateSet ==============================================
154 /** Checks whether the current state set is empty.
157 Returns <TRUE/> if there is no state in this state set and
158 <FALSE/> if there is at least one state set in it.
160 sal_Bool SAL_CALL
AccessibleStateSetHelper::isEmpty ()
161 throw (uno::RuntimeException
, std::exception
)
163 osl::MutexGuard
aGuard (maMutex
);
164 return mpHelperImpl
->IsEmpty();
167 /** Checks if the given state is a member of the state set of this
171 The state for which to check membership. This has to be one of
172 the constants of <type>AccessibleStateType</type>.
175 Returns <TRUE/> if the given state is a member of this object's
176 state set and <FALSE/> otherwise.
178 sal_Bool SAL_CALL
AccessibleStateSetHelper::contains (sal_Int16 aState
)
179 throw (uno::RuntimeException
, std::exception
)
181 osl::MutexGuard
aGuard (maMutex
);
182 return mpHelperImpl
->Contains(aState
);
185 /** Checks if all of the given states are in this object's state
189 This sequence of states is interpreted as set and every of its
190 members, duplicates are ignored, is checked for membership in
191 this object's state set. Each state has to be one of the
192 constants of <type>AccessibleStateType</type>.
195 Returns <TRUE/> if all states of the given state set are members
196 of this object's state set. <FALSE/> is returned if at least
197 one of the states in the given state is not a member of this
200 sal_Bool SAL_CALL
AccessibleStateSetHelper::containsAll
201 (const uno::Sequence
<sal_Int16
>& rStateSet
)
202 throw (uno::RuntimeException
, std::exception
)
204 osl::MutexGuard
aGuard (maMutex
);
205 sal_Int32
nCount(rStateSet
.getLength());
206 const sal_Int16
* pStates
= rStateSet
.getConstArray();
211 bFound
= mpHelperImpl
->Contains(pStates
[i
]);
217 uno::Sequence
<sal_Int16
> SAL_CALL
AccessibleStateSetHelper::getStates()
218 throw (uno::RuntimeException
, std::exception
)
220 osl::MutexGuard
aGuard(maMutex
);
221 return mpHelperImpl
->GetStates();
224 void AccessibleStateSetHelper::AddState(sal_Int16 aState
)
225 throw (uno::RuntimeException
)
227 osl::MutexGuard
aGuard (maMutex
);
228 mpHelperImpl
->AddState(aState
);
231 void AccessibleStateSetHelper::RemoveState(sal_Int16 aState
)
232 throw (uno::RuntimeException
)
234 osl::MutexGuard
aGuard (maMutex
);
235 mpHelperImpl
->RemoveState(aState
);
238 //===== XTypeProvider =======================================================
240 uno::Sequence
< css::uno::Type
> AccessibleStateSetHelper::getTypes()
241 throw (css::uno::RuntimeException
, std::exception
)
243 css::uno::Sequence
< css::uno::Type
> aTypeSequence
{
244 cppu::UnoType
<XAccessibleStateSet
>::get(),
245 cppu::UnoType
<lang::XTypeProvider
>::get()
247 return aTypeSequence
;
250 uno::Sequence
<sal_Int8
> SAL_CALL
AccessibleStateSetHelper::getImplementationId()
251 throw (css::uno::RuntimeException
, std::exception
)
253 return css::uno::Sequence
<sal_Int8
>();
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */