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 10*2d785d7eSAndrew Rist * 11*2d785d7eSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*2d785d7eSAndrew Rist * 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. 19*2d785d7eSAndrew Rist * 20*2d785d7eSAndrew Rist *************************************************************/ 21*2d785d7eSAndrew Rist 22*2d785d7eSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #ifndef _NAMECONTAINER_HXX 25cdf0e10cSrcweir #define _NAMECONTAINER_HXX 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx> 28cdf0e10cSrcweir #include <map> 29cdf0e10cSrcweir 30cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp> 31cdf0e10cSrcweir #include <com/sun/star/container/NoSuchElementException.hpp> 32cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp> 33cdf0e10cSrcweir #include <com/sun/star/lang/WrappedTargetException.hpp> 34cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx> 35cdf0e10cSrcweir #include <com/sun/star/uno/Reference.hxx> 36cdf0e10cSrcweir #include <com/sun/star/uno/RuntimeException.hpp> 37cdf0e10cSrcweir #include <com/sun/star/uno/Type.hxx> 38cdf0e10cSrcweir 39cdf0e10cSrcweir typedef cppu::WeakImplHelper1< 40cdf0e10cSrcweir com::sun::star::container::XNameContainer 41cdf0e10cSrcweir > NameContainer_t; 42cdf0e10cSrcweir 43cdf0e10cSrcweir template<class T> 44cdf0e10cSrcweir class NameContainer : public NameContainer_t 45cdf0e10cSrcweir { 46cdf0e10cSrcweir protected: 47cdf0e10cSrcweir typedef std::map<rtl::OUString,T> map_t; 48cdf0e10cSrcweir map_t maItems; 49cdf0e10cSrcweir 50cdf0e10cSrcweir 51cdf0e10cSrcweir bool hasItems() 52cdf0e10cSrcweir { 53cdf0e10cSrcweir return ! maItems.empty(); 54cdf0e10cSrcweir } 55cdf0e10cSrcweir 56cdf0e10cSrcweir typename map_t::const_iterator findItem( const rtl::OUString& rName ) 57cdf0e10cSrcweir { 58cdf0e10cSrcweir return maItems.find( rName ); 59cdf0e10cSrcweir } 60cdf0e10cSrcweir 61cdf0e10cSrcweir bool hasItem( const rtl::OUString& rName ) 62cdf0e10cSrcweir { 63cdf0e10cSrcweir return findItem( rName ) != maItems.end(); 64cdf0e10cSrcweir } 65cdf0e10cSrcweir 66cdf0e10cSrcweir T getItem( const rtl::OUString& rName ) 67cdf0e10cSrcweir { 68cdf0e10cSrcweir OSL_ENSURE( hasItem( rName ), "can't get non-existant item" ); 69cdf0e10cSrcweir return maItems[ rName ]; 70cdf0e10cSrcweir } 71cdf0e10cSrcweir 72cdf0e10cSrcweir 73cdf0e10cSrcweir void replace( const rtl::OUString& rName, 74cdf0e10cSrcweir const T& aElement ) 75cdf0e10cSrcweir { 76cdf0e10cSrcweir OSL_ENSURE( hasItem( rName ), "unknown item" ); 77cdf0e10cSrcweir maItems[ rName ] = aElement; 78cdf0e10cSrcweir } 79cdf0e10cSrcweir 80cdf0e10cSrcweir void insert( const rtl::OUString& rName, 81cdf0e10cSrcweir const T& aElement ) 82cdf0e10cSrcweir { 83cdf0e10cSrcweir OSL_ENSURE( ! hasItem( rName ), "item already in set" ); 84cdf0e10cSrcweir maItems[ rName ] = aElement; 85cdf0e10cSrcweir } 86cdf0e10cSrcweir 87cdf0e10cSrcweir void remove( const rtl::OUString& rName ) 88cdf0e10cSrcweir { 89cdf0e10cSrcweir OSL_ENSURE( hasItem( rName ), "item not in set" ); 90cdf0e10cSrcweir maItems.erase( rName ); 91cdf0e10cSrcweir } 92cdf0e10cSrcweir 93cdf0e10cSrcweir 94cdf0e10cSrcweir public: 95cdf0e10cSrcweir 96cdf0e10cSrcweir NameContainer() {} 97cdf0e10cSrcweir virtual ~NameContainer() {} 98cdf0e10cSrcweir 99cdf0e10cSrcweir // 100cdf0e10cSrcweir // methods for XElementAccess 101cdf0e10cSrcweir // 102cdf0e10cSrcweir 103cdf0e10cSrcweir virtual com::sun::star::uno::Type SAL_CALL getElementType() 104cdf0e10cSrcweir throw( com::sun::star::uno::RuntimeException ) 105cdf0e10cSrcweir { 106cdf0e10cSrcweir return getCppuType( static_cast<T*>( NULL ) ); 107cdf0e10cSrcweir } 108cdf0e10cSrcweir 109cdf0e10cSrcweir virtual sal_Bool SAL_CALL hasElements() 110cdf0e10cSrcweir throw( com::sun::star::uno::RuntimeException ) 111cdf0e10cSrcweir { 112cdf0e10cSrcweir return hasItems(); 113cdf0e10cSrcweir } 114cdf0e10cSrcweir 115cdf0e10cSrcweir 116cdf0e10cSrcweir // 117cdf0e10cSrcweir // methods for XNameAccess (inherits XElementAccess) 118cdf0e10cSrcweir // 119cdf0e10cSrcweir 120cdf0e10cSrcweir virtual com::sun::star::uno::Any SAL_CALL getByName( 121cdf0e10cSrcweir const rtl::OUString& rName ) 122cdf0e10cSrcweir throw( com::sun::star::container::NoSuchElementException, 123cdf0e10cSrcweir com::sun::star::lang::WrappedTargetException, 124cdf0e10cSrcweir com::sun::star::uno::RuntimeException ) 125cdf0e10cSrcweir { 126cdf0e10cSrcweir typename map_t::const_iterator aIter = findItem( rName ); 127cdf0e10cSrcweir if( aIter == maItems.end() ) 128cdf0e10cSrcweir throw com::sun::star::container::NoSuchElementException(); 129cdf0e10cSrcweir else 130cdf0e10cSrcweir return com::sun::star::uno::makeAny( aIter->second ); 131cdf0e10cSrcweir } 132cdf0e10cSrcweir 133cdf0e10cSrcweir virtual com::sun::star::uno::Sequence<rtl::OUString> SAL_CALL getElementNames() 134cdf0e10cSrcweir throw( com::sun::star::uno::RuntimeException ) 135cdf0e10cSrcweir { 136cdf0e10cSrcweir com::sun::star::uno::Sequence<rtl::OUString> aSequence( maItems.size() ); 137cdf0e10cSrcweir typename map_t::const_iterator aIter = maItems.begin(); 138cdf0e10cSrcweir rtl::OUString* pStrings = aSequence.getArray(); 139cdf0e10cSrcweir while( aIter != maItems.end() ) 140cdf0e10cSrcweir { 141cdf0e10cSrcweir *pStrings = aIter->first; 142cdf0e10cSrcweir ++aIter; 143cdf0e10cSrcweir ++pStrings; 144cdf0e10cSrcweir } 145cdf0e10cSrcweir OSL_ENSURE( pStrings == aSequence.getArray() + aSequence.getLength(), 146cdf0e10cSrcweir "sequence not of right size; possible buffer overflow" ); 147cdf0e10cSrcweir return aSequence; 148cdf0e10cSrcweir } 149cdf0e10cSrcweir 150cdf0e10cSrcweir virtual sal_Bool SAL_CALL hasByName( 151cdf0e10cSrcweir const rtl::OUString& rName ) 152cdf0e10cSrcweir throw( com::sun::star::uno::RuntimeException ) 153cdf0e10cSrcweir { 154cdf0e10cSrcweir return hasItem( rName ); 155cdf0e10cSrcweir } 156cdf0e10cSrcweir 157cdf0e10cSrcweir 158cdf0e10cSrcweir // 159cdf0e10cSrcweir // methods for XNameReplace (inherits XNameAccess) 160cdf0e10cSrcweir // 161cdf0e10cSrcweir 162cdf0e10cSrcweir virtual void SAL_CALL replaceByName( 163cdf0e10cSrcweir const rtl::OUString& rName, 164cdf0e10cSrcweir const com::sun::star::uno::Any& aElement ) 165cdf0e10cSrcweir throw( com::sun::star::lang::IllegalArgumentException, 166cdf0e10cSrcweir com::sun::star::container::NoSuchElementException, 167cdf0e10cSrcweir com::sun::star::lang::WrappedTargetException, 168cdf0e10cSrcweir com::sun::star::uno::RuntimeException) 169cdf0e10cSrcweir { 170cdf0e10cSrcweir T aItem; 171cdf0e10cSrcweir if( aElement >>= aItem ) 172cdf0e10cSrcweir if( hasByName( rName ) ) 173cdf0e10cSrcweir replace( rName, aItem ); 174cdf0e10cSrcweir else 175cdf0e10cSrcweir throw com::sun::star::container::NoSuchElementException(); 176cdf0e10cSrcweir else 177cdf0e10cSrcweir throw com::sun::star::lang::IllegalArgumentException(); 178cdf0e10cSrcweir } 179cdf0e10cSrcweir 180cdf0e10cSrcweir 181cdf0e10cSrcweir // 182cdf0e10cSrcweir // methods for XNameContainer (inherits XNameReplace) 183cdf0e10cSrcweir // 184cdf0e10cSrcweir 185cdf0e10cSrcweir virtual void SAL_CALL insertByName( 186cdf0e10cSrcweir const rtl::OUString& rName, 187cdf0e10cSrcweir const com::sun::star::uno::Any& aElement ) 188cdf0e10cSrcweir throw( com::sun::star::lang::IllegalArgumentException, 189cdf0e10cSrcweir com::sun::star::container::ElementExistException, 190cdf0e10cSrcweir com::sun::star::lang::WrappedTargetException, 191cdf0e10cSrcweir com::sun::star::uno::RuntimeException ) 192cdf0e10cSrcweir { 193cdf0e10cSrcweir T aItem; 194cdf0e10cSrcweir if( aElement >>= aItem ) 195cdf0e10cSrcweir if( ! hasByName( rName ) ) 196cdf0e10cSrcweir insert( rName, aItem ); 197cdf0e10cSrcweir else 198cdf0e10cSrcweir throw com::sun::star::container::ElementExistException(); 199cdf0e10cSrcweir else 200cdf0e10cSrcweir throw com::sun::star::lang::IllegalArgumentException(); 201cdf0e10cSrcweir } 202cdf0e10cSrcweir 203cdf0e10cSrcweir virtual void SAL_CALL removeByName( 204cdf0e10cSrcweir const rtl::OUString& rName ) 205cdf0e10cSrcweir throw( com::sun::star::container::NoSuchElementException, 206cdf0e10cSrcweir com::sun::star::lang::WrappedTargetException, 207cdf0e10cSrcweir com::sun::star::uno::RuntimeException) 208cdf0e10cSrcweir { 209cdf0e10cSrcweir if( hasByName( rName ) ) 210cdf0e10cSrcweir remove( rName ); 211cdf0e10cSrcweir else 212cdf0e10cSrcweir throw com::sun::star::container::NoSuchElementException(); 213cdf0e10cSrcweir } 214cdf0e10cSrcweir 215cdf0e10cSrcweir }; 216cdf0e10cSrcweir 217cdf0e10cSrcweir #endif 218