xref: /trunk/main/forms/source/xforms/NameContainer.hxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _NAMECONTAINER_HXX
25 #define _NAMECONTAINER_HXX
26 
27 #include <cppuhelper/implbase1.hxx>
28 #include <map>
29 
30 #include <com/sun/star/container/XNameContainer.hpp>
31 #include <com/sun/star/container/NoSuchElementException.hpp>
32 #include <com/sun/star/lang/IllegalArgumentException.hpp>
33 #include <com/sun/star/lang/WrappedTargetException.hpp>
34 #include <com/sun/star/uno/Any.hxx>
35 #include <com/sun/star/uno/Reference.hxx>
36 #include <com/sun/star/uno/RuntimeException.hpp>
37 #include <com/sun/star/uno/Type.hxx>
38 
39 typedef cppu::WeakImplHelper1<
40     com::sun::star::container::XNameContainer
41 > NameContainer_t;
42 
43 template<class T>
44 class NameContainer : public NameContainer_t
45 {
46 protected:
47     typedef std::map<rtl::OUString,T> map_t;
48     map_t maItems;
49 
50 
hasItems()51     bool hasItems()
52     {
53         return ! maItems.empty();
54     }
55 
findItem(const rtl::OUString & rName)56     typename map_t::const_iterator findItem( const rtl::OUString& rName )
57     {
58         return maItems.find( rName );
59     }
60 
hasItem(const rtl::OUString & rName)61     bool hasItem( const rtl::OUString& rName )
62     {
63         return findItem( rName ) != maItems.end();
64     }
65 
getItem(const rtl::OUString & rName)66     T getItem( const rtl::OUString& rName )
67     {
68         OSL_ENSURE( hasItem( rName ), "can't get non-existent item" );
69         return maItems[ rName ];
70     }
71 
72 
replace(const rtl::OUString & rName,const T & aElement)73     void replace( const rtl::OUString& rName,
74                   const T& aElement )
75     {
76         OSL_ENSURE( hasItem( rName ), "unknown item" );
77         maItems[ rName ] = aElement;
78     }
79 
insert(const rtl::OUString & rName,const T & aElement)80     void insert( const rtl::OUString& rName,
81                  const T& aElement )
82     {
83         OSL_ENSURE( ! hasItem( rName ), "item already in set" );
84         maItems[ rName ] = aElement;
85     }
86 
remove(const rtl::OUString & rName)87     void remove( const rtl::OUString& rName )
88     {
89         OSL_ENSURE( hasItem( rName ), "item not in set" );
90         maItems.erase( rName );
91     }
92 
93 
94 public:
95 
NameContainer()96     NameContainer() {}
~NameContainer()97     virtual ~NameContainer() {}
98 
99     //
100     // methods for XElementAccess
101     //
102 
getElementType()103     virtual com::sun::star::uno::Type SAL_CALL getElementType()
104     {
105         return getCppuType( static_cast<T*>( NULL ) );
106     }
107 
hasElements()108     virtual sal_Bool SAL_CALL hasElements()
109     {
110         return hasItems();
111     }
112 
113 
114     //
115     // methods for XNameAccess (inherits XElementAccess)
116     //
117 
getByName(const rtl::OUString & rName)118     virtual com::sun::star::uno::Any SAL_CALL getByName(
119         const rtl::OUString& rName )
120     {
121         typename map_t::const_iterator aIter = findItem( rName );
122         if( aIter == maItems.end() )
123             throw com::sun::star::container::NoSuchElementException();
124         else
125             return com::sun::star::uno::makeAny( aIter->second );
126     }
127 
getElementNames()128     virtual com::sun::star::uno::Sequence<rtl::OUString> SAL_CALL getElementNames()
129     {
130         com::sun::star::uno::Sequence<rtl::OUString> aSequence( maItems.size() );
131         typename map_t::const_iterator aIter = maItems.begin();
132         rtl::OUString* pStrings = aSequence.getArray();
133         while( aIter != maItems.end() )
134         {
135             *pStrings = aIter->first;
136             ++aIter;
137             ++pStrings;
138         }
139         OSL_ENSURE( pStrings == aSequence.getArray() + aSequence.getLength(),
140                     "sequence not of right size; possible buffer overflow" );
141         return aSequence;
142     }
143 
hasByName(const rtl::OUString & rName)144     virtual sal_Bool SAL_CALL hasByName(
145         const rtl::OUString& rName )
146     {
147         return hasItem( rName );
148     }
149 
150 
151     //
152     // methods for XNameReplace (inherits XNameAccess)
153     //
154 
replaceByName(const rtl::OUString & rName,const com::sun::star::uno::Any & aElement)155     virtual void SAL_CALL replaceByName(
156         const rtl::OUString& rName,
157         const com::sun::star::uno::Any& aElement )
158     {
159         T aItem;
160         if( aElement >>= aItem )
161             if( hasByName( rName ) )
162                 replace( rName, aItem );
163             else
164                 throw com::sun::star::container::NoSuchElementException();
165         else
166             throw com::sun::star::lang::IllegalArgumentException();
167     }
168 
169 
170     //
171     // methods for XNameContainer (inherits XNameReplace)
172     //
173 
insertByName(const rtl::OUString & rName,const com::sun::star::uno::Any & aElement)174     virtual void SAL_CALL insertByName(
175         const rtl::OUString& rName,
176         const com::sun::star::uno::Any& aElement )
177     {
178         T aItem;
179         if( aElement >>= aItem )
180             if( ! hasByName( rName ) )
181                 insert( rName, aItem );
182             else
183                 throw com::sun::star::container::ElementExistException();
184         else
185             throw com::sun::star::lang::IllegalArgumentException();
186     }
187 
removeByName(const rtl::OUString & rName)188     virtual void SAL_CALL removeByName(
189         const rtl::OUString& rName )
190     {
191         if( hasByName( rName ) )
192             remove( rName );
193         else
194             throw com::sun::star::container::NoSuchElementException();
195     }
196 
197 };
198 
199 #endif
200