LibreOffice
LibreOffice 24.2 SDK C/C++ API Reference
Any.h
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 
20 /*
21  * This file is part of LibreOffice published API.
22  */
23 #ifndef INCLUDED_COM_SUN_STAR_UNO_ANY_H
24 #define INCLUDED_COM_SUN_STAR_UNO_ANY_H
25 
26 #include "sal/config.h"
27 
28 #include <cstddef>
29 
30 #include "rtl/ustring.hxx"
31 #include "uno/any2.h"
33 #include "cppu/unotype.hxx"
34 #include "com/sun/star/uno/TypeClass.hdl"
35 #include "rtl/alloc.h"
36 
37 namespace com
38 {
39 namespace sun
40 {
41 namespace star
42 {
43 namespace uno
44 {
45 
46 class Type;
47 template<class interface_type> class Reference;
48 
57 class SAL_WARN_UNUSED SAL_DLLPUBLIC_RTTI Any : public uno_Any
58 {
59 public:
61  // these are here to force memory de/allocation to sal lib.
62  static void * SAL_CALL operator new ( size_t nSize )
63  { return ::rtl_allocateMemory( nSize ); }
64  static void SAL_CALL operator delete ( void * pMem )
65  { ::rtl_freeMemory( pMem ); }
66  static void * SAL_CALL operator new ( size_t, void * pMem )
67  { return pMem; }
68  static void SAL_CALL operator delete ( void *, void * )
69  {}
71 
74  inline Any();
75 
80  template <typename T>
81  explicit inline Any( T const & value );
83  explicit inline Any( bool value );
84 
85 #if defined LIBO_INTERNAL_ONLY
86  template<typename T1, typename T2>
87  explicit inline Any(rtl::OUStringConcat<T1, T2> && value);
88  template<typename T1, typename T2>
89  explicit Any(rtl::OUStringConcat<T1, T2> const &) = delete;
90  template<std::size_t nBufSize> explicit inline Any(rtl::StringNumber<sal_Unicode, nBufSize> && value);
91  template<std::size_t nBufSize> explicit Any(rtl::StringNumber<sal_Unicode, nBufSize> const &) = delete;
92  template <std::size_t N> explicit inline Any(const rtl::OUStringLiteral<N>& value);
93 #endif
94 
99  inline Any( const Any & rAny );
100 
106  inline Any( const void * pData_, const Type & rType );
107 
113  inline Any( const void * pData_, typelib_TypeDescription * pTypeDescr );
114 
120  inline Any( const void * pData_, typelib_TypeDescriptionReference * pType_ );
121 
122 #if defined LIBO_INTERNAL_ONLY
123  Any(bool const *, Type const &) = delete;
124  Any(bool const *, typelib_TypeDescription *) = delete;
125  Any(bool const *, typelib_TypeDescriptionReference *) = delete;
126  Any(sal_Bool const *, Type const &) = delete;
127  Any(sal_Bool const *, typelib_TypeDescription *) = delete;
128  Any(sal_Bool const *, typelib_TypeDescriptionReference *) = delete;
129  Any(std::nullptr_t, Type const & type):
130  Any(static_cast<void *>(nullptr), type) {}
131  Any(std::nullptr_t, typelib_TypeDescription * type):
132  Any(static_cast<void *>(nullptr), type) {}
133  Any(std::nullptr_t, typelib_TypeDescriptionReference * type):
134  Any(static_cast<void *>(nullptr), type) {}
135 #endif
136 
139  inline ~Any();
140 
146  inline Any & SAL_CALL operator = ( const Any & rAny );
147 
148 #if defined LIBO_INTERNAL_ONLY
149  inline Any(Any && other) noexcept;
150  inline Any & operator =(Any && other) noexcept;
151 #endif
152 
157  const Type & SAL_CALL getValueType() const
158  { return * reinterpret_cast< const Type * >( &pType ); }
164  { return pType; }
165 
171  void SAL_CALL getValueTypeDescription( typelib_TypeDescription ** ppTypeDescr ) const
172  { ::typelib_typedescriptionreference_getDescription( ppTypeDescr, pType ); }
173 
178  TypeClass SAL_CALL getValueTypeClass() const
179  { return static_cast<TypeClass>(pType->eTypeClass); }
180 
185  inline ::rtl::OUString SAL_CALL getValueTypeName() const;
186 
191  bool SAL_CALL hasValue() const
192  { return (typelib_TypeClass_VOID != pType->eTypeClass); }
193 
198  const void * SAL_CALL getValue() const
199  { return pData; }
200 
213  template <typename T>
214  inline T get() const;
215 
222  inline void SAL_CALL setValue( const void * pData_, const Type & rType );
229  inline void SAL_CALL setValue( const void * pData_, typelib_TypeDescriptionReference * pType_ );
236  inline void SAL_CALL setValue( const void * pData_, typelib_TypeDescription * pTypeDescr );
237 
238 #if defined LIBO_INTERNAL_ONLY
239  void setValue(bool const *, Type const &) = delete;
240  void setValue(bool const *, typelib_TypeDescriptionReference *) = delete;
241  void setValue(bool const *, typelib_TypeDescription *) = delete;
242  void setValue(sal_Bool const *, Type const &) = delete;
243  void setValue(sal_Bool const *, typelib_TypeDescriptionReference *)
244  = delete;
245  void setValue(sal_Bool const *, typelib_TypeDescription *) = delete;
246  void setValue(std::nullptr_t, Type const & type)
247  { setValue(static_cast<void *>(nullptr), type); }
248  void setValue(std::nullptr_t, typelib_TypeDescriptionReference * type)
249  { setValue(static_cast<void *>(nullptr), type); }
250  void setValue(std::nullptr_t, typelib_TypeDescription * type)
251  { setValue(static_cast<void *>(nullptr), type); }
252 #endif
253 
257  inline void SAL_CALL clear();
258 
265  inline bool SAL_CALL isExtractableTo( const Type & rType ) const;
266 
273  template <typename T>
274  inline bool has() const;
275 
282  inline bool SAL_CALL operator == ( const Any & rAny ) const;
289  inline bool SAL_CALL operator != ( const Any & rAny ) const;
290 
291 #if defined LIBO_INTERNAL_ONLY
292  // Similar to Reference::query/queryThrow, these allow to simplify calling constructors of
293  // Reference taking Any. queryThrow is functionally similar to get(), but doesn't require
294  // to specify the full Reference type explicitly, only the interface type.
295  template<class interface_type> inline Reference<interface_type> query() const;
296  template<class interface_type> inline Reference<interface_type> queryThrow() const;
297 #endif
298 
299 private:
300 #if !defined LIBO_INTERNAL_ONLY
301  // Forbid use with ambiguous type (sal_Unicode, sal_uInt16):
303  explicit Any(sal_uInt16) SAL_DELETED_FUNCTION;
305 #endif
306 };
307 
308 #if !defined LIBO_INTERNAL_ONLY
309 // Forbid use with ambiguous type (sal_Unicode, sal_uInt16):
311 template<> sal_uInt16 Any::get<sal_uInt16>() const SAL_DELETED_FUNCTION;
312 template<> bool Any::has<sal_uInt16>() const SAL_DELETED_FUNCTION;
314 #endif
315 
316 #if !defined LIBO_INTERNAL_ONLY
317 
328 template< class C >
329 inline Any SAL_CALL makeAny( const C & value );
330 
331 template<> inline Any SAL_CALL makeAny(sal_uInt16 const & value);
332 
333 template<> Any SAL_CALL makeAny(Any const &) SAL_DELETED_FUNCTION;
334 #endif
335 
343 template<typename T> inline Any toAny(T const & value);
344 
345 template<> inline Any toAny(Any const & value);
346 
347 #if defined LIBO_INTERNAL_ONLY
348 
366 template<typename T> inline bool fromAny(Any const & any, T * value);
367 
368 template<> inline bool fromAny(Any const & any, Any * value);
369 
370 #endif
371 
372 class BaseReference;
373 
380 template< class C >
381 inline void SAL_CALL operator <<= ( Any & rAny, const C & value );
382 
383 // additionally for C++ bool:
384 template<>
385 inline void SAL_CALL operator <<= ( Any & rAny, bool const & value );
386 
396 template< class C >
397 inline bool SAL_CALL operator >>= ( const Any & rAny, C & value );
398 
409 template< class C >
410 inline bool SAL_CALL operator == ( const Any & rAny, const C & value );
421 template< class C >
422 inline bool SAL_CALL operator != ( const Any & rAny, const C & value );
423 
424 // additional specialized >>= and == operators
425 // bool
426 template<>
427 inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Bool & value );
428 template<>
429 inline bool SAL_CALL operator == ( const Any & rAny, const sal_Bool & value );
430 template<>
431 inline bool SAL_CALL operator >>= ( Any const & rAny, bool & value );
432 template<>
433 inline bool SAL_CALL operator == ( Any const & rAny, bool const & value );
434 // byte
435 template<>
436 inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Int8 & value );
437 // short
438 template<>
439 inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Int16 & value );
440 template<>
441 inline bool SAL_CALL operator >>= ( const Any & rAny, sal_uInt16 & value );
442 // long
443 template<>
444 inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Int32 & value );
445 template<>
446 inline bool SAL_CALL operator >>= ( const Any & rAny, sal_uInt32 & value );
447 // hyper
448 template<>
449 inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Int64 & value );
450 template<>
451 inline bool SAL_CALL operator >>= ( const Any & rAny, sal_uInt64 & value );
452 // float
453 template<>
454 inline bool SAL_CALL operator >>= ( const Any & rAny, float & value );
455 // double
456 template<>
457 inline bool SAL_CALL operator >>= ( const Any & rAny, double & value );
458 // string
459 template<>
460 inline bool SAL_CALL operator >>= ( const Any & rAny, ::rtl::OUString & value );
461 template<>
462 inline bool SAL_CALL operator == ( const Any & rAny, const ::rtl::OUString & value );
463 #if defined LIBO_INTERNAL_ONLY
464 template<std::size_t N>
465 inline bool SAL_CALL operator == (const Any& rAny, const rtl::OUStringLiteral<N>& value);
466 #endif
467 // type
468 template<>
469 inline bool SAL_CALL operator >>= ( const Any & rAny, Type & value );
470 template<>
471 inline bool SAL_CALL operator == ( const Any & rAny, const Type & value );
472 // any
473 #if !defined LIBO_INTERNAL_ONLY
474 template<>
475 inline bool SAL_CALL operator >>= ( const Any & rAny, Any & value );
476 #endif
477 // interface
478 template<>
479 inline bool SAL_CALL operator == ( const Any & rAny, const BaseReference & value );
480 
481 }
482 }
483 }
484 }
485 
498 SAL_DEPRECATED("use cppu::UnoType")
499 inline const ::com::sun::star::uno::Type & SAL_CALL getCppuType( SAL_UNUSED_PARAMETER const ::com::sun::star::uno::Any * )
500 {
501  return ::cppu::UnoType< ::com::sun::star::uno::Any >::get();
502 }
503 
504 #endif
505 
506 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
typelib_TypeDescriptionReference * getValueTypeRef() const
Gets the type of the set value.
Definition: Any.h:163
bool hasValue() const
Tests if any contains a value.
Definition: Any.h:191
signed char sal_Int8
Definition: types.h:43
Any toAny(T const &value)
Wrap a value in an Any, if necessary.
Definition: Any.hxx:245
const ::com::sun::star::uno::Type & getCppuType(SAL_UNUSED_PARAMETER const ::com::sun::star::uno::Any *)
Gets the meta type of IDL type any.
Definition: Any.h:499
bool operator!=(const Any &rAny, const C &value)
Template inequality operator: compares set value of left side any to right side value.
Definition: Any.hxx:675
bool operator==(const Any &rAny, const C &value)
Template equality operator: compares set value of left side any to right side value.
Definition: Any.hxx:664
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:378
struct SAL_DLLPUBLIC_RTTI _typelib_TypeDescriptionReference typelib_TypeDescriptionReference
Holds a weak reference to a type description.
#define SAL_UNUSED_PARAMETER
Annotate unused but required C++ function parameters.
Definition: types.h:568
bool operator>>=(const Any &rAny, C &value)
Template binary >>= operator to assign a value from an any.
Definition: Any.hxx:327
#define SAL_DEPRECATED(message)
Use as follows: SAL_DEPRECATED("Don&#39;t use, it&#39;s evil.") void doit(int nPara);.
Definition: types.h:474
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition: types.h:587
const void * getValue() const
Gets a pointer to the set value.
Definition: Any.h:198
C++ class representing an IDL any.
Definition: Any.h:57
SAL_DLLPUBLIC void * rtl_allocateMemory(sal_Size Bytes) SAL_THROW_EXTERN_C()
Allocate memory.
CPPU_DLLPUBLIC void typelib_typedescriptionreference_getDescription(typelib_TypeDescription **ppRet, typelib_TypeDescriptionReference *pRef) SAL_THROW_EXTERN_C()
Retrieves the type description for a given reference.
Definition: types.h:359
unsigned char sal_Bool
Definition: types.h:38
type class of void
Definition: typeclass.h:32
void operator<<=(Any &rAny, const C &value)
Template binary <<= operator to set the value of an any.
Definition: Any.hxx:277
struct SAL_DLLPUBLIC_RTTI _typelib_TypeDescription typelib_TypeDescription
Full type description of a type.
This String class provides base functionality for C++ like Unicode character array handling...
Definition: ustring.hxx:170
void getValueTypeDescription(typelib_TypeDescription **ppTypeDescr) const
Gets the type description of the set value.
Definition: Any.h:171
struct SAL_DLLPUBLIC_RTTI _uno_Any uno_Any
This is the binary specification of a UNO any.
SAL_DLLPUBLIC void rtl_freeMemory(void *Ptr) SAL_THROW_EXTERN_C()
Free memory.
const Type & getValueType() const
Gets the type of the set value.
Definition: Any.h:157
C++ class representing an IDL meta type.
Definition: Type.h:58
TypeClass getValueTypeClass() const
Gets the type class of the set value.
Definition: Any.h:178
Any makeAny(const C &value)
Template function to generically construct an any from a C++ value.
Definition: Any.hxx:236