1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: namedcollection.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef _NAMEDCOLLECTION_HXX
32 #define _NAMEDCOLLECTION_HXX
34 #include <collection.hxx>
35 #include <cppuhelper/implbase1.hxx>
36 #include <com/sun/star/container/XNameAccess.hpp>
41 class NamedCollection
: public cppu::ImplInheritanceHelper1
<
43 com::sun::star::container::XNameAccess
>
45 using Collection
<T
>::maItems
;
46 using Collection
<T
>::getItem
;
47 using Collection
<T
>::hasItem
;
51 virtual ~NamedCollection() {}
53 const T
& getItem( const rtl::OUString
& rName
) const
55 OSL_ENSURE( hasItem( rName
), "invalid name" );
56 return *findItem( rName
);
59 bool hasItem( const rtl::OUString
& rName
) const
61 return findItem( rName
) != maItems
.end();
64 typedef com::sun::star::uno::Sequence
<rtl::OUString
> Names_t
;
65 Names_t
getNames() const
67 // iterate over members, and collect all those that have names
68 std::vector
<rtl::OUString
> aNames
;
69 for( typename
std::vector
<T
>::const_iterator aIter
= maItems
.begin();
70 aIter
!= maItems
.end();
73 com::sun::star::uno::Reference
<com::sun::star::container::XNamed
>
74 xNamed( *aIter
, com::sun::star::uno::UNO_QUERY
);
76 aNames
.push_back( xNamed
->getName() );
79 // copy names to Sequence and return
80 Names_t
aResult( aNames
.size() );
81 rtl::OUString
* pStrings
= aResult
.getArray();
82 std::copy( aNames
.begin(), aNames
.end(), pStrings
);
88 typename
std::vector
<T
>::const_iterator
findItem( const rtl::OUString
& rName
) const
90 for( typename
std::vector
<T
>::const_iterator aIter
= maItems
.begin();
91 aIter
!= maItems
.end();
94 com::sun::star::uno::Reference
<com::sun::star::container::XNamed
>
95 xNamed( *aIter
, com::sun::star::uno::UNO_QUERY
);
96 if( xNamed
.is() && xNamed
->getName() == rName
)
105 virtual typename Collection
<T
>::Type_t SAL_CALL
getElementType()
106 throw( typename Collection
<T
>::RuntimeException_t
)
108 return Collection
<T
>::getElementType();
111 virtual sal_Bool SAL_CALL
hasElements()
112 throw( typename Collection
<T
>::RuntimeException_t
)
114 return Collection
<T
>::hasElements();
117 // XNameAccess : XElementAccess
118 virtual typename Collection
<T
>::Any_t SAL_CALL
getByName(
119 const rtl::OUString
& aName
)
120 throw( typename Collection
<T
>::NoSuchElementException_t
,
121 typename Collection
<T
>::WrappedTargetException_t
,
122 typename Collection
<T
>::RuntimeException_t
)
124 if( hasItem( aName
) )
125 return com::sun::star::uno::makeAny( getItem( aName
) );
127 throw typename Collection
<T
>::NoSuchElementException_t();
131 virtual Names_t SAL_CALL
getElementNames()
132 throw( typename Collection
<T
>::RuntimeException_t
)
137 virtual sal_Bool SAL_CALL
hasByName(
138 const rtl::OUString
& aName
)
139 throw( typename Collection
<T
>::RuntimeException_t
)
141 return hasItem( aName
) ? sal_True
: sal_False
;