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>
25 #include <comphelper/namecontainer.hxx>
26 #include <comphelper/sequence.hxx>
27 #include <cppuhelper/implbase.hxx>
28 #include <osl/mutex.hxx>
29 #include <com/sun/star/container/XNameContainer.hpp>
31 typedef std::map
<OUString
, css::uno::Any
> SvGenericNameContainerMapImpl
;
37 /** this is the base helper class for NameContainer that's also declared in this header. */
38 class NameContainer
: public ::cppu::WeakImplHelper
< css::container::XNameContainer
>
41 explicit NameContainer( const css::uno::Type
& aType
);
44 virtual void SAL_CALL
insertByName( const OUString
& aName
, const css::uno::Any
& aElement
) override
;
45 virtual void SAL_CALL
removeByName( const OUString
& Name
) override
;
48 virtual void SAL_CALL
replaceByName( const OUString
& aName
, const css::uno::Any
& aElement
) override
;
51 virtual css::uno::Any SAL_CALL
getByName( const OUString
& aName
) override
;
52 virtual css::uno::Sequence
< OUString
> SAL_CALL
getElementNames( ) override
;
53 virtual sal_Bool SAL_CALL
hasByName( const OUString
& aName
) override
;
56 virtual sal_Bool SAL_CALL
hasElements( ) override
;
57 virtual css::uno::Type SAL_CALL
getElementType( ) override
;
60 SvGenericNameContainerMapImpl maProperties
;
61 const css::uno::Type maType
;
68 using namespace ::comphelper
;
69 using namespace ::osl
;
70 using namespace ::com::sun::star::uno
;
71 using namespace ::com::sun::star::container
;
72 using namespace ::com::sun::star::lang
;
75 NameContainer::NameContainer( const css::uno::Type
& aType
)
81 void SAL_CALL
NameContainer::insertByName( const OUString
& aName
, const Any
& aElement
)
83 std::scoped_lock
aGuard( maMutex
);
85 if( maProperties
.find( aName
) != maProperties
.end() )
86 throw ElementExistException();
88 if( aElement
.getValueType() != maType
)
89 throw IllegalArgumentException("element is wrong type", static_cast<cppu::OWeakObject
*>(this), 2);
91 maProperties
.emplace(aName
,aElement
);
94 void SAL_CALL
NameContainer::removeByName( const OUString
& Name
)
96 std::scoped_lock
aGuard( maMutex
);
98 SvGenericNameContainerMapImpl::iterator aIter
= maProperties
.find( Name
);
99 if( aIter
== maProperties
.end() )
100 throw NoSuchElementException();
102 maProperties
.erase( aIter
);
107 void SAL_CALL
NameContainer::replaceByName( const OUString
& aName
, const Any
& aElement
)
109 std::scoped_lock
aGuard( maMutex
);
111 SvGenericNameContainerMapImpl::iterator
aIter( maProperties
.find( aName
) );
112 if( aIter
== maProperties
.end() )
113 throw NoSuchElementException();
115 if( aElement
.getValueType() != maType
)
116 throw IllegalArgumentException("element is wrong type", static_cast<cppu::OWeakObject
*>(this), 2);
118 (*aIter
).second
= aElement
;
123 Any SAL_CALL
NameContainer::getByName( const OUString
& aName
)
125 std::scoped_lock
aGuard( maMutex
);
127 SvGenericNameContainerMapImpl::iterator aIter
= maProperties
.find( aName
);
128 if( aIter
== maProperties
.end() )
129 throw NoSuchElementException();
131 return (*aIter
).second
;
134 Sequence
< OUString
> SAL_CALL
NameContainer::getElementNames( )
136 std::scoped_lock
aGuard( maMutex
);
138 return comphelper::mapKeysToSequence(maProperties
);
141 sal_Bool SAL_CALL
NameContainer::hasByName( const OUString
& aName
)
143 std::scoped_lock
aGuard( maMutex
);
145 SvGenericNameContainerMapImpl::iterator aIter
= maProperties
.find( aName
);
146 return aIter
!= maProperties
.end();
149 sal_Bool SAL_CALL
NameContainer::hasElements( )
151 std::scoped_lock
aGuard( maMutex
);
153 return !maProperties
.empty();
156 Type SAL_CALL
NameContainer::getElementType()
161 Reference
< XNameContainer
> comphelper::NameContainer_createInstance( const Type
& aType
)
163 return new NameContainer(aType
);
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */