merge the formfield patch from ooo-build
[ooovba.git] / forms / source / xforms / NameContainer.hxx
blob5208bb210af05ecd7d93229bb8843503ec21c527
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: NameContainer.hxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef _NAMECONTAINER_HXX
32 #define _NAMECONTAINER_HXX
34 #include <cppuhelper/implbase1.hxx>
35 #include <map>
37 #include <com/sun/star/container/XNameContainer.hpp>
38 #include <com/sun/star/container/NoSuchElementException.hpp>
39 #include <com/sun/star/lang/IllegalArgumentException.hpp>
40 #include <com/sun/star/lang/WrappedTargetException.hpp>
41 #include <com/sun/star/uno/Any.hxx>
42 #include <com/sun/star/uno/Reference.hxx>
43 #include <com/sun/star/uno/RuntimeException.hpp>
44 #include <com/sun/star/uno/Type.hxx>
46 typedef cppu::WeakImplHelper1<
47 com::sun::star::container::XNameContainer
48 > NameContainer_t;
50 template<class T>
51 class NameContainer : public NameContainer_t
53 protected:
54 typedef std::map<rtl::OUString,T> map_t;
55 map_t maItems;
58 bool hasItems()
60 return ! maItems.empty();
63 typename map_t::const_iterator findItem( const rtl::OUString& rName )
65 return maItems.find( rName );
68 bool hasItem( const rtl::OUString& rName )
70 return findItem( rName ) != maItems.end();
73 T getItem( const rtl::OUString& rName )
75 OSL_ENSURE( hasItem( rName ), "can't get non-existant item" );
76 return maItems[ rName ];
80 void replace( const rtl::OUString& rName,
81 const T& aElement )
83 OSL_ENSURE( hasItem( rName ), "unknown item" );
84 maItems[ rName ] = aElement;
87 void insert( const rtl::OUString& rName,
88 const T& aElement )
90 OSL_ENSURE( ! hasItem( rName ), "item already in set" );
91 maItems[ rName ] = aElement;
94 void remove( const rtl::OUString& rName )
96 OSL_ENSURE( hasItem( rName ), "item not in set" );
97 maItems.erase( rName );
101 public:
103 NameContainer() {}
104 virtual ~NameContainer() {}
107 // methods for XElementAccess
110 virtual com::sun::star::uno::Type SAL_CALL getElementType()
111 throw( com::sun::star::uno::RuntimeException )
113 return getCppuType( static_cast<T*>( NULL ) );
116 virtual sal_Bool SAL_CALL hasElements()
117 throw( com::sun::star::uno::RuntimeException )
119 return hasItems();
124 // methods for XNameAccess (inherits XElementAccess)
127 virtual com::sun::star::uno::Any SAL_CALL getByName(
128 const rtl::OUString& rName )
129 throw( com::sun::star::container::NoSuchElementException,
130 com::sun::star::lang::WrappedTargetException,
131 com::sun::star::uno::RuntimeException )
133 typename map_t::const_iterator aIter = findItem( rName );
134 if( aIter == maItems.end() )
135 throw com::sun::star::container::NoSuchElementException();
136 else
137 return com::sun::star::uno::makeAny( aIter->second );
140 virtual com::sun::star::uno::Sequence<rtl::OUString> SAL_CALL getElementNames()
141 throw( com::sun::star::uno::RuntimeException )
143 com::sun::star::uno::Sequence<rtl::OUString> aSequence( maItems.size() );
144 typename map_t::const_iterator aIter = maItems.begin();
145 rtl::OUString* pStrings = aSequence.getArray();
146 while( aIter != maItems.end() )
148 *pStrings = aIter->first;
149 ++aIter;
150 ++pStrings;
152 OSL_ENSURE( pStrings == aSequence.getArray() + aSequence.getLength(),
153 "sequence not of right size; possible buffer overflow" );
154 return aSequence;
157 virtual sal_Bool SAL_CALL hasByName(
158 const rtl::OUString& rName )
159 throw( com::sun::star::uno::RuntimeException )
161 return hasItem( rName );
166 // methods for XNameReplace (inherits XNameAccess)
169 virtual void SAL_CALL replaceByName(
170 const rtl::OUString& rName,
171 const com::sun::star::uno::Any& aElement )
172 throw( com::sun::star::lang::IllegalArgumentException,
173 com::sun::star::container::NoSuchElementException,
174 com::sun::star::lang::WrappedTargetException,
175 com::sun::star::uno::RuntimeException)
177 T aItem;
178 if( aElement >>= aItem )
179 if( hasByName( rName ) )
180 replace( rName, aItem );
181 else
182 throw com::sun::star::container::NoSuchElementException();
183 else
184 throw com::sun::star::lang::IllegalArgumentException();
189 // methods for XNameContainer (inherits XNameReplace)
192 virtual void SAL_CALL insertByName(
193 const rtl::OUString& rName,
194 const com::sun::star::uno::Any& aElement )
195 throw( com::sun::star::lang::IllegalArgumentException,
196 com::sun::star::container::ElementExistException,
197 com::sun::star::lang::WrappedTargetException,
198 com::sun::star::uno::RuntimeException )
200 T aItem;
201 if( aElement >>= aItem )
202 if( ! hasByName( rName ) )
203 insert( rName, aItem );
204 else
205 throw com::sun::star::container::ElementExistException();
206 else
207 throw com::sun::star::lang::IllegalArgumentException();
210 virtual void SAL_CALL removeByName(
211 const rtl::OUString& rName )
212 throw( com::sun::star::container::NoSuchElementException,
213 com::sun::star::lang::WrappedTargetException,
214 com::sun::star::uno::RuntimeException)
216 if( hasByName( rName ) )
217 remove( rName );
218 else
219 throw com::sun::star::container::NoSuchElementException();
224 #endif