1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <sal/config.h>
24 #include <comphelper/namecontainer.hxx>
25 #include <cppuhelper/implbase1.hxx>
26 #include <osl/diagnose.h>
27 #include <osl/mutex.hxx>
29 typedef std::map
<OUString
, ::com::sun::star::uno::Any
> SvGenericNameContainerMapImpl
;
33 class NameContainerImpl
39 /** this is the base helper class for NameContainer thats also declared in this header. */
40 class NameContainer
: public ::cppu::WeakImplHelper1
< ::com::sun::star::container::XNameContainer
>, private NameContainerImpl
43 NameContainer( ::com::sun::star::uno::Type aType
);
44 virtual ~NameContainer();
47 virtual void SAL_CALL
insertByName( const OUString
& aName
, const ::com::sun::star::uno::Any
& aElement
)
48 throw(::com::sun::star::lang::IllegalArgumentException
, ::com::sun::star::container::ElementExistException
,
49 ::com::sun::star::lang::WrappedTargetException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
50 virtual void SAL_CALL
removeByName( const OUString
& Name
)
51 throw(::com::sun::star::container::NoSuchElementException
, ::com::sun::star::lang::WrappedTargetException
,
52 ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
55 virtual void SAL_CALL
replaceByName( const OUString
& aName
, const ::com::sun::star::uno::Any
& aElement
)
56 throw(::com::sun::star::lang::IllegalArgumentException
, ::com::sun::star::container::NoSuchElementException
,
57 ::com::sun::star::lang::WrappedTargetException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
60 virtual ::com::sun::star::uno::Any SAL_CALL
getByName( const OUString
& aName
)
61 throw(::com::sun::star::container::NoSuchElementException
, ::com::sun::star::lang::WrappedTargetException
,
62 ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
63 virtual ::com::sun::star::uno::Sequence
< OUString
> SAL_CALL
getElementNames( )
64 throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
65 virtual sal_Bool SAL_CALL
hasByName( const OUString
& aName
)
66 throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
69 virtual sal_Bool SAL_CALL
hasElements( )
70 throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
71 virtual ::com::sun::star::uno::Type SAL_CALL
getElementType( )
72 throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
75 SvGenericNameContainerMapImpl maProperties
;
76 const ::com::sun::star::uno::Type maType
;
80 using namespace ::comphelper
;
81 using namespace ::osl
;
82 using namespace ::com::sun::star::uno
;
83 using namespace ::com::sun::star::container
;
84 using namespace ::com::sun::star::lang
;
87 NameContainer::NameContainer( ::com::sun::star::uno::Type aType
)
92 NameContainer::~NameContainer()
97 void SAL_CALL
NameContainer::insertByName( const OUString
& aName
, const Any
& aElement
)
98 throw(IllegalArgumentException
, ElementExistException
,
99 WrappedTargetException
, RuntimeException
, std::exception
)
101 MutexGuard
aGuard( maMutex
);
103 if( maProperties
.find( aName
) != maProperties
.end() )
104 throw ElementExistException();
106 if( aElement
.getValueType() != maType
)
107 throw IllegalArgumentException();
109 maProperties
.insert( SvGenericNameContainerMapImpl::value_type(aName
,aElement
));
112 void SAL_CALL
NameContainer::removeByName( const OUString
& Name
)
113 throw(NoSuchElementException
, WrappedTargetException
,
114 RuntimeException
, std::exception
)
116 MutexGuard
aGuard( maMutex
);
118 SvGenericNameContainerMapImpl::iterator aIter
= maProperties
.find( Name
);
119 if( aIter
== maProperties
.end() )
120 throw NoSuchElementException();
122 maProperties
.erase( aIter
);
127 void SAL_CALL
NameContainer::replaceByName( const OUString
& aName
, const Any
& aElement
)
128 throw(IllegalArgumentException
, NoSuchElementException
,
129 WrappedTargetException
, RuntimeException
, std::exception
)
131 MutexGuard
aGuard( maMutex
);
133 SvGenericNameContainerMapImpl::iterator
aIter( maProperties
.find( aName
) );
134 if( aIter
== maProperties
.end() )
135 throw NoSuchElementException();
137 if( aElement
.getValueType() != maType
)
138 throw IllegalArgumentException();
140 (*aIter
).second
= aElement
;
145 Any SAL_CALL
NameContainer::getByName( const OUString
& aName
)
146 throw(NoSuchElementException
, WrappedTargetException
,
147 RuntimeException
, std::exception
)
149 MutexGuard
aGuard( maMutex
);
151 SvGenericNameContainerMapImpl::iterator aIter
= maProperties
.find( aName
);
152 if( aIter
== maProperties
.end() )
153 throw NoSuchElementException();
155 return (*aIter
).second
;
158 Sequence
< OUString
> SAL_CALL
NameContainer::getElementNames( )
159 throw(RuntimeException
, std::exception
)
161 MutexGuard
aGuard( maMutex
);
163 SvGenericNameContainerMapImpl::iterator aIter
= maProperties
.begin();
164 const SvGenericNameContainerMapImpl::iterator aEnd
= maProperties
.end();
166 Sequence
< OUString
> aNames( maProperties
.size() );
167 OUString
* pNames
= aNames
.getArray();
169 while( aIter
!= aEnd
)
171 *pNames
++ = (*aIter
++).first
;
177 sal_Bool SAL_CALL
NameContainer::hasByName( const OUString
& aName
)
178 throw(RuntimeException
, std::exception
)
180 MutexGuard
aGuard( maMutex
);
182 SvGenericNameContainerMapImpl::iterator aIter
= maProperties
.find( aName
);
183 return aIter
!= maProperties
.end();
186 sal_Bool SAL_CALL
NameContainer::hasElements( )
187 throw(RuntimeException
, std::exception
)
189 MutexGuard
aGuard( maMutex
);
191 return !maProperties
.empty();
194 Type SAL_CALL
NameContainer::getElementType()
195 throw( RuntimeException
, std::exception
)
200 Reference
< XNameContainer
> comphelper::NameContainer_createInstance( Type aType
)
202 return (XNameContainer
*) new NameContainer( aType
);
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */