LibreOffice Module comphelper (master)  1
namecontainer.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 <sal/config.h>
21 
22 #include <map>
23 #include <mutex>
24 
26 #include <comphelper/sequence.hxx>
27 #include <cppuhelper/implbase.hxx>
28 #include <osl/mutex.hxx>
29 #include <com/sun/star/container/XNameContainer.hpp>
30 
31 typedef std::map<OUString, css::uno::Any> SvGenericNameContainerMapImpl;
32 
33 namespace comphelper
34 {
35  namespace {
36 
38  class NameContainer : public ::cppu::WeakImplHelper< css::container::XNameContainer >
39  {
40  public:
41  explicit NameContainer( const css::uno::Type& aType );
42 
43  // XNameContainer
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;
46 
47  // XNameReplace
48  virtual void SAL_CALL replaceByName( const OUString& aName, const css::uno::Any& aElement ) override;
49 
50  // XNameAccess
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;
54 
55  // XElementAccess
56  virtual sal_Bool SAL_CALL hasElements( ) override;
57  virtual css::uno::Type SAL_CALL getElementType( ) override;
58 
59  private:
61  const css::uno::Type maType;
63  };
64 
65  }
66 }
67 
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;
73 
74 
75 NameContainer::NameContainer( const css::uno::Type& aType )
76 : maType( aType )
77 {
78 }
79 
80 // XNameContainer
81 void SAL_CALL NameContainer::insertByName( const OUString& aName, const Any& aElement )
82 {
83  std::scoped_lock aGuard( maMutex );
84 
85  if( maProperties.find( aName ) != maProperties.end() )
86  throw ElementExistException();
87 
88  if( aElement.getValueType() != maType )
89  throw IllegalArgumentException("element is wrong type", static_cast<cppu::OWeakObject*>(this), 2);
90 
91  maProperties.emplace(aName,aElement);
92 }
93 
94 void SAL_CALL NameContainer::removeByName( const OUString& Name )
95 {
96  std::scoped_lock aGuard( maMutex );
97 
98  SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( Name );
99  if( aIter == maProperties.end() )
100  throw NoSuchElementException();
101 
102  maProperties.erase( aIter );
103 }
104 
105 // XNameReplace
106 
107 void SAL_CALL NameContainer::replaceByName( const OUString& aName, const Any& aElement )
108 {
109  std::scoped_lock aGuard( maMutex );
110 
111  SvGenericNameContainerMapImpl::iterator aIter( maProperties.find( aName ) );
112  if( aIter == maProperties.end() )
113  throw NoSuchElementException();
114 
115  if( aElement.getValueType() != maType )
116  throw IllegalArgumentException("element is wrong type", static_cast<cppu::OWeakObject*>(this), 2);
117 
118  (*aIter).second = aElement;
119 }
120 
121 // XNameAccess
122 
123 Any SAL_CALL NameContainer::getByName( const OUString& aName )
124 {
125  std::scoped_lock aGuard( maMutex );
126 
127  SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( aName );
128  if( aIter == maProperties.end() )
129  throw NoSuchElementException();
130 
131  return (*aIter).second;
132 }
133 
135 {
136  std::scoped_lock aGuard( maMutex );
137 
139 }
140 
141 sal_Bool SAL_CALL NameContainer::hasByName( const OUString& aName )
142 {
143  std::scoped_lock aGuard( maMutex );
144 
145  SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( aName );
146  return aIter != maProperties.end();
147 }
148 
150 {
151  std::scoped_lock aGuard( maMutex );
152 
153  return !maProperties.empty();
154 }
155 
157 {
158  return maType;
159 }
160 
161 Reference< XNameContainer > comphelper::NameContainer_createInstance( const Type& aType )
162 {
163  return new NameContainer(aType);
164 }
165 
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type
COMPHELPER_DLLPUBLIC css::uno::Reference< css::container::XNameContainer > NameContainer_createInstance(const css::uno::Type &aType)
const css::uno::Type maType
OUString Name
std::map< OUString, css::uno::Any > SvGenericNameContainerMapImpl
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
virtual void SAL_CALL removeByName(const OUString &rName) override
SvGenericNameContainerMapImpl maProperties
virtual css::uno::Type SAL_CALL getElementType() override
unsigned char sal_Bool
virtual sal_Bool SAL_CALL hasElements() override
std::mutex mutex
Definition: random.cxx:41
std::mutex maMutex
OUString aName
virtual void SAL_CALL insertByName(const OUString &rName, const css::uno::Any &aElement) override
virtual sal_Bool SAL_CALL hasByName(const OUString &rName) override
virtual css::uno::Any SAL_CALL getByName(const OUString &rName) override
virtual void SAL_CALL replaceByName(const OUString &rName, const css::uno::Any &aElement) 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