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         throw( com::sun::star::uno::RuntimeException )
105     {
106         return getCppuType( static_cast<T*>( NULL ) );
107     }
108 
hasElements()109     virtual sal_Bool SAL_CALL hasElements()
110         throw( com::sun::star::uno::RuntimeException )
111     {
112         return hasItems();
113     }
114 
115 
116     //
117     // methods for XNameAccess (inherits XElementAccess)
118     //
119 
getByName(const rtl::OUString & rName)120     virtual com::sun::star::uno::Any SAL_CALL getByName(
121         const rtl::OUString& rName )
122         throw( com::sun::star::container::NoSuchElementException,
123                com::sun::star::lang::WrappedTargetException,
124                com::sun::star::uno::RuntimeException )
125     {
126         typename map_t::const_iterator aIter = findItem( rName );
127         if( aIter == maItems.end() )
128             throw com::sun::star::container::NoSuchElementException();
129         else
130             return com::sun::star::uno::makeAny( aIter->second );
131     }
132 
getElementNames()133     virtual com::sun::star::uno::Sequence<rtl::OUString> SAL_CALL getElementNames()
134         throw( com::sun::star::uno::RuntimeException )
135     {
136         com::sun::star::uno::Sequence<rtl::OUString> aSequence( maItems.size() );
137         typename map_t::const_iterator aIter = maItems.begin();
138         rtl::OUString* pStrings = aSequence.getArray();
139         while( aIter != maItems.end() )
140         {
141             *pStrings = aIter->first;
142             ++aIter;
143             ++pStrings;
144         }
145         OSL_ENSURE( pStrings == aSequence.getArray() + aSequence.getLength(),
146                     "sequence not of right size; possible buffer overflow" );
147         return aSequence;
148     }
149 
hasByName(const rtl::OUString & rName)150     virtual sal_Bool SAL_CALL hasByName(
151         const rtl::OUString& rName )
152         throw( com::sun::star::uno::RuntimeException )
153     {
154         return hasItem( rName );
155     }
156 
157 
158     //
159     // methods for XNameReplace (inherits XNameAccess)
160     //
161 
replaceByName(const rtl::OUString & rName,const com::sun::star::uno::Any & aElement)162     virtual void SAL_CALL replaceByName(
163         const rtl::OUString& rName,
164         const com::sun::star::uno::Any& aElement )
165         throw( com::sun::star::lang::IllegalArgumentException,
166                com::sun::star::container::NoSuchElementException,
167                com::sun::star::lang::WrappedTargetException,
168                com::sun::star::uno::RuntimeException)
169     {
170         T aItem;
171         if( aElement >>= aItem )
172             if( hasByName( rName ) )
173                 replace( rName, aItem );
174             else
175                 throw com::sun::star::container::NoSuchElementException();
176         else
177             throw com::sun::star::lang::IllegalArgumentException();
178     }
179 
180 
181     //
182     // methods for XNameContainer (inherits XNameReplace)
183     //
184 
insertByName(const rtl::OUString & rName,const com::sun::star::uno::Any & aElement)185     virtual void SAL_CALL insertByName(
186         const rtl::OUString& rName,
187         const com::sun::star::uno::Any& aElement )
188         throw( com::sun::star::lang::IllegalArgumentException,
189                com::sun::star::container::ElementExistException,
190                com::sun::star::lang::WrappedTargetException,
191                com::sun::star::uno::RuntimeException )
192     {
193         T aItem;
194         if( aElement >>= aItem )
195             if( ! hasByName( rName ) )
196                 insert( rName, aItem );
197             else
198                 throw com::sun::star::container::ElementExistException();
199         else
200             throw com::sun::star::lang::IllegalArgumentException();
201     }
202 
removeByName(const rtl::OUString & rName)203     virtual void SAL_CALL removeByName(
204         const rtl::OUString& rName )
205         throw( com::sun::star::container::NoSuchElementException,
206                com::sun::star::lang::WrappedTargetException,
207                com::sun::star::uno::RuntimeException)
208     {
209         if( hasByName( rName ) )
210             remove( rName );
211         else
212             throw com::sun::star::container::NoSuchElementException();
213     }
214 
215 };
216 
217 #endif
218