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