bump product version to 6.3.0.0.beta1
[LibreOffice.git] / include / com / sun / star / uno / Any.h
blob4c52167a6b67df0211aa1d39d3490297cc3f3ea5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #ifndef INCLUDED_COM_SUN_STAR_UNO_ANY_H
20 #define INCLUDED_COM_SUN_STAR_UNO_ANY_H
22 #include "sal/config.h"
24 #include <cstddef>
26 #include "rtl/ustring.hxx"
27 #include "uno/any2.h"
28 #include "typelib/typedescription.h"
29 #include "cppu/unotype.hxx"
30 #include "com/sun/star/uno/TypeClass.hdl"
31 #include "rtl/alloc.h"
33 namespace com
35 namespace sun
37 namespace star
39 namespace uno
42 class Type;
44 /** C++ class representing an IDL any.
45 This class is used to transport any type defined in IDL. The class inherits from the
46 binary C representation of uno_Any.
47 You can insert a value by either using the <<= operators or the template function makeAny().
48 No any can hold an any. You can extract values from an any by using the >>= operators which
49 return true if the any contains an assignable value (no data loss), e.g. the any contains a
50 short and you >>= it into a long variable.
52 class SAL_WARN_UNUSED SAL_DLLPUBLIC_RTTI Any : public uno_Any
54 public:
55 /// @cond INTERNAL
56 // these are here to force memory de/allocation to sal lib.
57 static void * SAL_CALL operator new ( size_t nSize )
58 { return ::rtl_allocateMemory( nSize ); }
59 static void SAL_CALL operator delete ( void * pMem )
60 { ::rtl_freeMemory( pMem ); }
61 static void * SAL_CALL operator new ( size_t, void * pMem )
62 { return pMem; }
63 static void SAL_CALL operator delete ( void *, void * )
65 /// @endcond
67 /** Default constructor: Any holds no value; its type is void.
69 inline Any();
71 /** Templated ctor. Sets a copy of the given value.
73 @param value value of the Any
75 template <typename T>
76 explicit inline Any( T const & value );
77 /// Ctor support for C++ bool.
78 explicit inline Any( bool value );
80 #if defined LIBO_INTERNAL_ONLY
81 template<typename T1, typename T2>
82 explicit inline Any(rtl::OUStringConcat<T1, T2> && value);
83 template<typename T1, typename T2>
84 explicit Any(rtl::OUStringConcat<T1, T2> const &) = delete;
85 #endif
87 /** Copy constructor: Sets value of the given any.
89 @param rAny another any
91 inline Any( const Any & rAny );
93 /** Constructor: Sets a copy of the given data.
95 @param pData_ value
96 @param rType type of value
98 inline Any( const void * pData_, const Type & rType );
100 /** Constructor: Sets a copy of the given data.
102 @param pData_ value
103 @param pTypeDescr type of value
105 inline Any( const void * pData_, typelib_TypeDescription * pTypeDescr );
107 /** Constructor: Sets a copy of the given data.
109 @param pData_ value
110 @param pType_ type of value
112 inline Any( const void * pData_, typelib_TypeDescriptionReference * pType_ );
114 #if defined LIBO_INTERNAL_ONLY
115 Any(bool const *, Type const &) = delete;
116 Any(bool const *, typelib_TypeDescription *) = delete;
117 Any(bool const *, typelib_TypeDescriptionReference *) = delete;
118 Any(sal_Bool const *, Type const &) = delete;
119 Any(sal_Bool const *, typelib_TypeDescription *) = delete;
120 Any(sal_Bool const *, typelib_TypeDescriptionReference *) = delete;
121 Any(std::nullptr_t, Type const & type):
122 Any(static_cast<void *>(nullptr), type) {}
123 Any(std::nullptr_t, typelib_TypeDescription * type):
124 Any(static_cast<void *>(nullptr), type) {}
125 Any(std::nullptr_t, typelib_TypeDescriptionReference * type):
126 Any(static_cast<void *>(nullptr), type) {}
127 #endif
129 /** Destructor: Destructs any content and frees memory.
131 inline ~Any();
133 /** Assignment operator: Sets the value of the given any.
135 @param rAny another any (right side)
136 @return this any
138 inline Any & SAL_CALL operator = ( const Any & rAny );
140 #if defined LIBO_INTERNAL_ONLY
141 inline Any(Any && other);
142 inline Any & operator =(Any && other);
143 #endif
145 /** Gets the type of the set value.
147 @return a Type object of the set value
149 const Type & SAL_CALL getValueType() const
150 { return * reinterpret_cast< const Type * >( &pType ); }
151 /** Gets the type of the set value.
153 @return the unacquired type description reference of the set value
155 typelib_TypeDescriptionReference * SAL_CALL getValueTypeRef() const
156 { return pType; }
158 /** Gets the type description of the set value. Provides ownership of the type description!
159 Call an explicit typelib_typedescription_release() to release afterwards.
161 @param ppTypeDescr a pointer to type description pointer
163 void SAL_CALL getValueTypeDescription( typelib_TypeDescription ** ppTypeDescr ) const
164 { ::typelib_typedescriptionreference_getDescription( ppTypeDescr, pType ); }
166 /** Gets the type class of the set value.
168 @return the type class of the set value
170 TypeClass SAL_CALL getValueTypeClass() const
171 { return static_cast<TypeClass>(pType->eTypeClass); }
173 /** Gets the type name of the set value.
175 @return the type name of the set value
177 inline ::rtl::OUString SAL_CALL getValueTypeName() const;
179 /** Tests if any contains a value.
181 @return true if any has a value, false otherwise
183 bool SAL_CALL hasValue() const
184 { return (typelib_TypeClass_VOID != pType->eTypeClass); }
186 /** Gets a pointer to the set value.
188 @return a pointer to the set value
190 const void * SAL_CALL getValue() const
191 { return pData; }
193 /** Provides a value of specified type, so you can easily write e.g.
194 <pre>
195 sal_Int32 myVal = myAny.get<sal_Int32>();
196 </pre>
197 Widening conversion without data loss is taken into account.
198 Throws a com::sun::star::uno::RuntimeException if the specified type
199 cannot be provided.
201 @return value of specified type
202 @exception com::sun::star::uno::RuntimeException
203 in case the specified type cannot be provided
205 template <typename T>
206 inline T get() const;
208 /** Sets a value. If the any already contains a value, that value will be destructed
209 and its memory freed.
211 @param pData_ pointer to value
212 @param rType type of value
214 inline void SAL_CALL setValue( const void * pData_, const Type & rType );
215 /** Sets a value. If the any already contains a value, that value will be destructed
216 and its memory freed.
218 @param pData_ pointer to value
219 @param pType_ type of value
221 inline void SAL_CALL setValue( const void * pData_, typelib_TypeDescriptionReference * pType_ );
222 /** Sets a value. If the any already contains a value, that value will be destructed
223 and its memory freed.
225 @param pData_ pointer to value
226 @param pTypeDescr type description of value
228 inline void SAL_CALL setValue( const void * pData_, typelib_TypeDescription * pTypeDescr );
230 #if defined LIBO_INTERNAL_ONLY
231 void setValue(bool const *, Type const &) = delete;
232 void setValue(bool const *, typelib_TypeDescriptionReference *) = delete;
233 void setValue(bool const *, typelib_TypeDescription *) = delete;
234 void setValue(sal_Bool const *, Type const &) = delete;
235 void setValue(sal_Bool const *, typelib_TypeDescriptionReference *)
236 = delete;
237 void setValue(sal_Bool const *, typelib_TypeDescription *) = delete;
238 void setValue(std::nullptr_t, Type const & type)
239 { setValue(static_cast<void *>(nullptr), type); }
240 void setValue(std::nullptr_t, typelib_TypeDescriptionReference * type)
241 { setValue(static_cast<void *>(nullptr), type); }
242 void setValue(std::nullptr_t, typelib_TypeDescription * type)
243 { setValue(static_cast<void *>(nullptr), type); }
244 #endif
246 /** Clears this any. If the any already contains a value, that value will be destructed
247 and its memory freed. After this has been called, the any does not contain a value.
249 inline void SAL_CALL clear();
251 /** Tests whether this any is extractable to a value of given type.
252 Widening conversion without data loss is taken into account.
254 @param rType destination type
255 @return true if this any is extractable to value of given type (e.g. using >>= operator)
257 inline bool SAL_CALL isExtractableTo( const Type & rType ) const;
259 /** Tests whether this any can provide a value of specified type.
260 Widening conversion without data loss is taken into account.
262 @return true if this any can provide a value of specified type
263 (e.g. using >>= operator)
265 template <typename T>
266 inline bool has() const;
268 /** Equality operator: compares two anys.
269 The values need not be of equal type, e.g. a short integer is compared to a long integer.
271 @param rAny another any (right side)
272 @return true if both any contains equal values
274 inline bool SAL_CALL operator == ( const Any & rAny ) const;
275 /** Unequality operator: compares two anys.
276 The values need not be of equal type, e.g. a short integer is compared to a long integer.
278 @param rAny another any (right side)
279 @return true if both any contains unequal values
281 inline bool SAL_CALL operator != ( const Any & rAny ) const;
283 private:
284 #if !defined LIBO_INTERNAL_ONLY
285 /// @cond INTERNAL
286 // Forbid use with ambiguous type (sal_Unicode, sal_uInt16):
287 explicit Any(sal_uInt16) SAL_DELETED_FUNCTION;
288 /// @endcond
289 #endif
292 #if !defined LIBO_INTERNAL_ONLY
293 /// @cond INTERNAL
294 // Forbid use with ambiguous type (sal_Unicode, sal_uInt16):
295 template<> sal_uInt16 Any::get<sal_uInt16>() const SAL_DELETED_FUNCTION;
296 template<> bool Any::has<sal_uInt16>() const SAL_DELETED_FUNCTION;
297 /// @endcond
298 #endif
300 /** Template function to generically construct an any from a C++ value.
302 This can be useful with an explicitly specified template parameter, when the
303 (UNO) type recorded in the Any instance shall be different from what would
304 be deduced from the (C++) type of the argument if no template parameter were
305 specified explicitly.
307 @tparam C value type
308 @param value a value
309 @return an any
311 template< class C >
312 inline Any SAL_CALL makeAny( const C & value );
314 #if !defined LIBO_INTERNAL_ONLY
315 template<> inline Any SAL_CALL makeAny(sal_uInt16 const & value);
316 #endif
318 template<> Any SAL_CALL makeAny(Any const &) SAL_DELETED_FUNCTION;
320 /** Wrap a value in an Any, if necessary.
322 The difference to makeAny is that makeAny cannot be called on an Any, while
323 toAny just returns the given Any.
325 @since LibreOffice 5.0
327 template<typename T> inline Any toAny(T const & value);
329 template<> inline Any toAny(Any const & value);
331 #if defined LIBO_INTERNAL_ONLY
333 /** Extract a value from an Any, if necessary.
335 The difference to operator >>= is that operator >>= cannot be called with an
336 Any as right-hand side (in LIBO_INTERNAL_ONLY), while fromAny just passes on
337 the given Any (and always succeeds) in the specialization for T = Any.
339 @tparam T any type representing a UNO type
341 @param any any Any value
343 @param value a non-null pointer, receiving the extracted value if
344 extraction succeeded (and left unmodified otherwise)
346 @return true iff extraction succeeded
348 @since LibreOffice 5.3
350 template<typename T> inline bool fromAny(Any const & any, T * value);
352 template<> inline bool fromAny(Any const & any, Any * value);
354 #endif
356 class BaseReference;
358 /** Template binary <<= operator to set the value of an any.
360 @tparam C value type
361 @param rAny destination any (left side)
362 @param value source value (right side)
364 template< class C >
365 inline void SAL_CALL operator <<= ( Any & rAny, const C & value );
367 // additionally for C++ bool:
368 template<>
369 inline void SAL_CALL operator <<= ( Any & rAny, bool const & value );
371 /** Template binary >>= operator to assign a value from an any.
372 If the any does not contain a value that can be assigned without data loss, then this
373 operation will fail returning false.
375 @tparam C value type
376 @param rAny source any (left side)
377 @param value destination value (right side)
378 @return true if assignment was possible without data loss
380 template< class C >
381 inline bool SAL_CALL operator >>= ( const Any & rAny, C & value );
383 /** Template equality operator: compares set value of left side any to right side value.
384 The values need not be of equal type, e.g. a short integer is compared to a long integer.
385 This operator can be implemented as template member function, if all supported compilers
386 can cope with template member functions.
388 @tparam C value type
389 @param rAny another any (left side)
390 @param value a value (right side)
391 @return true if values are equal, false otherwise
393 template< class C >
394 inline bool SAL_CALL operator == ( const Any & rAny, const C & value );
395 /** Template unequality operator: compares set value of left side any to right side value.
396 The values need not be of equal type, e.g. a short integer is compared to a long integer.
397 This operator can be implemented as template member function, if all supported compilers
398 can cope with template member functions.
400 @tparam C value type
401 @param rAny another any (left side)
402 @param value a value (right side)
403 @return true if values are unequal, false otherwise
405 template< class C >
406 inline bool SAL_CALL operator != ( const Any & rAny, const C & value );
408 // additional specialized >>= and == operators
409 // bool
410 template<>
411 inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Bool & value );
412 template<>
413 inline bool SAL_CALL operator == ( const Any & rAny, const sal_Bool & value );
414 template<>
415 inline bool SAL_CALL operator >>= ( Any const & rAny, bool & value );
416 template<>
417 inline bool SAL_CALL operator == ( Any const & rAny, bool const & value );
418 // byte
419 template<>
420 inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Int8 & value );
421 // short
422 template<>
423 inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Int16 & value );
424 template<>
425 inline bool SAL_CALL operator >>= ( const Any & rAny, sal_uInt16 & value );
426 // long
427 template<>
428 inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Int32 & value );
429 template<>
430 inline bool SAL_CALL operator >>= ( const Any & rAny, sal_uInt32 & value );
431 // hyper
432 template<>
433 inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Int64 & value );
434 template<>
435 inline bool SAL_CALL operator >>= ( const Any & rAny, sal_uInt64 & value );
436 // float
437 template<>
438 inline bool SAL_CALL operator >>= ( const Any & rAny, float & value );
439 // double
440 template<>
441 inline bool SAL_CALL operator >>= ( const Any & rAny, double & value );
442 // string
443 template<>
444 inline bool SAL_CALL operator >>= ( const Any & rAny, ::rtl::OUString & value );
445 template<>
446 inline bool SAL_CALL operator == ( const Any & rAny, const ::rtl::OUString & value );
447 // type
448 template<>
449 inline bool SAL_CALL operator >>= ( const Any & rAny, Type & value );
450 template<>
451 inline bool SAL_CALL operator == ( const Any & rAny, const Type & value );
452 // any
453 #if !defined LIBO_INTERNAL_ONLY
454 template<>
455 inline bool SAL_CALL operator >>= ( const Any & rAny, Any & value );
456 #endif
457 // interface
458 template<>
459 inline bool SAL_CALL operator == ( const Any & rAny, const BaseReference & value );
466 /** Gets the meta type of IDL type any.
468 There are cases (involving templates) where uses of getCppuType are known to
469 not compile. Use cppu::UnoType or cppu::getTypeFavourUnsigned instead.
471 The dummy parameter is just a typed pointer for function signature.
473 @return type of IDL type any
475 @deprecated
476 Use cppu::UnoType instead.
478 SAL_DEPRECATED("use cppu::UnoType")
479 inline const ::com::sun::star::uno::Type & SAL_CALL getCppuType( SAL_UNUSED_PARAMETER const ::com::sun::star::uno::Any * )
481 return ::cppu::UnoType< ::com::sun::star::uno::Any >::get();
484 #endif
486 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */