My Project
UDK 3.2.7 C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Any.hxx
Go to the documentation of this file.
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  * OpenOffice.org is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 3
14  * only, as published by the Free Software Foundation.
15  *
16  * OpenOffice.org is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License version 3 for more details
20  * (a copy is included in the LICENSE file that accompanied this code).
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * version 3 along with OpenOffice.org. If not, see
24  * <http://www.openoffice.org/license.html>
25  * for a copy of the LGPLv3 License.
26  *
27  ************************************************************************/
28 #ifndef _COM_SUN_STAR_UNO_ANY_HXX_
29 #define _COM_SUN_STAR_UNO_ANY_HXX_
30 
31 #include <com/sun/star/uno/Any.h>
32 #include <uno/data.h>
34 #include <com/sun/star/uno/XInterface.hpp>
36 #include "cppu/unotype.hxx"
37 
38 namespace com
39 {
40 namespace sun
41 {
42 namespace star
43 {
44 namespace uno
45 {
46 
47 //__________________________________________________________________________________________________
48 inline Any::Any() SAL_THROW(())
49 {
51 }
52 
53 //______________________________________________________________________________
54 template <typename T>
55 inline Any::Any( T const & value )
56 {
58  this, const_cast<T *>(&value),
59  ::cppu::getTypeFavourUnsigned(&value).getTypeLibType(),
61 }
62 //______________________________________________________________________________
63 inline Any::Any( bool value )
64 {
65  sal_Bool b = value;
67  this, &b, ::getCppuBooleanType().getTypeLibType(),
69 }
70 
71 //__________________________________________________________________________________________________
72 inline Any::Any( const Any & rAny ) SAL_THROW(())
73 {
74  ::uno_type_any_construct( this, rAny.pData, rAny.pType, (uno_AcquireFunc)cpp_acquire );
75 }
76 //__________________________________________________________________________________________________
77 inline Any::Any( const void * pData_, const Type & rType ) SAL_THROW(())
78 {
80  this, const_cast< void * >( pData_ ), rType.getTypeLibType(),
82 }
83 //__________________________________________________________________________________________________
84 inline Any::Any( const void * pData_, typelib_TypeDescription * pTypeDescr ) SAL_THROW(())
85 {
87  this, const_cast< void * >( pData_ ), pTypeDescr, (uno_AcquireFunc)cpp_acquire );
88 }
89 //__________________________________________________________________________________________________
90 inline Any::Any( const void * pData_, typelib_TypeDescriptionReference * pType_ ) SAL_THROW(())
91 {
93  this, const_cast< void * >( pData_ ), pType_, (uno_AcquireFunc)cpp_acquire );
94 }
95 //__________________________________________________________________________________________________
96 inline Any::~Any() SAL_THROW(())
97 {
100 }
101 //__________________________________________________________________________________________________
102 inline Any & Any::operator = ( const Any & rAny ) SAL_THROW(())
103 {
104  if (this != &rAny)
105  {
107  this, rAny.pData, rAny.pType,
109  }
110  return *this;
111 }
112 //__________________________________________________________________________________________________
113 inline ::rtl::OUString Any::getValueTypeName() const SAL_THROW(())
114 {
115  return ::rtl::OUString( pType->pTypeName );
116 }
117 //__________________________________________________________________________________________________
118 inline void Any::setValue( const void * pData_, const Type & rType ) SAL_THROW(())
119 {
121  this, const_cast< void * >( pData_ ), rType.getTypeLibType(),
123 }
124 //__________________________________________________________________________________________________
125 inline void Any::setValue( const void * pData_, typelib_TypeDescriptionReference * pType_ ) SAL_THROW(())
126 {
128  this, const_cast< void * >( pData_ ), pType_,
130 }
131 //__________________________________________________________________________________________________
132 inline void Any::setValue( const void * pData_, typelib_TypeDescription * pTypeDescr ) SAL_THROW(())
133 {
135  this, const_cast< void * >( pData_ ), pTypeDescr,
137 }
138 //__________________________________________________________________________________________________
139 inline void Any::clear() SAL_THROW(())
140 {
142  this, (uno_ReleaseFunc)cpp_release );
143 }
144 //__________________________________________________________________________________________________
145 inline sal_Bool Any::isExtractableTo( const Type & rType ) const SAL_THROW(())
146 {
148  rType.getTypeLibType(), pData, pType,
150 }
151 
152 //______________________________________________________________________________
153 template <typename T>
154 inline bool Any::has() const
155 {
156  Type const & rType = ::cppu::getTypeFavourUnsigned(static_cast< T * >(0));
158  rType.getTypeLibType(), pData, pType,
161 }
162 #if ! defined(__SUNPRO_CC)
163 // not impl: forbid use with ambiguous type (sal_Unicode, sal_uInt16)
164 template <>
165 bool Any::has<sal_uInt16>() const;
166 #endif // ! defined(__SUNPRO_CC)
167 
168 //__________________________________________________________________________________________________
169 inline sal_Bool Any::operator == ( const Any & rAny ) const SAL_THROW(())
170 {
172  pData, pType, rAny.pData, rAny.pType,
174 }
175 //__________________________________________________________________________________________________
176 inline sal_Bool Any::operator != ( const Any & rAny ) const SAL_THROW(())
177 {
178  return (! ::uno_type_equalData(
179  pData, pType, rAny.pData, rAny.pType,
181 }
182 
183 //__________________________________________________________________________________________________
184 template< class C >
185 inline Any SAL_CALL makeAny( const C & value ) SAL_THROW(())
186 {
187  return Any( &value, ::cppu::getTypeFavourUnsigned(&value) );
188 }
189 
190 // additionally specialized for C++ bool
191 //______________________________________________________________________________
192 template<>
193 inline Any SAL_CALL makeAny( bool const & value ) SAL_THROW(())
194 {
195  const sal_Bool b = value;
196  return Any( &b, ::getCppuBooleanType() );
197 }
198 
199 //__________________________________________________________________________________________________
200 template< class C >
201 inline void SAL_CALL operator <<= ( Any & rAny, const C & value ) SAL_THROW(())
202 {
203  const Type & rType = ::cppu::getTypeFavourUnsigned(&value);
205  &rAny, const_cast< C * >( &value ), rType.getTypeLibType(),
207 }
208 
209 // additionally for C++ bool:
210 //______________________________________________________________________________
211 inline void SAL_CALL operator <<= ( Any & rAny, bool const & value )
212  SAL_THROW(())
213 {
214  sal_Bool b = value;
216  &rAny, &b, ::getCppuBooleanType().getTypeLibType(),
218 }
219 
220 //__________________________________________________________________________________________________
221 template< class C >
222 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, C & value ) SAL_THROW(())
223 {
224  const Type & rType = ::cppu::getTypeFavourUnsigned(&value);
226  &value, rType.getTypeLibType(),
227  rAny.pData, rAny.pType,
230 }
231 
232 // bool
233 //__________________________________________________________________________________________________
234 inline sal_Bool SAL_CALL operator >>= ( const ::com::sun::star::uno::Any & rAny, sal_Bool & value ) SAL_THROW(())
235 {
236  if (typelib_TypeClass_BOOLEAN == rAny.pType->eTypeClass)
237  {
238  value = (* reinterpret_cast< const sal_Bool * >( rAny.pData ) != sal_False);
239  return sal_True;
240  }
241  return sal_False;
242 }
243 //__________________________________________________________________________________________________
244 inline sal_Bool SAL_CALL operator == ( const Any & rAny, const sal_Bool & value ) SAL_THROW(())
245 {
246  return (typelib_TypeClass_BOOLEAN == rAny.pType->eTypeClass &&
247  (value != sal_False) == (* reinterpret_cast< const sal_Bool * >( rAny.pData ) != sal_False));
248 }
249 
250 //______________________________________________________________________________
251 template<>
252 inline sal_Bool SAL_CALL operator >>= ( Any const & rAny, bool & value )
253  SAL_THROW(())
254 {
255  if (rAny.pType->eTypeClass == typelib_TypeClass_BOOLEAN)
256  {
257  value = *reinterpret_cast< sal_Bool const * >(
258  rAny.pData ) != sal_False;
259  return true;
260  }
261  return false;
262 }
263 
264 //______________________________________________________________________________
265 template<>
266 inline sal_Bool SAL_CALL operator == ( Any const & rAny, bool const & value )
267  SAL_THROW(())
268 {
269  return (rAny.pType->eTypeClass == typelib_TypeClass_BOOLEAN &&
270  (value ==
271  (*reinterpret_cast< sal_Bool const * >( rAny.pData )
272  != sal_False)));
273 }
274 
275 // byte
276 //__________________________________________________________________________________________________
277 inline sal_Bool SAL_CALL operator >>= ( const ::com::sun::star::uno::Any & rAny, sal_Int8 & value ) SAL_THROW(())
278 {
279  if (typelib_TypeClass_BYTE == rAny.pType->eTypeClass)
280  {
281  value = * reinterpret_cast< const sal_Int8 * >( rAny.pData );
282  return sal_True;
283  }
284  return sal_False;
285 }
286 // short
287 //__________________________________________________________________________________________________
288 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, sal_Int16 & value ) SAL_THROW(())
289 {
290  switch (rAny.pType->eTypeClass)
291  {
293  value = * reinterpret_cast< const sal_Int8 * >( rAny.pData );
294  return sal_True;
297  value = * reinterpret_cast< const sal_Int16 * >( rAny.pData );
298  return sal_True;
299  default:
300  return sal_False;
301  }
302 }
303 //__________________________________________________________________________________________________
304 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, sal_uInt16 & value ) SAL_THROW(())
305 {
306  switch (rAny.pType->eTypeClass)
307  {
309  value = (sal_uInt16)( * reinterpret_cast< const sal_Int8 * >( rAny.pData ) );
310  return sal_True;
313  value = * reinterpret_cast< const sal_uInt16 * >( rAny.pData );
314  return sal_True;
315  default:
316  return sal_False;
317  }
318 }
319 // long
320 //__________________________________________________________________________________________________
321 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, sal_Int32 & value ) SAL_THROW(())
322 {
323  switch (rAny.pType->eTypeClass)
324  {
326  value = * reinterpret_cast< const sal_Int8 * >( rAny.pData );
327  return sal_True;
329  value = * reinterpret_cast< const sal_Int16 * >( rAny.pData );
330  return sal_True;
332  value = * reinterpret_cast< const sal_uInt16 * >( rAny.pData );
333  return sal_True;
336  value = * reinterpret_cast< const sal_Int32 * >( rAny.pData );
337  return sal_True;
338  default:
339  return sal_False;
340  }
341 }
342 //__________________________________________________________________________________________________
343 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, sal_uInt32 & value ) SAL_THROW(())
344 {
345  switch (rAny.pType->eTypeClass)
346  {
348  value = (sal_uInt32)( * reinterpret_cast< const sal_Int8 * >( rAny.pData ) );
349  return sal_True;
351  value = (sal_uInt32)( * reinterpret_cast< const sal_Int16 * >( rAny.pData ) );
352  return sal_True;
354  value = * reinterpret_cast< const sal_uInt16 * >( rAny.pData );
355  return sal_True;
358  value = * reinterpret_cast< const sal_uInt32 * >( rAny.pData );
359  return sal_True;
360  default:
361  return sal_False;
362  }
363 }
364 // hyper
365 //__________________________________________________________________________________________________
366 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, sal_Int64 & value ) SAL_THROW(())
367 {
368  switch (rAny.pType->eTypeClass)
369  {
371  value = * reinterpret_cast< const sal_Int8 * >( rAny.pData );
372  return sal_True;
374  value = * reinterpret_cast< const sal_Int16 * >( rAny.pData );
375  return sal_True;
377  value = * reinterpret_cast< const sal_uInt16 * >( rAny.pData );
378  return sal_True;
380  value = * reinterpret_cast< const sal_Int32 * >( rAny.pData );
381  return sal_True;
383  value = * reinterpret_cast< const sal_uInt32 * >( rAny.pData );
384  return sal_True;
387  value = * reinterpret_cast< const sal_Int64 * >( rAny.pData );
388  return sal_True;
389  default:
390  return sal_False;
391  }
392 }
393 //__________________________________________________________________________________________________
394 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, sal_uInt64 & value ) SAL_THROW(())
395 {
396  switch (rAny.pType->eTypeClass)
397  {
399  value = (sal_uInt64)( * reinterpret_cast< const sal_Int8 * >( rAny.pData ) );
400  return sal_True;
402  value = (sal_uInt64)( * reinterpret_cast< const sal_Int16 * >( rAny.pData ) );
403  return sal_True;
405  value = * reinterpret_cast< const sal_uInt16 * >( rAny.pData );
406  return sal_True;
408  value = (sal_uInt64)( * reinterpret_cast< const sal_Int32 * >( rAny.pData ) );
409  return sal_True;
411  value = * reinterpret_cast< const sal_uInt32 * >( rAny.pData );
412  return sal_True;
415  value = * reinterpret_cast< const sal_uInt64 * >( rAny.pData );
416  return sal_True;
417  default:
418  return sal_False;
419  }
420 }
421 // float
422 //__________________________________________________________________________________________________
423 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, float & value ) SAL_THROW(())
424 {
425  switch (rAny.pType->eTypeClass)
426  {
428  value = * reinterpret_cast< const sal_Int8 * >( rAny.pData );
429  return sal_True;
431  value = * reinterpret_cast< const sal_Int16 * >( rAny.pData );
432  return sal_True;
434  value = * reinterpret_cast< const sal_uInt16 * >( rAny.pData );
435  return sal_True;
437  value = * reinterpret_cast< const float * >( rAny.pData );
438  return sal_True;
439  default:
440  return sal_False;
441  }
442 }
443 // double
444 //__________________________________________________________________________________________________
445 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, double & value ) SAL_THROW(())
446 {
447  switch (rAny.pType->eTypeClass)
448  {
450  value = * reinterpret_cast< const sal_Int8 * >( rAny.pData );
451  return sal_True;
453  value = * reinterpret_cast< const sal_Int16 * >( rAny.pData );
454  return sal_True;
456  value = * reinterpret_cast< const sal_uInt16 * >( rAny.pData );
457  return sal_True;
459  value = * reinterpret_cast< const sal_Int32 * >( rAny.pData );
460  return sal_True;
462  value = * reinterpret_cast< const sal_uInt32 * >( rAny.pData );
463  return sal_True;
465  value = * reinterpret_cast< const float * >( rAny.pData );
466  return sal_True;
468  value = * reinterpret_cast< const double * >( rAny.pData );
469  return sal_True;
470  default:
471  return sal_False;
472  }
473 }
474 // string
475 //__________________________________________________________________________________________________
476 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, ::rtl::OUString & value ) SAL_THROW(())
477 {
478  if (typelib_TypeClass_STRING == rAny.pType->eTypeClass)
479  {
480  value = * reinterpret_cast< const ::rtl::OUString * >( rAny.pData );
481  return sal_True;
482  }
483  return sal_False;
484 }
485 //__________________________________________________________________________________________________
486 inline sal_Bool SAL_CALL operator == ( const Any & rAny, const ::rtl::OUString & value ) SAL_THROW(())
487 {
488  return (typelib_TypeClass_STRING == rAny.pType->eTypeClass &&
489  value.equals( * reinterpret_cast< const ::rtl::OUString * >( rAny.pData ) ));
490 }
491 // type
492 //__________________________________________________________________________________________________
493 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, Type & value ) SAL_THROW(())
494 {
495  if (typelib_TypeClass_TYPE == rAny.pType->eTypeClass)
496  {
497  value = * reinterpret_cast< const Type * >( rAny.pData );
498  return sal_True;
499  }
500  return sal_False;
501 }
502 //__________________________________________________________________________________________________
503 inline sal_Bool SAL_CALL operator == ( const Any & rAny, const Type & value ) SAL_THROW(())
504 {
505  return (typelib_TypeClass_TYPE == rAny.pType->eTypeClass &&
506  value.equals( * reinterpret_cast< const Type * >( rAny.pData ) ));
507 }
508 // any
509 //__________________________________________________________________________________________________
510 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, Any & value ) SAL_THROW(())
511 {
512  if (&rAny != &value)
513  {
515  &value, rAny.pData, rAny.pType,
517  }
518  return sal_True;
519 }
520 // interface
521 //__________________________________________________________________________________________________
522 inline sal_Bool SAL_CALL operator == ( const Any & rAny, const BaseReference & value ) SAL_THROW(())
523 {
524  if (typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass)
525  {
526  return reinterpret_cast< const BaseReference * >( rAny.pData )->operator == ( value );
527  }
528  return sal_False;
529 }
530 
531 // operator to compare to an any.
532 //__________________________________________________________________________________________________
533 template< class C >
534 inline sal_Bool SAL_CALL operator == ( const Any & rAny, const C & value ) SAL_THROW(())
535 {
536  const Type & rType = ::cppu::getTypeFavourUnsigned(&value);
538  rAny.pData, rAny.pType,
539  const_cast< C * >( &value ), rType.getTypeLibType(),
541 }
542 // operator to compare to an any. may use specialized operators ==.
543 //__________________________________________________________________________________________________
544 template< class C >
545 inline sal_Bool SAL_CALL operator != ( const Any & rAny, const C & value ) SAL_THROW(())
546 {
547  return (! operator == ( rAny, value ));
548 }
549 
550 #if ! defined(EXCEPTIONS_OFF)
551 extern "C" rtl_uString * SAL_CALL cppu_Any_extraction_failure_msg(
552  uno_Any const * pAny, typelib_TypeDescriptionReference * pType )
554 
555 //______________________________________________________________________________
556 template <typename T>
557 T Any::get() const
558 {
559  T value = T();
560  if (! (*this >>= value)) {
561  throw RuntimeException(
562  ::rtl::OUString(
564  this,
565  ::cppu::getTypeFavourUnsigned(&value).getTypeLibType() ),
566  SAL_NO_ACQUIRE ),
568  }
569  return value;
570 }
571 // not impl: forbid use with ambiguous type (sal_Unicode, sal_uInt16)
572 template <>
573 sal_uInt16 Any::get<sal_uInt16>() const;
574 #endif // ! defined(EXCEPTIONS_OFF)
575 
576 }
577 }
578 }
579 }
580 
581 #endif
582 
583 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */