Line data Source code
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 : #ifndef INCLUDED_COM_SUN_STAR_UNO_ANY_H
20 : #define INCLUDED_COM_SUN_STAR_UNO_ANY_H
21 :
22 : #include <uno/any2.h>
23 : #include <typelib/typedescription.h>
24 : #include <cppu/unotype.hxx>
25 : #include <com/sun/star/uno/TypeClass.hdl>
26 : #include <rtl/alloc.h>
27 :
28 : namespace rtl { class OUString; }
29 :
30 : namespace com
31 : {
32 : namespace sun
33 : {
34 : namespace star
35 : {
36 : namespace uno
37 : {
38 :
39 : class Type;
40 :
41 : /** C++ class representing an IDL any.
42 : This class is used to transport any type defined in IDL. The class inherits from the
43 : binary C representation of uno_Any.
44 : You can insert a value by either using the <<= operators or the template function makeAny().
45 : No any can hold an any. You can extract values from an any by using the >>= operators which
46 : return true if the any contains an assignable value (no data loss), e.g. the any contains a
47 : short and you >>= it into a long variable.
48 : */
49 : class SAL_WARN_UNUSED SAL_DLLPUBLIC_RTTI Any : public uno_Any
50 : {
51 : public:
52 : /// @cond INTERNAL
53 : // these are here to force memory de/allocation to sal lib.
54 293008 : inline static void * SAL_CALL operator new ( size_t nSize )
55 293008 : { return ::rtl_allocateMemory( nSize ); }
56 293008 : inline static void SAL_CALL operator delete ( void * pMem )
57 293008 : { ::rtl_freeMemory( pMem ); }
58 : inline static void * SAL_CALL operator new ( size_t, void * pMem )
59 : { return pMem; }
60 : inline static void SAL_CALL operator delete ( void *, void * )
61 : {}
62 : /// @endcond
63 :
64 : /** Default constructor: Any holds no value; its type is void.
65 : */
66 : inline Any();
67 :
68 : /** Templated ctor. Sets a copy of the given value.
69 :
70 : @param value value of the Any
71 : */
72 : template <typename T>
73 : explicit inline Any( T const & value );
74 : /// Ctor support for C++ bool.
75 : explicit inline Any( bool value );
76 :
77 : /** Copy constructor: Sets value of the given any.
78 :
79 : @param rAny another any
80 : */
81 : inline Any( const Any & rAny );
82 :
83 : /** Constructor: Sets a copy of the given data.
84 :
85 : @param pData_ value
86 : @param rType type of value
87 : */
88 : inline Any( const void * pData_, const Type & rType );
89 :
90 : /** Constructor: Sets a copy of the given data.
91 :
92 : @param pData_ value
93 : @param pTypeDescr type of value
94 : */
95 : inline Any( const void * pData_, typelib_TypeDescription * pTypeDescr );
96 :
97 : /** Constructor: Sets a copy of the given data.
98 :
99 : @param pData_ value
100 : @param pType type of value
101 : */
102 : inline Any( const void * pData_, typelib_TypeDescriptionReference * pType );
103 :
104 : /** Destructor: Destructs any content and frees memory.
105 : */
106 : inline ~Any();
107 :
108 : /** Assignment operator: Sets the value of the given any.
109 :
110 : @param rAny another any (right side)
111 : @return this any
112 : */
113 : inline Any & SAL_CALL operator = ( const Any & rAny );
114 :
115 : /** Gets the type of the set value.
116 :
117 : @return a Type object of the set value
118 : */
119 6169960 : inline const Type & SAL_CALL getValueType() const
120 6169960 : { return * reinterpret_cast< const Type * >( &pType ); }
121 : /** Gets the type of the set value.
122 :
123 : @return the unacquired type description reference of the set value
124 : */
125 32475 : inline typelib_TypeDescriptionReference * SAL_CALL getValueTypeRef() const
126 32475 : { return pType; }
127 :
128 : /** Gets the type description of the set value. Provides ownership of the type description!
129 : Call an explicit typelib_typedescription_release() to release afterwards.
130 :
131 : @param ppTypeDescr a pointer to type description pointer
132 : */
133 : inline void SAL_CALL getValueTypeDescription( typelib_TypeDescription ** ppTypeDescr ) const
134 : { ::typelib_typedescriptionreference_getDescription( ppTypeDescr, pType ); }
135 :
136 : /** Gets the type class of the set value.
137 :
138 : @return the type class of the set value
139 : */
140 1198232 : inline TypeClass SAL_CALL getValueTypeClass() const
141 1198232 : { return (TypeClass)pType->eTypeClass; }
142 :
143 : /** Gets the type name of the set value.
144 :
145 : @return the type name of the set value
146 : */
147 : inline ::rtl::OUString SAL_CALL getValueTypeName() const;
148 :
149 : /** Tests if any contains a value.
150 :
151 : @return true if any has a value, false otherwise
152 : */
153 510354844 : inline bool SAL_CALL hasValue() const
154 510354844 : { return (typelib_TypeClass_VOID != pType->eTypeClass); }
155 :
156 : /** Gets a pointer to the set value.
157 :
158 : @return a pointer to the set value
159 : */
160 2391789 : inline const void * SAL_CALL getValue() const
161 2391789 : { return pData; }
162 :
163 : /** Provides a value of specified type, so you can easily write e.g.
164 : <pre>
165 : sal_Int32 myVal = myAny.get<sal_Int32>();
166 : </pre>
167 : Widening conversion without data loss is taken into account.
168 : Throws a com::sun::star::uno::RuntimeException if the specified type
169 : cannot be provided.
170 :
171 : @return value of specified type
172 : @exception com::sun::star::uno::RuntimeException
173 : in case the specified type cannot be provided
174 : */
175 : template <typename T>
176 : inline T get() const;
177 :
178 : /** Sets a value. If the any already contains a value, that value will be destructed
179 : and its memory freed.
180 :
181 : @param pData_ pointer to value
182 : @param rType type of value
183 : */
184 : inline void SAL_CALL setValue( const void * pData_, const Type & rType );
185 : /** Sets a value. If the any already contains a value, that value will be destructed
186 : and its memory freed.
187 :
188 : @param pData_ pointer to value
189 : @param pType type of value
190 : */
191 : inline void SAL_CALL setValue( const void * pData_, typelib_TypeDescriptionReference * pType );
192 : /** Sets a value. If the any already contains a value, that value will be destructed
193 : and its memory freed.
194 :
195 : @param pData_ pointer to value
196 : @param pTypeDescr type description of value
197 : */
198 : inline void SAL_CALL setValue( const void * pData_, typelib_TypeDescription * pTypeDescr );
199 :
200 : /** Clears this any. If the any already contains a value, that value will be destructed
201 : and its memory freed. After this has been called, the any does not contain a value.
202 : */
203 : inline void SAL_CALL clear();
204 :
205 : /** Tests whether this any is extractable to a value of given type.
206 : Widening conversion without data loss is taken into account.
207 :
208 : @param rType destination type
209 : @return true if this any is extractable to value of given type (e.g. using >>= operator)
210 : */
211 : inline bool SAL_CALL isExtractableTo( const Type & rType ) const;
212 :
213 : /** Tests whether this any can provide a value of specified type.
214 : Widening conversion without data loss is taken into account.
215 :
216 : @return true if this any can provide a value of specified type
217 : (e.g. using >>= operator)
218 : */
219 : template <typename T>
220 : inline bool has() const;
221 :
222 : /** Equality operator: compares two anys.
223 : The values need not be of equal type, e.g. a short integer is compared to a long integer.
224 :
225 : @param rAny another any (right side)
226 : @return true if both any contains equal values
227 : */
228 : inline bool SAL_CALL operator == ( const Any & rAny ) const;
229 : /** Unequality operator: compares two anys.
230 : The values need not be of equal type, e.g. a short integer is compared to a long integer.
231 :
232 : @param rAny another any (right side)
233 : @return true if both any contains unequal values
234 : */
235 : inline bool SAL_CALL operator != ( const Any & rAny ) const;
236 :
237 : private:
238 : /// @cond INTERNAL
239 : // Forbid use with ambiguous type (sal_Unicode, sal_uInt16):
240 : explicit Any(sal_uInt16) SAL_DELETED_FUNCTION;
241 : /// @endcond
242 : };
243 :
244 : /// @cond INTERNAL
245 : // Forbid use with ambiguous type (sal_Unicode, sal_uInt16):
246 : template<> sal_uInt16 Any::get<sal_uInt16>() const SAL_DELETED_FUNCTION;
247 : template<> bool Any::has<sal_uInt16>() const SAL_DELETED_FUNCTION;
248 : /// @endcond
249 :
250 : /** Template function to generically construct an any from a C++ value.
251 :
252 : @tparam C value type
253 : @param value a value
254 : @return an any
255 : */
256 : template< class C >
257 : inline Any SAL_CALL makeAny( const C & value );
258 :
259 : // additionally specialized for C++ bool
260 : template<>
261 : inline Any SAL_CALL makeAny( bool const & value );
262 :
263 : template<> Any SAL_CALL makeAny(Any const &) SAL_DELETED_FUNCTION;
264 :
265 : /** Wrap a value in an Any, if necessary.
266 :
267 : The difference to makeAny is that makeAny cannot be called on an Any, while
268 : toAny just returns the given Any.
269 :
270 : @since LibreOffice 5.0
271 : */
272 : template<typename T> inline Any toAny(T const & value);
273 :
274 : template<> inline Any toAny(Any const & value);
275 :
276 : class BaseReference;
277 :
278 : /** Template binary <<= operator to set the value of an any.
279 :
280 : @tparam C value type
281 : @param rAny destination any (left side)
282 : @param value source value (right side)
283 : */
284 : template< class C >
285 : inline void SAL_CALL operator <<= ( Any & rAny, const C & value );
286 :
287 : // additionally for C++ bool:
288 : template<>
289 : inline void SAL_CALL operator <<= ( Any & rAny, bool const & value );
290 :
291 : /** Template binary >>= operator to assign a value from an any.
292 : If the any does not contain a value that can be assigned without data loss, then this
293 : operation will fail returning false.
294 :
295 : @tparam C value type
296 : @param rAny source any (left side)
297 : @param value destination value (right side)
298 : @return true if assignment was possible without data loss
299 : */
300 : template< class C >
301 : inline bool SAL_CALL operator >>= ( const Any & rAny, C & value );
302 :
303 : /** Template equality operator: compares set value of left side any to right side value.
304 : The values need not be of equal type, e.g. a short integer is compared to a long integer.
305 : This operator can be implemented as template member function, if all supported compilers
306 : can cope with template member functions.
307 :
308 : @tparam C value type
309 : @param rAny another any (left side)
310 : @param value a value (right side)
311 : @return true if values are equal, false otherwise
312 : */
313 : template< class C >
314 : inline bool SAL_CALL operator == ( const Any & rAny, const C & value );
315 : /** Template unequality operator: compares set value of left side any to right side value.
316 : The values need not be of equal type, e.g. a short integer is compared to a long integer.
317 : This operator can be implemented as template member function, if all supported compilers
318 : can cope with template member functions.
319 :
320 : @tparam C value type
321 : @param rAny another any (left side)
322 : @param value a value (right side)
323 : @return true if values are unequal, false otherwise
324 : */
325 : template< class C >
326 : inline bool SAL_CALL operator != ( const Any & rAny, const C & value );
327 :
328 : // additional specialized >>= and == operators
329 : // bool
330 : template<>
331 : inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Bool & value );
332 : template<>
333 : inline bool SAL_CALL operator == ( const Any & rAny, const sal_Bool & value );
334 : template<>
335 : inline bool SAL_CALL operator >>= ( Any const & rAny, bool & value );
336 : template<>
337 : inline bool SAL_CALL operator == ( Any const & rAny, bool const & value );
338 : // byte
339 : template<>
340 : inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Int8 & value );
341 : // short
342 : template<>
343 : inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Int16 & value );
344 : template<>
345 : inline bool SAL_CALL operator >>= ( const Any & rAny, sal_uInt16 & value );
346 : // long
347 : template<>
348 : inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Int32 & value );
349 : template<>
350 : inline bool SAL_CALL operator >>= ( const Any & rAny, sal_uInt32 & value );
351 : // hyper
352 : template<>
353 : inline bool SAL_CALL operator >>= ( const Any & rAny, sal_Int64 & value );
354 : template<>
355 : inline bool SAL_CALL operator >>= ( const Any & rAny, sal_uInt64 & value );
356 : // float
357 : template<>
358 : inline bool SAL_CALL operator >>= ( const Any & rAny, float & value );
359 : // double
360 : template<>
361 : inline bool SAL_CALL operator >>= ( const Any & rAny, double & value );
362 : // string
363 : template<>
364 : inline bool SAL_CALL operator >>= ( const Any & rAny, ::rtl::OUString & value );
365 : template<>
366 : inline bool SAL_CALL operator == ( const Any & rAny, const ::rtl::OUString & value );
367 : // type
368 : template<>
369 : inline bool SAL_CALL operator >>= ( const Any & rAny, Type & value );
370 : template<>
371 : inline bool SAL_CALL operator == ( const Any & rAny, const Type & value );
372 : // any
373 : template<>
374 : inline bool SAL_CALL operator >>= ( const Any & rAny, Any & value );
375 : // interface
376 : template<>
377 : inline bool SAL_CALL operator == ( const Any & rAny, const BaseReference & value );
378 :
379 : }
380 : }
381 : }
382 : }
383 :
384 : /** Gets the meta type of IDL type any.
385 :
386 : There are cases (involving templates) where uses of getCppuType are known to
387 : not compile. Use cppu::UnoType or cppu::getTypeFavourUnsigned instead.
388 :
389 : The dummy parameter is just a typed pointer for function signature.
390 :
391 : @return type of IDL type any
392 :
393 : @deprecated
394 : Use cppu::UnoType instead.
395 : */
396 : SAL_DEPRECATED("use cppu::UnoType")
397 : inline const ::com::sun::star::uno::Type & SAL_CALL getCppuType( SAL_UNUSED_PARAMETER const ::com::sun::star::uno::Any * )
398 : {
399 : return ::cppu::UnoType< ::com::sun::star::uno::Any >::get();
400 : }
401 :
402 : #endif
403 :
404 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|