Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / comphelper / source / container / namecontainer.cxx
blob28bd6a5be7c6e5b0cfe988b474b92487a5cc8fe2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <comphelper/namecontainer.hxx>
21 #include <cppuhelper/implbase1.hxx>
22 #include <osl/diagnose.h>
23 #include <osl/mutex.hxx>
24 #include <comphelper/stl_types.hxx>
26 DECLARE_STL_USTRINGACCESS_MAP( ::com::sun::star::uno::Any, SvGenericNameContainerMapImpl );
28 namespace comphelper
30 class NameContainerImpl
32 public:
33 osl::Mutex maMutex;
36 /** this is the base helper class for NameContainer thats also declared in this header. */
37 class NameContainer : public ::cppu::WeakImplHelper1< ::com::sun::star::container::XNameContainer >, private NameContainerImpl
39 public:
40 NameContainer( ::com::sun::star::uno::Type aType );
41 virtual ~NameContainer();
43 // XNameContainer
44 virtual void SAL_CALL insertByName( const ::rtl::OUString& aName, const ::com::sun::star::uno::Any& aElement )
45 throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::ElementExistException,
46 ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
47 virtual void SAL_CALL removeByName( const ::rtl::OUString& Name )
48 throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
49 ::com::sun::star::uno::RuntimeException);
51 // XNameReplace
52 virtual void SAL_CALL replaceByName( const ::rtl::OUString& aName, const ::com::sun::star::uno::Any& aElement )
53 throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::NoSuchElementException,
54 ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
56 // XNameAccess
57 virtual ::com::sun::star::uno::Any SAL_CALL getByName( const ::rtl::OUString& aName )
58 throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
59 ::com::sun::star::uno::RuntimeException);
60 virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames( )
61 throw(::com::sun::star::uno::RuntimeException);
62 virtual sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName )
63 throw(::com::sun::star::uno::RuntimeException);
65 // XElementAccess
66 virtual sal_Bool SAL_CALL hasElements( )
67 throw(::com::sun::star::uno::RuntimeException);
68 virtual ::com::sun::star::uno::Type SAL_CALL getElementType( )
69 throw(::com::sun::star::uno::RuntimeException);
71 private:
72 SvGenericNameContainerMapImpl maProperties;
73 const ::com::sun::star::uno::Type maType;
77 using namespace ::comphelper;
78 using namespace ::osl;
79 using namespace ::rtl;
80 using namespace ::com::sun::star::uno;
81 using namespace ::com::sun::star::container;
82 using namespace ::com::sun::star::lang;
85 NameContainer::NameContainer( ::com::sun::star::uno::Type aType )
86 : maType( aType )
90 NameContainer::~NameContainer()
94 // XNameContainer
95 void SAL_CALL NameContainer::insertByName( const rtl::OUString& aName, const Any& aElement )
96 throw(IllegalArgumentException, ElementExistException,
97 WrappedTargetException, RuntimeException)
99 MutexGuard aGuard( maMutex );
101 if( maProperties.find( aName ) != maProperties.end() )
102 throw ElementExistException();
104 if( aElement.getValueType() != maType )
105 throw IllegalArgumentException();
107 maProperties.insert( SvGenericNameContainerMapImpl::value_type(aName,aElement));
110 void SAL_CALL NameContainer::removeByName( const ::rtl::OUString& Name )
111 throw(NoSuchElementException, WrappedTargetException,
112 RuntimeException)
114 MutexGuard aGuard( maMutex );
116 SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( Name );
117 if( aIter == maProperties.end() )
118 throw NoSuchElementException();
120 maProperties.erase( aIter );
123 // XNameReplace
125 void SAL_CALL NameContainer::replaceByName( const ::rtl::OUString& aName, const Any& aElement )
126 throw(IllegalArgumentException, NoSuchElementException,
127 WrappedTargetException, RuntimeException)
129 MutexGuard aGuard( maMutex );
131 SvGenericNameContainerMapImpl::iterator aIter( maProperties.find( aName ) );
132 if( aIter == maProperties.end() )
133 throw NoSuchElementException();
135 if( aElement.getValueType() != maType )
136 throw IllegalArgumentException();
138 (*aIter).second = aElement;
141 // XNameAccess
143 Any SAL_CALL NameContainer::getByName( const ::rtl::OUString& aName )
144 throw(NoSuchElementException, WrappedTargetException,
145 RuntimeException)
147 MutexGuard aGuard( maMutex );
149 SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( aName );
150 if( aIter == maProperties.end() )
151 throw NoSuchElementException();
153 return (*aIter).second;
156 Sequence< ::rtl::OUString > SAL_CALL NameContainer::getElementNames( )
157 throw(RuntimeException)
159 MutexGuard aGuard( maMutex );
161 SvGenericNameContainerMapImpl::iterator aIter = maProperties.begin();
162 const SvGenericNameContainerMapImpl::iterator aEnd = maProperties.end();
164 Sequence< rtl::OUString > aNames( maProperties.size() );
165 rtl::OUString* pNames = aNames.getArray();
167 while( aIter != aEnd )
169 *pNames++ = (*aIter++).first;
172 return aNames;
175 sal_Bool SAL_CALL NameContainer::hasByName( const ::rtl::OUString& aName )
176 throw(RuntimeException)
178 MutexGuard aGuard( maMutex );
180 SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( aName );
181 return aIter != maProperties.end();
184 sal_Bool SAL_CALL NameContainer::hasElements( )
185 throw(RuntimeException)
187 MutexGuard aGuard( maMutex );
189 return !maProperties.empty();
192 Type SAL_CALL NameContainer::getElementType()
193 throw( RuntimeException )
195 return maType;
198 Reference< XNameContainer > comphelper::NameContainer_createInstance( Type aType )
200 return (XNameContainer*) new NameContainer( aType );
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */