LibreOffice Module comphelper (master) 1
NamedPropertyValuesContainer.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <com/sun/star/container/XNameContainer.hpp>
21#include <com/sun/star/uno/Sequence.h>
22#include <com/sun/star/beans/PropertyValue.hpp>
25#include <com/sun/star/lang/XServiceInfo.hpp>
27#include <map>
28
29
30namespace com::sun::star::uno { class XComponentContext; }
31using namespace com::sun::star;
32
33typedef std::map< OUString, uno::Sequence<beans::PropertyValue> > NamedPropertyValues;
34
35namespace {
36
37class NamedPropertyValuesContainer : public cppu::WeakImplHelper< container::XNameContainer, lang::XServiceInfo >
38{
39public:
40 NamedPropertyValuesContainer() noexcept;
41
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;
45
46 // XNameReplace
47 virtual void SAL_CALL replaceByName( const OUString& aName, const css::uno::Any& aElement ) override;
48
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;
53
54 // XElementAccess
55 virtual css::uno::Type SAL_CALL getElementType( ) override;
56 virtual sal_Bool SAL_CALL hasElements( ) override;
57
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;
62
63private:
65};
66
67}
68
69NamedPropertyValuesContainer::NamedPropertyValuesContainer() noexcept
70{
71}
72
73// XNameContainer
74void SAL_CALL NamedPropertyValuesContainer::insertByName( const OUString& aName, const uno::Any& aElement )
75{
76 if( maProperties.find( aName ) != maProperties.end() )
77 throw container::ElementExistException();
78
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);
82
83 maProperties.emplace( aName, aProps );
84}
85
86void SAL_CALL NamedPropertyValuesContainer::removeByName( const OUString& Name )
87{
88 NamedPropertyValues::iterator aIter = maProperties.find( Name );
89 if( aIter == maProperties.end() )
90 throw container::NoSuchElementException();
91
92 maProperties.erase( aIter );
93}
94
95// XNameReplace
96void SAL_CALL NamedPropertyValuesContainer::replaceByName( const OUString& aName, const css::uno::Any& aElement )
97{
98 NamedPropertyValues::iterator aIter = maProperties.find( aName );
99 if( aIter == maProperties.end() )
100 throw container::NoSuchElementException();
101
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);
105
106 (*aIter).second = aProps;
107}
108
109// XNameAccess
110css::uno::Any SAL_CALL NamedPropertyValuesContainer::getByName( const OUString& aName )
111{
112 NamedPropertyValues::iterator aIter = maProperties.find( aName );
113 if( aIter == maProperties.end() )
114 throw container::NoSuchElementException();
115
116 uno::Any aElement;
117
118 aElement <<= (*aIter).second;
119
120 return aElement;
121}
122
123css::uno::Sequence< OUString > SAL_CALL NamedPropertyValuesContainer::getElementNames( )
124{
126}
127
128sal_Bool SAL_CALL NamedPropertyValuesContainer::hasByName( const OUString& aName )
129{
130 NamedPropertyValues::iterator aIter = maProperties.find( aName );
131 return aIter != maProperties.end();
132}
133
134// XElementAccess
135css::uno::Type SAL_CALL NamedPropertyValuesContainer::getElementType( )
136{
138}
139
140sal_Bool SAL_CALL NamedPropertyValuesContainer::hasElements( )
141{
142 return !maProperties.empty();
143}
144
145//XServiceInfo
146OUString SAL_CALL NamedPropertyValuesContainer::getImplementationName( )
147{
148 return "NamedPropertyValuesContainer";
149}
150
151sal_Bool SAL_CALL NamedPropertyValuesContainer::supportsService( const OUString& ServiceName )
152{
153 return cppu::supportsService(this, ServiceName);
154}
155
156css::uno::Sequence< OUString > SAL_CALL NamedPropertyValuesContainer::getSupportedServiceNames( )
157{
158 return { "com.sun.star.document.NamedPropertyValues" };
159}
160
161extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
163 css::uno::XComponentContext *,
164 css::uno::Sequence<css::uno::Any> const &)
165{
166 return cppu::acquire(new NamedPropertyValuesContainer());
167}
168
169/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::map< OUString, uno::Sequence< beans::PropertyValue > > NamedPropertyValues
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * NamedPropertyValuesContainer_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
OUString aName
SvGenericNameContainerMapImpl maProperties
css::uno::Sequence< typename M::key_type > mapKeysToSequence(M const &map)
Copy (keys or values) from an associate container into a Sequence.
Definition: sequence.hxx:300
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
OUString Name
unsigned char sal_Bool