merged tag ooo/OOO330_m14
[LibreOffice.git] / comphelper / source / container / namecontainer.cxx
blob7239a8d573dcb076c6c42af0950c26157dca6c40
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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 #include <comphelper/namecontainer.hxx>
31 #include <cppuhelper/implbase1.hxx>
32 #include <osl/diagnose.h>
33 #include <osl/mutex.hxx>
34 #include <comphelper/stl_types.hxx>
36 DECLARE_STL_USTRINGACCESS_MAP( ::com::sun::star::uno::Any, SvGenericNameContainerMapImpl );
38 namespace comphelper
40 class NameContainerImpl
42 public:
43 osl::Mutex maMutex;
46 /** this is the base helper class for NameContainer thats also declared in this header. */
47 class NameContainer : public ::cppu::WeakImplHelper1< ::com::sun::star::container::XNameContainer >, private NameContainerImpl
49 public:
50 NameContainer( ::com::sun::star::uno::Type aType );
51 virtual ~NameContainer();
53 // XNameContainer
54 virtual void SAL_CALL insertByName( const ::rtl::OUString& aName, const ::com::sun::star::uno::Any& aElement )
55 throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::ElementExistException,
56 ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
57 virtual void SAL_CALL removeByName( const ::rtl::OUString& Name )
58 throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
59 ::com::sun::star::uno::RuntimeException);
61 // XNameReplace
62 virtual void SAL_CALL replaceByName( const ::rtl::OUString& aName, const ::com::sun::star::uno::Any& aElement )
63 throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::NoSuchElementException,
64 ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
66 // XNameAccess
67 virtual ::com::sun::star::uno::Any SAL_CALL getByName( const ::rtl::OUString& aName )
68 throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
69 ::com::sun::star::uno::RuntimeException);
70 virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames( )
71 throw(::com::sun::star::uno::RuntimeException);
72 virtual sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName )
73 throw(::com::sun::star::uno::RuntimeException);
75 // XElementAccess
76 virtual sal_Bool SAL_CALL hasElements( )
77 throw(::com::sun::star::uno::RuntimeException);
78 virtual ::com::sun::star::uno::Type SAL_CALL getElementType( )
79 throw(::com::sun::star::uno::RuntimeException);
81 private:
82 SvGenericNameContainerMapImpl maProperties;
83 const ::com::sun::star::uno::Type maType;
87 using namespace ::comphelper;
88 using namespace ::osl;
89 using namespace ::rtl;
90 using namespace ::com::sun::star::uno;
91 using namespace ::com::sun::star::container;
92 using namespace ::com::sun::star::lang;
95 NameContainer::NameContainer( ::com::sun::star::uno::Type aType )
96 : maType( aType )
100 NameContainer::~NameContainer()
104 // XNameContainer
105 void SAL_CALL NameContainer::insertByName( const rtl::OUString& aName, const Any& aElement )
106 throw(IllegalArgumentException, ElementExistException,
107 WrappedTargetException, RuntimeException)
109 MutexGuard aGuard( maMutex );
111 if( maProperties.find( aName ) != maProperties.end() )
112 throw ElementExistException();
114 if( aElement.getValueType() != maType )
115 throw IllegalArgumentException();
117 maProperties.insert( SvGenericNameContainerMapImpl::value_type(aName,aElement));
120 void SAL_CALL NameContainer::removeByName( const ::rtl::OUString& Name )
121 throw(NoSuchElementException, WrappedTargetException,
122 RuntimeException)
124 MutexGuard aGuard( maMutex );
126 SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( Name );
127 if( aIter == maProperties.end() )
128 throw NoSuchElementException();
130 maProperties.erase( aIter );
133 // XNameReplace
135 void SAL_CALL NameContainer::replaceByName( const ::rtl::OUString& aName, const Any& aElement )
136 throw(IllegalArgumentException, NoSuchElementException,
137 WrappedTargetException, RuntimeException)
139 MutexGuard aGuard( maMutex );
141 SvGenericNameContainerMapImpl::iterator aIter( maProperties.find( aName ) );
142 if( aIter == maProperties.end() )
143 throw NoSuchElementException();
145 if( aElement.getValueType() != maType )
146 throw IllegalArgumentException();
148 (*aIter).second = aElement;
151 // XNameAccess
153 Any SAL_CALL NameContainer::getByName( const ::rtl::OUString& aName )
154 throw(NoSuchElementException, WrappedTargetException,
155 RuntimeException)
157 MutexGuard aGuard( maMutex );
159 SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( aName );
160 if( aIter == maProperties.end() )
161 throw NoSuchElementException();
163 return (*aIter).second;
166 Sequence< ::rtl::OUString > SAL_CALL NameContainer::getElementNames( )
167 throw(RuntimeException)
169 MutexGuard aGuard( maMutex );
171 SvGenericNameContainerMapImpl::iterator aIter = maProperties.begin();
172 const SvGenericNameContainerMapImpl::iterator aEnd = maProperties.end();
174 Sequence< rtl::OUString > aNames( maProperties.size() );
175 rtl::OUString* pNames = aNames.getArray();
177 while( aIter != aEnd )
179 *pNames++ = (*aIter++).first;
182 return aNames;
185 sal_Bool SAL_CALL NameContainer::hasByName( const ::rtl::OUString& aName )
186 throw(RuntimeException)
188 MutexGuard aGuard( maMutex );
190 SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( aName );
191 return aIter != maProperties.end();
194 sal_Bool SAL_CALL NameContainer::hasElements( )
195 throw(RuntimeException)
197 MutexGuard aGuard( maMutex );
199 return !maProperties.empty();
202 Type SAL_CALL NameContainer::getElementType()
203 throw( RuntimeException )
205 return maType;
208 Reference< XNameContainer > comphelper::NameContainer_createInstance( Type aType )
210 return (XNameContainer*) new NameContainer( aType );