Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_FORMS_SOURCE_XFORMS_NAMECONTAINER_HXX
21 : #define INCLUDED_FORMS_SOURCE_XFORMS_NAMECONTAINER_HXX
22 :
23 : #include <cppuhelper/implbase1.hxx>
24 : #include <map>
25 :
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>
34 :
35 : typedef cppu::WeakImplHelper1<
36 : com::sun::star::container::XNameContainer
37 : > NameContainer_t;
38 :
39 : template<class T>
40 : class NameContainer : public NameContainer_t
41 : {
42 : protected:
43 : typedef std::map<OUString,T> map_t;
44 : map_t maItems;
45 :
46 :
47 0 : bool hasItems()
48 : {
49 0 : return ! maItems.empty();
50 : }
51 :
52 0 : typename map_t::const_iterator findItem( const OUString& rName )
53 : {
54 0 : return maItems.find( rName );
55 : }
56 :
57 0 : bool hasItem( const OUString& rName )
58 : {
59 0 : return findItem( rName ) != maItems.end();
60 : }
61 :
62 : T getItem( const OUString& rName )
63 : {
64 : OSL_ENSURE( hasItem( rName ), "can't get non-existant item" );
65 : return maItems[ rName ];
66 : }
67 :
68 :
69 0 : void replace( const OUString& rName,
70 : const T& aElement )
71 : {
72 : OSL_ENSURE( hasItem( rName ), "unknown item" );
73 0 : maItems[ rName ] = aElement;
74 0 : }
75 :
76 0 : void insert( const OUString& rName,
77 : const T& aElement )
78 : {
79 : OSL_ENSURE( ! hasItem( rName ), "item already in set" );
80 0 : maItems[ rName ] = aElement;
81 0 : }
82 :
83 0 : void remove( const OUString& rName )
84 : {
85 : OSL_ENSURE( hasItem( rName ), "item not in set" );
86 0 : maItems.erase( rName );
87 0 : }
88 :
89 :
90 : public:
91 :
92 0 : NameContainer() {}
93 0 : virtual ~NameContainer() {}
94 :
95 :
96 : // methods for XElementAccess
97 :
98 :
99 0 : virtual com::sun::star::uno::Type SAL_CALL getElementType()
100 : throw( com::sun::star::uno::RuntimeException, std::exception ) SAL_OVERRIDE
101 : {
102 0 : return getCppuType( static_cast<T*>( NULL ) );
103 : }
104 :
105 0 : virtual sal_Bool SAL_CALL hasElements()
106 : throw( com::sun::star::uno::RuntimeException, std::exception ) SAL_OVERRIDE
107 : {
108 0 : return hasItems();
109 : }
110 :
111 :
112 :
113 : // methods for XNameAccess (inherits XElementAccess)
114 :
115 :
116 0 : 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, std::exception ) SAL_OVERRIDE
121 : {
122 0 : typename map_t::const_iterator aIter = findItem( rName );
123 0 : if( aIter == maItems.end() )
124 0 : throw com::sun::star::container::NoSuchElementException();
125 : else
126 0 : return com::sun::star::uno::makeAny( aIter->second );
127 : }
128 :
129 0 : virtual com::sun::star::uno::Sequence<OUString> SAL_CALL getElementNames()
130 : throw( com::sun::star::uno::RuntimeException, std::exception ) SAL_OVERRIDE
131 : {
132 0 : com::sun::star::uno::Sequence<OUString> aSequence( maItems.size() );
133 0 : typename map_t::const_iterator aIter = maItems.begin();
134 0 : OUString* pStrings = aSequence.getArray();
135 0 : while( aIter != maItems.end() )
136 : {
137 0 : *pStrings = aIter->first;
138 0 : ++aIter;
139 0 : ++pStrings;
140 : }
141 : OSL_ENSURE( pStrings == aSequence.getArray() + aSequence.getLength(),
142 : "sequence not of right size; possible buffer overflow" );
143 0 : return aSequence;
144 : }
145 :
146 0 : virtual sal_Bool SAL_CALL hasByName(
147 : const OUString& rName )
148 : throw( com::sun::star::uno::RuntimeException, std::exception ) SAL_OVERRIDE
149 : {
150 0 : return hasItem( rName );
151 : }
152 :
153 :
154 :
155 : // methods for XNameReplace (inherits XNameAccess)
156 :
157 :
158 0 : 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, std::exception) SAL_OVERRIDE
165 : {
166 0 : T aItem;
167 0 : if( aElement >>= aItem )
168 0 : if( hasByName( rName ) )
169 0 : replace( rName, aItem );
170 : else
171 0 : throw com::sun::star::container::NoSuchElementException();
172 : else
173 0 : throw com::sun::star::lang::IllegalArgumentException();
174 0 : }
175 :
176 :
177 :
178 : // methods for XNameContainer (inherits XNameReplace)
179 :
180 :
181 0 : 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, std::exception ) SAL_OVERRIDE
188 : {
189 0 : T aItem;
190 0 : if( aElement >>= aItem )
191 0 : if( ! hasByName( rName ) )
192 0 : insert( rName, aItem );
193 : else
194 0 : throw com::sun::star::container::ElementExistException();
195 : else
196 0 : throw com::sun::star::lang::IllegalArgumentException();
197 0 : }
198 :
199 0 : 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, std::exception) SAL_OVERRIDE
204 : {
205 0 : if( hasByName( rName ) )
206 0 : remove( rName );
207 : else
208 0 : throw com::sun::star::container::NoSuchElementException();
209 0 : }
210 :
211 : };
212 :
213 : #endif
214 :
215 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|