masterfix DEV300: #i10000# build fix
[LibreOffice.git] / forms / source / xforms / namedcollection.hxx
blobffae93ed2c90e8cf63f7fddda310f0550a623545
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 #ifndef _NAMEDCOLLECTION_HXX
29 #define _NAMEDCOLLECTION_HXX
31 #include <collection.hxx>
32 #include <cppuhelper/implbase1.hxx>
33 #include <com/sun/star/container/XNameAccess.hpp>
35 #include <algorithm>
37 template<class T>
38 class NamedCollection : public cppu::ImplInheritanceHelper1<
39 Collection<T>,
40 com::sun::star::container::XNameAccess>
42 using Collection<T>::maItems;
43 using Collection<T>::getItem;
44 using Collection<T>::hasItem;
46 public:
47 NamedCollection() {}
48 virtual ~NamedCollection() {}
50 const T& getItem( const rtl::OUString& rName ) const
52 OSL_ENSURE( hasItem( rName ), "invalid name" );
53 return *findItem( rName );
56 bool hasItem( const rtl::OUString& rName ) const
58 return findItem( rName ) != maItems.end();
61 typedef com::sun::star::uno::Sequence<rtl::OUString> Names_t;
62 Names_t getNames() const
64 // iterate over members, and collect all those that have names
65 std::vector<rtl::OUString> aNames;
66 for( typename std::vector<T>::const_iterator aIter = maItems.begin();
67 aIter != maItems.end();
68 aIter++ )
70 com::sun::star::uno::Reference<com::sun::star::container::XNamed>
71 xNamed( *aIter, com::sun::star::uno::UNO_QUERY );
72 if( xNamed.is() )
73 aNames.push_back( xNamed->getName() );
76 // copy names to Sequence and return
77 Names_t aResult( aNames.size() );
78 rtl::OUString* pStrings = aResult.getArray();
79 std::copy( aNames.begin(), aNames.end(), pStrings );
81 return aResult;
84 protected:
85 typename std::vector<T>::const_iterator findItem( const rtl::OUString& rName ) const
87 for( typename std::vector<T>::const_iterator aIter = maItems.begin();
88 aIter != maItems.end();
89 aIter++ )
91 com::sun::star::uno::Reference<com::sun::star::container::XNamed>
92 xNamed( *aIter, com::sun::star::uno::UNO_QUERY );
93 if( xNamed.is() && xNamed->getName() == rName )
94 return aIter;
96 return maItems.end();
99 public:
101 // XElementAccess
102 virtual typename Collection<T>::Type_t SAL_CALL getElementType()
103 throw( typename Collection<T>::RuntimeException_t )
105 return Collection<T>::getElementType();
108 virtual sal_Bool SAL_CALL hasElements()
109 throw( typename Collection<T>::RuntimeException_t )
111 return Collection<T>::hasElements();
114 // XNameAccess : XElementAccess
115 virtual typename Collection<T>::Any_t SAL_CALL getByName(
116 const rtl::OUString& aName )
117 throw( typename Collection<T>::NoSuchElementException_t,
118 typename Collection<T>::WrappedTargetException_t,
119 typename Collection<T>::RuntimeException_t )
121 if( hasItem( aName ) )
122 return com::sun::star::uno::makeAny( getItem( aName ) );
123 else
124 throw typename Collection<T>::NoSuchElementException_t();
128 virtual Names_t SAL_CALL getElementNames()
129 throw( typename Collection<T>::RuntimeException_t )
131 return getNames();
134 virtual sal_Bool SAL_CALL hasByName(
135 const rtl::OUString& aName )
136 throw( typename Collection<T>::RuntimeException_t )
138 return hasItem( aName ) ? sal_True : sal_False;
142 #endif