Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / unotools / source / accessibility / accessiblerelationsethelper.cxx
blob323b12c535062da861c02b0d7f10160d3af27551
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <sal/config.h>
22 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
23 #include <o3tl/safeint.hxx>
24 #include <unotools/accessiblerelationsethelper.hxx>
25 #include <comphelper/sequence.hxx>
27 using namespace ::utl;
28 using namespace ::com::sun::star;
29 using namespace ::com::sun::star::accessibility;
31 namespace
33 AccessibleRelation lcl_getRelationByType( std::vector<AccessibleRelation>& raRelations, sal_Int16 aRelationType )
35 for (const auto& aRelation: raRelations)
37 if (aRelation.RelationType == aRelationType)
38 return aRelation;
40 return AccessibleRelation();
43 //===== internal ============================================================
45 AccessibleRelationSetHelper::AccessibleRelationSetHelper ()
49 AccessibleRelationSetHelper::AccessibleRelationSetHelper (const AccessibleRelationSetHelper& rHelper)
50 : cppu::WeakImplHelper<XAccessibleRelationSet>(rHelper),
51 maRelations(rHelper.maRelations)
55 AccessibleRelationSetHelper::~AccessibleRelationSetHelper()
59 //===== XAccessibleRelationSet ==============================================
61 /** Returns the number of relations in this relation set.
63 @return
64 Returns the number of relations or zero if there are none.
66 sal_Int32 SAL_CALL
67 AccessibleRelationSetHelper::getRelationCount( )
69 std::scoped_lock aGuard (maMutex);
71 return maRelations.size();
74 /** Returns the relation of this relation set that is specified by
75 the given index.
77 @param nIndex
78 This index specifies the relatio to return.
80 @return
81 For a valid index, i.e. inside the range 0 to the number of
82 relations minus one, the returned value is the requested
83 relation. If the index is invalid then the returned relation
84 has the type INVALID.
87 AccessibleRelation SAL_CALL
88 AccessibleRelationSetHelper::getRelation( sal_Int32 nIndex )
90 std::scoped_lock aGuard (maMutex);
92 if ((nIndex < 0) || (o3tl::make_unsigned(nIndex) >= maRelations.size()))
93 throw lang::IndexOutOfBoundsException();
95 return maRelations[nIndex];
98 /** Tests whether the relation set contains a relation matching the
99 specified key.
101 @param aRelationType
102 The type of relation to look for in this set of relations. This
103 has to be one of the constants of
104 <type>AccessibleRelationType</type>.
106 @return
107 Returns <TRUE/> if there is a (at least one) relation of the
108 given type and <FALSE/> if there is no such relation in the set.
110 sal_Bool SAL_CALL
111 AccessibleRelationSetHelper::containsRelation( sal_Int16 aRelationType )
113 std::scoped_lock aGuard (maMutex);
115 AccessibleRelation defaultRelation; // default is INVALID
116 AccessibleRelation relationByType = lcl_getRelationByType(maRelations, aRelationType);
117 return relationByType.RelationType != defaultRelation.RelationType;
120 /** Retrieve and return the relation with the given relation type.
122 @param aRelationType
123 The type of the relation to return. This has to be one of the
124 constants of <type>AccessibleRelationType</type>.
126 @return
127 If a relation with the given type could be found than (a copy
128 of) this relation is returned. Otherwise a relation with the
129 type INVALID is returned.
131 AccessibleRelation SAL_CALL
132 AccessibleRelationSetHelper::getRelationByType( sal_Int16 aRelationType )
134 std::scoped_lock aGuard (maMutex);
136 return lcl_getRelationByType(maRelations, aRelationType);
139 void AccessibleRelationSetHelper::AddRelation(const AccessibleRelation& rRelation)
141 std::scoped_lock aGuard (maMutex);
143 for (auto& aRelation: maRelations)
145 if (aRelation.RelationType == rRelation.RelationType)
147 aRelation.TargetSet = comphelper::concatSequences(aRelation.TargetSet, rRelation.TargetSet);
148 return;
151 maRelations.push_back(rRelation);
154 //===== XTypeProvider =======================================================
156 uno::Sequence< css::uno::Type> AccessibleRelationSetHelper::getTypes()
158 static const uno::Sequence< css::uno::Type> aTypes {
159 cppu::UnoType<XAccessibleRelationSet>::get(),
160 cppu::UnoType<lang::XTypeProvider>::get()
162 return aTypes;
165 uno::Sequence<sal_Int8> SAL_CALL AccessibleRelationSetHelper::getImplementationId()
167 return css::uno::Sequence<sal_Int8>();
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */