1*dde7d3faSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*dde7d3faSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*dde7d3faSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*dde7d3faSAndrew Rist  * distributed with this work for additional information
6*dde7d3faSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*dde7d3faSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*dde7d3faSAndrew Rist  * "License"); you may not use this file except in compliance
9*dde7d3faSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*dde7d3faSAndrew Rist  *
11*dde7d3faSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*dde7d3faSAndrew Rist  *
13*dde7d3faSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*dde7d3faSAndrew Rist  * software distributed under the License is distributed on an
15*dde7d3faSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*dde7d3faSAndrew Rist  * KIND, either express or implied.  See the License for the
17*dde7d3faSAndrew Rist  * specific language governing permissions and limitations
18*dde7d3faSAndrew Rist  * under the License.
19*dde7d3faSAndrew Rist  *
20*dde7d3faSAndrew Rist  *************************************************************/
21*dde7d3faSAndrew Rist 
22*dde7d3faSAndrew 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 
42cdf0e10cSrcweir 	/** this is the base helper class for NameContainer thats 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 
NameContainer(::com::sun::star::uno::Type aType)91cdf0e10cSrcweir NameContainer::NameContainer( ::com::sun::star::uno::Type aType )
92cdf0e10cSrcweir : maType( aType )
93cdf0e10cSrcweir {
94cdf0e10cSrcweir }
95cdf0e10cSrcweir 
~NameContainer()96cdf0e10cSrcweir NameContainer::~NameContainer()
97cdf0e10cSrcweir {
98cdf0e10cSrcweir }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir // XNameContainer
insertByName(const rtl::OUString & aName,const Any & aElement)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 
removeByName(const::rtl::OUString & Name)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 
replaceByName(const::rtl::OUString & aName,const Any & aElement)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 
getByName(const::rtl::OUString & aName)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 
getElementNames()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 
hasByName(const::rtl::OUString & aName)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 
hasElements()190cdf0e10cSrcweir sal_Bool SAL_CALL NameContainer::hasElements(  )
191cdf0e10cSrcweir 	throw(RuntimeException)
192cdf0e10cSrcweir {
193cdf0e10cSrcweir 	MutexGuard aGuard( maMutex );
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 	return !maProperties.empty();
196cdf0e10cSrcweir }
197cdf0e10cSrcweir 
getElementType()198cdf0e10cSrcweir Type SAL_CALL NameContainer::getElementType()
199cdf0e10cSrcweir 	throw( RuntimeException )
200cdf0e10cSrcweir {
201cdf0e10cSrcweir 	return maType;
202cdf0e10cSrcweir }
203cdf0e10cSrcweir 
NameContainer_createInstance(Type aType)204cdf0e10cSrcweir Reference< XNameContainer > comphelper::NameContainer_createInstance( Type aType )
205cdf0e10cSrcweir {
206cdf0e10cSrcweir 	return (XNameContainer*) new NameContainer( aType );
207cdf0e10cSrcweir }
208