Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / comphelper / source / container / NamedPropertyValuesContainer.cxx
bloba44837f11700978809c7e5dfc20af44c2142f07e
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 <com/sun/star/container/XNameContainer.hpp>
21 #include <com/sun/star/uno/Sequence.h>
22 #include <com/sun/star/beans/PropertyValue.hpp>
23 #include <comphelper/sequence.hxx>
24 #include <cppuhelper/implbase.hxx>
25 #include <com/sun/star/lang/XServiceInfo.hpp>
26 #include <cppuhelper/supportsservice.hxx>
27 #include <map>
30 namespace com::sun::star::uno { class XComponentContext; }
31 using namespace com::sun::star;
33 typedef std::map< OUString, uno::Sequence<beans::PropertyValue> > NamedPropertyValues;
35 namespace {
37 class NamedPropertyValuesContainer : public cppu::WeakImplHelper< container::XNameContainer, lang::XServiceInfo >
39 public:
40 NamedPropertyValuesContainer() noexcept;
42 // XNameContainer
43 virtual void SAL_CALL insertByName( const OUString& aName, const css::uno::Any& aElement ) override;
44 virtual void SAL_CALL removeByName( const OUString& Name ) override;
46 // XNameReplace
47 virtual void SAL_CALL replaceByName( const OUString& aName, const css::uno::Any& aElement ) override;
49 // XNameAccess
50 virtual css::uno::Any SAL_CALL getByName( const OUString& aName ) override;
51 virtual css::uno::Sequence< OUString > SAL_CALL getElementNames( ) override;
52 virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) override;
54 // XElementAccess
55 virtual css::uno::Type SAL_CALL getElementType( ) override;
56 virtual sal_Bool SAL_CALL hasElements( ) override;
58 //XServiceInfo
59 virtual OUString SAL_CALL getImplementationName( ) override;
60 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
61 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames( ) override;
63 private:
64 NamedPropertyValues maProperties;
69 NamedPropertyValuesContainer::NamedPropertyValuesContainer() noexcept
73 // XNameContainer
74 void SAL_CALL NamedPropertyValuesContainer::insertByName( const OUString& aName, const uno::Any& aElement )
76 if( maProperties.find( aName ) != maProperties.end() )
77 throw container::ElementExistException();
79 uno::Sequence<beans::PropertyValue> aProps;
80 if( !(aElement >>= aProps ) )
81 throw lang::IllegalArgumentException("element is not beans::PropertyValue", static_cast<cppu::OWeakObject*>(this), 2);
83 maProperties.emplace( aName, aProps );
86 void SAL_CALL NamedPropertyValuesContainer::removeByName( const OUString& Name )
88 NamedPropertyValues::iterator aIter = maProperties.find( Name );
89 if( aIter == maProperties.end() )
90 throw container::NoSuchElementException();
92 maProperties.erase( aIter );
95 // XNameReplace
96 void SAL_CALL NamedPropertyValuesContainer::replaceByName( const OUString& aName, const css::uno::Any& aElement )
98 NamedPropertyValues::iterator aIter = maProperties.find( aName );
99 if( aIter == maProperties.end() )
100 throw container::NoSuchElementException();
102 uno::Sequence<beans::PropertyValue> aProps;
103 if( !(aElement >>= aProps) )
104 throw lang::IllegalArgumentException("element is not beans::PropertyValue", static_cast<cppu::OWeakObject*>(this), 2);
106 (*aIter).second = aProps;
109 // XNameAccess
110 css::uno::Any SAL_CALL NamedPropertyValuesContainer::getByName( const OUString& aName )
112 NamedPropertyValues::iterator aIter = maProperties.find( aName );
113 if( aIter == maProperties.end() )
114 throw container::NoSuchElementException();
116 uno::Any aElement;
118 aElement <<= (*aIter).second;
120 return aElement;
123 css::uno::Sequence< OUString > SAL_CALL NamedPropertyValuesContainer::getElementNames( )
125 return comphelper::mapKeysToSequence(maProperties);
128 sal_Bool SAL_CALL NamedPropertyValuesContainer::hasByName( const OUString& aName )
130 NamedPropertyValues::iterator aIter = maProperties.find( aName );
131 return aIter != maProperties.end();
134 // XElementAccess
135 css::uno::Type SAL_CALL NamedPropertyValuesContainer::getElementType( )
137 return cppu::UnoType<uno::Sequence<beans::PropertyValue>>::get();
140 sal_Bool SAL_CALL NamedPropertyValuesContainer::hasElements( )
142 return !maProperties.empty();
145 //XServiceInfo
146 OUString SAL_CALL NamedPropertyValuesContainer::getImplementationName( )
148 return "NamedPropertyValuesContainer";
151 sal_Bool SAL_CALL NamedPropertyValuesContainer::supportsService( const OUString& ServiceName )
153 return cppu::supportsService(this, ServiceName);
156 css::uno::Sequence< OUString > SAL_CALL NamedPropertyValuesContainer::getSupportedServiceNames( )
158 return { "com.sun.star.document.NamedPropertyValues" };
161 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
162 NamedPropertyValuesContainer_get_implementation(
163 css::uno::XComponentContext *,
164 css::uno::Sequence<css::uno::Any> const &)
166 return cppu::acquire(new NamedPropertyValuesContainer());
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */