Coverage Report

Created: 2026-06-30 11:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/include/com/sun/star/uno/Any.h
Line
Count
Source
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"
32
#include "typelib/typedescription.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
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
53
/** C++ class representing an IDL any.
54
    This class is used to transport any type defined in IDL. The class inherits from the
55
    binary C representation of uno_Any.
56
    You can insert a value by using the <<= operators.
57
    No any can hold an any. You can extract values from an any by using the >>= operators which
58
    return true if the any contains an assignable value (no data loss), e.g. the any contains a
59
    short and you >>= it into a long variable.
60
*/
61
class SAL_WARN_UNUSED SAL_DLLPUBLIC_RTTI Any : public uno_Any
62
{
63
public:
64
    /// @cond INTERNAL
65
    // these are here to force memory de/allocation to sal lib.
66
    static void * SAL_CALL operator new ( size_t nSize )
67
11.6M
        { return ::rtl_allocateMemory( nSize ); }
68
    static void SAL_CALL operator delete ( void * pMem )
69
11.6M
        { ::rtl_freeMemory( pMem ); }
70
    static void * SAL_CALL operator new ( size_t, void * pMem )
71
0
        { return pMem; }
72
    static void SAL_CALL operator delete ( void *, void * )
73
0
        {}
74
    /// @endcond
75
76
    /** Default constructor: Any holds no value; its type is void.
77
    */
78
    inline Any();
79
80
    /** Templated ctor.  Sets a copy of the given value.
81
82
        @param value value of the Any
83
    */
84
    template <typename T>
85
#if defined LIBO_INTERNAL_ONLY
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 );
92
    /// Ctor support for C++ bool.
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
105
    /** Copy constructor: Sets value of the given any.
106
107
        @param rAny another any
108
    */
109
    inline Any( const Any & rAny );
110
111
    /** Constructor: Sets a copy of the given data.
112
113
        @param pData_ value
114
        @param rType type of value
115
    */
116
    inline Any( const void * pData_, const Type & rType );
117
118
    /** Constructor: Sets a copy of the given data.
119
120
        @param pData_ value
121
        @param pTypeDescr type of value
122
    */
123
    inline Any( const void * pData_, typelib_TypeDescription * pTypeDescr );
124
125
    /** Constructor: Sets a copy of the given data.
126
127
        @param pData_ value
128
        @param pType_ type of value
129
    */
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
0
        Any(static_cast<void *>(nullptr), type) {}
141
    Any(std::nullptr_t, typelib_TypeDescription * type):
142
0
        Any(static_cast<void *>(nullptr), type) {}
143
    Any(std::nullptr_t, typelib_TypeDescriptionReference * type):
144
1.62M
        Any(static_cast<void *>(nullptr), type) {}
145
#endif
146
147
    /** Destructor: Destructs any content and frees memory.
148
    */
149
    inline ~Any();
150
151
    /** Assignment operator: Sets the value of the given any.
152
153
        @param rAny another any (right side)
154
        @return this any
155
    */
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
165
    /** Gets the type of the set value.
166
167
        @return a Type object of the set value
168
     */
169
    const Type & SAL_CALL getValueType() const
170
12.2M
        { return * reinterpret_cast< const Type * >( &pType ); }
171
    /** Gets the type of the set value.
172
173
        @return the unacquired type description reference of the set value
174
     */
175
    typelib_TypeDescriptionReference * SAL_CALL getValueTypeRef() const
176
2.40M
        { return pType; }
177
178
    /** Gets the type description of the set value. Provides ownership of the type description!
179
        Call an explicit typelib_typedescription_release() to release afterwards.
180
181
        @param ppTypeDescr a pointer to type description pointer
182
    */
183
    void SAL_CALL getValueTypeDescription( typelib_TypeDescription ** ppTypeDescr ) const
184
0
        { ::typelib_typedescriptionreference_getDescription( ppTypeDescr, pType ); }
185
186
    /** Gets the type class of the set value.
187
188
        @return the type class of the set value
189
     */
190
    TypeClass SAL_CALL getValueTypeClass() const
191
2.05M
        { return static_cast<TypeClass>(pType->eTypeClass); }
192
193
    /** Gets the type name of the set value.
194
195
        @return the type name of the set value
196
    */
197
    inline ::rtl::OUString SAL_CALL getValueTypeName() const;
198
199
    /** Tests if any contains a value.
200
201
        @return true if any has a value, false otherwise
202
    */
203
    bool SAL_CALL hasValue() const
204
235M
        { return (typelib_TypeClass_VOID != pType->eTypeClass); }
205
206
    /** Gets a pointer to the set value.
207
208
        @return a pointer to the set value
209
    */
210
    const void * SAL_CALL getValue() const
211
10.9M
        { return pData; }
212
213
    /** Provides a value of specified type, so you can easily write e.g.
214
        <pre>
215
        sal_Int32 myVal = myAny.get<sal_Int32>();
216
        </pre>
217
        Widening conversion without data loss is taken into account.
218
        Throws a com::sun::star::uno::RuntimeException if the specified type
219
        cannot be provided.
220
221
        @return value of specified type
222
        @exception com::sun::star::uno::RuntimeException
223
                   in case the specified type cannot be provided
224
    */
225
    template <typename T>
226
    inline T get() const;
227
228
    /** Sets a value. If the any already contains a value, that value will be destructed
229
        and its memory freed.
230
231
        @param pData_ pointer to value
232
        @param rType type of value
233
    */
234
    inline void SAL_CALL setValue( const void * pData_, const Type & rType );
235
    /** Sets a value. If the any already contains a value, that value will be destructed
236
        and its memory freed.
237
238
        @param pData_ pointer to value
239
        @param pType_ type of value
240
    */
241
    inline void SAL_CALL setValue( const void * pData_, typelib_TypeDescriptionReference * pType_ );
242
    /** Sets a value. If the any already contains a value, that value will be destructed
243
        and its memory freed.
244
245
        @param pData_ pointer to value
246
        @param pTypeDescr type description of value
247
    */
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
0
    { setValue(static_cast<void *>(nullptr), type); }
260
    void setValue(std::nullptr_t, typelib_TypeDescriptionReference * type)
261
0
    { setValue(static_cast<void *>(nullptr), type); }
262
    void setValue(std::nullptr_t, typelib_TypeDescription * type)
263
0
    { setValue(static_cast<void *>(nullptr), type); }
264
#endif
265
266
    /** Clears this any. If the any already contains a value, that value will be destructed
267
        and its memory freed. After this has been called, the any does not contain a value.
268
    */
269
    inline void SAL_CALL clear();
270
271
    /** Tests whether this any is extractable to a value of given type.
272
        Widening conversion without data loss is taken into account.
273
274
        @param rType destination type
275
        @return true if this any is extractable to value of given type (e.g. using >>= operator)
276
    */
277
    inline bool SAL_CALL isExtractableTo( const Type & rType ) const;
278
279
    /** Tests whether this any can provide a value of specified type.
280
        Widening conversion without data loss is taken into account.
281
282
        @return true if this any can provide a value of specified type
283
        (e.g. using >>= operator)
284
    */
285
    template <typename T>
286
    inline bool has() const;
287
288
    /** Equality operator: compares two anys.
289
        The values need not be of equal type, e.g. a short integer is compared to a long integer.
290
291
        @param rAny another any (right side)
292
        @return true if both any contains equal values
293
    */
294
    inline bool SAL_CALL operator == ( const Any & rAny ) const;
295
    /** Inequality operator: compares two anys.
296
        The values need not be of equal type, e.g. a short integer is compared to a long integer.
297
298
        @param rAny another any (right side)
299
        @return true if both any contains unequal values
300
    */
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
313
    /// @cond INTERNAL
314
    // Forbid use with ambiguous type (sal_Unicode, sal_uInt16):
315
    explicit Any(sal_uInt16) SAL_DELETED_FUNCTION;
316
    /// @endcond
317
#endif
318
};
319
320
#if !defined LIBO_INTERNAL_ONLY
321
/// @cond INTERNAL
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;
325
/// @endcond
326
#endif
327
328
#if !defined LIBO_INTERNAL_ONLY
329
/** Template function to generically construct an any from a C++ value.
330
331
    @deprecated Just use an Any constructor with an appropriately typed argument.  (When the
332
    (UNO) type recorded in the Any instance shall be different from what would
333
    be deduced from the (C++) type of the argument, cast the argument to the appropriate type
334
    first.)
335
336
    @tparam C value type
337
    @param value a value
338
    @return an any
339
*/
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
348
/** Wrap a value in an Any, if necessary.
349
350
    (A difference to the deprecated makeAny is that makeAny cannot be called on an Any, while
351
    toAny just returns the given Any.)
352
353
    @since LibreOffice 5.0
354
*/
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
361
/** Extract a value from an Any, if necessary.
362
363
    The difference to operator >>= is that operator >>= cannot be called with an
364
    Any as right-hand side (in LIBO_INTERNAL_ONLY), while fromAny just passes on
365
    the given Any (and always succeeds) in the specialization for T = Any.
366
367
    @tparam T  any type representing a UNO type
368
369
    @param any  any Any value
370
371
    @param value  a non-null pointer, receiving the extracted value if
372
    extraction succeeded (and left unmodified otherwise)
373
374
    @return  true iff extraction succeeded
375
376
    @since LibreOffice 5.3
377
*/
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
386
/** Template binary <<= operator to set the value of an any.
387
388
    @tparam C value type
389
    @param rAny destination any (left side)
390
    @param value source value (right side)
391
*/
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
399
/** Template binary >>= operator to assign a value from an any.
400
    If the any does not contain a value that can be assigned without data loss, then this
401
    operation will fail returning false.
402
403
    @tparam C value type
404
    @param rAny source any (left side)
405
    @param value destination value (right side)
406
    @return true if assignment was possible without data loss
407
*/
408
template< class C >
409
inline bool SAL_CALL operator >>= ( const Any & rAny, C & value );
410
411
/** Template equality operator: compares set value of left side any to right side value.
412
    The values need not be of equal type, e.g. a short integer is compared to a long integer.
413
    This operator can be implemented as template member function, if all supported compilers
414
    can cope with template member functions.
415
416
    @tparam C value type
417
    @param rAny another any (left side)
418
    @param value a value (right side)
419
    @return true if values are equal, false otherwise
420
*/
421
template< class C >
422
inline bool SAL_CALL operator == ( const Any & rAny, const C & value );
423
/** Template inequality operator: compares set value of left side any to right side value.
424
    The values need not be of equal type, e.g. a short integer is compared to a long integer.
425
    This operator can be implemented as template member function, if all supported compilers
426
    can cope with template member functions.
427
428
    @tparam C value type
429
    @param rAny another any (left side)
430
    @param value a value (right side)
431
    @return true if values are unequal, false otherwise
432
*/
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
498
/** Gets the meta type of IDL type any.
499
500
    There are cases (involving templates) where uses of getCppuType are known to
501
    not compile.  Use cppu::UnoType or cppu::getTypeFavourUnsigned instead.
502
503
    The dummy parameter is just a typed pointer for function signature.
504
505
    @return type of IDL type any
506
507
    @deprecated
508
    Use cppu::UnoType instead.
509
*/
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
0
{
513
0
    return ::cppu::UnoType< ::com::sun::star::uno::Any >::get();
514
0
}
515
516
#endif
517
518
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */