1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: MultiPropertySetHelper.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #ifndef _XMLOFF_CONDITIONALMULTIPROPERTYSETHELPER_HXX
31 #define _XMLOFF_CONDITIONALMULTIPROPERTYSETHELPER_HXX
34 #ifndef _COM_SUN_STAR_UNO_SEQUENCE_HXX_
35 #include <com/sun/star/uno/Sequence.hxx>
38 #ifndef _TOOLS_DEBUG_HXX
39 #include <tools/debug.hxx>
43 namespace com
{ namespace sun
{ namespace star
{
44 namespace beans
{ class XMultiPropertySet
; }
45 namespace beans
{ class XPropertySet
; }
46 namespace beans
{ class XPropertySetInfo
; }
52 * The MultiPropertySetHelper performs the follwing functions:
54 * Given a list of property names (as sal_Char** or OUString*), it can
55 * query an XMultiPropertySet (or XPropertySet) which of these properties
56 * it supports (method hasProperties(...)). The properties *MUST* be
57 * sorted alphabetically.
59 * Then, the X(Multi)PropertySet can be queried for values, and only
60 * the supported properties are queried. (method getValues(...)) The
61 * values are stored in the helper itself.
63 * Finally, each property can be queried for existence
64 * (method hasProperty(...)) or its value (method (getValue(...))).
66 * After some initial preparation (hasProperties, getValues) the
67 * MultiPropertySetHelper can be used similarly to an
68 * XPropertySet in that you can query the values in the places where you
69 * need them. However, if an XMultiPropertySet is supplied, the queries
70 * are more efficient, often significantly so.
72 class MultiPropertySetHelper
74 /// names of all properties
75 ::rtl::OUString
* pPropertyNames
;
77 /// length of pPropertyNames array
80 /// the sequence of property names that the current (multi)
81 /// property set implementation supports
82 ::com::sun::star::uno::Sequence
< ::rtl::OUString
> aPropertySequence
;
84 /// an array of indices that maps from pPropertyNames indices to
85 /// aPropertySequence indices
86 sal_Int16
* pSequenceIndex
;
88 /// the last set of values retrieved by getValues
89 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
> aValues
;
91 /// result of aValues.getConstArray()
92 const ::com::sun::star::uno::Any
* pValues
;
95 ::com::sun::star::uno::Any aEmptyAny
;
99 MultiPropertySetHelper( const sal_Char
** pNames
);
101 ~MultiPropertySetHelper();
105 * Call hasPropertiesByName for the provided XPropertySetInfo and build
106 * list of allowed properties.
108 void hasProperties( const ::com::sun::star::uno::Reference
<
109 ::com::sun::star::beans::XPropertySetInfo
> & );
113 * Return whether hasProperties was called
114 * (i.e. if we are ready to call getValues)
116 sal_Bool
checkedProperties();
119 * Get values from the XMultiPropertySet.
121 * May only be called after hasProperties() was called for the
122 * appropriate XPropertySetInfo.
124 void getValues( const ::com::sun::star::uno::Reference
<
125 ::com::sun::star::beans::XMultiPropertySet
> & );
128 * Get values from the XPropertySet. This can be much slower than
129 * getValues( const Reference<XMultiPropertySet& ) and hence
132 * May only be called after hasProperties() was called for the
133 * appropriate XPropertySetInfo.
135 void getValues( const ::com::sun::star::uno::Reference
<
136 ::com::sun::star::beans::XPropertySet
> & );
141 * Get a value from the values array.
143 * May only be called after getValues() was called.
145 inline const ::com::sun::star::uno::Any
& getValue( sal_Int16 nIndex
);
148 * Find out if this property is supported.
150 * May only be called after hasProperties() was called.
152 inline sal_Bool
hasProperty( sal_Int16 nIndex
);
155 * Get a value from the XPropertySet on demand.
157 * If neither getValues nor getValueOnDemand has been called already
158 * after the last call to resetValues, the values are retrieved
159 * using getValues. Otherwise the value already retrieved is returned.
160 * In case XMultiPropertySet is supported by the XPropertySet and
161 * bTryMult is set, the XMultiPropertySet is used to get the values.
164 const ::com::sun::star::uno::Any
& getValue( sal_Int16 nIndex
,
165 const ::com::sun::star::uno::Reference
<
166 ::com::sun::star::beans::XPropertySet
> &,
167 sal_Bool bTryMulti
= sal_False
);
170 * Get a value from the XMultiPropertySet on demand.
172 * If neither getValues nor getValueOnDemand has been called already
173 * after the last call to resetValues, the values are retrieved
174 * using getValues. Otherwise the value already retrieved is returned.
175 * In case XMultiPropertySet is supported by the XPropertySet,
176 * XMultiPropertySet is used to get the values.
179 const ::com::sun::star::uno::Any
& getValue( sal_Int16 nIndex
,
180 const ::com::sun::star::uno::Reference
<
181 ::com::sun::star::beans::XMultiPropertySet
> & );
183 inline void resetValues() { pValues
= 0; }
187 // inline implementations of the often-called methods getValue and hasProperty:
189 const ::com::sun::star::uno::Any
& MultiPropertySetHelper::getValue(
192 DBG_ASSERT( pValues
!= NULL
,
193 "called getValue() without calling getValues() before");
194 DBG_ASSERT( pSequenceIndex
!= NULL
,
195 "called getValue() without calling hasProperties() before" );
196 DBG_ASSERT( nValueNo
< nLength
, "index out of range" );
198 sal_Int16 nIndex
= pSequenceIndex
[ nValueNo
];
199 return ( nIndex
!= -1 ) ? pValues
[ nIndex
] : aEmptyAny
;
202 sal_Bool
MultiPropertySetHelper::hasProperty( sal_Int16 nValueNo
)
204 DBG_ASSERT( pSequenceIndex
!= NULL
,
205 "called getValue() without calling hasProperties() before" );
206 DBG_ASSERT( nValueNo
< nLength
, "index out of range" );
208 return pSequenceIndex
[ nValueNo
] != -1;
211 }//end of namespace binfilter