namedvaluecollection.hxx 14.7 KB
Newer Older
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 13 14
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
15
 *
16 17 18 19 20
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
21
 *
22 23 24 25
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
26 27 28 29 30 31 32 33 34 35
 *
 ************************************************************************/

#ifndef COMPHELPER_NAMEDVALUECOLLECTION_HXX
#define COMPHELPER_NAMEDVALUECOLLECTION_HXX

#include <comphelper/comphelperdllapi.h>

#include <com/sun/star/uno/Sequence.hxx>
#include <com/sun/star/uno/Any.hxx>
36
#include <com/sun/star/beans/PropertyValue.hpp>
37
#include <com/sun/star/beans/NamedValue.hpp>
38 39

#include <memory>
40
#include <algorithm>
41
#include <vector>
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

//........................................................................
namespace comphelper
{
//........................................................................

    // ====================================================================
    // = NamedValueCollection
    // ====================================================================
    struct NamedValueCollection_Impl;
    /** a collection of named values, packed in various formats.
    */
    class COMPHELPER_DLLPUBLIC NamedValueCollection
    {
    private:
        ::std::auto_ptr< NamedValueCollection_Impl >    m_pImpl;

    public:
        NamedValueCollection();

62 63
        NamedValueCollection( const NamedValueCollection& _rCopySource );

64 65
        NamedValueCollection& operator=( const NamedValueCollection& i_rCopySource );

66 67 68 69 70 71 72 73
        /** constructs a collection
            @param  _rElements
                the wrapped elements of the collection. The <code>Any</code> might contain a sequence of
                property values, a sequence of named values, or directly a property value or named value.
                All other cases are worth an assertion in non-product builds.
        */
        NamedValueCollection( const ::com::sun::star::uno::Any& _rElements );

74 75 76 77 78
        /** constructs a collection
            @param _rArguments
                a sequence of Any's containing either PropertyValue's or NamedValue's.
        */
        NamedValueCollection( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& _rArguments );
79 80 81 82 83 84 85

        /** constructs a collection
            @param _rArguments
                a sequence of PropertyValues's
        */
        NamedValueCollection( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& _rArguments );

86 87 88 89 90 91
        /** constructs a collection
            @param _rArguments
                a sequence of NamedValue's
        */
        NamedValueCollection( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& _rArguments );

92 93
        ~NamedValueCollection();

94 95 96 97 98
        inline void assign( const ::com::sun::star::uno::Any& i_rWrappedElements )
        {
            impl_assign( i_rWrappedElements );
        }

99 100 101 102 103 104
        inline void assign( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& _rArguments )
        {
            impl_assign( _rArguments );
        }

        inline void assign( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& _rArguments )
105 106 107 108
        {
            impl_assign( _rArguments );
        }

109 110 111 112 113
        inline void assign( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& _rArguments )
        {
            impl_assign( _rArguments );
        }

114 115 116 117 118
        inline void clear()
        {
            impl_assign( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >() );
        }

119 120 121 122 123 124 125 126
        /** determines whether or not named values can be extracted from the given value

            @return
                <TRUE/> if and only if the given <code>Any</code> contains a <code>NamedValue</code>, a
                <code>PropertyValue</code>, or a sequence thereof.
        */
        static bool canExtractFrom( ::com::sun::star::uno::Any const & i_value );

127 128 129 130 131 132
        /// returns the number of elements in the collection
        size_t  size() const;

        /// determines whether the collection is empty
        bool    empty() const;

133 134
        /** returns the names of all elements in the collection
        */
135
        ::std::vector< OUString >
136 137
                getNames() const;

138 139 140 141 142 143 144 145 146 147 148 149 150 151
        /** merges the content of another collection into |this|
            @param _rAdditionalValues
                the collection whose values are to be merged
            @param _bOverwriteExisting
                defines whether or not elements which are already present in |this|
                should be overwritten (<TRUE/>) or preserved (<FALSE/>).
            @return |*this|
        */
        NamedValueCollection&
                merge(
                    const NamedValueCollection& _rAdditionalValues,
                    bool _bOverwriteExisting
                );

152 153 154 155 156 157 158 159 160 161 162
        /** retrieves a value with a given name from the collection, if it is present

            @param _pAsciiValueName
                the ASCII name of the value to retrieve

            @param _out_rValue
                is the output parameter taking the desired value upon successful return. If
                a value with the given name is not present in the collection, or if a wrong-typed
                value is present, then this parameter will not be touched.

            @return
163 164 165 166 167 168 169
                <TRUE/> if there is a value with the given name, which could successfully
                be extraced. In this case, <arg>_out_rValue</arg> will contain the requested
                value.<br/>
                <FALSE/>, if there is no value with the given name.
            @throws IllegalArgumentException
                in case there is a value with the given name, but it cannot legally assigned to
                _out_rValue.
170 171
        */
        template < typename VALUE_TYPE >
172
        bool get_ensureType( const sal_Char* _pAsciiValueName, VALUE_TYPE& _out_rValue ) const
173
        {
174
            return get_ensureType( OUString::createFromAscii( _pAsciiValueName ), &_out_rValue, ::cppu::UnoType< VALUE_TYPE >::get() );
175 176 177
        }

        template < typename VALUE_TYPE >
178
        bool    get_ensureType( const OUString& _rValueName, VALUE_TYPE& _out_rValue ) const
179
        {
180
            return get_ensureType( _rValueName, &_out_rValue, ::cppu::UnoType< VALUE_TYPE >::get() );
181 182 183 184 185 186 187 188
        }

        /** retrieves a value with a given name, or defaults it to a given value, if its not present
            in the colllection
        */
        template < typename VALUE_TYPE >
        VALUE_TYPE  getOrDefault( const sal_Char* _pAsciiValueName, const VALUE_TYPE& _rDefault ) const
        {
189
            return getOrDefault( OUString::createFromAscii( _pAsciiValueName ), _rDefault );
190 191 192
        }

        template < typename VALUE_TYPE >
193
        VALUE_TYPE  getOrDefault( const OUString& _rValueName, const VALUE_TYPE& _rDefault ) const
194 195
        {
            VALUE_TYPE retVal( _rDefault );
196
            get_ensureType( _rValueName, retVal );
197 198 199 200 201 202 203 204 205 206
            return retVal;
        }

        /** retrieves a (untyped) value with a given name

            If the collection does not contain a value with the given name, an empty
            Any is returned.
        */
        const ::com::sun::star::uno::Any& get( const sal_Char* _pAsciiValueName ) const
        {
207
            return get( OUString::createFromAscii( _pAsciiValueName ) );
208 209
        }

210 211 212 213 214
        /** retrieves a (untyped) value with a given name

            If the collection does not contain a value with the given name, an empty
            Any is returned.
        */
215
        const ::com::sun::star::uno::Any& get( const OUString& _rValueName ) const
216 217 218 219
        {
            return impl_get( _rValueName );
        }

220 221 222
        /// determines whether a value with a given name is present in the collection
        inline bool has( const sal_Char* _pAsciiValueName ) const
        {
223
            return impl_has( OUString::createFromAscii( _pAsciiValueName ) );
224 225 226
        }

        /// determines whether a value with a given name is present in the collection
227
        inline bool has( const OUString& _rValueName ) const
228 229 230 231
        {
            return impl_has( _rValueName );
        }

232 233 234 235 236 237 238 239
        /** puts a value into the collection

            @return <TRUE/> if and only if a value was already present previously, in
                which case it has been overwritten.
        */
        template < typename VALUE_TYPE >
        inline bool put( const sal_Char* _pAsciiValueName, const VALUE_TYPE& _rValue )
        {
240
            return impl_put( OUString::createFromAscii( _pAsciiValueName ), ::com::sun::star::uno::makeAny( _rValue ) );
241 242 243 244 245 246 247 248
        }

        /** puts a value into the collection

            @return <TRUE/> if and only if a value was already present previously, in
                which case it has been overwritten.
        */
        template < typename VALUE_TYPE >
249
        inline bool put( const OUString& _rValueName, const VALUE_TYPE& _rValue )
250 251 252 253 254 255
        {
            return impl_put( _rValueName, ::com::sun::star::uno::makeAny( _rValue ) );
        }

        inline bool put( const sal_Char* _pAsciiValueName, const ::com::sun::star::uno::Any& _rValue )
        {
256
            return impl_put( OUString::createFromAscii( _pAsciiValueName ), _rValue );
257 258
        }

259
        inline bool put( const OUString& _rValueName, const ::com::sun::star::uno::Any& _rValue )
260 261 262 263
        {
            return impl_put( _rValueName, _rValue );
        }

264 265 266 267 268 269
        /** removes the value with the given name from the collection

            @return <TRUE/> if and only if a value with the given name existed in the collection.
        */
        inline bool remove( const sal_Char* _pAsciiValueName )
        {
270
            return impl_remove( OUString::createFromAscii( _pAsciiValueName ) );
271 272 273 274 275 276
        }

        /** removes the value with the given name from the collection

            @return <TRUE/> if and only if a value with the given name existed in the collection.
        */
277
        inline bool remove( const OUString& _rValueName )
278 279 280 281
        {
            return impl_remove( _rValueName );
        }

282 283 284 285 286
        /** transforms the collection to a sequence of PropertyValues

            @return
                the  number of elements in the sequence
        */
287
        sal_Int32 operator >>= ( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& _out_rValues ) const;
288 289 290 291 292 293

        /** transforms the collection to a sequence of NamedValues

            @return
                the  number of elements in the sequence
        */
294 295 296 297 298 299 300 301 302 303 304 305
        sal_Int32 operator >>= ( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& _out_rValues ) const;

        /** transforms the collection into a sequence of PropertyValues
        */
        inline ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >
                getPropertyValues() const
        {
            ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > aValues;
            *this >>= aValues;
            return aValues;
        }

306
        /** returns a Sequence< Any >, containing PropertyValues
307 308 309 310 311 312 313
        */
        inline ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >
                getWrappedPropertyValues() const
        {
            return impl_wrap< ::com::sun::star::beans::PropertyValue >();
        }

314 315 316 317 318 319 320 321
        /** returns a Sequence< Any >, containing NamedValues
        */
        inline ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >
                getWrappedNamedValues() const
        {
            return impl_wrap< ::com::sun::star::beans::NamedValue >();
        }

322 323 324 325 326 327 328 329 330
        /** transforms the collection into a sequence of NamedValues
        */
        inline ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >
                getNamedValues() const
        {
            ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > aValues;
            *this >>= aValues;
            return aValues;
        }
331

332
    private:
333
        void    impl_assign( const ::com::sun::star::uno::Any& i_rWrappedElements );
334
        void    impl_assign( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& _rArguments );
335
        void    impl_assign( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& _rArguments );
336
        void    impl_assign( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& _rArguments );
337

338
        bool    get_ensureType(
339
                    const OUString& _rValueName,
340 341 342 343 344
                    void* _pValueLocation,
                    const ::com::sun::star::uno::Type& _rExpectedValueType
                ) const;

        const ::com::sun::star::uno::Any&
345
                impl_get( const OUString& _rValueName ) const;
346

347
        bool    impl_has( const OUString& _rValueName ) const;
348

349
        bool    impl_put( const OUString& _rValueName, const ::com::sun::star::uno::Any& _rValue );
350

351
        bool    impl_remove( const OUString& _rValueName );
352 353 354 355 356 357 358

        template< class VALUE_TYPE >
        ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > impl_wrap() const
        {
            ::com::sun::star::uno::Sequence< VALUE_TYPE > aValues;
            *this >>= aValues;
            ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > aWrappedValues( aValues.getLength() );
359
            ::com::sun::star::uno::Any (* const makeAny)(const VALUE_TYPE&) = ::com::sun::star::uno::makeAny< VALUE_TYPE >;
360 361 362 363
            ::std::transform(
                aValues.getConstArray(),
                aValues.getConstArray() + aValues.getLength(),
                aWrappedValues.getArray(),
364
                makeAny
365 366 367
            );
            return aWrappedValues;
        }
368 369 370 371 372 373 374 375
    };

//........................................................................
} // namespace comphelper
//........................................................................

#endif // COMPHELPER_NAMEDVALUECOLLECTION_HXX

376
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */