Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <unotools/accessiblestatesethelper.hxx>
21 : #include <tools/debug.hxx>
22 : #include <comphelper/servicehelper.hxx>
23 :
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
27 :
28 : using namespace ::utl;
29 : using namespace ::com::sun::star;
30 : using namespace ::com::sun::star::accessibility;
31 :
32 : class AccessibleStateSetHelperImpl
33 : {
34 : public:
35 : AccessibleStateSetHelperImpl();
36 : AccessibleStateSetHelperImpl(const AccessibleStateSetHelperImpl& rImpl);
37 : ~AccessibleStateSetHelperImpl();
38 :
39 : bool IsEmpty () const
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);
49 :
50 : inline void AddStates( const sal_Int64 _nStates );
51 :
52 : private:
53 : sal_uInt64 maStates;
54 : };
55 :
56 7292 : AccessibleStateSetHelperImpl::AccessibleStateSetHelperImpl()
57 7292 : : maStates(0)
58 : {
59 7292 : }
60 :
61 84 : AccessibleStateSetHelperImpl::AccessibleStateSetHelperImpl(const AccessibleStateSetHelperImpl& rImpl)
62 84 : : maStates(rImpl.maStates)
63 : {
64 84 : }
65 :
66 7376 : AccessibleStateSetHelperImpl::~AccessibleStateSetHelperImpl()
67 : {
68 7376 : }
69 :
70 0 : inline bool AccessibleStateSetHelperImpl::IsEmpty () const
71 : throw (uno::RuntimeException)
72 : {
73 0 : return maStates == 0;
74 : }
75 :
76 8830 : inline bool AccessibleStateSetHelperImpl::Contains (sal_Int16 aState) const
77 : throw (uno::RuntimeException)
78 : {
79 : DBG_ASSERT(aState < BITFIELDSIZE, "the statesset is too small");
80 8830 : sal_uInt64 aTempBitSet(1);
81 8830 : aTempBitSet <<= aState;
82 8830 : return ((aTempBitSet & maStates) != 0);
83 : }
84 :
85 24 : inline uno::Sequence<sal_Int16> AccessibleStateSetHelperImpl::GetStates() const
86 : throw (uno::RuntimeException)
87 : {
88 24 : uno::Sequence<sal_Int16> aRet(BITFIELDSIZE);
89 24 : sal_Int16* pSeq = aRet.getArray();
90 24 : sal_Int16 nStateCount(0);
91 1560 : for (sal_Int16 i = 0; i < BITFIELDSIZE; ++i)
92 1536 : if (Contains(i))
93 : {
94 204 : *pSeq = i;
95 204 : ++pSeq;
96 204 : ++nStateCount;
97 : }
98 24 : aRet.realloc(nStateCount);
99 24 : return aRet;
100 : }
101 :
102 0 : inline void AccessibleStateSetHelperImpl::AddStates( const sal_Int64 _nStates )
103 : {
104 0 : maStates |= _nStates;
105 0 : }
106 :
107 34215 : inline void AccessibleStateSetHelperImpl::AddState(sal_Int16 aState)
108 : throw (uno::RuntimeException)
109 : {
110 : DBG_ASSERT(aState < BITFIELDSIZE, "the statesset is too small");
111 34215 : sal_uInt64 aTempBitSet(1);
112 34215 : aTempBitSet <<= aState;
113 34215 : maStates |= aTempBitSet;
114 34215 : }
115 :
116 140 : inline void AccessibleStateSetHelperImpl::RemoveState(sal_Int16 aState)
117 : throw (uno::RuntimeException)
118 : {
119 : DBG_ASSERT(aState < BITFIELDSIZE, "the statesset is too small");
120 140 : sal_uInt64 aTempBitSet(1);
121 140 : aTempBitSet <<= aState;
122 140 : aTempBitSet = ~aTempBitSet;
123 140 : maStates &= aTempBitSet;
124 140 : }
125 :
126 : //===== internal ============================================================
127 :
128 7292 : AccessibleStateSetHelper::AccessibleStateSetHelper ()
129 7292 : : mpHelperImpl(NULL)
130 : {
131 7292 : mpHelperImpl = new AccessibleStateSetHelperImpl();
132 7292 : }
133 :
134 0 : AccessibleStateSetHelper::AccessibleStateSetHelper ( const sal_Int64 _nInitialStates )
135 0 : : mpHelperImpl(NULL)
136 : {
137 0 : mpHelperImpl = new AccessibleStateSetHelperImpl();
138 0 : mpHelperImpl->AddStates( _nInitialStates );
139 0 : }
140 :
141 84 : AccessibleStateSetHelper::AccessibleStateSetHelper (const AccessibleStateSetHelper& rHelper)
142 : : cppu::WeakImplHelper1<XAccessibleStateSet>()
143 84 : , mpHelperImpl(NULL)
144 : {
145 84 : if (rHelper.mpHelperImpl)
146 84 : mpHelperImpl = new AccessibleStateSetHelperImpl(*rHelper.mpHelperImpl);
147 : else
148 0 : mpHelperImpl = new AccessibleStateSetHelperImpl();
149 84 : }
150 :
151 22128 : AccessibleStateSetHelper::~AccessibleStateSetHelper(void)
152 : {
153 7376 : delete mpHelperImpl;
154 14752 : }
155 :
156 : //===== XAccessibleStateSet ==============================================
157 :
158 : /** Checks whether the current state set is empty.
159 :
160 : @return
161 : Returns <TRUE/> if there is no state in this state set and
162 : <FALSE/> if there is at least one state set in it.
163 : */
164 0 : sal_Bool SAL_CALL AccessibleStateSetHelper::isEmpty ()
165 : throw (uno::RuntimeException, std::exception)
166 : {
167 0 : osl::MutexGuard aGuard (maMutex);
168 0 : return mpHelperImpl->IsEmpty();
169 : }
170 :
171 : /** Checks if the given state is a member of the state set of this
172 : object.
173 :
174 : @param aState
175 : The state for which to check membership. This has to be one of
176 : the constants of <type>AccessibleStateType</type>.
177 :
178 : @return
179 : Returns <TRUE/> if the given state is a member of this object's
180 : state set and <FALSE/> otherwise.
181 : */
182 7294 : sal_Bool SAL_CALL AccessibleStateSetHelper::contains (sal_Int16 aState)
183 : throw (uno::RuntimeException, std::exception)
184 : {
185 7294 : osl::MutexGuard aGuard (maMutex);
186 7294 : return mpHelperImpl->Contains(aState);
187 : }
188 :
189 : /** Checks if all of the given states are in this object's state
190 : set.
191 :
192 : @param aStateSet
193 : This sequence of states is interpreted as set and every of its
194 : members, duplicates are ignored, is checked for membership in
195 : this object's state set. Each state has to be one of the
196 : constants of <type>AccessibleStateType</type>.
197 :
198 : @return
199 : Returns <TRUE/> if all states of the given state set are members
200 : of this object's state set. <FALSE/> is returned if at least
201 : one of the states in the given state is not a member of this
202 : object's state set.
203 : */
204 0 : sal_Bool SAL_CALL AccessibleStateSetHelper::containsAll
205 : (const uno::Sequence<sal_Int16>& rStateSet)
206 : throw (uno::RuntimeException, std::exception)
207 : {
208 0 : osl::MutexGuard aGuard (maMutex);
209 0 : sal_Int32 nCount(rStateSet.getLength());
210 0 : const sal_Int16* pStates = rStateSet.getConstArray();
211 0 : sal_Int32 i = 0;
212 0 : bool bFound(true);
213 0 : while (i < nCount)
214 : {
215 0 : bFound = mpHelperImpl->Contains(pStates[i]);
216 0 : i++;
217 : }
218 0 : return bFound;
219 : }
220 :
221 24 : uno::Sequence<sal_Int16> SAL_CALL AccessibleStateSetHelper::getStates()
222 : throw (uno::RuntimeException, std::exception)
223 : {
224 24 : osl::MutexGuard aGuard(maMutex);
225 24 : return mpHelperImpl->GetStates();
226 : }
227 :
228 34215 : void AccessibleStateSetHelper::AddState(sal_Int16 aState)
229 : throw (uno::RuntimeException)
230 : {
231 34215 : osl::MutexGuard aGuard (maMutex);
232 34215 : mpHelperImpl->AddState(aState);
233 34215 : }
234 :
235 140 : void AccessibleStateSetHelper::RemoveState(sal_Int16 aState)
236 : throw (uno::RuntimeException)
237 : {
238 140 : osl::MutexGuard aGuard (maMutex);
239 140 : mpHelperImpl->RemoveState(aState);
240 140 : }
241 :
242 : //===== XTypeProvider =======================================================
243 :
244 : uno::Sequence< ::com::sun::star::uno::Type>
245 0 : AccessibleStateSetHelper::getTypes (void)
246 : throw (::com::sun::star::uno::RuntimeException, std::exception)
247 : {
248 : const ::com::sun::star::uno::Type aTypeList[] = {
249 0 : cppu::UnoType<XAccessibleStateSet>::get(),
250 0 : cppu::UnoType<lang::XTypeProvider>::get()
251 0 : };
252 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type>
253 0 : aTypeSequence (aTypeList, 2);
254 0 : return aTypeSequence;
255 : }
256 :
257 : uno::Sequence<sal_Int8> SAL_CALL
258 0 : AccessibleStateSetHelper::getImplementationId (void)
259 : throw (::com::sun::star::uno::RuntimeException, std::exception)
260 : {
261 0 : return css::uno::Sequence<sal_Int8>();
262 : }
263 :
264 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|