merge the formfield patch from ooo-build
[ooovba.git] / comphelper / source / container / namecontainer.cxx
blob841e60bbf7a3b23850007d5eb15813247731f8b5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: namecontainer.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
33 #include <comphelper/namecontainer.hxx>
34 #include <cppuhelper/implbase1.hxx>
35 #include <osl/diagnose.h>
36 #include <osl/mutex.hxx>
37 #include <comphelper/stl_types.hxx>
39 DECLARE_STL_USTRINGACCESS_MAP( ::com::sun::star::uno::Any, SvGenericNameContainerMapImpl );
41 namespace comphelper
43 class NameContainerImpl
45 public:
46 osl::Mutex maMutex;
49 /** this is the base helper class for NameContainer thats also declared in this header. */
50 class NameContainer : public ::cppu::WeakImplHelper1< ::com::sun::star::container::XNameContainer >, private NameContainerImpl
52 public:
53 NameContainer( ::com::sun::star::uno::Type aType );
54 virtual ~NameContainer();
56 // XNameContainer
57 virtual void SAL_CALL insertByName( const ::rtl::OUString& aName, const ::com::sun::star::uno::Any& aElement )
58 throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::ElementExistException,
59 ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
60 virtual void SAL_CALL removeByName( const ::rtl::OUString& Name )
61 throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
62 ::com::sun::star::uno::RuntimeException);
64 // XNameReplace
65 virtual void SAL_CALL replaceByName( const ::rtl::OUString& aName, const ::com::sun::star::uno::Any& aElement )
66 throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::NoSuchElementException,
67 ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
69 // XNameAccess
70 virtual ::com::sun::star::uno::Any SAL_CALL getByName( const ::rtl::OUString& aName )
71 throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
72 ::com::sun::star::uno::RuntimeException);
73 virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames( )
74 throw(::com::sun::star::uno::RuntimeException);
75 virtual sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName )
76 throw(::com::sun::star::uno::RuntimeException);
78 // XElementAccess
79 virtual sal_Bool SAL_CALL hasElements( )
80 throw(::com::sun::star::uno::RuntimeException);
81 virtual ::com::sun::star::uno::Type SAL_CALL getElementType( )
82 throw(::com::sun::star::uno::RuntimeException);
84 private:
85 SvGenericNameContainerMapImpl maProperties;
86 const ::com::sun::star::uno::Type maType;
90 using namespace ::comphelper;
91 using namespace ::osl;
92 using namespace ::rtl;
93 using namespace ::com::sun::star::uno;
94 using namespace ::com::sun::star::container;
95 using namespace ::com::sun::star::lang;
98 NameContainer::NameContainer( ::com::sun::star::uno::Type aType )
99 : maType( aType )
103 NameContainer::~NameContainer()
107 // XNameContainer
108 void SAL_CALL NameContainer::insertByName( const rtl::OUString& aName, const Any& aElement )
109 throw(IllegalArgumentException, ElementExistException,
110 WrappedTargetException, RuntimeException)
112 MutexGuard aGuard( maMutex );
114 if( maProperties.find( aName ) != maProperties.end() )
115 throw ElementExistException();
117 if( aElement.getValueType() != maType )
118 throw IllegalArgumentException();
120 maProperties.insert( SvGenericNameContainerMapImpl::value_type(aName,aElement));
123 void SAL_CALL NameContainer::removeByName( const ::rtl::OUString& Name )
124 throw(NoSuchElementException, WrappedTargetException,
125 RuntimeException)
127 MutexGuard aGuard( maMutex );
129 SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( Name );
130 if( aIter == maProperties.end() )
131 throw NoSuchElementException();
133 maProperties.erase( aIter );
136 // XNameReplace
138 void SAL_CALL NameContainer::replaceByName( const ::rtl::OUString& aName, const Any& aElement )
139 throw(IllegalArgumentException, NoSuchElementException,
140 WrappedTargetException, RuntimeException)
142 MutexGuard aGuard( maMutex );
144 SvGenericNameContainerMapImpl::iterator aIter( maProperties.find( aName ) );
145 if( aIter == maProperties.end() )
146 throw NoSuchElementException();
148 if( aElement.getValueType() != maType )
149 throw IllegalArgumentException();
151 (*aIter).second = aElement;
154 // XNameAccess
156 Any SAL_CALL NameContainer::getByName( const ::rtl::OUString& aName )
157 throw(NoSuchElementException, WrappedTargetException,
158 RuntimeException)
160 MutexGuard aGuard( maMutex );
162 SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( aName );
163 if( aIter == maProperties.end() )
164 throw NoSuchElementException();
166 return (*aIter).second;
169 Sequence< ::rtl::OUString > SAL_CALL NameContainer::getElementNames( )
170 throw(RuntimeException)
172 MutexGuard aGuard( maMutex );
174 SvGenericNameContainerMapImpl::iterator aIter = maProperties.begin();
175 const SvGenericNameContainerMapImpl::iterator aEnd = maProperties.end();
177 Sequence< rtl::OUString > aNames( maProperties.size() );
178 rtl::OUString* pNames = aNames.getArray();
180 while( aIter != aEnd )
182 *pNames++ = (*aIter++).first;
185 return aNames;
188 sal_Bool SAL_CALL NameContainer::hasByName( const ::rtl::OUString& aName )
189 throw(RuntimeException)
191 MutexGuard aGuard( maMutex );
193 SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( aName );
194 return aIter != maProperties.end();
197 sal_Bool SAL_CALL NameContainer::hasElements( )
198 throw(RuntimeException)
200 MutexGuard aGuard( maMutex );
202 return !maProperties.empty();
205 Type SAL_CALL NameContainer::getElementType()
206 throw( RuntimeException )
208 return maType;
211 Reference< XNameContainer > comphelper::NameContainer_createInstance( Type aType )
213 return (XNameContainer*) new NameContainer( aType );