Line data Source code
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 :
24 : #include <comphelper/namecontainer.hxx>
25 : #include <cppuhelper/implbase1.hxx>
26 : #include <osl/diagnose.h>
27 : #include <osl/mutex.hxx>
28 :
29 : typedef std::map<OUString, ::com::sun::star::uno::Any> SvGenericNameContainerMapImpl;
30 :
31 : namespace comphelper
32 : {
33 0 : class NameContainerImpl
34 : {
35 : public:
36 : osl::Mutex maMutex;
37 : };
38 :
39 : /** this is the base helper class for NameContainer thats also declared in this header. */
40 : class NameContainer : public ::cppu::WeakImplHelper1< ::com::sun::star::container::XNameContainer >, private NameContainerImpl
41 : {
42 : public:
43 : NameContainer( ::com::sun::star::uno::Type aType );
44 : virtual ~NameContainer();
45 :
46 : // XNameContainer
47 : virtual void SAL_CALL insertByName( const OUString& aName, const ::com::sun::star::uno::Any& aElement )
48 : throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::ElementExistException,
49 : ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
50 : virtual void SAL_CALL removeByName( const OUString& Name )
51 : throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
52 : ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
53 :
54 : // XNameReplace
55 : virtual void SAL_CALL replaceByName( const OUString& aName, const ::com::sun::star::uno::Any& aElement )
56 : throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::NoSuchElementException,
57 : ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
58 :
59 : // XNameAccess
60 : virtual ::com::sun::star::uno::Any SAL_CALL getByName( const OUString& aName )
61 : throw(::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException,
62 : ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
63 : virtual ::com::sun::star::uno::Sequence< OUString > SAL_CALL getElementNames( )
64 : throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
65 : virtual sal_Bool SAL_CALL hasByName( const OUString& aName )
66 : throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
67 :
68 : // XElementAccess
69 : virtual sal_Bool SAL_CALL hasElements( )
70 : throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
71 : virtual ::com::sun::star::uno::Type SAL_CALL getElementType( )
72 : throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
73 :
74 : private:
75 : SvGenericNameContainerMapImpl maProperties;
76 : const ::com::sun::star::uno::Type maType;
77 : };
78 : }
79 :
80 : using namespace ::comphelper;
81 : using namespace ::osl;
82 : using namespace ::rtl;
83 : using namespace ::com::sun::star::uno;
84 : using namespace ::com::sun::star::container;
85 : using namespace ::com::sun::star::lang;
86 :
87 :
88 0 : NameContainer::NameContainer( ::com::sun::star::uno::Type aType )
89 0 : : maType( aType )
90 : {
91 0 : }
92 :
93 0 : NameContainer::~NameContainer()
94 : {
95 0 : }
96 :
97 : // XNameContainer
98 0 : void SAL_CALL NameContainer::insertByName( const OUString& aName, const Any& aElement )
99 : throw(IllegalArgumentException, ElementExistException,
100 : WrappedTargetException, RuntimeException, std::exception)
101 : {
102 0 : MutexGuard aGuard( maMutex );
103 :
104 0 : if( maProperties.find( aName ) != maProperties.end() )
105 0 : throw ElementExistException();
106 :
107 0 : if( aElement.getValueType() != maType )
108 0 : throw IllegalArgumentException();
109 :
110 0 : maProperties.insert( SvGenericNameContainerMapImpl::value_type(aName,aElement));
111 0 : }
112 :
113 0 : void SAL_CALL NameContainer::removeByName( const OUString& Name )
114 : throw(NoSuchElementException, WrappedTargetException,
115 : RuntimeException, std::exception)
116 : {
117 0 : MutexGuard aGuard( maMutex );
118 :
119 0 : SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( Name );
120 0 : if( aIter == maProperties.end() )
121 0 : throw NoSuchElementException();
122 :
123 0 : maProperties.erase( aIter );
124 0 : }
125 :
126 : // XNameReplace
127 :
128 0 : void SAL_CALL NameContainer::replaceByName( const OUString& aName, const Any& aElement )
129 : throw(IllegalArgumentException, NoSuchElementException,
130 : WrappedTargetException, RuntimeException, std::exception)
131 : {
132 0 : MutexGuard aGuard( maMutex );
133 :
134 0 : SvGenericNameContainerMapImpl::iterator aIter( maProperties.find( aName ) );
135 0 : if( aIter == maProperties.end() )
136 0 : throw NoSuchElementException();
137 :
138 0 : if( aElement.getValueType() != maType )
139 0 : throw IllegalArgumentException();
140 :
141 0 : (*aIter).second = aElement;
142 0 : }
143 :
144 : // XNameAccess
145 :
146 0 : Any SAL_CALL NameContainer::getByName( const OUString& aName )
147 : throw(NoSuchElementException, WrappedTargetException,
148 : RuntimeException, std::exception)
149 : {
150 0 : MutexGuard aGuard( maMutex );
151 :
152 0 : SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( aName );
153 0 : if( aIter == maProperties.end() )
154 0 : throw NoSuchElementException();
155 :
156 0 : return (*aIter).second;
157 : }
158 :
159 0 : Sequence< OUString > SAL_CALL NameContainer::getElementNames( )
160 : throw(RuntimeException, std::exception)
161 : {
162 0 : MutexGuard aGuard( maMutex );
163 :
164 0 : SvGenericNameContainerMapImpl::iterator aIter = maProperties.begin();
165 0 : const SvGenericNameContainerMapImpl::iterator aEnd = maProperties.end();
166 :
167 0 : Sequence< OUString > aNames( maProperties.size() );
168 0 : OUString* pNames = aNames.getArray();
169 :
170 0 : while( aIter != aEnd )
171 : {
172 0 : *pNames++ = (*aIter++).first;
173 : }
174 :
175 0 : return aNames;
176 : }
177 :
178 0 : sal_Bool SAL_CALL NameContainer::hasByName( const OUString& aName )
179 : throw(RuntimeException, std::exception)
180 : {
181 0 : MutexGuard aGuard( maMutex );
182 :
183 0 : SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( aName );
184 0 : return aIter != maProperties.end();
185 : }
186 :
187 0 : sal_Bool SAL_CALL NameContainer::hasElements( )
188 : throw(RuntimeException, std::exception)
189 : {
190 0 : MutexGuard aGuard( maMutex );
191 :
192 0 : return !maProperties.empty();
193 : }
194 :
195 0 : Type SAL_CALL NameContainer::getElementType()
196 : throw( RuntimeException, std::exception )
197 : {
198 0 : return maType;
199 : }
200 :
201 0 : Reference< XNameContainer > comphelper::NameContainer_createInstance( Type aType )
202 : {
203 0 : return (XNameContainer*) new NameContainer( aType );
204 : }
205 :
206 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|