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 <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
;
33 AccessibleRelation
lcl_getRelationByType( std::vector
<AccessibleRelation
>& raRelations
, sal_Int16 aRelationType
)
35 for (const auto& aRelation
: raRelations
)
37 if (aRelation
.RelationType
== aRelationType
)
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.
64 Returns the number of relations or zero if there are none.
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
78 This index specifies the relatio to 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
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
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>.
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.
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.
123 The type of the relation to return. This has to be one of the
124 constants of <type>AccessibleRelationType</type>.
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
);
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()
165 uno::Sequence
<sal_Int8
> SAL_CALL
AccessibleRelationSetHelper::getImplementationId()
167 return css::uno::Sequence
<sal_Int8
>();
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */