xref: /AOO41X/main/forms/source/xforms/NameContainer.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef _NAMECONTAINER_HXX
29*cdf0e10cSrcweir #define _NAMECONTAINER_HXX
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
32*cdf0e10cSrcweir #include <map>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/container/NoSuchElementException.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/lang/WrappedTargetException.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx>
39*cdf0e10cSrcweir #include <com/sun/star/uno/Reference.hxx>
40*cdf0e10cSrcweir #include <com/sun/star/uno/RuntimeException.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/uno/Type.hxx>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir typedef cppu::WeakImplHelper1<
44*cdf0e10cSrcweir     com::sun::star::container::XNameContainer
45*cdf0e10cSrcweir > NameContainer_t;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir template<class T>
48*cdf0e10cSrcweir class NameContainer : public NameContainer_t
49*cdf0e10cSrcweir {
50*cdf0e10cSrcweir protected:
51*cdf0e10cSrcweir     typedef std::map<rtl::OUString,T> map_t;
52*cdf0e10cSrcweir     map_t maItems;
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir     bool hasItems()
56*cdf0e10cSrcweir     {
57*cdf0e10cSrcweir         return ! maItems.empty();
58*cdf0e10cSrcweir     }
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir     typename map_t::const_iterator findItem( const rtl::OUString& rName )
61*cdf0e10cSrcweir     {
62*cdf0e10cSrcweir         return maItems.find( rName );
63*cdf0e10cSrcweir     }
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir     bool hasItem( const rtl::OUString& rName )
66*cdf0e10cSrcweir     {
67*cdf0e10cSrcweir         return findItem( rName ) != maItems.end();
68*cdf0e10cSrcweir     }
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir     T getItem( const rtl::OUString& rName )
71*cdf0e10cSrcweir     {
72*cdf0e10cSrcweir         OSL_ENSURE( hasItem( rName ), "can't get non-existant item" );
73*cdf0e10cSrcweir         return maItems[ rName ];
74*cdf0e10cSrcweir     }
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir     void replace( const rtl::OUString& rName,
78*cdf0e10cSrcweir                   const T& aElement )
79*cdf0e10cSrcweir     {
80*cdf0e10cSrcweir         OSL_ENSURE( hasItem( rName ), "unknown item" );
81*cdf0e10cSrcweir         maItems[ rName ] = aElement;
82*cdf0e10cSrcweir     }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir     void insert( const rtl::OUString& rName,
85*cdf0e10cSrcweir                  const T& aElement )
86*cdf0e10cSrcweir     {
87*cdf0e10cSrcweir         OSL_ENSURE( ! hasItem( rName ), "item already in set" );
88*cdf0e10cSrcweir         maItems[ rName ] = aElement;
89*cdf0e10cSrcweir     }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir     void remove( const rtl::OUString& rName )
92*cdf0e10cSrcweir     {
93*cdf0e10cSrcweir         OSL_ENSURE( hasItem( rName ), "item not in set" );
94*cdf0e10cSrcweir         maItems.erase( rName );
95*cdf0e10cSrcweir     }
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir public:
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir     NameContainer() {}
101*cdf0e10cSrcweir     virtual ~NameContainer() {}
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir     //
104*cdf0e10cSrcweir     // methods for XElementAccess
105*cdf0e10cSrcweir     //
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir     virtual com::sun::star::uno::Type SAL_CALL getElementType()
108*cdf0e10cSrcweir         throw( com::sun::star::uno::RuntimeException )
109*cdf0e10cSrcweir     {
110*cdf0e10cSrcweir         return getCppuType( static_cast<T*>( NULL ) );
111*cdf0e10cSrcweir     }
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasElements()
114*cdf0e10cSrcweir         throw( com::sun::star::uno::RuntimeException )
115*cdf0e10cSrcweir     {
116*cdf0e10cSrcweir         return hasItems();
117*cdf0e10cSrcweir     }
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir     //
121*cdf0e10cSrcweir     // methods for XNameAccess (inherits XElementAccess)
122*cdf0e10cSrcweir     //
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir     virtual com::sun::star::uno::Any SAL_CALL getByName(
125*cdf0e10cSrcweir         const rtl::OUString& rName )
126*cdf0e10cSrcweir         throw( com::sun::star::container::NoSuchElementException,
127*cdf0e10cSrcweir                com::sun::star::lang::WrappedTargetException,
128*cdf0e10cSrcweir                com::sun::star::uno::RuntimeException )
129*cdf0e10cSrcweir     {
130*cdf0e10cSrcweir         typename map_t::const_iterator aIter = findItem( rName );
131*cdf0e10cSrcweir         if( aIter == maItems.end() )
132*cdf0e10cSrcweir             throw com::sun::star::container::NoSuchElementException();
133*cdf0e10cSrcweir         else
134*cdf0e10cSrcweir             return com::sun::star::uno::makeAny( aIter->second );
135*cdf0e10cSrcweir     }
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir     virtual com::sun::star::uno::Sequence<rtl::OUString> SAL_CALL getElementNames()
138*cdf0e10cSrcweir         throw( com::sun::star::uno::RuntimeException )
139*cdf0e10cSrcweir     {
140*cdf0e10cSrcweir         com::sun::star::uno::Sequence<rtl::OUString> aSequence( maItems.size() );
141*cdf0e10cSrcweir         typename map_t::const_iterator aIter = maItems.begin();
142*cdf0e10cSrcweir         rtl::OUString* pStrings = aSequence.getArray();
143*cdf0e10cSrcweir         while( aIter != maItems.end() )
144*cdf0e10cSrcweir         {
145*cdf0e10cSrcweir             *pStrings = aIter->first;
146*cdf0e10cSrcweir             ++aIter;
147*cdf0e10cSrcweir             ++pStrings;
148*cdf0e10cSrcweir         }
149*cdf0e10cSrcweir         OSL_ENSURE( pStrings == aSequence.getArray() + aSequence.getLength(),
150*cdf0e10cSrcweir                     "sequence not of right size; possible buffer overflow" );
151*cdf0e10cSrcweir         return aSequence;
152*cdf0e10cSrcweir     }
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasByName(
155*cdf0e10cSrcweir         const rtl::OUString& rName )
156*cdf0e10cSrcweir         throw( com::sun::star::uno::RuntimeException )
157*cdf0e10cSrcweir     {
158*cdf0e10cSrcweir         return hasItem( rName );
159*cdf0e10cSrcweir     }
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir     //
163*cdf0e10cSrcweir     // methods for XNameReplace (inherits XNameAccess)
164*cdf0e10cSrcweir     //
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir     virtual void SAL_CALL replaceByName(
167*cdf0e10cSrcweir         const rtl::OUString& rName,
168*cdf0e10cSrcweir         const com::sun::star::uno::Any& aElement )
169*cdf0e10cSrcweir         throw( com::sun::star::lang::IllegalArgumentException,
170*cdf0e10cSrcweir                com::sun::star::container::NoSuchElementException,
171*cdf0e10cSrcweir                com::sun::star::lang::WrappedTargetException,
172*cdf0e10cSrcweir                com::sun::star::uno::RuntimeException)
173*cdf0e10cSrcweir     {
174*cdf0e10cSrcweir         T aItem;
175*cdf0e10cSrcweir         if( aElement >>= aItem )
176*cdf0e10cSrcweir             if( hasByName( rName ) )
177*cdf0e10cSrcweir                 replace( rName, aItem );
178*cdf0e10cSrcweir             else
179*cdf0e10cSrcweir                 throw com::sun::star::container::NoSuchElementException();
180*cdf0e10cSrcweir         else
181*cdf0e10cSrcweir             throw com::sun::star::lang::IllegalArgumentException();
182*cdf0e10cSrcweir     }
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir     //
186*cdf0e10cSrcweir     // methods for XNameContainer (inherits XNameReplace)
187*cdf0e10cSrcweir     //
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir     virtual void SAL_CALL insertByName(
190*cdf0e10cSrcweir         const rtl::OUString& rName,
191*cdf0e10cSrcweir         const com::sun::star::uno::Any& aElement )
192*cdf0e10cSrcweir         throw( com::sun::star::lang::IllegalArgumentException,
193*cdf0e10cSrcweir                com::sun::star::container::ElementExistException,
194*cdf0e10cSrcweir                com::sun::star::lang::WrappedTargetException,
195*cdf0e10cSrcweir                com::sun::star::uno::RuntimeException )
196*cdf0e10cSrcweir     {
197*cdf0e10cSrcweir         T aItem;
198*cdf0e10cSrcweir         if( aElement >>= aItem )
199*cdf0e10cSrcweir             if( ! hasByName( rName ) )
200*cdf0e10cSrcweir                 insert( rName, aItem );
201*cdf0e10cSrcweir             else
202*cdf0e10cSrcweir                 throw com::sun::star::container::ElementExistException();
203*cdf0e10cSrcweir         else
204*cdf0e10cSrcweir             throw com::sun::star::lang::IllegalArgumentException();
205*cdf0e10cSrcweir     }
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir     virtual void SAL_CALL removeByName(
208*cdf0e10cSrcweir         const rtl::OUString& rName )
209*cdf0e10cSrcweir         throw( com::sun::star::container::NoSuchElementException,
210*cdf0e10cSrcweir                com::sun::star::lang::WrappedTargetException,
211*cdf0e10cSrcweir                com::sun::star::uno::RuntimeException)
212*cdf0e10cSrcweir     {
213*cdf0e10cSrcweir         if( hasByName( rName ) )
214*cdf0e10cSrcweir             remove( rName );
215*cdf0e10cSrcweir         else
216*cdf0e10cSrcweir             throw com::sun::star::container::NoSuchElementException();
217*cdf0e10cSrcweir     }
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir };
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir #endif
222