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 
25 #include <comphelper/sequence.hxx>
26 #include <cppuhelper/implbase.hxx>
27 #include <osl/mutex.hxx>
28 #include <com/sun/star/container/XNameContainer.hpp>
29 
30 typedef std::map<OUString, css::uno::Any> SvGenericNameContainerMapImpl;
31 
32 namespace comphelper
33 {
34  namespace {
35 
37  class NameContainer : public ::cppu::WeakImplHelper< css::container::XNameContainer >
38  {
39  public:
40  explicit NameContainer( const css::uno::Type& aType );
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 sal_Bool SAL_CALL hasElements( ) override;
56  virtual css::uno::Type SAL_CALL getElementType( ) override;
57 
58  private:
60  const css::uno::Type maType;
61  osl::Mutex maMutex;
62  };
63 
64  }
65 }
66 
67 using namespace ::comphelper;
68 using namespace ::osl;
69 using namespace ::com::sun::star::uno;
70 using namespace ::com::sun::star::container;
71 using namespace ::com::sun::star::lang;
72 
73 
74 NameContainer::NameContainer( const css::uno::Type& aType )
75 : maType( aType )
76 {
77 }
78 
79 // XNameContainer
80 void SAL_CALL NameContainer::insertByName( const OUString& aName, const Any& aElement )
81 {
82  MutexGuard aGuard( maMutex );
83 
84  if( maProperties.find( aName ) != maProperties.end() )
85  throw ElementExistException();
86 
87  if( aElement.getValueType() != maType )
88  throw IllegalArgumentException();
89 
90  maProperties.emplace(aName,aElement);
91 }
92 
93 void SAL_CALL NameContainer::removeByName( const OUString& Name )
94 {
95  MutexGuard aGuard( maMutex );
96 
97  SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( Name );
98  if( aIter == maProperties.end() )
99  throw NoSuchElementException();
100 
101  maProperties.erase( aIter );
102 }
103 
104 // XNameReplace
105 
106 void SAL_CALL NameContainer::replaceByName( const OUString& aName, const Any& aElement )
107 {
108  MutexGuard aGuard( maMutex );
109 
110  SvGenericNameContainerMapImpl::iterator aIter( maProperties.find( aName ) );
111  if( aIter == maProperties.end() )
112  throw NoSuchElementException();
113 
114  if( aElement.getValueType() != maType )
115  throw IllegalArgumentException();
116 
117  (*aIter).second = aElement;
118 }
119 
120 // XNameAccess
121 
122 Any SAL_CALL NameContainer::getByName( const OUString& aName )
123 {
124  MutexGuard aGuard( maMutex );
125 
126  SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( aName );
127  if( aIter == maProperties.end() )
128  throw NoSuchElementException();
129 
130  return (*aIter).second;
131 }
132 
134 {
135  MutexGuard aGuard( maMutex );
136 
138 }
139 
140 sal_Bool SAL_CALL NameContainer::hasByName( const OUString& aName )
141 {
142  MutexGuard aGuard( maMutex );
143 
144  SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( aName );
145  return aIter != maProperties.end();
146 }
147 
149 {
150  MutexGuard aGuard( maMutex );
151 
152  return !maProperties.empty();
153 }
154 
156 {
157  return maType;
158 }
159 
160 Reference< XNameContainer > comphelper::NameContainer_createInstance( const Type& aType )
161 {
162  return new NameContainer(aType);
163 }
164 
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type
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
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
COMPHELPER_DLLPUBLIC css::uno::Reference< css::container::XNameContainer > NameContainer_createInstance(const css::uno::Type &aType)
osl::Mutex maMutex
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