LibreOfficeDev
LibreOfficeDev 26.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 #if defined LIBO_INTERNAL_ONLY && !(defined _MSC_VER && _MSC_VER <= 1929 && defined _MANAGED)
38 #include <type_traits>
39 #endif
40 
41 namespace com
42 {
43 namespace sun
44 {
45 namespace star
46 {
47 namespace uno
48 {
49 
50 class Type;
51 template<class interface_type> class Reference;
52 
61 class SAL_WARN_UNUSED SAL_DLLPUBLIC_RTTI Any : public uno_Any
62 {
63 public:
65  // these are here to force memory de/allocation to sal lib.
66  static void * SAL_CALL operator new ( size_t nSize )
67  { return ::rtl_allocateMemory( nSize ); }
68  static void SAL_CALL operator delete ( void * pMem )
69  { ::rtl_freeMemory( pMem ); }
70  static void * SAL_CALL operator new ( size_t, void * pMem )
71  { return pMem; }
72  static void SAL_CALL operator delete ( void *, void * )
73  {}
75 
78  inline Any();
79 
84  template <typename T>
85 #if defined LIBO_INTERNAL_ONLY && !(defined _MSC_VER && _MSC_VER <= 1929 && defined _MANAGED)
86  // Disallow things like
87  // Reference<XInterface> x(...);
88  // Any a(*x);
89  requires (!std::is_base_of_v<XInterface, T>)
90 #endif
91  explicit inline Any( T const & value );
93  explicit inline Any( bool value );
94 
95 #if defined LIBO_INTERNAL_ONLY
96  template<typename T1, typename T2>
97  explicit inline Any(rtl::OUStringConcat<T1, T2> && value);
98  template<typename T1, typename T2>
99  explicit Any(rtl::OUStringConcat<T1, T2> const &) = delete;
100  template<std::size_t nBufSize> explicit inline Any(rtl::StringNumber<sal_Unicode, nBufSize> && value);
101  template<std::size_t nBufSize> explicit Any(rtl::StringNumber<sal_Unicode, nBufSize> const &) = delete;
102  template <std::size_t N> explicit inline Any(const rtl::OUStringLiteral<N>& value);
103 #endif
104 
109  inline Any( const Any & rAny );
110 
116  inline Any( const void * pData_, const Type & rType );
117 
123  inline Any( const void * pData_, typelib_TypeDescription * pTypeDescr );
124 
130  inline Any( const void * pData_, typelib_TypeDescriptionReference * pType_ );
131 
132 #if defined LIBO_INTERNAL_ONLY
133  Any(bool const *, Type const &) = delete;
134  Any(bool const *, typelib_TypeDescription *) = delete;
135  Any(bool const *, typelib_TypeDescriptionReference *) = delete;
136  Any(sal_Bool const *, Type const &) = delete;
137  Any(sal_Bool const *, typelib_TypeDescription *) = delete;
138  Any(sal_Bool const *, typelib_TypeDescriptionReference *) = delete;
139  Any(std::nullptr_t, Type const & type):
140  Any(static_cast<void *>(nullptr), type) {}
141  Any(std::nullptr_t, typelib_TypeDescription * type):
142  Any(static_cast<void *>(nullptr), type) {}
143  Any(std::nullptr_t, typelib_TypeDescriptionReference * type):
144  Any(static_cast<void *>(nullptr), type) {}
145 #endif
146 
149  inline ~Any();
150 
156  inline Any & SAL_CALL operator = ( const Any & rAny );
157 
158 #if defined LIBO_INTERNAL_ONLY
159 #if !defined(__COVERITY__) // suppress COPY_INSTEAD_OF_MOVE suggestions
160  inline Any(Any && other) noexcept;
161 #endif
162  inline Any & operator =(Any && other) noexcept;
163 #endif
164 
169  const Type & SAL_CALL getValueType() const
170  { return * reinterpret_cast< const Type * >( &pType ); }
176  { return pType; }
177 
183  void SAL_CALL getValueTypeDescription( typelib_TypeDescription ** ppTypeDescr ) const
184  { ::typelib_typedescriptionreference_getDescription( ppTypeDescr, pType ); }
185 
190  TypeClass SAL_CALL getValueTypeClass() const
191  { return static_cast<TypeClass>(pType->eTypeClass); }
192 
197  inline ::rtl::OUString SAL_CALL getValueTypeName() const;
198 
203  bool SAL_CALL hasValue() const
204  { return (typelib_TypeClass_VOID != pType->eTypeClass); }
205 
210  const void * SAL_CALL getValue() const
211  { return pData; }
212 
225  template <typename T>
226  inline T get() const;
227 
234  inline void SAL_CALL setValue( const void * pData_, const Type & rType );
241  inline void SAL_CALL setValue( const void * pData_, typelib_TypeDescriptionReference * pType_ );
248  inline void SAL_CALL setValue( const void * pData_, typelib_TypeDescription * pTypeDescr );
249 
250 #if defined LIBO_INTERNAL_ONLY
251  void setValue(bool const *, Type const &) = delete;
252  void setValue(bool const *, typelib_TypeDescriptionReference *) = delete;
253  void setValue(bool const *, typelib_TypeDescription *) = delete;
254  void setValue(sal_Bool const *, Type const &) = delete;
255  void setValue(sal_Bool const *, typelib_TypeDescriptionReference *)
256  = delete;
257  void setValue(sal_Bool const *, typelib_TypeDescription *) = delete;
258  void setValue(std::nullptr_t, Type const & type)
259  { setValue(static_cast<void *>(nullptr), type); }
260  void setValue(std::nullptr_t, typelib_TypeDescriptionReference * type)
261  { setValue(static_cast<void *>(nullptr), type); }
262  void setValue(std::nullptr_t, typelib_TypeDescription * type)
263  { setValue(static_cast<void *>(nullptr), type); }
264 #endif
265 
269  inline void SAL_CALL clear();
270 
277  inline bool SAL_CALL isExtractableTo( const Type & rType ) const;
278 
285  template <typename T>
286  inline bool has() const;
287 
294  inline bool SAL_CALL operator == ( const Any & rAny ) const;
301  inline bool SAL_CALL operator != ( const Any & rAny ) const;
302 
303 #if defined LIBO_INTERNAL_ONLY
304  // Similar to Reference::query/queryThrow, these allow to simplify calling constructors of
305  // Reference taking Any. queryThrow is functionally similar to get(), but doesn't require
306  // to specify the full Reference type explicitly, only the interface type.
307  template<class interface_type> inline Reference<interface_type> query() const;
308  template<class interface_type> inline Reference<interface_type> queryThrow() const;
309 #endif
310 
311 private:
312 #if !defined LIBO_INTERNAL_ONLY
314  // Forbid use with ambiguous type (sal_Unicode, sal_uInt16):
315  explicit Any(sal_uInt16) SAL_DELETED_FUNCTION;
317 #endif
318 };
319 
320 #if !defined LIBO_INTERNAL_ONLY
322 // Forbid use with ambiguous type (sal_Unicode, sal_uInt16):
323 template<> sal_uInt16 Any::get<sal_uInt16>() const SAL_DELETED_FUNCTION;
324 template<> bool Any::has<sal_uInt16>() const SAL_DELETED_FUNCTION;
326 #endif
327 
328 #if !defined LIBO_INTERNAL_ONLY
340 template< class C >
341 inline Any SAL_CALL makeAny( const C & value );
342 
343 template<> inline Any SAL_CALL makeAny(sal_uInt16 const & value);
344 
345 template<> Any SAL_CALL makeAny(Any const &) SAL_DELETED_FUNCTION;
346 #endif
347 
355 template<typename T> inline Any toAny(T const & value);
356 
357 template<> inline Any toAny(Any const & value);
358 
359 #if defined LIBO_INTERNAL_ONLY
360 
378 template<typename T> inline bool fromAny(Any const & any, T * value);
379 
380 template<> inline bool fromAny(Any const & any, Any * value);
381 
382 #endif
383 
384 class BaseReference;
385 
392 template< class C >
393 inline void SAL_CALL operator <<= ( Any & rAny, const C & value );
394 
395 // additionally for C++ bool:
396 template<>
397 inline void SAL_CALL operator <<= ( Any & rAny, bool const & value );
398 
408 template< class C >
409 inline bool SAL_CALL operator >>= ( const Any & rAny, C & value );
410 
421 template< class C >
422 inline bool SAL_CALL operator == ( const Any & rAny, const C & value );
433 template< class C >
434 inline bool SAL_CALL operator != ( const Any & rAny, const C & value );
435 
436 // additional specialized >>= and == operators
437 // bool
438 template<>
439 inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Bool & value );
440 template<>
441 inline bool SAL_CALL operator == ( const Any & rAny, const sal_Bool & value );
442 template<>
443 inline bool SAL_CALL operator >>= ( Any const & rAny, bool & value );
444 template<>
445 inline bool SAL_CALL operator == ( Any const & rAny, bool const & value );
446 // byte
447 template<>
448 inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Int8 & value );
449 // short
450 template<>
451 inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Int16 & value );
452 template<>
453 inline bool SAL_CALL operator >>= ( const Any & rAny, sal_uInt16 & value );
454 // long
455 template<>
456 inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Int32 & value );
457 template<>
458 inline bool SAL_CALL operator >>= ( const Any & rAny, sal_uInt32 & value );
459 // hyper
460 template<>
461 inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Int64 & value );
462 template<>
463 inline bool SAL_CALL operator >>= ( const Any & rAny, sal_uInt64 & value );
464 // float
465 template<>
466 inline bool SAL_CALL operator >>= ( const Any & rAny, float & value );
467 // double
468 template<>
469 inline bool SAL_CALL operator >>= ( const Any & rAny, double & value );
470 // string
471 template<>
472 inline bool SAL_CALL operator >>= ( const Any & rAny, ::rtl::OUString & value );
473 template<>
474 inline bool SAL_CALL operator == ( const Any & rAny, const ::rtl::OUString & value );
475 #if defined LIBO_INTERNAL_ONLY
476 template<std::size_t N>
477 inline bool SAL_CALL operator == (const Any& rAny, const rtl::OUStringLiteral<N>& value);
478 #endif
479 // type
480 template<>
481 inline bool SAL_CALL operator >>= ( const Any & rAny, Type & value );
482 template<>
483 inline bool SAL_CALL operator == ( const Any & rAny, const Type & value );
484 // any
485 #if !defined LIBO_INTERNAL_ONLY
486 template<>
487 inline bool SAL_CALL operator >>= ( const Any & rAny, Any & value );
488 #endif
489 // interface
490 template<>
491 inline bool SAL_CALL operator == ( const Any & rAny, const BaseReference & value );
492 
493 }
494 }
495 }
496 }
497 
510 SAL_DEPRECATED("use cppu::UnoType")
511 inline const ::com::sun::star::uno::Type & SAL_CALL getCppuType( SAL_UNUSED_PARAMETER const ::com::sun::star::uno::Any * )
512 {
513  return ::cppu::UnoType< ::com::sun::star::uno::Any >::get();
514 }
515 
516 #endif
517 
518 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define SAL_DEPRECATED(message)
Use as follows: SAL_DEPRECATED("Don't use, it's evil.") void doit(int nPara);.
Definition: types.h:492
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:396
#define SAL_UNUSED_PARAMETER
Annotate unused but required C++ function parameters.
Definition: types.h:592
unsigned char sal_Bool
Definition: types.h:38
signed char sal_Int8
Definition: types.h:43
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition: types.h:611
SAL_DLLPUBLIC void rtl_freeMemory(void *Ptr) SAL_THROW_EXTERN_C()
Free memory.
SAL_DLLPUBLIC void * rtl_allocateMemory(sal_Size Bytes) SAL_THROW_EXTERN_C()
Allocate memory.
@ typelib_TypeClass_VOID
type class of void
Definition: typeclass.h:32
struct SAL_DLLPUBLIC_RTTI _typelib_TypeDescription typelib_TypeDescription
Full type description of a type.
CPPU_DLLPUBLIC void typelib_typedescriptionreference_getDescription(typelib_TypeDescription **ppRet, typelib_TypeDescriptionReference *pRef) SAL_THROW_EXTERN_C()
Retrieves the type description for a given reference.
struct SAL_DLLPUBLIC_RTTI _typelib_TypeDescriptionReference typelib_TypeDescriptionReference
Holds a weak reference to a type description.
struct SAL_DLLPUBLIC_RTTI _uno_Any uno_Any
This is the binary specification of a UNO any.
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:511
Definition: types.h:377
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:649
Any makeAny(const C &value)
Template function to generically construct an any from a C++ value.
Definition: Any.hxx:234
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:660
bool operator>>=(const Any &rAny, C &value)
Template binary >>= operator to assign a value from an any.
Definition: Any.hxx:313
Any toAny(T const &value)
Wrap a value in an Any, if necessary.
Definition: Any.hxx:243
void operator<<=(Any &rAny, const C &value)
Template binary <<= operator to set the value of an any.
Definition: Any.hxx:275
This String class provides base functionality for C++ like Unicode character array handling.
Definition: ustring.hxx:172
C++ class representing an IDL any.
Definition: Any.h:62
void getValueTypeDescription(typelib_TypeDescription **ppTypeDescr) const
Gets the type description of the set value.
Definition: Any.h:183
bool hasValue() const
Tests if any contains a value.
Definition: Any.h:203
const void * getValue() const
Gets a pointer to the set value.
Definition: Any.h:210
const Type & getValueType() const
Gets the type of the set value.
Definition: Any.h:169
typelib_TypeDescriptionReference * getValueTypeRef() const
Gets the type of the set value.
Definition: Any.h:175
TypeClass getValueTypeClass() const
Gets the type class of the set value.
Definition: Any.h:190
This base class serves as a base class for all template reference classes and has been introduced due...
Definition: Reference.h:67
C++ class representing an IDL meta type.
Definition: Type.h:59