1dde7d3faSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3dde7d3faSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4dde7d3faSAndrew Rist * or more contributor license agreements. See the NOTICE file 5dde7d3faSAndrew Rist * distributed with this work for additional information 6dde7d3faSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7dde7d3faSAndrew Rist * to you under the Apache License, Version 2.0 (the 8dde7d3faSAndrew Rist * "License"); you may not use this file except in compliance 9dde7d3faSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11dde7d3faSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13dde7d3faSAndrew Rist * Unless required by applicable law or agreed to in writing, 14dde7d3faSAndrew Rist * software distributed under the License is distributed on an 15dde7d3faSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16dde7d3faSAndrew Rist * KIND, either express or implied. See the License for the 17dde7d3faSAndrew Rist * specific language governing permissions and limitations 18dde7d3faSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20dde7d3faSAndrew Rist *************************************************************/ 21dde7d3faSAndrew Rist 22dde7d3faSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_comphelper.hxx" 26cdf0e10cSrcweir #include <comphelper/namecontainer.hxx> 27cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx> 28cdf0e10cSrcweir #include <osl/diagnose.h> 29cdf0e10cSrcweir #include <osl/mutex.hxx> 30cdf0e10cSrcweir #include <comphelper/stl_types.hxx> 31cdf0e10cSrcweir 32cdf0e10cSrcweir DECLARE_STL_USTRINGACCESS_MAP( ::com::sun::star::uno::Any, SvGenericNameContainerMapImpl ); 33cdf0e10cSrcweir 34cdf0e10cSrcweir namespace comphelper 35cdf0e10cSrcweir { 36cdf0e10cSrcweir class NameContainerImpl 37cdf0e10cSrcweir { 38cdf0e10cSrcweir public: 39cdf0e10cSrcweir osl::Mutex maMutex; 40cdf0e10cSrcweir }; 41cdf0e10cSrcweir 42*cfd52e18Smseidel /** this is the base helper class for NameContainer that's also declared in this header. */ 43cdf0e10cSrcweir class NameContainer : public ::cppu::WeakImplHelper1< ::com::sun::star::container::XNameContainer >, private NameContainerImpl 44cdf0e10cSrcweir { 45cdf0e10cSrcweir public: 46cdf0e10cSrcweir NameContainer( ::com::sun::star::uno::Type aType ); 47cdf0e10cSrcweir virtual ~NameContainer(); 48cdf0e10cSrcweir 49cdf0e10cSrcweir // XNameContainer 50cdf0e10cSrcweir virtual void SAL_CALL insertByName( const ::rtl::OUString& aName, const ::com::sun::star::uno::Any& aElement ) 51cdf0e10cSrcweir throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::ElementExistException, 52cdf0e10cSrcweir ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException); 53cdf0e10cSrcweir virtual void SAL_CALL removeByName( const ::rtl::OUString& Name ) 54cdf0e10cSrcweir throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException, 55cdf0e10cSrcweir ::com::sun::star::uno::RuntimeException); 56cdf0e10cSrcweir 57cdf0e10cSrcweir // XNameReplace 58cdf0e10cSrcweir virtual void SAL_CALL replaceByName( const ::rtl::OUString& aName, const ::com::sun::star::uno::Any& aElement ) 59cdf0e10cSrcweir throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::NoSuchElementException, 60cdf0e10cSrcweir ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException); 61cdf0e10cSrcweir 62cdf0e10cSrcweir // XNameAccess 63cdf0e10cSrcweir virtual ::com::sun::star::uno::Any SAL_CALL getByName( const ::rtl::OUString& aName ) 64cdf0e10cSrcweir throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException, 65cdf0e10cSrcweir ::com::sun::star::uno::RuntimeException); 66cdf0e10cSrcweir virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames( ) 67cdf0e10cSrcweir throw(::com::sun::star::uno::RuntimeException); 68cdf0e10cSrcweir virtual sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) 69cdf0e10cSrcweir throw(::com::sun::star::uno::RuntimeException); 70cdf0e10cSrcweir 71cdf0e10cSrcweir // XElementAccess 72cdf0e10cSrcweir virtual sal_Bool SAL_CALL hasElements( ) 73cdf0e10cSrcweir throw(::com::sun::star::uno::RuntimeException); 74cdf0e10cSrcweir virtual ::com::sun::star::uno::Type SAL_CALL getElementType( ) 75cdf0e10cSrcweir throw(::com::sun::star::uno::RuntimeException); 76cdf0e10cSrcweir 77cdf0e10cSrcweir private: 78cdf0e10cSrcweir SvGenericNameContainerMapImpl maProperties; 79cdf0e10cSrcweir const ::com::sun::star::uno::Type maType; 80cdf0e10cSrcweir }; 81cdf0e10cSrcweir } 82cdf0e10cSrcweir 83cdf0e10cSrcweir using namespace ::comphelper; 84cdf0e10cSrcweir using namespace ::osl; 85cdf0e10cSrcweir using namespace ::rtl; 86cdf0e10cSrcweir using namespace ::com::sun::star::uno; 87cdf0e10cSrcweir using namespace ::com::sun::star::container; 88cdf0e10cSrcweir using namespace ::com::sun::star::lang; 89cdf0e10cSrcweir 90cdf0e10cSrcweir 91cdf0e10cSrcweir NameContainer::NameContainer( ::com::sun::star::uno::Type aType ) 92cdf0e10cSrcweir : maType( aType ) 93cdf0e10cSrcweir { 94cdf0e10cSrcweir } 95cdf0e10cSrcweir 96cdf0e10cSrcweir NameContainer::~NameContainer() 97cdf0e10cSrcweir { 98cdf0e10cSrcweir } 99cdf0e10cSrcweir 100cdf0e10cSrcweir // XNameContainer 101cdf0e10cSrcweir void SAL_CALL NameContainer::insertByName( const rtl::OUString& aName, const Any& aElement ) 102cdf0e10cSrcweir throw(IllegalArgumentException, ElementExistException, 103cdf0e10cSrcweir WrappedTargetException, RuntimeException) 104cdf0e10cSrcweir { 105cdf0e10cSrcweir MutexGuard aGuard( maMutex ); 106cdf0e10cSrcweir 107cdf0e10cSrcweir if( maProperties.find( aName ) != maProperties.end() ) 108cdf0e10cSrcweir throw ElementExistException(); 109cdf0e10cSrcweir 110cdf0e10cSrcweir if( aElement.getValueType() != maType ) 111cdf0e10cSrcweir throw IllegalArgumentException(); 112cdf0e10cSrcweir 113cdf0e10cSrcweir maProperties.insert( SvGenericNameContainerMapImpl::value_type(aName,aElement)); 114cdf0e10cSrcweir } 115cdf0e10cSrcweir 116cdf0e10cSrcweir void SAL_CALL NameContainer::removeByName( const ::rtl::OUString& Name ) 117cdf0e10cSrcweir throw(NoSuchElementException, WrappedTargetException, 118cdf0e10cSrcweir RuntimeException) 119cdf0e10cSrcweir { 120cdf0e10cSrcweir MutexGuard aGuard( maMutex ); 121cdf0e10cSrcweir 122cdf0e10cSrcweir SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( Name ); 123cdf0e10cSrcweir if( aIter == maProperties.end() ) 124cdf0e10cSrcweir throw NoSuchElementException(); 125cdf0e10cSrcweir 126cdf0e10cSrcweir maProperties.erase( aIter ); 127cdf0e10cSrcweir } 128cdf0e10cSrcweir 129cdf0e10cSrcweir // XNameReplace 130cdf0e10cSrcweir 131cdf0e10cSrcweir void SAL_CALL NameContainer::replaceByName( const ::rtl::OUString& aName, const Any& aElement ) 132cdf0e10cSrcweir throw(IllegalArgumentException, NoSuchElementException, 133cdf0e10cSrcweir WrappedTargetException, RuntimeException) 134cdf0e10cSrcweir { 135cdf0e10cSrcweir MutexGuard aGuard( maMutex ); 136cdf0e10cSrcweir 137cdf0e10cSrcweir SvGenericNameContainerMapImpl::iterator aIter( maProperties.find( aName ) ); 138cdf0e10cSrcweir if( aIter == maProperties.end() ) 139cdf0e10cSrcweir throw NoSuchElementException(); 140cdf0e10cSrcweir 141cdf0e10cSrcweir if( aElement.getValueType() != maType ) 142cdf0e10cSrcweir throw IllegalArgumentException(); 143cdf0e10cSrcweir 144cdf0e10cSrcweir (*aIter).second = aElement; 145cdf0e10cSrcweir } 146cdf0e10cSrcweir 147cdf0e10cSrcweir // XNameAccess 148cdf0e10cSrcweir 149cdf0e10cSrcweir Any SAL_CALL NameContainer::getByName( const ::rtl::OUString& aName ) 150cdf0e10cSrcweir throw(NoSuchElementException, WrappedTargetException, 151cdf0e10cSrcweir RuntimeException) 152cdf0e10cSrcweir { 153cdf0e10cSrcweir MutexGuard aGuard( maMutex ); 154cdf0e10cSrcweir 155cdf0e10cSrcweir SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( aName ); 156cdf0e10cSrcweir if( aIter == maProperties.end() ) 157cdf0e10cSrcweir throw NoSuchElementException(); 158cdf0e10cSrcweir 159cdf0e10cSrcweir return (*aIter).second; 160cdf0e10cSrcweir } 161cdf0e10cSrcweir 162cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL NameContainer::getElementNames( ) 163cdf0e10cSrcweir throw(RuntimeException) 164cdf0e10cSrcweir { 165cdf0e10cSrcweir MutexGuard aGuard( maMutex ); 166cdf0e10cSrcweir 167cdf0e10cSrcweir SvGenericNameContainerMapImpl::iterator aIter = maProperties.begin(); 168cdf0e10cSrcweir const SvGenericNameContainerMapImpl::iterator aEnd = maProperties.end(); 169cdf0e10cSrcweir 170cdf0e10cSrcweir Sequence< rtl::OUString > aNames( maProperties.size() ); 171cdf0e10cSrcweir rtl::OUString* pNames = aNames.getArray(); 172cdf0e10cSrcweir 173cdf0e10cSrcweir while( aIter != aEnd ) 174cdf0e10cSrcweir { 175cdf0e10cSrcweir *pNames++ = (*aIter++).first; 176cdf0e10cSrcweir } 177cdf0e10cSrcweir 178cdf0e10cSrcweir return aNames; 179cdf0e10cSrcweir } 180cdf0e10cSrcweir 181cdf0e10cSrcweir sal_Bool SAL_CALL NameContainer::hasByName( const ::rtl::OUString& aName ) 182cdf0e10cSrcweir throw(RuntimeException) 183cdf0e10cSrcweir { 184cdf0e10cSrcweir MutexGuard aGuard( maMutex ); 185cdf0e10cSrcweir 186cdf0e10cSrcweir SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( aName ); 187cdf0e10cSrcweir return aIter != maProperties.end(); 188cdf0e10cSrcweir } 189cdf0e10cSrcweir 190cdf0e10cSrcweir sal_Bool SAL_CALL NameContainer::hasElements( ) 191cdf0e10cSrcweir throw(RuntimeException) 192cdf0e10cSrcweir { 193cdf0e10cSrcweir MutexGuard aGuard( maMutex ); 194cdf0e10cSrcweir 195cdf0e10cSrcweir return !maProperties.empty(); 196cdf0e10cSrcweir } 197cdf0e10cSrcweir 198cdf0e10cSrcweir Type SAL_CALL NameContainer::getElementType() 199cdf0e10cSrcweir throw( RuntimeException ) 200cdf0e10cSrcweir { 201cdf0e10cSrcweir return maType; 202cdf0e10cSrcweir } 203cdf0e10cSrcweir 204cdf0e10cSrcweir Reference< XNameContainer > comphelper::NameContainer_createInstance( Type aType ) 205cdf0e10cSrcweir { 206cdf0e10cSrcweir return (XNameContainer*) new NameContainer( aType ); 207cdf0e10cSrcweir } 208