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 .
22 #include "collection.hxx"
23 #include <cppuhelper/implbase.hxx>
24 #include <comphelper/sequence.hxx>
25 #include <com/sun/star/container/XNameAccess.hpp>
26 #include <com/sun/star/container/XNamed.hpp>
31 class NamedCollection
: public cppu::ImplInheritanceHelper
<
33 css::container::XNameAccess
>
35 using Collection
<T
>::maItems
;
36 using Collection
<T
>::getItem
;
37 using Collection
<T
>::hasItem
;
41 const T
& getItem( const OUString
& rName
) const
43 OSL_ENSURE( hasItem( rName
), "invalid name" );
44 return *findItem( rName
);
47 bool hasItem( const OUString
& rName
) const
49 return findItem( rName
) != maItems
.end();
52 css::uno::Sequence
<OUString
> getNames() const
54 // iterate over members, and collect all those that have names
55 std::vector
<OUString
> aNames
;
56 for( const T
& rItem
: maItems
)
58 css::uno::Reference
<css::container::XNamed
>
59 xNamed( rItem
, css::uno::UNO_QUERY
);
61 aNames
.push_back( xNamed
->getName() );
64 return comphelper::containerToSequence(aNames
);
68 typename
std::vector
<T
>::const_iterator
findItem( const OUString
& rName
) const
70 return std::find_if(maItems
.begin(), maItems
.end(), [&rName
](const T
& rItem
) {
71 css::uno::Reference
<css::container::XNamed
>
72 xNamed( rItem
, css::uno::UNO_QUERY
);
73 return xNamed
.is() && xNamed
->getName() == rName
;
80 virtual css::uno::Type SAL_CALL
getElementType() override
82 return Collection
<T
>::getElementType();
85 virtual sal_Bool SAL_CALL
hasElements() override
87 return Collection
<T
>::hasElements();
90 // XNameAccess : XElementAccess
91 virtual css::uno::Any SAL_CALL
getByName(
92 const OUString
& aName
) override
94 if( !hasItem( aName
) )
95 throw css::container::NoSuchElementException();
96 return css::uno::Any( getItem( aName
) );
99 virtual css::uno::Sequence
<OUString
> SAL_CALL
getElementNames() override
104 virtual sal_Bool SAL_CALL
hasByName(
105 const OUString
& aName
) override
107 return hasItem( aName
);
111 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */