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 #ifndef INCLUDED_FORMS_SOURCE_XFORMS_NAMEDCOLLECTION_HXX
21 #define INCLUDED_FORMS_SOURCE_XFORMS_NAMEDCOLLECTION_HXX
23 #include <collection.hxx>
24 #include <cppuhelper/implbase1.hxx>
25 #include <com/sun/star/container/XNameAccess.hpp>
30 class NamedCollection
: public cppu::ImplInheritanceHelper1
<
32 com::sun::star::container::XNameAccess
>
34 using Collection
<T
>::maItems
;
35 using Collection
<T
>::getItem
;
36 using Collection
<T
>::hasItem
;
40 virtual ~NamedCollection() {}
42 const T
& getItem( const OUString
& rName
) const
44 OSL_ENSURE( hasItem( rName
), "invalid name" );
45 return *findItem( rName
);
48 bool hasItem( const OUString
& rName
) const
50 return findItem( rName
) != maItems
.end();
53 typedef com::sun::star::uno::Sequence
<OUString
> Names_t
;
54 Names_t
getNames() const
56 // iterate over members, and collect all those that have names
57 std::vector
<OUString
> aNames
;
58 for( typename
std::vector
<T
>::const_iterator aIter
= maItems
.begin();
59 aIter
!= maItems
.end();
62 com::sun::star::uno::Reference
<com::sun::star::container::XNamed
>
63 xNamed( *aIter
, com::sun::star::uno::UNO_QUERY
);
65 aNames
.push_back( xNamed
->getName() );
68 // copy names to Sequence and return
69 Names_t
aResult( aNames
.size() );
70 OUString
* pStrings
= aResult
.getArray();
71 std::copy( aNames
.begin(), aNames
.end(), pStrings
);
77 typename
std::vector
<T
>::const_iterator
findItem( const OUString
& rName
) const
79 for( typename
std::vector
<T
>::const_iterator aIter
= maItems
.begin();
80 aIter
!= maItems
.end();
83 com::sun::star::uno::Reference
<com::sun::star::container::XNamed
>
84 xNamed( *aIter
, com::sun::star::uno::UNO_QUERY
);
85 if( xNamed
.is() && xNamed
->getName() == rName
)
94 virtual typename Collection
<T
>::Type_t SAL_CALL
getElementType()
95 throw( typename Collection
<T
>::RuntimeException_t
) SAL_OVERRIDE
97 return Collection
<T
>::getElementType();
100 virtual sal_Bool SAL_CALL
hasElements()
101 throw( typename Collection
<T
>::RuntimeException_t
) SAL_OVERRIDE
103 return Collection
<T
>::hasElements();
106 // XNameAccess : XElementAccess
107 virtual typename Collection
<T
>::Any_t SAL_CALL
getByName(
108 const OUString
& aName
)
109 throw( typename Collection
<T
>::NoSuchElementException_t
,
110 typename Collection
<T
>::WrappedTargetException_t
,
111 typename Collection
<T
>::RuntimeException_t
) SAL_OVERRIDE
113 if( hasItem( aName
) )
114 return com::sun::star::uno::makeAny( getItem( aName
) );
116 throw typename Collection
<T
>::NoSuchElementException_t();
120 virtual Names_t SAL_CALL
getElementNames()
121 throw( typename Collection
<T
>::RuntimeException_t
) SAL_OVERRIDE
126 virtual sal_Bool SAL_CALL
hasByName(
127 const OUString
& aName
)
128 throw( typename Collection
<T
>::RuntimeException_t
) SAL_OVERRIDE
130 return hasItem( aName
) ? sal_True
: sal_False
;
136 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */