1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 #ifndef _COM_SUN_STAR_UNO_ANY_HXX_ 24 #define _COM_SUN_STAR_UNO_ANY_HXX_ 25 26 #include <com/sun/star/uno/Any.h> 27 #include <uno/data.h> 28 #include <com/sun/star/uno/Type.hxx> 29 #include <com/sun/star/uno/XInterface.hpp> 30 #include <com/sun/star/uno/genfunc.hxx> 31 #include "cppu/unotype.hxx" 32 33 namespace com 34 { 35 namespace sun 36 { 37 namespace star 38 { 39 namespace uno 40 { 41 42 //__________________________________________________________________________________________________ 43 inline Any::Any() SAL_THROW( () ) 44 { 45 ::uno_any_construct( this, 0, 0, (uno_AcquireFunc)cpp_acquire ); 46 } 47 48 //______________________________________________________________________________ 49 template <typename T> 50 inline Any::Any( T const & value ) 51 { 52 ::uno_type_any_construct( 53 this, const_cast<T *>(&value), 54 ::cppu::getTypeFavourUnsigned(&value).getTypeLibType(), 55 (uno_AcquireFunc) cpp_acquire ); 56 } 57 //______________________________________________________________________________ 58 inline Any::Any( bool value ) 59 { 60 sal_Bool b = value; 61 ::uno_type_any_construct( 62 this, &b, ::getCppuBooleanType().getTypeLibType(), 63 (uno_AcquireFunc) cpp_acquire ); 64 } 65 66 //__________________________________________________________________________________________________ 67 inline Any::Any( const Any & rAny ) SAL_THROW( () ) 68 { 69 ::uno_type_any_construct( this, rAny.pData, rAny.pType, (uno_AcquireFunc)cpp_acquire ); 70 } 71 //__________________________________________________________________________________________________ 72 inline Any::Any( const void * pData_, const Type & rType ) SAL_THROW( () ) 73 { 74 ::uno_type_any_construct( 75 this, const_cast< void * >( pData_ ), rType.getTypeLibType(), 76 (uno_AcquireFunc)cpp_acquire ); 77 } 78 //__________________________________________________________________________________________________ 79 inline Any::Any( const void * pData_, typelib_TypeDescription * pTypeDescr ) SAL_THROW( () ) 80 { 81 ::uno_any_construct( 82 this, const_cast< void * >( pData_ ), pTypeDescr, (uno_AcquireFunc)cpp_acquire ); 83 } 84 //__________________________________________________________________________________________________ 85 inline Any::Any( const void * pData_, typelib_TypeDescriptionReference * pType_ ) SAL_THROW( () ) 86 { 87 ::uno_type_any_construct( 88 this, const_cast< void * >( pData_ ), pType_, (uno_AcquireFunc)cpp_acquire ); 89 } 90 //__________________________________________________________________________________________________ 91 inline Any::~Any() SAL_THROW( () ) 92 { 93 ::uno_any_destruct( 94 this, (uno_ReleaseFunc)cpp_release ); 95 } 96 //__________________________________________________________________________________________________ 97 inline Any & Any::operator = ( const Any & rAny ) SAL_THROW( () ) 98 { 99 if (this != &rAny) 100 { 101 ::uno_type_any_assign( 102 this, rAny.pData, rAny.pType, 103 (uno_AcquireFunc)cpp_acquire, (uno_ReleaseFunc)cpp_release ); 104 } 105 return *this; 106 } 107 //__________________________________________________________________________________________________ 108 inline ::rtl::OUString Any::getValueTypeName() const SAL_THROW( () ) 109 { 110 return ::rtl::OUString( pType->pTypeName ); 111 } 112 //__________________________________________________________________________________________________ 113 inline void Any::setValue( const void * pData_, const Type & rType ) SAL_THROW( () ) 114 { 115 ::uno_type_any_assign( 116 this, const_cast< void * >( pData_ ), rType.getTypeLibType(), 117 (uno_AcquireFunc)cpp_acquire, (uno_ReleaseFunc)cpp_release ); 118 } 119 //__________________________________________________________________________________________________ 120 inline void Any::setValue( const void * pData_, typelib_TypeDescriptionReference * pType_ ) SAL_THROW( () ) 121 { 122 ::uno_type_any_assign( 123 this, const_cast< void * >( pData_ ), pType_, 124 (uno_AcquireFunc)cpp_acquire, (uno_ReleaseFunc)cpp_release ); 125 } 126 //__________________________________________________________________________________________________ 127 inline void Any::setValue( const void * pData_, typelib_TypeDescription * pTypeDescr ) SAL_THROW( () ) 128 { 129 ::uno_any_assign( 130 this, const_cast< void * >( pData_ ), pTypeDescr, 131 (uno_AcquireFunc)cpp_acquire, (uno_ReleaseFunc)cpp_release ); 132 } 133 //__________________________________________________________________________________________________ 134 inline void Any::clear() SAL_THROW( () ) 135 { 136 ::uno_any_clear( 137 this, (uno_ReleaseFunc)cpp_release ); 138 } 139 //__________________________________________________________________________________________________ 140 inline sal_Bool Any::isExtractableTo( const Type & rType ) const SAL_THROW( () ) 141 { 142 return ::uno_type_isAssignableFromData( 143 rType.getTypeLibType(), pData, pType, 144 (uno_QueryInterfaceFunc)cpp_queryInterface, (uno_ReleaseFunc)cpp_release ); 145 } 146 147 //______________________________________________________________________________ 148 template <typename T> 149 inline bool Any::has() const 150 { 151 Type const & rType = ::cppu::getTypeFavourUnsigned(static_cast< T * >(0)); 152 return ::uno_type_isAssignableFromData( 153 rType.getTypeLibType(), pData, pType, 154 (uno_QueryInterfaceFunc) cpp_queryInterface, 155 (uno_ReleaseFunc) cpp_release ); 156 } 157 #if ! defined(__SUNPRO_CC) 158 // not impl: forbid use with ambiguous type (sal_Unicode, sal_uInt16) 159 template <> 160 bool Any::has<sal_uInt16>() const; 161 #endif // ! defined(__SUNPRO_CC) 162 163 //__________________________________________________________________________________________________ 164 inline sal_Bool Any::operator == ( const Any & rAny ) const SAL_THROW( () ) 165 { 166 return ::uno_type_equalData( 167 pData, pType, rAny.pData, rAny.pType, 168 (uno_QueryInterfaceFunc)cpp_queryInterface, (uno_ReleaseFunc)cpp_release ); 169 } 170 //__________________________________________________________________________________________________ 171 inline sal_Bool Any::operator != ( const Any & rAny ) const SAL_THROW( () ) 172 { 173 return (! ::uno_type_equalData( 174 pData, pType, rAny.pData, rAny.pType, 175 (uno_QueryInterfaceFunc)cpp_queryInterface, (uno_ReleaseFunc)cpp_release )); 176 } 177 178 //__________________________________________________________________________________________________ 179 template< class C > 180 inline Any SAL_CALL makeAny( const C & value ) SAL_THROW( () ) 181 { 182 return Any( &value, ::cppu::getTypeFavourUnsigned(&value) ); 183 } 184 185 // additionally specialized for C++ bool 186 //______________________________________________________________________________ 187 template<> 188 inline Any SAL_CALL makeAny( bool const & value ) SAL_THROW( () ) 189 { 190 const sal_Bool b = value; 191 return Any( &b, ::getCppuBooleanType() ); 192 } 193 194 //__________________________________________________________________________________________________ 195 template< class C > 196 inline void SAL_CALL operator <<= ( Any & rAny, const C & value ) SAL_THROW( () ) 197 { 198 const Type & rType = ::cppu::getTypeFavourUnsigned(&value); 199 ::uno_type_any_assign( 200 &rAny, const_cast< C * >( &value ), rType.getTypeLibType(), 201 (uno_AcquireFunc)cpp_acquire, (uno_ReleaseFunc)cpp_release ); 202 } 203 204 // additionally for C++ bool: 205 //______________________________________________________________________________ 206 inline void SAL_CALL operator <<= ( Any & rAny, bool const & value ) 207 SAL_THROW( () ) 208 { 209 sal_Bool b = value; 210 ::uno_type_any_assign( 211 &rAny, &b, ::getCppuBooleanType().getTypeLibType(), 212 (uno_AcquireFunc) cpp_acquire, (uno_ReleaseFunc) cpp_release ); 213 } 214 215 //__________________________________________________________________________________________________ 216 template< class C > 217 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, C & value ) SAL_THROW( () ) 218 { 219 const Type & rType = ::cppu::getTypeFavourUnsigned(&value); 220 return ::uno_type_assignData( 221 &value, rType.getTypeLibType(), 222 rAny.pData, rAny.pType, 223 (uno_QueryInterfaceFunc)cpp_queryInterface, 224 (uno_AcquireFunc)cpp_acquire, (uno_ReleaseFunc)cpp_release ); 225 } 226 227 // bool 228 //__________________________________________________________________________________________________ 229 inline sal_Bool SAL_CALL operator >>= ( const ::com::sun::star::uno::Any & rAny, sal_Bool & value ) SAL_THROW( () ) 230 { 231 if (typelib_TypeClass_BOOLEAN == rAny.pType->eTypeClass) 232 { 233 value = (* reinterpret_cast< const sal_Bool * >( rAny.pData ) != sal_False); 234 return sal_True; 235 } 236 return sal_False; 237 } 238 //__________________________________________________________________________________________________ 239 inline sal_Bool SAL_CALL operator == ( const Any & rAny, const sal_Bool & value ) SAL_THROW( () ) 240 { 241 return (typelib_TypeClass_BOOLEAN == rAny.pType->eTypeClass && 242 (value != sal_False) == (* reinterpret_cast< const sal_Bool * >( rAny.pData ) != sal_False)); 243 } 244 245 //______________________________________________________________________________ 246 template<> 247 inline sal_Bool SAL_CALL operator >>= ( Any const & rAny, bool & value ) 248 SAL_THROW( () ) 249 { 250 if (rAny.pType->eTypeClass == typelib_TypeClass_BOOLEAN) 251 { 252 value = *reinterpret_cast< sal_Bool const * >( 253 rAny.pData ) != sal_False; 254 return true; 255 } 256 return false; 257 } 258 259 //______________________________________________________________________________ 260 template<> 261 inline sal_Bool SAL_CALL operator == ( Any const & rAny, bool const & value ) 262 SAL_THROW( () ) 263 { 264 return (rAny.pType->eTypeClass == typelib_TypeClass_BOOLEAN && 265 (value == 266 (*reinterpret_cast< sal_Bool const * >( rAny.pData ) 267 != sal_False))); 268 } 269 270 // byte 271 //__________________________________________________________________________________________________ 272 inline sal_Bool SAL_CALL operator >>= ( const ::com::sun::star::uno::Any & rAny, sal_Int8 & value ) SAL_THROW( () ) 273 { 274 if (typelib_TypeClass_BYTE == rAny.pType->eTypeClass) 275 { 276 value = * reinterpret_cast< const sal_Int8 * >( rAny.pData ); 277 return sal_True; 278 } 279 return sal_False; 280 } 281 // short 282 //__________________________________________________________________________________________________ 283 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, sal_Int16 & value ) SAL_THROW( () ) 284 { 285 switch (rAny.pType->eTypeClass) 286 { 287 case typelib_TypeClass_BYTE: 288 value = * reinterpret_cast< const sal_Int8 * >( rAny.pData ); 289 return sal_True; 290 case typelib_TypeClass_SHORT: 291 case typelib_TypeClass_UNSIGNED_SHORT: 292 value = * reinterpret_cast< const sal_Int16 * >( rAny.pData ); 293 return sal_True; 294 default: 295 return sal_False; 296 } 297 } 298 //__________________________________________________________________________________________________ 299 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, sal_uInt16 & value ) SAL_THROW( () ) 300 { 301 switch (rAny.pType->eTypeClass) 302 { 303 case typelib_TypeClass_BYTE: 304 value = * reinterpret_cast< const sal_Int8 * >( rAny.pData ); 305 return sal_True; 306 case typelib_TypeClass_SHORT: 307 case typelib_TypeClass_UNSIGNED_SHORT: 308 value = * reinterpret_cast< const sal_uInt16 * >( rAny.pData ); 309 return sal_True; 310 default: 311 return sal_False; 312 } 313 } 314 // long 315 //__________________________________________________________________________________________________ 316 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, sal_Int32 & value ) SAL_THROW( () ) 317 { 318 switch (rAny.pType->eTypeClass) 319 { 320 case typelib_TypeClass_BYTE: 321 value = * reinterpret_cast< const sal_Int8 * >( rAny.pData ); 322 return sal_True; 323 case typelib_TypeClass_SHORT: 324 value = * reinterpret_cast< const sal_Int16 * >( rAny.pData ); 325 return sal_True; 326 case typelib_TypeClass_UNSIGNED_SHORT: 327 value = * reinterpret_cast< const sal_uInt16 * >( rAny.pData ); 328 return sal_True; 329 case typelib_TypeClass_LONG: 330 case typelib_TypeClass_UNSIGNED_LONG: 331 value = * reinterpret_cast< const sal_Int32 * >( rAny.pData ); 332 return sal_True; 333 default: 334 return sal_False; 335 } 336 } 337 //__________________________________________________________________________________________________ 338 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, sal_uInt32 & value ) SAL_THROW( () ) 339 { 340 switch (rAny.pType->eTypeClass) 341 { 342 case typelib_TypeClass_BYTE: 343 value = * reinterpret_cast< const sal_Int8 * >( rAny.pData ); 344 return sal_True; 345 case typelib_TypeClass_SHORT: 346 value = * reinterpret_cast< const sal_Int16 * >( rAny.pData ); 347 return sal_True; 348 case typelib_TypeClass_UNSIGNED_SHORT: 349 value = * reinterpret_cast< const sal_uInt16 * >( rAny.pData ); 350 return sal_True; 351 case typelib_TypeClass_LONG: 352 case typelib_TypeClass_UNSIGNED_LONG: 353 value = * reinterpret_cast< const sal_uInt32 * >( rAny.pData ); 354 return sal_True; 355 default: 356 return sal_False; 357 } 358 } 359 // hyper 360 //__________________________________________________________________________________________________ 361 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, sal_Int64 & value ) SAL_THROW( () ) 362 { 363 switch (rAny.pType->eTypeClass) 364 { 365 case typelib_TypeClass_BYTE: 366 value = * reinterpret_cast< const sal_Int8 * >( rAny.pData ); 367 return sal_True; 368 case typelib_TypeClass_SHORT: 369 value = * reinterpret_cast< const sal_Int16 * >( rAny.pData ); 370 return sal_True; 371 case typelib_TypeClass_UNSIGNED_SHORT: 372 value = * reinterpret_cast< const sal_uInt16 * >( rAny.pData ); 373 return sal_True; 374 case typelib_TypeClass_LONG: 375 value = * reinterpret_cast< const sal_Int32 * >( rAny.pData ); 376 return sal_True; 377 case typelib_TypeClass_UNSIGNED_LONG: 378 value = * reinterpret_cast< const sal_uInt32 * >( rAny.pData ); 379 return sal_True; 380 case typelib_TypeClass_HYPER: 381 case typelib_TypeClass_UNSIGNED_HYPER: 382 value = * reinterpret_cast< const sal_Int64 * >( rAny.pData ); 383 return sal_True; 384 385 default: 386 return sal_False; 387 } 388 } 389 //__________________________________________________________________________________________________ 390 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, sal_uInt64 & value ) SAL_THROW( () ) 391 { 392 switch (rAny.pType->eTypeClass) 393 { 394 case typelib_TypeClass_BYTE: 395 value = * reinterpret_cast< const sal_Int8 * >( rAny.pData ); 396 return sal_True; 397 case typelib_TypeClass_SHORT: 398 value = * reinterpret_cast< const sal_Int16 * >( rAny.pData ); 399 return sal_True; 400 case typelib_TypeClass_UNSIGNED_SHORT: 401 value = * reinterpret_cast< const sal_uInt16 * >( rAny.pData ); 402 return sal_True; 403 case typelib_TypeClass_LONG: 404 value = * reinterpret_cast< const sal_Int32 * >( rAny.pData ); 405 return sal_True; 406 case typelib_TypeClass_UNSIGNED_LONG: 407 value = * reinterpret_cast< const sal_uInt32 * >( rAny.pData ); 408 return sal_True; 409 case typelib_TypeClass_HYPER: 410 case typelib_TypeClass_UNSIGNED_HYPER: 411 value = * reinterpret_cast< const sal_uInt64 * >( rAny.pData ); 412 return sal_True; 413 414 default: 415 return sal_False; 416 } 417 } 418 // float 419 //__________________________________________________________________________________________________ 420 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, float & value ) SAL_THROW( () ) 421 { 422 switch (rAny.pType->eTypeClass) 423 { 424 case typelib_TypeClass_BYTE: 425 value = * reinterpret_cast< const sal_Int8 * >( rAny.pData ); 426 return sal_True; 427 case typelib_TypeClass_SHORT: 428 value = * reinterpret_cast< const sal_Int16 * >( rAny.pData ); 429 return sal_True; 430 case typelib_TypeClass_UNSIGNED_SHORT: 431 value = * reinterpret_cast< const sal_uInt16 * >( rAny.pData ); 432 return sal_True; 433 case typelib_TypeClass_FLOAT: 434 value = * reinterpret_cast< const float * >( rAny.pData ); 435 return sal_True; 436 437 default: 438 return sal_False; 439 } 440 } 441 // double 442 //__________________________________________________________________________________________________ 443 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, double & value ) SAL_THROW( () ) 444 { 445 switch (rAny.pType->eTypeClass) 446 { 447 case typelib_TypeClass_BYTE: 448 value = * reinterpret_cast< const sal_Int8 * >( rAny.pData ); 449 return sal_True; 450 case typelib_TypeClass_SHORT: 451 value = * reinterpret_cast< const sal_Int16 * >( rAny.pData ); 452 return sal_True; 453 case typelib_TypeClass_UNSIGNED_SHORT: 454 value = * reinterpret_cast< const sal_uInt16 * >( rAny.pData ); 455 return sal_True; 456 case typelib_TypeClass_LONG: 457 value = * reinterpret_cast< const sal_Int32 * >( rAny.pData ); 458 return sal_True; 459 case typelib_TypeClass_UNSIGNED_LONG: 460 value = * reinterpret_cast< const sal_uInt32 * >( rAny.pData ); 461 return sal_True; 462 case typelib_TypeClass_FLOAT: 463 value = * reinterpret_cast< const float * >( rAny.pData ); 464 return sal_True; 465 case typelib_TypeClass_DOUBLE: 466 value = * reinterpret_cast< const double * >( rAny.pData ); 467 return sal_True; 468 469 default: 470 return sal_False; 471 } 472 } 473 // string 474 //__________________________________________________________________________________________________ 475 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, ::rtl::OUString & value ) SAL_THROW( () ) 476 { 477 if (typelib_TypeClass_STRING == rAny.pType->eTypeClass) 478 { 479 value = * reinterpret_cast< const ::rtl::OUString * >( rAny.pData ); 480 return sal_True; 481 } 482 return sal_False; 483 } 484 //__________________________________________________________________________________________________ 485 inline sal_Bool SAL_CALL operator == ( const Any & rAny, const ::rtl::OUString & value ) SAL_THROW( () ) 486 { 487 return (typelib_TypeClass_STRING == rAny.pType->eTypeClass && 488 value.equals( * reinterpret_cast< const ::rtl::OUString * >( rAny.pData ) )); 489 } 490 // type 491 //__________________________________________________________________________________________________ 492 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, Type & value ) SAL_THROW( () ) 493 { 494 if (typelib_TypeClass_TYPE == rAny.pType->eTypeClass) 495 { 496 value = * reinterpret_cast< const Type * >( rAny.pData ); 497 return sal_True; 498 } 499 return sal_False; 500 } 501 //__________________________________________________________________________________________________ 502 inline sal_Bool SAL_CALL operator == ( const Any & rAny, const Type & value ) SAL_THROW( () ) 503 { 504 return (typelib_TypeClass_TYPE == rAny.pType->eTypeClass && 505 value.equals( * reinterpret_cast< const Type * >( rAny.pData ) )); 506 } 507 // any 508 //__________________________________________________________________________________________________ 509 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, Any & value ) SAL_THROW( () ) 510 { 511 if (&rAny != &value) 512 { 513 ::uno_type_any_assign( 514 &value, rAny.pData, rAny.pType, 515 (uno_AcquireFunc)cpp_acquire, (uno_ReleaseFunc)cpp_release ); 516 } 517 return sal_True; 518 } 519 // interface 520 //__________________________________________________________________________________________________ 521 inline sal_Bool SAL_CALL operator == ( const Any & rAny, const BaseReference & value ) SAL_THROW( () ) 522 { 523 if (typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass) 524 { 525 return reinterpret_cast< const BaseReference * >( rAny.pData )->operator == ( value ); 526 } 527 return sal_False; 528 } 529 530 // operator to compare to an any. 531 //__________________________________________________________________________________________________ 532 template< class C > 533 inline sal_Bool SAL_CALL operator == ( const Any & rAny, const C & value ) SAL_THROW( () ) 534 { 535 const Type & rType = ::cppu::getTypeFavourUnsigned(&value); 536 return ::uno_type_equalData( 537 rAny.pData, rAny.pType, 538 const_cast< C * >( &value ), rType.getTypeLibType(), 539 (uno_QueryInterfaceFunc)cpp_queryInterface, (uno_ReleaseFunc)cpp_release ); 540 } 541 // operator to compare to an any. may use specialized operators ==. 542 //__________________________________________________________________________________________________ 543 template< class C > 544 inline sal_Bool SAL_CALL operator != ( const Any & rAny, const C & value ) SAL_THROW( () ) 545 { 546 return (! operator == ( rAny, value )); 547 } 548 549 #if ! defined(EXCEPTIONS_OFF) 550 extern "C" rtl_uString * SAL_CALL cppu_Any_extraction_failure_msg( 551 uno_Any const * pAny, typelib_TypeDescriptionReference * pType ) 552 SAL_THROW_EXTERN_C(); 553 554 //______________________________________________________________________________ 555 template <typename T> 556 T Any::get() const 557 { 558 T value = T(); 559 if (! (*this >>= value)) { 560 throw RuntimeException( 561 ::rtl::OUString( 562 cppu_Any_extraction_failure_msg( 563 this, 564 ::cppu::getTypeFavourUnsigned(&value).getTypeLibType() ), 565 SAL_NO_ACQUIRE ), 566 Reference<XInterface>() ); 567 } 568 return value; 569 } 570 // not impl: forbid use with ambiguous type (sal_Unicode, sal_uInt16) 571 template <> 572 sal_uInt16 Any::get<sal_uInt16>() const; 573 #endif // ! defined(EXCEPTIONS_OFF) 574 575 } 576 } 577 } 578 } 579 580 #endif 581