LCOV - code coverage report
Current view: top level - forms/source/xforms - NameContainer.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 2 58 3.4 %
Date: 2015-06-13 12:38:46 Functions: 5 34 14.7 %
Legend: Lines: hit not hit

          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             : #include <osl/diagnose.h>
      35             : 
      36             : typedef cppu::WeakImplHelper1<
      37             :     com::sun::star::container::XNameContainer
      38             : > NameContainer_t;
      39             : 
      40             : template<class T>
      41             : class NameContainer : public NameContainer_t
      42             : {
      43             : protected:
      44             :     typedef std::map<OUString,T> map_t;
      45             :     map_t maItems;
      46             : 
      47             : 
      48           0 :     bool hasItems()
      49             :     {
      50           0 :         return ! maItems.empty();
      51             :     }
      52             : 
      53           0 :     typename map_t::const_iterator findItem( const OUString& rName )
      54             :     {
      55           0 :         return maItems.find( rName );
      56             :     }
      57             : 
      58           0 :     bool hasItem( const OUString& rName )
      59             :     {
      60           0 :         return findItem( rName ) != maItems.end();
      61             :     }
      62             : 
      63             :     T getItem( const OUString& rName )
      64             :     {
      65             :         OSL_ENSURE( hasItem( rName ), "can't get non-existent item" );
      66             :         return maItems[ rName ];
      67             :     }
      68             : 
      69             : 
      70           0 :     void replace( const OUString& rName,
      71             :                   const T& aElement )
      72             :     {
      73             :         OSL_ENSURE( hasItem( rName ), "unknown item" );
      74           0 :         maItems[ rName ] = aElement;
      75           0 :     }
      76             : 
      77           0 :     void insert( const OUString& rName,
      78             :                  const T& aElement )
      79             :     {
      80             :         OSL_ENSURE( ! hasItem( rName ), "item already in set" );
      81           0 :         maItems[ rName ] = aElement;
      82           0 :     }
      83             : 
      84           0 :     void remove( const OUString& rName )
      85             :     {
      86             :         OSL_ENSURE( hasItem( rName ), "item not in set" );
      87           0 :         maItems.erase( rName );
      88           0 :     }
      89             : 
      90             : 
      91             : public:
      92             : 
      93           2 :     NameContainer() {}
      94           3 :     virtual ~NameContainer() {}
      95             : 
      96             : 
      97             :     // methods for XElementAccess
      98             : 
      99             : 
     100           0 :     virtual com::sun::star::uno::Type SAL_CALL getElementType()
     101             :         throw( com::sun::star::uno::RuntimeException, std::exception ) SAL_OVERRIDE
     102             :     {
     103           0 :         return cppu::UnoType<T>::get();
     104             :     }
     105             : 
     106           0 :     virtual sal_Bool SAL_CALL hasElements()
     107             :         throw( com::sun::star::uno::RuntimeException, std::exception ) SAL_OVERRIDE
     108             :     {
     109           0 :         return hasItems();
     110             :     }
     111             : 
     112             : 
     113             : 
     114             :     // methods for XNameAccess (inherits XElementAccess)
     115             : 
     116             : 
     117           0 :     virtual com::sun::star::uno::Any SAL_CALL getByName(
     118             :         const OUString& rName )
     119             :         throw( com::sun::star::container::NoSuchElementException,
     120             :                com::sun::star::lang::WrappedTargetException,
     121             :                com::sun::star::uno::RuntimeException, std::exception ) SAL_OVERRIDE
     122             :     {
     123           0 :         typename map_t::const_iterator aIter = findItem( rName );
     124           0 :         if( aIter == maItems.end() )
     125           0 :             throw com::sun::star::container::NoSuchElementException();
     126             :         else
     127           0 :             return com::sun::star::uno::makeAny( aIter->second );
     128             :     }
     129             : 
     130           0 :     virtual com::sun::star::uno::Sequence<OUString> SAL_CALL getElementNames()
     131             :         throw( com::sun::star::uno::RuntimeException, std::exception ) SAL_OVERRIDE
     132             :     {
     133           0 :         com::sun::star::uno::Sequence<OUString> aSequence( maItems.size() );
     134           0 :         typename map_t::const_iterator aIter = maItems.begin();
     135           0 :         OUString* pStrings = aSequence.getArray();
     136           0 :         while( aIter != maItems.end() )
     137             :         {
     138           0 :             *pStrings = aIter->first;
     139           0 :             ++aIter;
     140           0 :             ++pStrings;
     141             :         }
     142             :         OSL_ENSURE( pStrings == aSequence.getArray() + aSequence.getLength(),
     143             :                     "sequence not of right size; possible buffer overflow" );
     144           0 :         return aSequence;
     145             :     }
     146             : 
     147           0 :     virtual sal_Bool SAL_CALL hasByName(
     148             :         const OUString& rName )
     149             :         throw( com::sun::star::uno::RuntimeException, std::exception ) SAL_OVERRIDE
     150             :     {
     151           0 :         return hasItem( rName );
     152             :     }
     153             : 
     154             : 
     155             : 
     156             :     // methods for XNameReplace (inherits XNameAccess)
     157             : 
     158             : 
     159           0 :     virtual void SAL_CALL replaceByName(
     160             :         const OUString& rName,
     161             :         const com::sun::star::uno::Any& aElement )
     162             :         throw( com::sun::star::lang::IllegalArgumentException,
     163             :                com::sun::star::container::NoSuchElementException,
     164             :                com::sun::star::lang::WrappedTargetException,
     165             :                com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
     166             :     {
     167           0 :         T aItem;
     168           0 :         if( aElement >>= aItem )
     169           0 :             if( hasByName( rName ) )
     170           0 :                 replace( rName, aItem );
     171             :             else
     172           0 :                 throw com::sun::star::container::NoSuchElementException();
     173             :         else
     174           0 :             throw com::sun::star::lang::IllegalArgumentException();
     175           0 :     }
     176             : 
     177             : 
     178             : 
     179             :     // methods for XNameContainer (inherits XNameReplace)
     180             : 
     181             : 
     182           0 :     virtual void SAL_CALL insertByName(
     183             :         const OUString& rName,
     184             :         const com::sun::star::uno::Any& aElement )
     185             :         throw( com::sun::star::lang::IllegalArgumentException,
     186             :                com::sun::star::container::ElementExistException,
     187             :                com::sun::star::lang::WrappedTargetException,
     188             :                com::sun::star::uno::RuntimeException, std::exception ) SAL_OVERRIDE
     189             :     {
     190           0 :         T aItem;
     191           0 :         if( aElement >>= aItem )
     192           0 :             if( ! hasByName( rName ) )
     193           0 :                 insert( rName, aItem );
     194             :             else
     195           0 :                 throw com::sun::star::container::ElementExistException();
     196             :         else
     197           0 :             throw com::sun::star::lang::IllegalArgumentException();
     198           0 :     }
     199             : 
     200           0 :     virtual void SAL_CALL removeByName(
     201             :         const OUString& rName )
     202             :         throw( com::sun::star::container::NoSuchElementException,
     203             :                com::sun::star::lang::WrappedTargetException,
     204             :                com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
     205             :     {
     206           0 :         if( hasByName( rName ) )
     207           0 :             remove( rName );
     208             :         else
     209           0 :             throw com::sun::star::container::NoSuchElementException();
     210           0 :     }
     211             : 
     212             : };
     213             : 
     214             : #endif
     215             : 
     216             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11