bump product version to 4.1.6.2
[LibreOffice.git] / forms / source / xforms / NameContainer.hxx
blobbd683100b0869d7fb35798ca3d082449b79e1818
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 _NAMECONTAINER_HXX
21 #define _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>
35 typedef cppu::WeakImplHelper1<
36 com::sun::star::container::XNameContainer
37 > NameContainer_t;
39 template<class T>
40 class NameContainer : public NameContainer_t
42 protected:
43 typedef std::map<OUString,T> map_t;
44 map_t maItems;
47 bool hasItems()
49 return ! maItems.empty();
52 typename map_t::const_iterator findItem( const OUString& rName )
54 return maItems.find( rName );
57 bool hasItem( const OUString& rName )
59 return findItem( rName ) != maItems.end();
62 T getItem( const OUString& rName )
64 OSL_ENSURE( hasItem( rName ), "can't get non-existant item" );
65 return maItems[ rName ];
69 void replace( const OUString& rName,
70 const T& aElement )
72 OSL_ENSURE( hasItem( rName ), "unknown item" );
73 maItems[ rName ] = aElement;
76 void insert( const OUString& rName,
77 const T& aElement )
79 OSL_ENSURE( ! hasItem( rName ), "item already in set" );
80 maItems[ rName ] = aElement;
83 void remove( const OUString& rName )
85 OSL_ENSURE( hasItem( rName ), "item not in set" );
86 maItems.erase( rName );
90 public:
92 NameContainer() {}
93 virtual ~NameContainer() {}
96 // methods for XElementAccess
99 virtual com::sun::star::uno::Type SAL_CALL getElementType()
100 throw( com::sun::star::uno::RuntimeException )
102 return getCppuType( static_cast<T*>( NULL ) );
105 virtual sal_Bool SAL_CALL hasElements()
106 throw( com::sun::star::uno::RuntimeException )
108 return hasItems();
113 // methods for XNameAccess (inherits XElementAccess)
116 virtual com::sun::star::uno::Any SAL_CALL getByName(
117 const OUString& rName )
118 throw( com::sun::star::container::NoSuchElementException,
119 com::sun::star::lang::WrappedTargetException,
120 com::sun::star::uno::RuntimeException )
122 typename map_t::const_iterator aIter = findItem( rName );
123 if( aIter == maItems.end() )
124 throw com::sun::star::container::NoSuchElementException();
125 else
126 return com::sun::star::uno::makeAny( aIter->second );
129 virtual com::sun::star::uno::Sequence<OUString> SAL_CALL getElementNames()
130 throw( com::sun::star::uno::RuntimeException )
132 com::sun::star::uno::Sequence<OUString> aSequence( maItems.size() );
133 typename map_t::const_iterator aIter = maItems.begin();
134 OUString* pStrings = aSequence.getArray();
135 while( aIter != maItems.end() )
137 *pStrings = aIter->first;
138 ++aIter;
139 ++pStrings;
141 OSL_ENSURE( pStrings == aSequence.getArray() + aSequence.getLength(),
142 "sequence not of right size; possible buffer overflow" );
143 return aSequence;
146 virtual sal_Bool SAL_CALL hasByName(
147 const OUString& rName )
148 throw( com::sun::star::uno::RuntimeException )
150 return hasItem( rName );
155 // methods for XNameReplace (inherits XNameAccess)
158 virtual void SAL_CALL replaceByName(
159 const OUString& rName,
160 const com::sun::star::uno::Any& aElement )
161 throw( com::sun::star::lang::IllegalArgumentException,
162 com::sun::star::container::NoSuchElementException,
163 com::sun::star::lang::WrappedTargetException,
164 com::sun::star::uno::RuntimeException)
166 T aItem;
167 if( aElement >>= aItem )
168 if( hasByName( rName ) )
169 replace( rName, aItem );
170 else
171 throw com::sun::star::container::NoSuchElementException();
172 else
173 throw com::sun::star::lang::IllegalArgumentException();
178 // methods for XNameContainer (inherits XNameReplace)
181 virtual void SAL_CALL insertByName(
182 const OUString& rName,
183 const com::sun::star::uno::Any& aElement )
184 throw( com::sun::star::lang::IllegalArgumentException,
185 com::sun::star::container::ElementExistException,
186 com::sun::star::lang::WrappedTargetException,
187 com::sun::star::uno::RuntimeException )
189 T aItem;
190 if( aElement >>= aItem )
191 if( ! hasByName( rName ) )
192 insert( rName, aItem );
193 else
194 throw com::sun::star::container::ElementExistException();
195 else
196 throw com::sun::star::lang::IllegalArgumentException();
199 virtual void SAL_CALL removeByName(
200 const OUString& rName )
201 throw( com::sun::star::container::NoSuchElementException,
202 com::sun::star::lang::WrappedTargetException,
203 com::sun::star::uno::RuntimeException)
205 if( hasByName( rName ) )
206 remove( rName );
207 else
208 throw com::sun::star::container::NoSuchElementException();
213 #endif
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */