1*2d785d7eSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*2d785d7eSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*2d785d7eSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*2d785d7eSAndrew Rist * distributed with this work for additional information 6*2d785d7eSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*2d785d7eSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*2d785d7eSAndrew Rist * "License"); you may not use this file except in compliance 9*2d785d7eSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*2d785d7eSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*2d785d7eSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*2d785d7eSAndrew Rist * software distributed under the License is distributed on an 15*2d785d7eSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*2d785d7eSAndrew Rist * KIND, either express or implied. See the License for the 17*2d785d7eSAndrew Rist * specific language governing permissions and limitations 18*2d785d7eSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*2d785d7eSAndrew Rist *************************************************************/ 21*2d785d7eSAndrew Rist 22*2d785d7eSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #ifndef _NAMEDCOLLECTION_HXX 25cdf0e10cSrcweir #define _NAMEDCOLLECTION_HXX 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include <collection.hxx> 28cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx> 29cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp> 30cdf0e10cSrcweir 31cdf0e10cSrcweir #include <algorithm> 32cdf0e10cSrcweir 33cdf0e10cSrcweir template<class T> 34cdf0e10cSrcweir class NamedCollection : public cppu::ImplInheritanceHelper1< 35cdf0e10cSrcweir Collection<T>, 36cdf0e10cSrcweir com::sun::star::container::XNameAccess> 37cdf0e10cSrcweir { 38cdf0e10cSrcweir using Collection<T>::maItems; 39cdf0e10cSrcweir using Collection<T>::getItem; 40cdf0e10cSrcweir using Collection<T>::hasItem; 41cdf0e10cSrcweir 42cdf0e10cSrcweir public: NamedCollection()43cdf0e10cSrcweir NamedCollection() {} ~NamedCollection()44cdf0e10cSrcweir virtual ~NamedCollection() {} 45cdf0e10cSrcweir getItem(const rtl::OUString & rName) const46cdf0e10cSrcweir const T& getItem( const rtl::OUString& rName ) const 47cdf0e10cSrcweir { 48cdf0e10cSrcweir OSL_ENSURE( hasItem( rName ), "invalid name" ); 49cdf0e10cSrcweir return *findItem( rName ); 50cdf0e10cSrcweir } 51cdf0e10cSrcweir hasItem(const rtl::OUString & rName) const52cdf0e10cSrcweir bool hasItem( const rtl::OUString& rName ) const 53cdf0e10cSrcweir { 54cdf0e10cSrcweir return findItem( rName ) != maItems.end(); 55cdf0e10cSrcweir } 56cdf0e10cSrcweir 57cdf0e10cSrcweir typedef com::sun::star::uno::Sequence<rtl::OUString> Names_t; getNames() const58cdf0e10cSrcweir Names_t getNames() const 59cdf0e10cSrcweir { 60cdf0e10cSrcweir // iterate over members, and collect all those that have names 61cdf0e10cSrcweir std::vector<rtl::OUString> aNames; 62cdf0e10cSrcweir for( typename std::vector<T>::const_iterator aIter = maItems.begin(); 63cdf0e10cSrcweir aIter != maItems.end(); 64cdf0e10cSrcweir aIter++ ) 65cdf0e10cSrcweir { 66cdf0e10cSrcweir com::sun::star::uno::Reference<com::sun::star::container::XNamed> 67cdf0e10cSrcweir xNamed( *aIter, com::sun::star::uno::UNO_QUERY ); 68cdf0e10cSrcweir if( xNamed.is() ) 69cdf0e10cSrcweir aNames.push_back( xNamed->getName() ); 70cdf0e10cSrcweir } 71cdf0e10cSrcweir 72cdf0e10cSrcweir // copy names to Sequence and return 73cdf0e10cSrcweir Names_t aResult( aNames.size() ); 74cdf0e10cSrcweir rtl::OUString* pStrings = aResult.getArray(); 75cdf0e10cSrcweir std::copy( aNames.begin(), aNames.end(), pStrings ); 76cdf0e10cSrcweir 77cdf0e10cSrcweir return aResult; 78cdf0e10cSrcweir } 79cdf0e10cSrcweir 80cdf0e10cSrcweir protected: findItem(const rtl::OUString & rName) const81cdf0e10cSrcweir typename std::vector<T>::const_iterator findItem( const rtl::OUString& rName ) const 82cdf0e10cSrcweir { 83cdf0e10cSrcweir for( typename std::vector<T>::const_iterator aIter = maItems.begin(); 84cdf0e10cSrcweir aIter != maItems.end(); 85cdf0e10cSrcweir aIter++ ) 86cdf0e10cSrcweir { 87cdf0e10cSrcweir com::sun::star::uno::Reference<com::sun::star::container::XNamed> 88cdf0e10cSrcweir xNamed( *aIter, com::sun::star::uno::UNO_QUERY ); 89cdf0e10cSrcweir if( xNamed.is() && xNamed->getName() == rName ) 90cdf0e10cSrcweir return aIter; 91cdf0e10cSrcweir } 92cdf0e10cSrcweir return maItems.end(); 93cdf0e10cSrcweir } 94cdf0e10cSrcweir 95cdf0e10cSrcweir public: 96cdf0e10cSrcweir 97cdf0e10cSrcweir // XElementAccess getElementType()98cdf0e10cSrcweir virtual typename Collection<T>::Type_t SAL_CALL getElementType() 99cdf0e10cSrcweir throw( typename Collection<T>::RuntimeException_t ) 100cdf0e10cSrcweir { 101cdf0e10cSrcweir return Collection<T>::getElementType(); 102cdf0e10cSrcweir } 103cdf0e10cSrcweir hasElements()104cdf0e10cSrcweir virtual sal_Bool SAL_CALL hasElements() 105cdf0e10cSrcweir throw( typename Collection<T>::RuntimeException_t ) 106cdf0e10cSrcweir { 107cdf0e10cSrcweir return Collection<T>::hasElements(); 108cdf0e10cSrcweir } 109cdf0e10cSrcweir 110cdf0e10cSrcweir // XNameAccess : XElementAccess getByName(const rtl::OUString & aName)111cdf0e10cSrcweir virtual typename Collection<T>::Any_t SAL_CALL getByName( 112cdf0e10cSrcweir const rtl::OUString& aName ) 113cdf0e10cSrcweir throw( typename Collection<T>::NoSuchElementException_t, 114cdf0e10cSrcweir typename Collection<T>::WrappedTargetException_t, 115cdf0e10cSrcweir typename Collection<T>::RuntimeException_t ) 116cdf0e10cSrcweir { 117cdf0e10cSrcweir if( hasItem( aName ) ) 118cdf0e10cSrcweir return com::sun::star::uno::makeAny( getItem( aName ) ); 119cdf0e10cSrcweir else 120cdf0e10cSrcweir throw typename Collection<T>::NoSuchElementException_t(); 121cdf0e10cSrcweir 122cdf0e10cSrcweir } 123cdf0e10cSrcweir getElementNames()124cdf0e10cSrcweir virtual Names_t SAL_CALL getElementNames() 125cdf0e10cSrcweir throw( typename Collection<T>::RuntimeException_t ) 126cdf0e10cSrcweir { 127cdf0e10cSrcweir return getNames(); 128cdf0e10cSrcweir } 129cdf0e10cSrcweir hasByName(const rtl::OUString & aName)130cdf0e10cSrcweir virtual sal_Bool SAL_CALL hasByName( 131cdf0e10cSrcweir const rtl::OUString& aName ) 132cdf0e10cSrcweir throw( typename Collection<T>::RuntimeException_t ) 133cdf0e10cSrcweir { 134cdf0e10cSrcweir return hasItem( aName ) ? sal_True : sal_False; 135cdf0e10cSrcweir } 136cdf0e10cSrcweir }; 137cdf0e10cSrcweir 138cdf0e10cSrcweir #endif 139