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 <comphelper/sequence.hxx>
26 #include <cppuhelper/implbase.hxx>
27 #include <osl/mutex.hxx>
28 #include <com/sun/star/container/XNameContainer.hpp>
30 typedef std::map
<OUString
, css::uno::Any
> SvGenericNameContainerMapImpl
;
34 /** this is the base helper class for NameContainer that's also declared in this header. */
35 class NameContainer
: public ::cppu::WeakImplHelper
< css::container::XNameContainer
>
38 explicit NameContainer( const css::uno::Type
& aType
);
41 virtual void SAL_CALL
insertByName( const OUString
& aName
, const css::uno::Any
& aElement
) override
;
42 virtual void SAL_CALL
removeByName( const OUString
& Name
) override
;
45 virtual void SAL_CALL
replaceByName( const OUString
& aName
, const css::uno::Any
& aElement
) override
;
48 virtual css::uno::Any SAL_CALL
getByName( const OUString
& aName
) override
;
49 virtual css::uno::Sequence
< OUString
> SAL_CALL
getElementNames( ) override
;
50 virtual sal_Bool SAL_CALL
hasByName( const OUString
& aName
) override
;
53 virtual sal_Bool SAL_CALL
hasElements( ) override
;
54 virtual css::uno::Type SAL_CALL
getElementType( ) override
;
57 SvGenericNameContainerMapImpl maProperties
;
58 const css::uno::Type maType
;
63 using namespace ::comphelper
;
64 using namespace ::osl
;
65 using namespace ::com::sun::star::uno
;
66 using namespace ::com::sun::star::container
;
67 using namespace ::com::sun::star::lang
;
70 NameContainer::NameContainer( const css::uno::Type
& aType
)
76 void SAL_CALL
NameContainer::insertByName( const OUString
& aName
, const Any
& aElement
)
78 MutexGuard
aGuard( maMutex
);
80 if( maProperties
.find( aName
) != maProperties
.end() )
81 throw ElementExistException();
83 if( aElement
.getValueType() != maType
)
84 throw IllegalArgumentException();
86 maProperties
.emplace(aName
,aElement
);
89 void SAL_CALL
NameContainer::removeByName( const OUString
& Name
)
91 MutexGuard
aGuard( maMutex
);
93 SvGenericNameContainerMapImpl::iterator aIter
= maProperties
.find( Name
);
94 if( aIter
== maProperties
.end() )
95 throw NoSuchElementException();
97 maProperties
.erase( aIter
);
102 void SAL_CALL
NameContainer::replaceByName( const OUString
& aName
, const Any
& aElement
)
104 MutexGuard
aGuard( maMutex
);
106 SvGenericNameContainerMapImpl::iterator
aIter( maProperties
.find( aName
) );
107 if( aIter
== maProperties
.end() )
108 throw NoSuchElementException();
110 if( aElement
.getValueType() != maType
)
111 throw IllegalArgumentException();
113 (*aIter
).second
= aElement
;
118 Any SAL_CALL
NameContainer::getByName( const OUString
& aName
)
120 MutexGuard
aGuard( maMutex
);
122 SvGenericNameContainerMapImpl::iterator aIter
= maProperties
.find( aName
);
123 if( aIter
== maProperties
.end() )
124 throw NoSuchElementException();
126 return (*aIter
).second
;
129 Sequence
< OUString
> SAL_CALL
NameContainer::getElementNames( )
131 MutexGuard
aGuard( maMutex
);
133 return comphelper::mapKeysToSequence(maProperties
);
136 sal_Bool SAL_CALL
NameContainer::hasByName( const OUString
& aName
)
138 MutexGuard
aGuard( maMutex
);
140 SvGenericNameContainerMapImpl::iterator aIter
= maProperties
.find( aName
);
141 return aIter
!= maProperties
.end();
144 sal_Bool SAL_CALL
NameContainer::hasElements( )
146 MutexGuard
aGuard( maMutex
);
148 return !maProperties
.empty();
151 Type SAL_CALL
NameContainer::getElementType()
156 Reference
< XNameContainer
> comphelper::NameContainer_createInstance( const Type
& aType
)
158 return new NameContainer(aType
);
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */