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