nss: upgrade to release 3.73
[LibreOffice.git] / forms / source / xforms / namedcollection.hxx
blob6abfb430e2b8ecd1211dfaad4c76f5f3a21443a3
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 #ifndef INCLUDED_FORMS_SOURCE_XFORMS_NAMEDCOLLECTION_HXX
21 #define INCLUDED_FORMS_SOURCE_XFORMS_NAMEDCOLLECTION_HXX
23 #include "collection.hxx"
24 #include <cppuhelper/implbase.hxx>
25 #include <comphelper/sequence.hxx>
26 #include <com/sun/star/container/XNameAccess.hpp>
27 #include <com/sun/star/container/XNamed.hpp>
29 #include <algorithm>
31 template<class T>
32 class NamedCollection : public cppu::ImplInheritanceHelper<
33 Collection<T>,
34 css::container::XNameAccess>
36 using Collection<T>::maItems;
37 using Collection<T>::getItem;
38 using Collection<T>::hasItem;
40 public:
41 NamedCollection() {}
43 const T& getItem( const OUString& rName ) const
45 OSL_ENSURE( hasItem( rName ), "invalid name" );
46 return *findItem( rName );
49 bool hasItem( const OUString& rName ) const
51 return findItem( rName ) != maItems.end();
54 css::uno::Sequence<OUString> getNames() const
56 // iterate over members, and collect all those that have names
57 std::vector<OUString> aNames;
58 for( const T& rItem : maItems )
60 css::uno::Reference<css::container::XNamed>
61 xNamed( rItem, css::uno::UNO_QUERY );
62 if( xNamed.is() )
63 aNames.push_back( xNamed->getName() );
66 return comphelper::containerToSequence(aNames);
69 protected:
70 typename std::vector<T>::const_iterator findItem( const OUString& rName ) const
72 return std::find_if(maItems.begin(), maItems.end(), [&rName](const T& rItem) {
73 css::uno::Reference<css::container::XNamed>
74 xNamed( rItem, css::uno::UNO_QUERY );
75 return xNamed.is() && xNamed->getName() == rName;
76 });
79 public:
81 // XElementAccess
82 virtual css::uno::Type SAL_CALL getElementType() override
84 return Collection<T>::getElementType();
87 virtual sal_Bool SAL_CALL hasElements() override
89 return Collection<T>::hasElements();
92 // XNameAccess : XElementAccess
93 virtual css::uno::Any SAL_CALL getByName(
94 const OUString& aName ) override
96 if( !hasItem( aName ) )
97 throw css::container::NoSuchElementException();
98 return css::uno::makeAny( getItem( aName ) );
101 virtual css::uno::Sequence<OUString> SAL_CALL getElementNames() override
103 return getNames();
106 virtual sal_Bool SAL_CALL hasByName(
107 const OUString& aName ) override
109 return hasItem( aName );
113 #endif
115 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */