LCOV - code coverage report
Current view: top level - include/com/sun/star/uno - Any.h (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 16 16 100.0 %
Date: 2014-11-03 Functions: 8 8 100.0 %
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             : #ifndef INCLUDED_COM_SUN_STAR_UNO_ANY_H
      20             : #define INCLUDED_COM_SUN_STAR_UNO_ANY_H
      21             : 
      22             : #include <uno/any2.h>
      23             : #include <typelib/typedescription.h>
      24             : #include <com/sun/star/uno/Type.h>
      25             : #include <cppu/unotype.hxx>
      26             : #include <rtl/alloc.h>
      27             : 
      28             : 
      29             : namespace com
      30             : {
      31             : namespace sun
      32             : {
      33             : namespace star
      34             : {
      35             : namespace uno
      36             : {
      37             : 
      38             : /** C++ class representing an IDL any.
      39             :     This class is used to transport any type defined in IDL. The class inherits from the
      40             :     binary C representation of uno_Any.
      41             :     You can insert a value by either using the <<= operators or the template function makeAny().
      42             :     No any can hold an any. You can extract values from an any by using the >>= operators which
      43             :     return true if the any contains an assignable value (no data loss), e.g. the any contains a
      44             :     short and you >>= it into a long variable.
      45             : */
      46             : class SAL_WARN_UNUSED Any : public uno_Any
      47             : {
      48             : public:
      49             :     /// @cond INTERNAL
      50             :     // these are here to force memory de/allocation to sal lib.
      51      453953 :     inline static void * SAL_CALL operator new ( size_t nSize )
      52      453953 :         { return ::rtl_allocateMemory( nSize ); }
      53      453953 :     inline static void SAL_CALL operator delete ( void * pMem )
      54      453953 :         { ::rtl_freeMemory( pMem ); }
      55             :     inline static void * SAL_CALL operator new ( size_t, void * pMem )
      56             :         { return pMem; }
      57             :     inline static void SAL_CALL operator delete ( void *, void * )
      58             :         {}
      59             :     /// @endcond
      60             : 
      61             :     /** Default constructor: Any holds no value; its type is void.
      62             :     */
      63             :     inline Any();
      64             : 
      65             :     /** Templated ctor.  Sets a copy of the given value.
      66             : 
      67             :         @param value value of the Any
      68             :     */
      69             :     template <typename T>
      70             :     explicit inline Any( T const & value );
      71             :     /// Ctor support for C++ bool.
      72             :     explicit inline Any( bool value );
      73             : 
      74             :     /** Copy constructor: Sets value of the given any.
      75             : 
      76             :         @param rAny another any
      77             :     */
      78             :     inline Any( const Any & rAny );
      79             : 
      80             :     /** Constructor: Sets a copy of the given data.
      81             : 
      82             :         @param pData_ value
      83             :         @param rType type of value
      84             :     */
      85             :     inline Any( const void * pData_, const Type & rType );
      86             : 
      87             :     /** Constructor: Sets a copy of the given data.
      88             : 
      89             :         @param pData_ value
      90             :         @param pTypeDescr type of value
      91             :     */
      92             :     inline Any( const void * pData_, typelib_TypeDescription * pTypeDescr );
      93             : 
      94             :     /** Constructor: Sets a copy of the given data.
      95             : 
      96             :         @param pData_ value
      97             :         @param pType type of value
      98             :     */
      99             :     inline Any( const void * pData_, typelib_TypeDescriptionReference * pType );
     100             : 
     101             :     /** Destructor: Destructs any content and frees memory.
     102             :     */
     103             :     inline ~Any();
     104             : 
     105             :     /** Assignment operator: Sets the value of the given any.
     106             : 
     107             :         @param rAny another any (right side)
     108             :         @return this any
     109             :     */
     110             :     inline Any & SAL_CALL operator = ( const Any & rAny );
     111             : 
     112             :     /** Gets the type of the set value.
     113             : 
     114             :         @return a Type object of the set value
     115             :      */
     116    10890673 :     inline const Type & SAL_CALL getValueType() const
     117    10890673 :         { return * reinterpret_cast< const Type * >( &pType ); }
     118             :     /** Gets the type of the set value.
     119             : 
     120             :         @return the unacquired type description reference of the set value
     121             :      */
     122       50307 :     inline typelib_TypeDescriptionReference * SAL_CALL getValueTypeRef() const
     123       50307 :         { return pType; }
     124             : 
     125             :     /** Gets the type description of the set value. Provides ownership of the type description!
     126             :         Call an explicit typelib_typedescription_release() to release afterwards.
     127             : 
     128             :         @param ppTypeDescr a pointer to type description pointer
     129             :     */
     130             :     inline void SAL_CALL getValueTypeDescription( typelib_TypeDescription ** ppTypeDescr ) const
     131             :         { ::typelib_typedescriptionreference_getDescription( ppTypeDescr, pType ); }
     132             : 
     133             :     /** Gets the type class of the set value.
     134             : 
     135             :         @return the type class of the set value
     136             :      */
     137     1928274 :     inline TypeClass SAL_CALL getValueTypeClass() const
     138     1928274 :         { return (TypeClass)pType->eTypeClass; }
     139             : 
     140             :     /** Gets the type name of the set value.
     141             : 
     142             :         @return the type name of the set value
     143             :     */
     144             :     inline ::rtl::OUString SAL_CALL getValueTypeName() const;
     145             : 
     146             :     /** Tests if any contains a value.
     147             : 
     148             :         @return true if any has a value, false otherwise
     149             :     */
     150    91171802 :     inline bool SAL_CALL hasValue() const
     151    91171802 :         { return (typelib_TypeClass_VOID != pType->eTypeClass); }
     152             : 
     153             :     /** Gets a pointer to the set value.
     154             : 
     155             :         @return a pointer to the set value
     156             :     */
     157     3917834 :     inline const void * SAL_CALL getValue() const
     158     3917834 :         { return pData; }
     159             : 
     160             :     /** Provides a value of specified type, so you can easily write e.g.
     161             :         <pre>
     162             :         sal_Int32 myVal = myAny.get<sal_Int32>();
     163             :         </pre>
     164             :         Widening conversion without data loss is taken into account.
     165             :         Throws a com::sun::star::uno::RuntimeException if the specified type
     166             :         cannot be provided.
     167             : 
     168             :         @return value of specified type
     169             :         @exception com::sun::star::uno::RuntimeException
     170             :                    in case the specified type cannot be provided
     171             :     */
     172             :     template <typename T>
     173             :     inline T get() const;
     174             : 
     175             :     /** Sets a value. If the any already contains a value, that value will be destructed
     176             :         and its memory freed.
     177             : 
     178             :         @param pData_ pointer to value
     179             :         @param rType type of value
     180             :     */
     181             :     inline void SAL_CALL setValue( const void * pData_, const Type & rType );
     182             :     /** Sets a value. If the any already contains a value, that value will be destructed
     183             :         and its memory freed.
     184             : 
     185             :         @param pData_ pointer to value
     186             :         @param pType type of value
     187             :     */
     188             :     inline void SAL_CALL setValue( const void * pData_, typelib_TypeDescriptionReference * pType );
     189             :     /** Sets a value. If the any already contains a value, that value will be destructed
     190             :         and its memory freed.
     191             : 
     192             :         @param pData_ pointer to value
     193             :         @param pTypeDescr type description of value
     194             :     */
     195             :     inline void SAL_CALL setValue( const void * pData_, typelib_TypeDescription * pTypeDescr );
     196             : 
     197             :     /** Clears this any. If the any already contains a value, that value will be destructed
     198             :         and its memory freed. After this has been called, the any does not contain a value.
     199             :     */
     200             :     inline void SAL_CALL clear();
     201             : 
     202             :     /** Tests whether this any is extractable to a value of given type.
     203             :         Widening conversion without data loss is taken into account.
     204             : 
     205             :         @param rType destination type
     206             :         @return true if this any is extractable to value of given type (e.g. using >>= operator)
     207             :     */
     208             :     inline bool SAL_CALL isExtractableTo( const Type & rType ) const;
     209             : 
     210             :     /** Tests whether this any can provide a value of specified type.
     211             :         Widening conversion without data loss is taken into account.
     212             : 
     213             :         @return true if this any can provide a value of specified type
     214             :         (e.g. using >>= operator)
     215             :     */
     216             :     template <typename T>
     217             :     inline bool has() const;
     218             : 
     219             :     /** Equality operator: compares two anys.
     220             :         The values need not be of equal type, e.g. a short integer is compared to a long integer.
     221             : 
     222             :         @param rAny another any (right side)
     223             :         @return true if both any contains equal values
     224             :     */
     225             :     inline bool SAL_CALL operator == ( const Any & rAny ) const;
     226             :     /** Unequality operator: compares two anys.
     227             :         The values need not be of equal type, e.g. a short integer is compared to a long integer.
     228             : 
     229             :         @param rAny another any (right side)
     230             :         @return true if both any contains unequal values
     231             :     */
     232             :     inline bool SAL_CALL operator != ( const Any & rAny ) const;
     233             : 
     234             : private:
     235             :     /// @cond INTERNAL
     236             :     // Forbid use with ambiguous type (sal_Unicode, sal_uInt16):
     237             :     explicit Any(sal_uInt16) SAL_DELETED_FUNCTION;
     238             :     /// @endcond
     239             : };
     240             : 
     241             : /// @cond INTERNAL
     242             : // Forbid use with ambiguous type (sal_Unicode, sal_uInt16):
     243             : template<> sal_uInt16 Any::get<sal_uInt16>() const SAL_DELETED_FUNCTION;
     244             : template<> bool Any::has<sal_uInt16>() const SAL_DELETED_FUNCTION;
     245             : /// @endcond
     246             : 
     247             : /** Template function to generically construct an any from a C++ value.
     248             : 
     249             :     @tparam C value type
     250             :     @param value a value
     251             :     @return an any
     252             : */
     253             : template< class C >
     254             : inline Any SAL_CALL makeAny( const C & value );
     255             : 
     256             : // additionally specialized for C++ bool
     257             : template<>
     258             : inline Any SAL_CALL makeAny( bool const & value );
     259             : 
     260             : class BaseReference;
     261             : class Type;
     262             : 
     263             : /** Template binary <<= operator to set the value of an any.
     264             : 
     265             :     @tparam C value type
     266             :     @param rAny destination any (left side)
     267             :     @param value source value (right side)
     268             : */
     269             : template< class C >
     270             : inline void SAL_CALL operator <<= ( Any & rAny, const C & value );
     271             : 
     272             : // additionally for C++ bool:
     273             : template<>
     274             : inline void SAL_CALL operator <<= ( Any & rAny, bool const & value );
     275             : 
     276             : /** Template binary >>= operator to assign a value from an any.
     277             :     If the any does not contain a value that can be assigned without data loss, then this
     278             :     operation will fail returning false.
     279             : 
     280             :     @tparam C value type
     281             :     @param rAny source any (left side)
     282             :     @param value destination value (right side)
     283             :     @return true if assignment was possible without data loss
     284             : */
     285             : template< class C >
     286             : inline bool SAL_CALL operator >>= ( const Any & rAny, C & value );
     287             : 
     288             : /** Template equality operator: compares set value of left side any to right side value.
     289             :     The values need not be of equal type, e.g. a short integer is compared to a long integer.
     290             :     This operator can be implemented as template member function, if all supported compilers
     291             :     can cope with template member functions.
     292             : 
     293             :     @tparam C value type
     294             :     @param rAny another any (left side)
     295             :     @param value a value (right side)
     296             :     @return true if values are equal, false otherwise
     297             : */
     298             : template< class C >
     299             : inline bool SAL_CALL operator == ( const Any & rAny, const C & value );
     300             : /** Template unequality operator: compares set value of left side any to right side value.
     301             :     The values need not be of equal type, e.g. a short integer is compared to a long integer.
     302             :     This operator can be implemented as template member function, if all supported compilers
     303             :     can cope with template member functions.
     304             : 
     305             :     @tparam C value type
     306             :     @param rAny another any (left side)
     307             :     @param value a value (right side)
     308             :     @return true if values are unequal, false otherwise
     309             : */
     310             : template< class C >
     311             : inline bool SAL_CALL operator != ( const Any & rAny, const C & value );
     312             : 
     313             : // additional specialized >>= and == operators
     314             : // bool
     315             : template<>
     316             : inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Bool & value );
     317             : template<>
     318             : inline bool SAL_CALL operator == ( const Any & rAny, const sal_Bool & value );
     319             : template<>
     320             : inline bool SAL_CALL operator >>= ( Any const & rAny, bool & value );
     321             : template<>
     322             : inline bool SAL_CALL operator == ( Any const & rAny, bool const & value );
     323             : // byte
     324             : template<>
     325             : inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Int8 & value );
     326             : // short
     327             : template<>
     328             : inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Int16 & value );
     329             : template<>
     330             : inline bool SAL_CALL operator >>= ( const Any & rAny, sal_uInt16 & value );
     331             : // long
     332             : template<>
     333             : inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Int32 & value );
     334             : template<>
     335             : inline bool SAL_CALL operator >>= ( const Any & rAny, sal_uInt32 & value );
     336             : // hyper
     337             : template<>
     338             : inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Int64 & value );
     339             : template<>
     340             : inline bool SAL_CALL operator >>= ( const Any & rAny, sal_uInt64 & value );
     341             : // float
     342             : template<>
     343             : inline bool SAL_CALL operator >>= ( const Any & rAny, float & value );
     344             : // double
     345             : template<>
     346             : inline bool SAL_CALL operator >>= ( const Any & rAny, double & value );
     347             : // string
     348             : template<>
     349             : inline bool SAL_CALL operator >>= ( const Any & rAny, ::rtl::OUString & value );
     350             : template<>
     351             : inline bool SAL_CALL operator == ( const Any & rAny, const ::rtl::OUString & value );
     352             : // type
     353             : template<>
     354             : inline bool SAL_CALL operator >>= ( const Any & rAny, Type & value );
     355             : template<>
     356             : inline bool SAL_CALL operator == ( const Any & rAny, const Type & value );
     357             : // any
     358             : template<>
     359             : inline bool SAL_CALL operator >>= ( const Any & rAny, Any & value );
     360             : // interface
     361             : template<>
     362             : inline bool SAL_CALL operator == ( const Any & rAny, const BaseReference & value );
     363             : 
     364             : }
     365             : }
     366             : }
     367             : }
     368             : 
     369             : /** Gets the meta type of IDL type any.
     370             : 
     371             :     There are cases (involving templates) where uses of getCppuType are known to
     372             :     not compile.  Use cppu::UnoType or cppu::getTypeFavourUnsigned instead.
     373             : 
     374             :     The dummy parameter is just a typed pointer for function signature.
     375             : 
     376             :     @return type of IDL type any
     377             : */
     378        4918 : inline const ::com::sun::star::uno::Type & SAL_CALL getCppuType( SAL_UNUSED_PARAMETER const ::com::sun::star::uno::Any * )
     379             : {
     380        4918 :     return ::cppu::UnoType< ::com::sun::star::uno::Any >::get();
     381             : }
     382             : 
     383             : #endif
     384             : 
     385             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10