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>
23 #include <comphelper/sequence.hxx>
24 #include <cppuhelper/implbase.hxx>
25 #include <com/sun/star/lang/XServiceInfo.hpp>
27 #include <map>
28 
29 
30 namespace com::sun::star::uno { class XComponentContext; }
31 using namespace com::sun::star;
32 
33 typedef std::map< OUString, uno::Sequence<beans::PropertyValue> > NamedPropertyValues;
34 
35 class NamedPropertyValuesContainer : public cppu::WeakImplHelper< container::XNameContainer, lang::XServiceInfo >
36 {
37 public:
39 
40  // XNameContainer
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;
43 
44  // XNameReplace
45  virtual void SAL_CALL replaceByName( const OUString& aName, const css::uno::Any& aElement ) override;
46 
47  // XNameAccess
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;
51 
52  // XElementAccess
53  virtual css::uno::Type SAL_CALL getElementType( ) override;
54  virtual sal_Bool SAL_CALL hasElements( ) override;
55 
56  //XServiceInfo
57  virtual OUString SAL_CALL getImplementationName( ) override;
58  virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
59  virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames( ) override;
60 
61 private:
63 };
64 
66 {
67 }
68 
69 // XNameContainer
70 void SAL_CALL NamedPropertyValuesContainer::insertByName( const OUString& aName, const uno::Any& aElement )
71 {
72  if( maProperties.find( aName ) != maProperties.end() )
73  throw container::ElementExistException();
74 
75  uno::Sequence<beans::PropertyValue> aProps;
76  if( !(aElement >>= aProps ) )
77  throw lang::IllegalArgumentException();
78 
79  maProperties.emplace( aName, aProps );
80 }
81 
82 void SAL_CALL NamedPropertyValuesContainer::removeByName( const OUString& Name )
83 {
84  NamedPropertyValues::iterator aIter = maProperties.find( Name );
85  if( aIter == maProperties.end() )
86  throw container::NoSuchElementException();
87 
88  maProperties.erase( aIter );
89 }
90 
91 // XNameReplace
92 void SAL_CALL NamedPropertyValuesContainer::replaceByName( const OUString& aName, const css::uno::Any& aElement )
93 {
94  NamedPropertyValues::iterator aIter = maProperties.find( aName );
95  if( aIter == maProperties.end() )
96  throw container::NoSuchElementException();
97 
98  uno::Sequence<beans::PropertyValue> aProps;
99  if( !(aElement >>= aProps) )
100  throw lang::IllegalArgumentException();
101 
102  (*aIter).second = aProps;
103 }
104 
105 // XNameAccess
106 css::uno::Any SAL_CALL NamedPropertyValuesContainer::getByName( const OUString& aName )
107 {
108  NamedPropertyValues::iterator aIter = maProperties.find( aName );
109  if( aIter == maProperties.end() )
110  throw container::NoSuchElementException();
111 
112  uno::Any aElement;
113 
114  aElement <<= (*aIter).second;
115 
116  return aElement;
117 }
118 
119 css::uno::Sequence< OUString > SAL_CALL NamedPropertyValuesContainer::getElementNames( )
120 {
121  return comphelper::mapKeysToSequence(maProperties);
122 }
123 
124 sal_Bool SAL_CALL NamedPropertyValuesContainer::hasByName( const OUString& aName )
125 {
126  NamedPropertyValues::iterator aIter = maProperties.find( aName );
127  return aIter != maProperties.end();
128 }
129 
130 // XElementAccess
132 {
134 }
135 
137 {
138  return !maProperties.empty();
139 }
140 
141 //XServiceInfo
143 {
144  return "NamedPropertyValuesContainer";
145 }
146 
147 sal_Bool SAL_CALL NamedPropertyValuesContainer::supportsService( const OUString& ServiceName )
148 {
149  return cppu::supportsService(this, ServiceName);
150 }
151 
152 css::uno::Sequence< OUString > SAL_CALL NamedPropertyValuesContainer::getSupportedServiceNames( )
153 {
154  return { "com.sun.star.document.NamedPropertyValues" };
155 }
156 
157 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
159  css::uno::XComponentContext *,
160  css::uno::Sequence<css::uno::Any> const &)
161 {
162  return cppu::acquire(new NamedPropertyValuesContainer());
163 }
164 
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Type SAL_CALL getElementType() override
virtual void SAL_CALL insertByName(const OUString &aName, const css::uno::Any &aElement) override
OUString getImplementationName()
OUString Name
virtual OUString SAL_CALL getImplementationName() override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * NamedPropertyValuesContainer_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
css::uno::Sequence< OUString > getSupportedServiceNames()
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
unsigned char sal_Bool
virtual void SAL_CALL removeByName(const OUString &Name) override
std::map< OUString, uno::Sequence< beans::PropertyValue > > NamedPropertyValues
virtual void SAL_CALL replaceByName(const OUString &aName, const css::uno::Any &aElement) override
virtual css::uno::Any SAL_CALL getByName(const OUString &aName) override
OString const aName
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
virtual sal_Bool SAL_CALL hasElements() override
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:293