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 1236 : 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 ::com::sun::star::uno;
83 : using namespace ::com::sun::star::container;
84 : using namespace ::com::sun::star::lang;
85 :
86 :
87 618 : NameContainer::NameContainer( ::com::sun::star::uno::Type aType )
88 618 : : maType( aType )
89 : {
90 618 : }
91 :
92 1236 : NameContainer::~NameContainer()
93 : {
94 1236 : }
95 :
96 : // XNameContainer
97 2578 : void SAL_CALL NameContainer::insertByName( const OUString& aName, const Any& aElement )
98 : throw(IllegalArgumentException, ElementExistException,
99 : WrappedTargetException, RuntimeException, std::exception)
100 : {
101 2578 : MutexGuard aGuard( maMutex );
102 :
103 2578 : if( maProperties.find( aName ) != maProperties.end() )
104 0 : throw ElementExistException();
105 :
106 2578 : if( aElement.getValueType() != maType )
107 0 : throw IllegalArgumentException();
108 :
109 2578 : maProperties.insert( SvGenericNameContainerMapImpl::value_type(aName,aElement));
110 2578 : }
111 :
112 0 : void SAL_CALL NameContainer::removeByName( const OUString& Name )
113 : throw(NoSuchElementException, WrappedTargetException,
114 : RuntimeException, std::exception)
115 : {
116 0 : MutexGuard aGuard( maMutex );
117 :
118 0 : SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( Name );
119 0 : if( aIter == maProperties.end() )
120 0 : throw NoSuchElementException();
121 :
122 0 : maProperties.erase( aIter );
123 0 : }
124 :
125 : // XNameReplace
126 :
127 0 : void SAL_CALL NameContainer::replaceByName( const OUString& aName, const Any& aElement )
128 : throw(IllegalArgumentException, NoSuchElementException,
129 : WrappedTargetException, RuntimeException, std::exception)
130 : {
131 0 : MutexGuard aGuard( maMutex );
132 :
133 0 : SvGenericNameContainerMapImpl::iterator aIter( maProperties.find( aName ) );
134 0 : if( aIter == maProperties.end() )
135 0 : throw NoSuchElementException();
136 :
137 0 : if( aElement.getValueType() != maType )
138 0 : throw IllegalArgumentException();
139 :
140 0 : (*aIter).second = aElement;
141 0 : }
142 :
143 : // XNameAccess
144 :
145 1626 : Any SAL_CALL NameContainer::getByName( const OUString& aName )
146 : throw(NoSuchElementException, WrappedTargetException,
147 : RuntimeException, std::exception)
148 : {
149 1626 : MutexGuard aGuard( maMutex );
150 :
151 1626 : SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( aName );
152 1626 : if( aIter == maProperties.end() )
153 0 : throw NoSuchElementException();
154 :
155 1626 : return (*aIter).second;
156 : }
157 :
158 266 : Sequence< OUString > SAL_CALL NameContainer::getElementNames( )
159 : throw(RuntimeException, std::exception)
160 : {
161 266 : MutexGuard aGuard( maMutex );
162 :
163 266 : SvGenericNameContainerMapImpl::iterator aIter = maProperties.begin();
164 266 : const SvGenericNameContainerMapImpl::iterator aEnd = maProperties.end();
165 :
166 266 : Sequence< OUString > aNames( maProperties.size() );
167 266 : OUString* pNames = aNames.getArray();
168 :
169 2110 : while( aIter != aEnd )
170 : {
171 1578 : *pNames++ = (*aIter++).first;
172 : }
173 :
174 266 : return aNames;
175 : }
176 :
177 48 : sal_Bool SAL_CALL NameContainer::hasByName( const OUString& aName )
178 : throw(RuntimeException, std::exception)
179 : {
180 48 : MutexGuard aGuard( maMutex );
181 :
182 48 : SvGenericNameContainerMapImpl::iterator aIter = maProperties.find( aName );
183 48 : return aIter != maProperties.end();
184 : }
185 :
186 0 : sal_Bool SAL_CALL NameContainer::hasElements( )
187 : throw(RuntimeException, std::exception)
188 : {
189 0 : MutexGuard aGuard( maMutex );
190 :
191 0 : return !maProperties.empty();
192 : }
193 :
194 0 : Type SAL_CALL NameContainer::getElementType()
195 : throw( RuntimeException, std::exception )
196 : {
197 0 : return maType;
198 : }
199 :
200 618 : Reference< XNameContainer > comphelper::NameContainer_createInstance( Type aType )
201 : {
202 618 : return (XNameContainer*) new NameContainer( aType );
203 : }
204 :
205 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|