1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
23 #include <typelib/typedescription.h>
24 #include <com/sun/star/uno/Type.h>
25 #include <cppu/unotype.hxx>
26 #include <rtl/alloc.h>
38 /** C++ class representing an IDL any.
39 This class is used to transport any type defined in IDL. The class inherits from the
40 binary C representation of uno_Any.
41 You can insert a value by either using the <<= operators or the template function makeAny().
42 No any can hold an any. You can extract values from an any by using the >>= operators which
43 return true if the any contains an assignable value (no data loss), e.g. the any contains a
44 short and you >>= it into a long variable.
46 class SAL_WARN_UNUSED Any
: public uno_Any
50 // these are here to force memory de/allocation to sal lib.
51 inline static void * SAL_CALL
operator new ( size_t nSize
) SAL_THROW(())
52 { return ::rtl_allocateMemory( nSize
); }
53 inline static void SAL_CALL
operator delete ( void * pMem
) SAL_THROW(())
54 { ::rtl_freeMemory( pMem
); }
55 inline static void * SAL_CALL
operator new ( size_t, void * pMem
) SAL_THROW(())
57 inline static void SAL_CALL
operator delete ( void *, void * ) SAL_THROW(())
61 /** Default constructor: Any holds no value; its type is void.
63 inline Any() SAL_THROW(());
65 /** Templated ctor. Sets a copy of the given value.
67 @param value value of the Any
70 explicit inline Any( T
const & value
);
71 /// Ctor support for C++ bool.
72 explicit inline Any( bool value
);
74 /** Copy constructor: Sets value of the given any.
76 @param rAny another any
78 inline Any( const Any
& rAny
) SAL_THROW(());
80 /** Constructor: Sets a copy of the given data.
83 @param rType type of value
85 inline Any( const void * pData_
, const Type
& rType
) SAL_THROW(());
87 /** Constructor: Sets a copy of the given data.
90 @param pTypeDescr type of value
92 inline Any( const void * pData_
, typelib_TypeDescription
* pTypeDescr
) SAL_THROW(());
94 /** Constructor: Sets a copy of the given data.
97 @param pType type of value
99 inline Any( const void * pData_
, typelib_TypeDescriptionReference
* pType
) SAL_THROW(());
101 /** Destructor: Destructs any content and frees memory.
103 inline ~Any() SAL_THROW(());
105 /** Assignment operator: Sets the value of the given any.
107 @param rAny another any (right side)
110 inline Any
& SAL_CALL
operator = ( const Any
& rAny
) SAL_THROW(());
112 /** Gets the type of the set value.
114 @return a Type object of the set value
116 inline const Type
& SAL_CALL
getValueType() const SAL_THROW(())
117 { return * reinterpret_cast< const Type
* >( &pType
); }
118 /** Gets the type of the set value.
120 @return the unacquired type description reference of the set value
122 inline typelib_TypeDescriptionReference
* SAL_CALL
getValueTypeRef() const SAL_THROW(())
125 /** Gets the type description of the set value. Provides ownership of the type description!
126 Call an explicit typelib_typedescription_release() to release afterwards.
128 @param ppTypeDescr a pointer to type description pointer
130 inline void SAL_CALL
getValueTypeDescription( typelib_TypeDescription
** ppTypeDescr
) const SAL_THROW(())
131 { ::typelib_typedescriptionreference_getDescription( ppTypeDescr
, pType
); }
133 /** Gets the type class of the set value.
135 @return the type class of the set value
137 inline TypeClass SAL_CALL
getValueTypeClass() const SAL_THROW(())
138 { return (TypeClass
)pType
->eTypeClass
; }
140 /** Gets the type name of the set value.
142 @return the type name of the set value
144 inline ::rtl::OUString SAL_CALL
getValueTypeName() const SAL_THROW(());
146 /** Tests if any contains a value.
148 @return true if any has a value, false otherwise
150 inline sal_Bool SAL_CALL
hasValue() const SAL_THROW(())
151 { return (typelib_TypeClass_VOID
!= pType
->eTypeClass
); }
153 /** Gets a pointer to the set value.
155 @return a pointer to the set value
157 inline const void * SAL_CALL
getValue() const SAL_THROW(())
160 /** Provides a value of specified type, so you can easily write e.g.
162 sal_Int32 myVal = myAny.get<sal_Int32>();
164 Widening conversion without data loss is taken into account.
165 Throws a com::sun::star::uno::RuntimeException if the specified type
168 @return value of specified type
169 @exception com::sun::star::uno::RuntimeException
170 in case the specified type cannot be provided
172 template <typename T
>
173 inline T
get() const;
175 /** Sets a value. If the any already contains a value, that value will be destructed
176 and its memory freed.
178 @param pData_ pointer to value
179 @param rType type of value
181 inline void SAL_CALL
setValue( const void * pData_
, const Type
& rType
) SAL_THROW(());
182 /** Sets a value. If the any already contains a value, that value will be destructed
183 and its memory freed.
185 @param pData_ pointer to value
186 @param pType type of value
188 inline void SAL_CALL
setValue( const void * pData_
, typelib_TypeDescriptionReference
* pType
) SAL_THROW(());
189 /** Sets a value. If the any already contains a value, that value will be destructed
190 and its memory freed.
192 @param pData_ pointer to value
193 @param pTypeDescr type description of value
195 inline void SAL_CALL
setValue( const void * pData_
, typelib_TypeDescription
* pTypeDescr
) SAL_THROW(());
197 /** Clears this any. If the any already contains a value, that value will be destructed
198 and its memory freed. After this has been called, the any does not contain a value.
200 inline void SAL_CALL
clear() SAL_THROW(());
202 /** Tests whether this any is extractable to a value of given type.
203 Widening conversion without data loss is taken into account.
205 @param rType destination type
206 @return true if this any is extractable to value of given type (e.g. using >>= operator)
208 inline sal_Bool SAL_CALL
isExtractableTo( const Type
& rType
) const SAL_THROW(());
210 /** Tests whether this any can provide a value of specified type.
211 Widening conversion without data loss is taken into account.
213 @return true if this any can provide a value of specified type
214 (e.g. using >>= operator)
216 template <typename T
>
217 inline bool has() const;
219 /** Equality operator: compares two anys.
220 The values need not be of equal type, e.g. a short integer is compared to a long integer.
222 @param rAny another any (right side)
223 @return true if both any contains equal values
225 inline sal_Bool SAL_CALL
operator == ( const Any
& rAny
) const SAL_THROW(());
226 /** Unequality operator: compares two anys.
227 The values need not be of equal type, e.g. a short integer is compared to a long integer.
229 @param rAny another any (right side)
230 @return true if both any contains unequal values
232 inline sal_Bool SAL_CALL
operator != ( const Any
& rAny
) const SAL_THROW(());
235 // not impl: forbid use with ambiguous type (sal_Unicode, sal_uInt16)
236 explicit Any( sal_uInt16
);
237 #if defined(_MSC_VER)
238 // Omitting the following private declarations leads to an internal compiler
239 // error on MSVC (version 1310).
240 // not impl: forbid use with ambiguous type (sal_Unicode, sal_uInt16)
242 sal_uInt16 get
<sal_uInt16
>() const;
244 bool has
<sal_uInt16
>() const;
245 #endif // defined(_MSC_VER)
248 /** Template function to generically construct an any from a C++ value.
255 inline Any SAL_CALL
makeAny( const C
& value
) SAL_THROW(());
257 // additionally specialized for C++ bool
259 inline Any SAL_CALL
makeAny( bool const & value
) SAL_THROW(());
264 /** Template binary <<= operator to set the value of an any.
267 @param rAny destination any (left side)
268 @param value source value (right side)
271 inline void SAL_CALL
operator <<= ( Any
& rAny
, const C
& value
) SAL_THROW(());
273 // additionally for C++ bool:
274 inline void SAL_CALL
operator <<= ( Any
& rAny
, bool const & value
)
277 /** Template binary >>= operator to assign a value from an any.
278 If the any does not contain a value that can be assigned without data loss, then this
279 operation will fail returning false.
282 @param rAny source any (left side)
283 @param value destination value (right side)
284 @return true if assignment was possible without data loss
287 inline sal_Bool SAL_CALL
operator >>= ( const Any
& rAny
, C
& value
) SAL_THROW(());
289 /** Template equality operator: compares set value of left side any to right side value.
290 The values need not be of equal type, e.g. a short integer is compared to a long integer.
291 This operator can be implemented as template member function, if all supported compilers
292 can cope with template member functions.
295 @param rAny another any (left side)
296 @param value a value (right side)
297 @return true if values are equal, false otherwise
300 inline sal_Bool SAL_CALL
operator == ( const Any
& rAny
, const C
& value
) SAL_THROW(());
301 /** Template unequality operator: compares set value of left side any to right side value.
302 The values need not be of equal type, e.g. a short integer is compared to a long integer.
303 This operator can be implemented as template member function, if all supported compilers
304 can cope with template member functions.
307 @param rAny another any (left side)
308 @param value a value (right side)
309 @return true if values are unequal, false otherwise
312 inline sal_Bool SAL_CALL
operator != ( const Any
& rAny
, const C
& value
) SAL_THROW(());
314 // additional specialized >>= and == operators
316 inline sal_Bool SAL_CALL
operator >>= ( const Any
& rAny
, sal_Bool
& value
) SAL_THROW(());
317 inline sal_Bool SAL_CALL
operator == ( const Any
& rAny
, const sal_Bool
& value
) SAL_THROW(());
319 inline sal_Bool SAL_CALL
operator >>= ( Any
const & rAny
, bool & value
)
322 inline sal_Bool SAL_CALL
operator == ( Any
const & rAny
, bool const & value
)
325 inline sal_Bool SAL_CALL
operator >>= ( const Any
& rAny
, sal_Int8
& value
) SAL_THROW(());
327 inline sal_Bool SAL_CALL
operator >>= ( const Any
& rAny
, sal_Int16
& value
) SAL_THROW(());
328 inline sal_Bool SAL_CALL
operator >>= ( const Any
& rAny
, sal_uInt16
& value
) SAL_THROW(());
330 inline sal_Bool SAL_CALL
operator >>= ( const Any
& rAny
, sal_Int32
& value
) SAL_THROW(());
331 inline sal_Bool SAL_CALL
operator >>= ( const Any
& rAny
, sal_uInt32
& value
) SAL_THROW(());
333 inline sal_Bool SAL_CALL
operator >>= ( const Any
& rAny
, sal_Int64
& value
) SAL_THROW(());
334 inline sal_Bool SAL_CALL
operator >>= ( const Any
& rAny
, sal_uInt64
& value
) SAL_THROW(());
336 inline sal_Bool SAL_CALL
operator >>= ( const Any
& rAny
, float & value
) SAL_THROW(());
338 inline sal_Bool SAL_CALL
operator >>= ( const Any
& rAny
, double & value
) SAL_THROW(());
340 inline sal_Bool SAL_CALL
operator >>= ( const Any
& rAny
, ::rtl::OUString
& value
) SAL_THROW(());
341 inline sal_Bool SAL_CALL
operator == ( const Any
& rAny
, const ::rtl::OUString
& value
) SAL_THROW(());
343 inline sal_Bool SAL_CALL
operator >>= ( const Any
& rAny
, Type
& value
) SAL_THROW(());
344 inline sal_Bool SAL_CALL
operator == ( const Any
& rAny
, const Type
& value
) SAL_THROW(());
346 inline sal_Bool SAL_CALL
operator >>= ( const Any
& rAny
, Any
& value
) SAL_THROW(());
348 inline sal_Bool SAL_CALL
operator == ( const Any
& rAny
, const BaseReference
& value
) SAL_THROW(());
355 /** Gets the meta type of IDL type any.
357 There are cases (involving templates) where uses of getCppuType are known to
358 not compile. Use cppu::UnoType or cppu::getTypeFavourUnsigned instead.
360 The dummy parameter is just a typed pointer for function signature.
362 @return type of IDL type any
364 inline const ::com::sun::star::uno::Type
& SAL_CALL
getCppuType( SAL_UNUSED_PARAMETER
const ::com::sun::star::uno::Any
* ) SAL_THROW(())
366 return ::cppu::UnoType
< ::com::sun::star::uno::Any
>::get();
371 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */