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 #pragma once 21 22 #include <comphelper/sequence.hxx> 23 #include <cppuhelper/implbase.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/uno/Any.hxx> 30 #include <com/sun/star/uno/Type.hxx> 31 #include <osl/diagnose.h> 32 33 typedef cppu::WeakImplHelper< 34 css::container::XNameContainer 35 > NameContainer_t; 36 37 template<class T> 38 class NameContainer : public NameContainer_t 39 { 40 typedef std::map<OUString,T> map_t; 41 map_t maItems; 42 43 protected: findItem(const OUString & rName)44 typename map_t::const_iterator findItem( const OUString& rName ) 45 { 46 return maItems.find( rName ); 47 } 48 hasItem(const OUString & rName)49 bool hasItem( const OUString& rName ) 50 { 51 return findItem( rName ) != maItems.end(); 52 } 53 replace(const OUString & rName,const T & aElement)54 void replace( const OUString& rName, 55 const T& aElement ) 56 { 57 OSL_ENSURE( hasItem( rName ), "unknown item" ); 58 maItems[ rName ] = aElement; 59 } 60 insert(const OUString & rName,const T & aElement)61 void insert( const OUString& rName, 62 const T& aElement ) 63 { 64 OSL_ENSURE( ! hasItem( rName ), "item already in set" ); 65 maItems[ rName ] = aElement; 66 } 67 remove(const OUString & rName)68 void remove( const OUString& rName ) 69 { 70 OSL_ENSURE( hasItem( rName ), "item not in set" ); 71 maItems.erase( rName ); 72 } 73 74 75 public: 76 NameContainer()77 NameContainer() {} 78 79 80 // methods for XElementAccess 81 82 getElementType()83 virtual css::uno::Type SAL_CALL getElementType() override 84 { 85 return cppu::UnoType<T>::get(); 86 } 87 hasElements()88 virtual sal_Bool SAL_CALL hasElements() override 89 { 90 return ! maItems.empty(); 91 } 92 93 94 // methods for XNameAccess (inherits XElementAccess) 95 96 getByName(const OUString & rName)97 virtual css::uno::Any SAL_CALL getByName( 98 const OUString& rName ) override 99 { 100 typename map_t::const_iterator aIter = findItem( rName ); 101 if( aIter == maItems.end() ) 102 throw css::container::NoSuchElementException(); 103 return css::uno::makeAny( aIter->second ); 104 } 105 getElementNames()106 virtual css::uno::Sequence<OUString> SAL_CALL getElementNames() override 107 { 108 return comphelper::mapKeysToSequence(maItems); 109 } 110 hasByName(const OUString & rName)111 virtual sal_Bool SAL_CALL hasByName( 112 const OUString& rName ) override 113 { 114 return hasItem( rName ); 115 } 116 117 118 // methods for XNameReplace (inherits XNameAccess) 119 120 replaceByName(const OUString & rName,const css::uno::Any & aElement)121 virtual void SAL_CALL replaceByName( 122 const OUString& rName, 123 const css::uno::Any& aElement ) override 124 { 125 T aItem; 126 if( !(aElement >>= aItem) ) 127 throw css::lang::IllegalArgumentException(); 128 if( !hasByName( rName ) ) 129 throw css::container::NoSuchElementException(); 130 replace( rName, aItem ); 131 } 132 133 134 // methods for XNameContainer (inherits XNameReplace) 135 136 insertByName(const OUString & rName,const css::uno::Any & aElement)137 virtual void SAL_CALL insertByName( 138 const OUString& rName, 139 const css::uno::Any& aElement ) override 140 { 141 T aItem; 142 if( !(aElement >>= aItem) ) 143 throw css::lang::IllegalArgumentException(); 144 if( hasByName( rName ) ) 145 throw css::container::ElementExistException(); 146 insert( rName, aItem ); 147 } 148 removeByName(const OUString & rName)149 virtual void SAL_CALL removeByName( 150 const OUString& rName ) override 151 { 152 if( !hasByName( rName ) ) 153 throw css::container::NoSuchElementException(); 154 remove( rName ); 155 } 156 157 }; 158 159 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 160