LCOV - code coverage report
Current view: top level - xmloff/inc - MultiPropertySetHelper.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 6 6 100.0 %
Date: 2012-08-25 Functions: 3 3 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 1 2 50.0 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*************************************************************************
       3                 :            :  *
       4                 :            :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5                 :            :  *
       6                 :            :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7                 :            :  *
       8                 :            :  * OpenOffice.org - a multi-platform office productivity suite
       9                 :            :  *
      10                 :            :  * This file is part of OpenOffice.org.
      11                 :            :  *
      12                 :            :  * OpenOffice.org is free software: you can redistribute it and/or modify
      13                 :            :  * it under the terms of the GNU Lesser General Public License version 3
      14                 :            :  * only, as published by the Free Software Foundation.
      15                 :            :  *
      16                 :            :  * OpenOffice.org is distributed in the hope that it will be useful,
      17                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19                 :            :  * GNU Lesser General Public License version 3 for more details
      20                 :            :  * (a copy is included in the LICENSE file that accompanied this code).
      21                 :            :  *
      22                 :            :  * You should have received a copy of the GNU Lesser General Public License
      23                 :            :  * version 3 along with OpenOffice.org.  If not, see
      24                 :            :  * <http://www.openoffice.org/license.html>
      25                 :            :  * for a copy of the LGPLv3 License.
      26                 :            :  *
      27                 :            :  ************************************************************************/
      28                 :            : #ifndef _XMLOFF_CONDITIONALMULTIPROPERTYSETHELPER_HXX
      29                 :            : #define _XMLOFF_CONDITIONALMULTIPROPERTYSETHELPER_HXX
      30                 :            : 
      31                 :            : #include <rtl/ustring.hxx>
      32                 :            : #include <com/sun/star/uno/Sequence.hxx>
      33                 :            : #include <tools/debug.hxx>
      34                 :            : 
      35                 :            : 
      36                 :            : namespace com { namespace sun { namespace star {
      37                 :            :     namespace beans { class XMultiPropertySet; }
      38                 :            :     namespace beans { class XPropertySet; }
      39                 :            :     namespace beans { class XPropertySetInfo; }
      40                 :            : } } }
      41                 :            : 
      42                 :            : 
      43                 :            : /**
      44                 :            :  * The MultiPropertySetHelper performs the follwing functions:
      45                 :            :  *
      46                 :            :  * Given a list of property names (as sal_Char** or OUString*), it can
      47                 :            :  * query an XMultiPropertySet (or XPropertySet) which of these properties
      48                 :            :  * it supports (method hasProperties(...)). The properties *MUST* be
      49                 :            :  * sorted alphabetically.
      50                 :            :  *
      51                 :            :  * Then, the X(Multi)PropertySet can be queried for values, and only
      52                 :            :  * the supported properties are queried. (method getValues(...)) The
      53                 :            :  * values are stored in the helper itself.
      54                 :            :  *
      55                 :            :  * Finally, each property can be queried for existence
      56                 :            :  * (method hasProperty(...)) or its value (method (getValue(...))).
      57                 :            :  *
      58                 :            :  * After some initial preparation (hasProperties, getValues) the
      59                 :            :  * MultiPropertySetHelper can be used similarly to an
      60                 :            :  * XPropertySet in that you can query the values in the places where you
      61                 :            :  * need them. However, if an XMultiPropertySet is supplied, the queries
      62                 :            :  * are more efficient, often significantly so.
      63                 :            :  */
      64                 :            : class MultiPropertySetHelper
      65                 :            : {
      66                 :            :     /// names of all properties
      67                 :            :     ::rtl::OUString* pPropertyNames;
      68                 :            : 
      69                 :            :     /// length of pPropertyNames array
      70                 :            :     sal_Int16 nLength;
      71                 :            : 
      72                 :            :     /// the sequence of property names that the current (multi)
      73                 :            :     /// property set implementation supports
      74                 :            :     ::com::sun::star::uno::Sequence< ::rtl::OUString > aPropertySequence;
      75                 :            : 
      76                 :            :     /// an array of indices that maps from pPropertyNames indices to
      77                 :            :     /// aPropertySequence indices
      78                 :            :     sal_Int16* pSequenceIndex;
      79                 :            : 
      80                 :            :     /// the last set of values retrieved by getValues
      81                 :            :     ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > aValues;
      82                 :            : 
      83                 :            :     /// result of aValues.getConstArray()
      84                 :            :     const ::com::sun::star::uno::Any* pValues;
      85                 :            : 
      86                 :            :     /// an empty Any
      87                 :            :     ::com::sun::star::uno::Any aEmptyAny;
      88                 :            : 
      89                 :            : public:
      90                 :            : 
      91                 :            :     MultiPropertySetHelper( const sal_Char** pNames );
      92                 :            : 
      93                 :            :     ~MultiPropertySetHelper();
      94                 :            : 
      95                 :            : 
      96                 :            :     /**
      97                 :            :      * Call hasPropertiesByName for the provided XPropertySetInfo and build
      98                 :            :      * list of allowed properties.
      99                 :            :      */
     100                 :            :     void hasProperties( const ::com::sun::star::uno::Reference<
     101                 :            :                             ::com::sun::star::beans::XPropertySetInfo> & );
     102                 :            : 
     103                 :            : 
     104                 :            :     /**
     105                 :            :      * Return whether hasProperties was called
     106                 :            :      * (i.e. if we are ready to call getValues)
     107                 :            :      */
     108                 :            :     sal_Bool checkedProperties();
     109                 :            : 
     110                 :            :     /**
     111                 :            :      * Get values from the XMultiPropertySet.
     112                 :            :      *
     113                 :            :      * May only be called after hasProperties() was called for the
     114                 :            :      * appropriate XPropertySetInfo.
     115                 :            :      */
     116                 :            :     void getValues( const ::com::sun::star::uno::Reference<
     117                 :            :                             ::com::sun::star::beans::XMultiPropertySet> & );
     118                 :            : 
     119                 :            :     /**
     120                 :            :      * Get values from the XPropertySet. This can be much slower than
     121                 :            :      * getValues( const Reference<XMultiPropertySet& ) and hence
     122                 :            :      * should be avoided.
     123                 :            :      *
     124                 :            :      * May only be called after hasProperties() was called for the
     125                 :            :      * appropriate XPropertySetInfo.
     126                 :            :      */
     127                 :            :     void getValues( const ::com::sun::star::uno::Reference<
     128                 :            :                             ::com::sun::star::beans::XPropertySet> & );
     129                 :            : 
     130                 :            : 
     131                 :            : 
     132                 :            :     /**
     133                 :            :      * Get a value from the values array.
     134                 :            :      *
     135                 :            :      * May only be called after getValues() was called.
     136                 :            :      */
     137                 :            :     inline const ::com::sun::star::uno::Any& getValue( sal_Int16 nIndex );
     138                 :            : 
     139                 :            :     /**
     140                 :            :      * Find out if this property is supported.
     141                 :            :      *
     142                 :            :      * May only be called after hasProperties() was called.
     143                 :            :      */
     144                 :            :     inline sal_Bool hasProperty( sal_Int16 nIndex );
     145                 :            : 
     146                 :            :     /**
     147                 :            :      * Get a value from the XPropertySet on demand.
     148                 :            :      *
     149                 :            :      * If neither getValues nor getValueOnDemand has been called already
     150                 :            :      * after the last call to resetValues, the values are retrieved
     151                 :            :      * using getValues. Otherwise the value already retrieved is returned.
     152                 :            :      * In case XMultiPropertySet is supported by the XPropertySet and
     153                 :            :      * bTryMult is set, the XMultiPropertySet is used to get the values.
     154                 :            :      *
     155                 :            :      */
     156                 :            :     const ::com::sun::star::uno::Any& getValue( sal_Int16 nIndex,
     157                 :            :                         const ::com::sun::star::uno::Reference<
     158                 :            :                             ::com::sun::star::beans::XPropertySet> &,
     159                 :            :                         sal_Bool bTryMulti = sal_False );
     160                 :            : 
     161                 :            :     /**
     162                 :            :      * Get a value from the XMultiPropertySet on demand.
     163                 :            :      *
     164                 :            :      * If neither getValues nor getValueOnDemand has been called already
     165                 :            :      * after the last call to resetValues, the values are retrieved
     166                 :            :      * using getValues. Otherwise the value already retrieved is returned.
     167                 :            :      * In case XMultiPropertySet is supported by the XPropertySet,
     168                 :            :      * XMultiPropertySet is used to get the values.
     169                 :            :      *
     170                 :            :      */
     171                 :            :     const ::com::sun::star::uno::Any& getValue( sal_Int16 nIndex,
     172                 :            :                         const ::com::sun::star::uno::Reference<
     173                 :            :                             ::com::sun::star::beans::XMultiPropertySet> & );
     174                 :            : 
     175                 :        390 :     inline void resetValues() { pValues = 0; }
     176                 :            : };
     177                 :            : 
     178                 :            : 
     179                 :            : // inline implementations of the often-called methods getValue and hasProperty:
     180                 :            : 
     181                 :        755 : const ::com::sun::star::uno::Any& MultiPropertySetHelper::getValue(
     182                 :            :     sal_Int16 nValueNo )
     183                 :            : {
     184                 :            :     DBG_ASSERT( pValues != NULL,
     185                 :            :                 "called getValue() without calling getValues() before");
     186                 :            :     DBG_ASSERT( pSequenceIndex != NULL,
     187                 :            :                 "called getValue() without calling hasProperties() before" );
     188                 :            :     DBG_ASSERT( nValueNo < nLength, "index out of range" );
     189                 :            : 
     190                 :        755 :     sal_Int16 nIndex = pSequenceIndex[ nValueNo ];
     191         [ +  - ]:        755 :     return ( nIndex != -1 ) ? pValues[ nIndex ] : aEmptyAny;
     192                 :            : }
     193                 :            : 
     194                 :       1051 : sal_Bool MultiPropertySetHelper::hasProperty( sal_Int16 nValueNo )
     195                 :            : {
     196                 :            :     DBG_ASSERT( pSequenceIndex != NULL,
     197                 :            :                 "called getValue() without calling hasProperties() before" );
     198                 :            :     DBG_ASSERT( nValueNo < nLength, "index out of range" );
     199                 :            : 
     200                 :       1051 :     return pSequenceIndex[ nValueNo ] != -1;
     201                 :            : }
     202                 :            : 
     203                 :            : #endif
     204                 :            : 
     205                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10