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