Bump version to 5.0-43
[LibreOffice.git] / forms / source / xforms / NameContainer.hxx
blob113a00568ab5281dbe57e3c30d6e67b2df13085e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_FORMS_SOURCE_XFORMS_NAMECONTAINER_HXX
21 #define INCLUDED_FORMS_SOURCE_XFORMS_NAMECONTAINER_HXX
23 #include <cppuhelper/implbase1.hxx>
24 #include <map>
26 #include <com/sun/star/container/XNameContainer.hpp>
27 #include <com/sun/star/container/NoSuchElementException.hpp>
28 #include <com/sun/star/lang/IllegalArgumentException.hpp>
29 #include <com/sun/star/lang/WrappedTargetException.hpp>
30 #include <com/sun/star/uno/Any.hxx>
31 #include <com/sun/star/uno/Reference.hxx>
32 #include <com/sun/star/uno/RuntimeException.hpp>
33 #include <com/sun/star/uno/Type.hxx>
34 #include <osl/diagnose.h>
36 typedef cppu::WeakImplHelper1<
37 com::sun::star::container::XNameContainer
38 > NameContainer_t;
40 template<class T>
41 class NameContainer : public NameContainer_t
43 protected:
44 typedef std::map<OUString,T> map_t;
45 map_t maItems;
48 bool hasItems()
50 return ! maItems.empty();
53 typename map_t::const_iterator findItem( const OUString& rName )
55 return maItems.find( rName );
58 bool hasItem( const OUString& rName )
60 return findItem( rName ) != maItems.end();
63 T getItem( const OUString& rName )
65 OSL_ENSURE( hasItem( rName ), "can't get non-existent item" );
66 return maItems[ rName ];
70 void replace( const OUString& rName,
71 const T& aElement )
73 OSL_ENSURE( hasItem( rName ), "unknown item" );
74 maItems[ rName ] = aElement;
77 void insert( const OUString& rName,
78 const T& aElement )
80 OSL_ENSURE( ! hasItem( rName ), "item already in set" );
81 maItems[ rName ] = aElement;
84 void remove( const OUString& rName )
86 OSL_ENSURE( hasItem( rName ), "item not in set" );
87 maItems.erase( rName );
91 public:
93 NameContainer() {}
94 virtual ~NameContainer() {}
97 // methods for XElementAccess
100 virtual com::sun::star::uno::Type SAL_CALL getElementType()
101 throw( com::sun::star::uno::RuntimeException, std::exception ) SAL_OVERRIDE
103 return cppu::UnoType<T>::get();
106 virtual sal_Bool SAL_CALL hasElements()
107 throw( com::sun::star::uno::RuntimeException, std::exception ) SAL_OVERRIDE
109 return hasItems();
114 // methods for XNameAccess (inherits XElementAccess)
117 virtual com::sun::star::uno::Any SAL_CALL getByName(
118 const OUString& rName )
119 throw( com::sun::star::container::NoSuchElementException,
120 com::sun::star::lang::WrappedTargetException,
121 com::sun::star::uno::RuntimeException, std::exception ) SAL_OVERRIDE
123 typename map_t::const_iterator aIter = findItem( rName );
124 if( aIter == maItems.end() )
125 throw com::sun::star::container::NoSuchElementException();
126 else
127 return com::sun::star::uno::makeAny( aIter->second );
130 virtual com::sun::star::uno::Sequence<OUString> SAL_CALL getElementNames()
131 throw( com::sun::star::uno::RuntimeException, std::exception ) SAL_OVERRIDE
133 com::sun::star::uno::Sequence<OUString> aSequence( maItems.size() );
134 typename map_t::const_iterator aIter = maItems.begin();
135 OUString* pStrings = aSequence.getArray();
136 while( aIter != maItems.end() )
138 *pStrings = aIter->first;
139 ++aIter;
140 ++pStrings;
142 OSL_ENSURE( pStrings == aSequence.getArray() + aSequence.getLength(),
143 "sequence not of right size; possible buffer overflow" );
144 return aSequence;
147 virtual sal_Bool SAL_CALL hasByName(
148 const OUString& rName )
149 throw( com::sun::star::uno::RuntimeException, std::exception ) SAL_OVERRIDE
151 return hasItem( rName );
156 // methods for XNameReplace (inherits XNameAccess)
159 virtual void SAL_CALL replaceByName(
160 const OUString& rName,
161 const com::sun::star::uno::Any& aElement )
162 throw( com::sun::star::lang::IllegalArgumentException,
163 com::sun::star::container::NoSuchElementException,
164 com::sun::star::lang::WrappedTargetException,
165 com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
167 T aItem;
168 if( aElement >>= aItem )
169 if( hasByName( rName ) )
170 replace( rName, aItem );
171 else
172 throw com::sun::star::container::NoSuchElementException();
173 else
174 throw com::sun::star::lang::IllegalArgumentException();
179 // methods for XNameContainer (inherits XNameReplace)
182 virtual void SAL_CALL insertByName(
183 const OUString& rName,
184 const com::sun::star::uno::Any& aElement )
185 throw( com::sun::star::lang::IllegalArgumentException,
186 com::sun::star::container::ElementExistException,
187 com::sun::star::lang::WrappedTargetException,
188 com::sun::star::uno::RuntimeException, std::exception ) SAL_OVERRIDE
190 T aItem;
191 if( aElement >>= aItem )
192 if( ! hasByName( rName ) )
193 insert( rName, aItem );
194 else
195 throw com::sun::star::container::ElementExistException();
196 else
197 throw com::sun::star::lang::IllegalArgumentException();
200 virtual void SAL_CALL removeByName(
201 const OUString& rName )
202 throw( com::sun::star::container::NoSuchElementException,
203 com::sun::star::lang::WrappedTargetException,
204 com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
206 if( hasByName( rName ) )
207 remove( rName );
208 else
209 throw com::sun::star::container::NoSuchElementException();
214 #endif
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */