1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
22 #include <comphelper/sequence.hxx>
23 #include <cppuhelper/implbase.hxx>
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/uno/Any.hxx>
30 #include <com/sun/star/uno/Type.hxx>
31 #include <osl/diagnose.h>
33 typedef cppu::WeakImplHelper
<
34 css::container::XNameContainer
38 class NameContainer
: public NameContainer_t
40 typedef std::map
<OUString
,T
> map_t
;
44 typename
map_t::const_iterator
findItem( const OUString
& rName
)
46 return maItems
.find( rName
);
49 bool hasItem( const OUString
& rName
)
51 return findItem( rName
) != maItems
.end();
54 void replace( const OUString
& rName
,
57 OSL_ENSURE( hasItem( rName
), "unknown item" );
58 maItems
[ rName
] = aElement
;
61 void insert( const OUString
& rName
,
64 OSL_ENSURE( ! hasItem( rName
), "item already in set" );
65 maItems
[ rName
] = aElement
;
68 void remove( const OUString
& rName
)
70 OSL_ENSURE( hasItem( rName
), "item not in set" );
71 maItems
.erase( rName
);
80 // methods for XElementAccess
83 virtual css::uno::Type SAL_CALL
getElementType() override
85 return cppu::UnoType
<T
>::get();
88 virtual sal_Bool SAL_CALL
hasElements() override
90 return ! maItems
.empty();
94 // methods for XNameAccess (inherits XElementAccess)
97 virtual css::uno::Any SAL_CALL
getByName(
98 const OUString
& rName
) override
100 typename
map_t::const_iterator aIter
= findItem( rName
);
101 if( aIter
== maItems
.end() )
102 throw css::container::NoSuchElementException();
103 return css::uno::Any( aIter
->second
);
106 virtual css::uno::Sequence
<OUString
> SAL_CALL
getElementNames() override
108 return comphelper::mapKeysToSequence(maItems
);
111 virtual sal_Bool SAL_CALL
hasByName(
112 const OUString
& rName
) override
114 return hasItem( rName
);
118 // methods for XNameReplace (inherits XNameAccess)
121 virtual void SAL_CALL
replaceByName(
122 const OUString
& rName
,
123 const css::uno::Any
& aElement
) override
126 if( !(aElement
>>= aItem
) )
127 throw css::lang::IllegalArgumentException();
128 if( !hasByName( rName
) )
129 throw css::container::NoSuchElementException();
130 replace( rName
, aItem
);
134 // methods for XNameContainer (inherits XNameReplace)
137 virtual void SAL_CALL
insertByName(
138 const OUString
& rName
,
139 const css::uno::Any
& aElement
) override
142 if( !(aElement
>>= aItem
) )
143 throw css::lang::IllegalArgumentException();
144 if( hasByName( rName
) )
145 throw css::container::ElementExistException();
146 insert( rName
, aItem
);
149 virtual void SAL_CALL
removeByName(
150 const OUString
& rName
) override
152 if( !hasByName( rName
) )
153 throw css::container::NoSuchElementException();
159 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */