LibreOffice
LibreOffice 4.1 SDK 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  * 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 _COM_SUN_STAR_UNO_ANY_HXX_
20 #define _COM_SUN_STAR_UNO_ANY_HXX_
21 
22 #include <com/sun/star/uno/Any.h>
23 #include <uno/data.h>
25 #include <com/sun/star/uno/XInterface.hpp>
27 #include "cppu/unotype.hxx"
28 
29 namespace com
30 {
31 namespace sun
32 {
33 namespace star
34 {
35 namespace uno
36 {
37 
38 //__________________________________________________________________________________________________
39 inline Any::Any() SAL_THROW(())
40 {
42 }
43 
44 //______________________________________________________________________________
45 template <typename T>
46 inline Any::Any( T const & value )
47 {
49  this, const_cast<T *>(&value),
50  ::cppu::getTypeFavourUnsigned(&value).getTypeLibType(),
52 }
53 //______________________________________________________________________________
54 inline Any::Any( bool value )
55 {
56  sal_Bool b = value;
58  this, &b, ::getCppuBooleanType().getTypeLibType(),
60 }
61 
62 //__________________________________________________________________________________________________
63 inline Any::Any( const Any & rAny ) SAL_THROW(())
64 {
65  ::uno_type_any_construct( this, rAny.pData, rAny.pType, (uno_AcquireFunc)cpp_acquire );
66 }
67 //__________________________________________________________________________________________________
68 inline Any::Any( const void * pData_, const Type & rType ) SAL_THROW(())
69 {
71  this, const_cast< void * >( pData_ ), rType.getTypeLibType(),
73 }
74 //__________________________________________________________________________________________________
75 inline Any::Any( const void * pData_, typelib_TypeDescription * pTypeDescr ) SAL_THROW(())
76 {
78  this, const_cast< void * >( pData_ ), pTypeDescr, (uno_AcquireFunc)cpp_acquire );
79 }
80 //__________________________________________________________________________________________________
81 inline Any::Any( const void * pData_, typelib_TypeDescriptionReference * pType_ ) SAL_THROW(())
82 {
84  this, const_cast< void * >( pData_ ), pType_, (uno_AcquireFunc)cpp_acquire );
85 }
86 //__________________________________________________________________________________________________
87 inline Any::~Any() SAL_THROW(())
88 {
91 }
92 //__________________________________________________________________________________________________
93 inline Any & Any::operator = ( const Any & rAny ) SAL_THROW(())
94 {
95  if (this != &rAny)
96  {
98  this, rAny.pData, rAny.pType,
100  }
101  return *this;
102 }
103 //__________________________________________________________________________________________________
104 inline ::rtl::OUString Any::getValueTypeName() const SAL_THROW(())
105 {
106  return ::rtl::OUString( pType->pTypeName );
107 }
108 //__________________________________________________________________________________________________
109 inline void Any::setValue( const void * pData_, const Type & rType ) SAL_THROW(())
110 {
112  this, const_cast< void * >( pData_ ), rType.getTypeLibType(),
114 }
115 //__________________________________________________________________________________________________
116 inline void Any::setValue( const void * pData_, typelib_TypeDescriptionReference * pType_ ) SAL_THROW(())
117 {
119  this, const_cast< void * >( pData_ ), pType_,
121 }
122 //__________________________________________________________________________________________________
123 inline void Any::setValue( const void * pData_, typelib_TypeDescription * pTypeDescr ) SAL_THROW(())
124 {
126  this, const_cast< void * >( pData_ ), pTypeDescr,
128 }
129 //__________________________________________________________________________________________________
130 inline void Any::clear() SAL_THROW(())
131 {
133  this, (uno_ReleaseFunc)cpp_release );
134 }
135 //__________________________________________________________________________________________________
136 inline sal_Bool Any::isExtractableTo( const Type & rType ) const SAL_THROW(())
137 {
139  rType.getTypeLibType(), pData, pType,
141 }
142 
143 //______________________________________________________________________________
144 template <typename T>
145 inline bool Any::has() const
146 {
147  Type const & rType = ::cppu::getTypeFavourUnsigned(static_cast< T * >(0));
149  rType.getTypeLibType(), pData, pType,
152 }
153 #if ! defined(__SUNPRO_CC)
154 // not impl: forbid use with ambiguous type (sal_Unicode, sal_uInt16)
155 template <>
156 bool Any::has<sal_uInt16>() const;
157 #endif // ! defined(__SUNPRO_CC)
158 
159 //__________________________________________________________________________________________________
160 inline sal_Bool Any::operator == ( const Any & rAny ) const SAL_THROW(())
161 {
163  pData, pType, rAny.pData, rAny.pType,
165 }
166 //__________________________________________________________________________________________________
167 inline sal_Bool Any::operator != ( const Any & rAny ) const SAL_THROW(())
168 {
169  return (! ::uno_type_equalData(
170  pData, pType, rAny.pData, rAny.pType,
172 }
173 
174 //__________________________________________________________________________________________________
175 template< class C >
176 inline Any SAL_CALL makeAny( const C & value ) SAL_THROW(())
177 {
178  return Any( &value, ::cppu::getTypeFavourUnsigned(&value) );
179 }
180 
181 // additionally specialized for C++ bool
182 //______________________________________________________________________________
183 template<>
184 inline Any SAL_CALL makeAny( bool const & value ) SAL_THROW(())
185 {
186  const sal_Bool b = value;
187  return Any( &b, ::getCppuBooleanType() );
188 }
189 
190 //__________________________________________________________________________________________________
191 #ifdef RTL_FAST_STRING
192 template< class C1, class C2 >
193 inline Any SAL_CALL makeAny( const rtl::OUStringConcat< C1, C2 >& value ) SAL_THROW(())
194 {
195  const rtl::OUString str( value );
196  return Any( &str, ::cppu::getTypeFavourUnsigned(&str) );
197 }
198 #endif
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 #ifdef RTL_FAST_STRING
222 template< class C1, class C2 >
223 inline void SAL_CALL operator <<= ( Any & rAny, const rtl::OUStringConcat< C1, C2 >& value )
224  SAL_THROW(())
225 {
226  const rtl::OUString str( value );
227  const Type & rType = ::cppu::getTypeFavourUnsigned(&str);
229  &rAny, const_cast< rtl::OUString * >( &str ), rType.getTypeLibType(),
231 }
232 #endif
233 //__________________________________________________________________________________________________
234 template< class C >
235 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, C & value ) SAL_THROW(())
236 {
237  const Type & rType = ::cppu::getTypeFavourUnsigned(&value);
239  &value, rType.getTypeLibType(),
240  rAny.pData, rAny.pType,
243 }
244 
245 // bool
246 //__________________________________________________________________________________________________
247 inline sal_Bool SAL_CALL operator >>= ( const ::com::sun::star::uno::Any & rAny, sal_Bool & value ) SAL_THROW(())
248 {
249  if (typelib_TypeClass_BOOLEAN == rAny.pType->eTypeClass)
250  {
251  value = (* reinterpret_cast< const sal_Bool * >( rAny.pData ) != sal_False);
252  return sal_True;
253  }
254  return sal_False;
255 }
256 //__________________________________________________________________________________________________
257 inline sal_Bool SAL_CALL operator == ( const Any & rAny, const sal_Bool & value ) SAL_THROW(())
258 {
259  return (typelib_TypeClass_BOOLEAN == rAny.pType->eTypeClass &&
260  (value != sal_False) == (* reinterpret_cast< const sal_Bool * >( rAny.pData ) != sal_False));
261 }
262 
263 //______________________________________________________________________________
264 template<>
265 inline sal_Bool SAL_CALL operator >>= ( Any const & rAny, bool & value )
266  SAL_THROW(())
267 {
268  if (rAny.pType->eTypeClass == typelib_TypeClass_BOOLEAN)
269  {
270  value = *reinterpret_cast< sal_Bool const * >(
271  rAny.pData ) != sal_False;
272  return true;
273  }
274  return false;
275 }
276 
277 //______________________________________________________________________________
278 template<>
279 inline sal_Bool SAL_CALL operator == ( Any const & rAny, bool const & value )
280  SAL_THROW(())
281 {
282  return (rAny.pType->eTypeClass == typelib_TypeClass_BOOLEAN &&
283  (value ==
284  (*reinterpret_cast< sal_Bool const * >( rAny.pData )
285  != sal_False)));
286 }
287 
288 // byte
289 //__________________________________________________________________________________________________
290 inline sal_Bool SAL_CALL operator >>= ( const ::com::sun::star::uno::Any & rAny, sal_Int8 & value ) SAL_THROW(())
291 {
292  if (typelib_TypeClass_BYTE == rAny.pType->eTypeClass)
293  {
294  value = * reinterpret_cast< const sal_Int8 * >( rAny.pData );
295  return sal_True;
296  }
297  return sal_False;
298 }
299 // short
300 //__________________________________________________________________________________________________
301 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, sal_Int16 & value ) SAL_THROW(())
302 {
303  switch (rAny.pType->eTypeClass)
304  {
306  value = * reinterpret_cast< const sal_Int8 * >( rAny.pData );
307  return sal_True;
310  value = * reinterpret_cast< const sal_Int16 * >( rAny.pData );
311  return sal_True;
312  default:
313  return sal_False;
314  }
315 }
316 //__________________________________________________________________________________________________
317 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, sal_uInt16 & value ) SAL_THROW(())
318 {
319  switch (rAny.pType->eTypeClass)
320  {
322  value = (sal_uInt16)( * reinterpret_cast< const sal_Int8 * >( rAny.pData ) );
323  return sal_True;
326  value = * reinterpret_cast< const sal_uInt16 * >( rAny.pData );
327  return sal_True;
328  default:
329  return sal_False;
330  }
331 }
332 // long
333 //__________________________________________________________________________________________________
334 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, sal_Int32 & value ) SAL_THROW(())
335 {
336  switch (rAny.pType->eTypeClass)
337  {
339  value = * reinterpret_cast< const sal_Int8 * >( rAny.pData );
340  return sal_True;
342  value = * reinterpret_cast< const sal_Int16 * >( rAny.pData );
343  return sal_True;
345  value = * reinterpret_cast< const sal_uInt16 * >( rAny.pData );
346  return sal_True;
349  value = * reinterpret_cast< const sal_Int32 * >( rAny.pData );
350  return sal_True;
351  default:
352  return sal_False;
353  }
354 }
355 //__________________________________________________________________________________________________
356 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, sal_uInt32 & value ) SAL_THROW(())
357 {
358  switch (rAny.pType->eTypeClass)
359  {
361  value = (sal_uInt32)( * reinterpret_cast< const sal_Int8 * >( rAny.pData ) );
362  return sal_True;
364  value = (sal_uInt32)( * reinterpret_cast< const sal_Int16 * >( rAny.pData ) );
365  return sal_True;
367  value = * reinterpret_cast< const sal_uInt16 * >( rAny.pData );
368  return sal_True;
371  value = * reinterpret_cast< const sal_uInt32 * >( rAny.pData );
372  return sal_True;
373  default:
374  return sal_False;
375  }
376 }
377 // hyper
378 //__________________________________________________________________________________________________
379 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, sal_Int64 & value ) SAL_THROW(())
380 {
381  switch (rAny.pType->eTypeClass)
382  {
384  value = * reinterpret_cast< const sal_Int8 * >( rAny.pData );
385  return sal_True;
387  value = * reinterpret_cast< const sal_Int16 * >( rAny.pData );
388  return sal_True;
390  value = * reinterpret_cast< const sal_uInt16 * >( rAny.pData );
391  return sal_True;
393  value = * reinterpret_cast< const sal_Int32 * >( rAny.pData );
394  return sal_True;
396  value = * reinterpret_cast< const sal_uInt32 * >( rAny.pData );
397  return sal_True;
400  value = * reinterpret_cast< const sal_Int64 * >( rAny.pData );
401  return sal_True;
402  default:
403  return sal_False;
404  }
405 }
406 //__________________________________________________________________________________________________
407 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, sal_uInt64 & value ) SAL_THROW(())
408 {
409  switch (rAny.pType->eTypeClass)
410  {
412  value = (sal_uInt64)( * reinterpret_cast< const sal_Int8 * >( rAny.pData ) );
413  return sal_True;
415  value = (sal_uInt64)( * reinterpret_cast< const sal_Int16 * >( rAny.pData ) );
416  return sal_True;
418  value = * reinterpret_cast< const sal_uInt16 * >( rAny.pData );
419  return sal_True;
421  value = (sal_uInt64)( * reinterpret_cast< const sal_Int32 * >( rAny.pData ) );
422  return sal_True;
424  value = * reinterpret_cast< const sal_uInt32 * >( rAny.pData );
425  return sal_True;
428  value = * reinterpret_cast< const sal_uInt64 * >( rAny.pData );
429  return sal_True;
430  default:
431  return sal_False;
432  }
433 }
434 // float
435 //__________________________________________________________________________________________________
436 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, float & value ) SAL_THROW(())
437 {
438  switch (rAny.pType->eTypeClass)
439  {
441  value = * reinterpret_cast< const sal_Int8 * >( rAny.pData );
442  return sal_True;
444  value = * reinterpret_cast< const sal_Int16 * >( rAny.pData );
445  return sal_True;
447  value = * reinterpret_cast< const sal_uInt16 * >( rAny.pData );
448  return sal_True;
450  value = * reinterpret_cast< const float * >( rAny.pData );
451  return sal_True;
452  default:
453  return sal_False;
454  }
455 }
456 // double
457 //__________________________________________________________________________________________________
458 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, double & value ) SAL_THROW(())
459 {
460  switch (rAny.pType->eTypeClass)
461  {
463  value = * reinterpret_cast< const sal_Int8 * >( rAny.pData );
464  return sal_True;
466  value = * reinterpret_cast< const sal_Int16 * >( rAny.pData );
467  return sal_True;
469  value = * reinterpret_cast< const sal_uInt16 * >( rAny.pData );
470  return sal_True;
472  value = * reinterpret_cast< const sal_Int32 * >( rAny.pData );
473  return sal_True;
475  value = * reinterpret_cast< const sal_uInt32 * >( rAny.pData );
476  return sal_True;
478  value = * reinterpret_cast< const float * >( rAny.pData );
479  return sal_True;
481  value = * reinterpret_cast< const double * >( rAny.pData );
482  return sal_True;
483  default:
484  return sal_False;
485  }
486 }
487 // string
488 //__________________________________________________________________________________________________
489 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, ::rtl::OUString & value ) SAL_THROW(())
490 {
491  if (typelib_TypeClass_STRING == rAny.pType->eTypeClass)
492  {
493  value = * reinterpret_cast< const ::rtl::OUString * >( rAny.pData );
494  return sal_True;
495  }
496  return sal_False;
497 }
498 //__________________________________________________________________________________________________
499 inline sal_Bool SAL_CALL operator == ( const Any & rAny, const ::rtl::OUString & value ) SAL_THROW(())
500 {
501  return (typelib_TypeClass_STRING == rAny.pType->eTypeClass &&
502  value.equals( * reinterpret_cast< const ::rtl::OUString * >( rAny.pData ) ));
503 }
504 // type
505 //__________________________________________________________________________________________________
506 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, Type & value ) SAL_THROW(())
507 {
508  if (typelib_TypeClass_TYPE == rAny.pType->eTypeClass)
509  {
510  value = * reinterpret_cast< const Type * >( rAny.pData );
511  return sal_True;
512  }
513  return sal_False;
514 }
515 //__________________________________________________________________________________________________
516 inline sal_Bool SAL_CALL operator == ( const Any & rAny, const Type & value ) SAL_THROW(())
517 {
518  return (typelib_TypeClass_TYPE == rAny.pType->eTypeClass &&
519  value.equals( * reinterpret_cast< const Type * >( rAny.pData ) ));
520 }
521 // any
522 //__________________________________________________________________________________________________
523 inline sal_Bool SAL_CALL operator >>= ( const Any & rAny, Any & value ) SAL_THROW(())
524 {
525  if (&rAny != &value)
526  {
528  &value, rAny.pData, rAny.pType,
530  }
531  return sal_True;
532 }
533 // interface
534 //__________________________________________________________________________________________________
535 inline sal_Bool SAL_CALL operator == ( const Any & rAny, const BaseReference & value ) SAL_THROW(())
536 {
537  if (typelib_TypeClass_INTERFACE == rAny.pType->eTypeClass)
538  {
539  return reinterpret_cast< const BaseReference * >( rAny.pData )->operator == ( value );
540  }
541  return sal_False;
542 }
543 
544 // operator to compare to an any.
545 //__________________________________________________________________________________________________
546 template< class C >
547 inline sal_Bool SAL_CALL operator == ( const Any & rAny, const C & value ) SAL_THROW(())
548 {
549  const Type & rType = ::cppu::getTypeFavourUnsigned(&value);
551  rAny.pData, rAny.pType,
552  const_cast< C * >( &value ), rType.getTypeLibType(),
554 }
555 // operator to compare to an any. may use specialized operators ==.
556 //__________________________________________________________________________________________________
557 template< class C >
558 inline sal_Bool SAL_CALL operator != ( const Any & rAny, const C & value ) SAL_THROW(())
559 {
560  return (! operator == ( rAny, value ));
561 }
562 
563 #if ! defined(EXCEPTIONS_OFF)
564 extern "C" rtl_uString * SAL_CALL cppu_Any_extraction_failure_msg(
565  uno_Any const * pAny, typelib_TypeDescriptionReference * pType )
567 
568 //______________________________________________________________________________
569 template <typename T>
570 T Any::get() const
571 {
572  T value = T();
573  if (! (*this >>= value)) {
574  throw RuntimeException(
575  ::rtl::OUString(
577  this,
578  ::cppu::getTypeFavourUnsigned(&value).getTypeLibType() ),
579  SAL_NO_ACQUIRE ),
581  }
582  return value;
583 }
584 // not impl: forbid use with ambiguous type (sal_Unicode, sal_uInt16)
585 template <>
586 sal_uInt16 Any::get<sal_uInt16>() const;
587 #endif // ! defined(EXCEPTIONS_OFF)
588 
589 }
590 }
591 }
592 }
593 
594 #endif
595 
596 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */