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 _CPPUHELPER_PROPTYPEHLP_HXX
20 #define _CPPUHELPER_PROPTYPEHLP_HXX
22 #include <cppuhelper/proptypehlp.h>
27 /** Converts the value stored in an any to a concrete C++ type.
28 The function does the same as the operator >>= () at the
29 Any class, except that it throws an IllegalArgumentException in case of
30 failures (the value cannot be extracted without data loss )
32 @exception com::sun::star::lang::IllegalArgumentException when the type could not be converted.
34 template < class target
>
35 inline void SAL_CALL
convertPropertyValue( target
&value
, const ::com::sun::star::uno::Any
& a
)
38 if( !( a
>>= value
) ) {
39 throw ::com::sun::star::lang::IllegalArgumentException();
44 // This template is needed at least for msci4 compiler
45 template < class target
>
46 inline void SAL_CALL
convertPropertyValue( target
&value
, ::com::sun::star::uno::Any
& a
)
48 convertPropertyValue( value
, (const ::com::sun::star::uno::Any
& ) a
);
52 conversion of basic types
54 inline void SAL_CALL
convertPropertyValue( sal_Bool
& b
, const ::com::sun::star::uno::Any
& a
)
55 SAL_THROW( (::com::sun::star::lang::IllegalArgumentException
) )
57 const enum ::com::sun::star::uno::TypeClass tc
= a
.getValueType().getTypeClass();
59 if( ::com::sun::star::uno::TypeClass_LONG
== tc
) {
64 else if ( ::com::sun::star::uno::TypeClass_CHAR
== tc
) {
65 sal_Unicode c
= *(sal_Unicode
*) a
.getValue();
68 else if ( ::com::sun::star::uno::TypeClass_SHORT
== tc
) {
73 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN
== tc
) {
74 b
= *((sal_Bool
*)a
.getValue());
76 else if ( ::com::sun::star::uno::TypeClass_BYTE
== tc
) {
81 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT
== tc
) {
86 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG
== tc
) {
92 throw ::com::sun::star::lang::IllegalArgumentException();
96 inline void SAL_CALL
convertPropertyValue( sal_Int64
& i
, const ::com::sun::star::uno::Any
& a
)
97 SAL_THROW( (::com::sun::star::lang::IllegalArgumentException
) )
99 const enum ::com::sun::star::uno::TypeClass tc
= a
.getValueType().getTypeClass();
101 if( ::com::sun::star::uno::TypeClass_HYPER
== tc
) {
104 else if( ::com::sun::star::uno::TypeClass_UNSIGNED_HYPER
== tc
) {
107 i
= ( sal_Int64
) i64
;
109 else if( ::com::sun::star::uno::TypeClass_LONG
== tc
) {
112 i
= ( sal_Int64
)i32
;
114 else if ( ::com::sun::star::uno::TypeClass_CHAR
== tc
) {
116 c
= *(sal_Unicode
*)a
.getValue();
119 else if ( ::com::sun::star::uno::TypeClass_SHORT
== tc
) {
122 i
= ( sal_Int64
) i16
;
124 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN
== tc
) {
126 b
= *((sal_Bool
* )a
.getValue());
129 else if ( ::com::sun::star::uno::TypeClass_BYTE
== tc
) {
132 i
= ( sal_Int64
) i8
;
134 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT
== tc
) {
137 i
= ( sal_Int64
) i16
;
139 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG
== tc
) {
142 i
= ( sal_Int64
) i32
;
145 throw ::com::sun::star::lang::IllegalArgumentException();
150 inline void SAL_CALL
convertPropertyValue( sal_uInt64
& i
, const ::com::sun::star::uno::Any
& a
)
151 SAL_THROW( (::com::sun::star::lang::IllegalArgumentException
) )
153 const enum ::com::sun::star::uno::TypeClass tc
= a
.getValueType().getTypeClass();
155 if( ::com::sun::star::uno::TypeClass_UNSIGNED_HYPER
== tc
) {
158 if( ::com::sun::star::uno::TypeClass_HYPER
== tc
) {
161 i
= ( sal_uInt64
) i64
;
163 else if( ::com::sun::star::uno::TypeClass_LONG
== tc
) {
166 i
= ( sal_uInt64
)i32
;
168 else if ( ::com::sun::star::uno::TypeClass_CHAR
== tc
) {
170 c
= *( sal_Unicode
* ) a
.getValue() ;
171 i
= ( sal_uInt64
) c
;
173 else if ( ::com::sun::star::uno::TypeClass_SHORT
== tc
) {
176 i
= ( sal_uInt64
) i16
;
178 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN
== tc
) {
180 b
= *((sal_Bool
* )a
.getValue());
181 i
= ( sal_uInt64
) b
;
183 else if ( ::com::sun::star::uno::TypeClass_BYTE
== tc
) {
186 i
= ( sal_uInt64
) i8
;
188 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT
== tc
) {
191 i
= ( sal_uInt64
) i16
;
193 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG
== tc
) {
196 i
= ( sal_uInt64
) i32
;
199 throw ::com::sun::star::lang::IllegalArgumentException();
205 inline void SAL_CALL
convertPropertyValue( sal_Int32
& i
, const ::com::sun::star::uno::Any
& a
)
206 SAL_THROW( (::com::sun::star::lang::IllegalArgumentException
) )
208 const enum ::com::sun::star::uno::TypeClass tc
= a
.getValueType().getTypeClass();
210 if( ::com::sun::star::uno::TypeClass_LONG
== tc
) {
213 else if ( ::com::sun::star::uno::TypeClass_CHAR
== tc
) {
215 c
= *(sal_Unicode
*) a
.getValue();
218 else if ( ::com::sun::star::uno::TypeClass_SHORT
== tc
) {
221 i
= ( sal_Int32
) i16
;
223 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN
== tc
) {
225 b
= *((sal_Bool
* )a
.getValue());
228 else if ( ::com::sun::star::uno::TypeClass_BYTE
== tc
) {
231 i
= ( sal_Int32
) i8
;
233 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT
== tc
) {
236 i
= ( sal_Int32
) i16
;
238 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG
== tc
) {
241 i
= ( sal_Int32
) i32
;
244 throw ::com::sun::star::lang::IllegalArgumentException();
248 inline void SAL_CALL
convertPropertyValue( sal_uInt32
& i
, const ::com::sun::star::uno::Any
& a
)
249 SAL_THROW( (::com::sun::star::lang::IllegalArgumentException
) )
251 const enum ::com::sun::star::uno::TypeClass tc
= a
.getValueType().getTypeClass();
253 if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG
== tc
) {
256 else if( ::com::sun::star::uno::TypeClass_LONG
== tc
) {
259 i
= (sal_uInt32
) i32
;
261 else if ( ::com::sun::star::uno::TypeClass_CHAR
== tc
) {
263 c
= *(sal_Unicode
*) a
.getValue();
264 i
= ( sal_uInt32
) c
;
266 else if ( ::com::sun::star::uno::TypeClass_SHORT
== tc
) {
269 i
= ( sal_uInt32
) i16
;
271 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN
== tc
) {
273 b
= *((sal_Bool
* )a
.getValue());
274 i
= ( sal_uInt32
) b
;
276 else if ( ::com::sun::star::uno::TypeClass_BYTE
== tc
) {
279 i
= ( sal_uInt32
) i8
;
281 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT
== tc
) {
284 i
= ( sal_uInt32
) i16
;
287 throw ::com::sun::star::lang::IllegalArgumentException();
292 inline void SAL_CALL
convertPropertyValue( sal_Int16
& i
, const ::com::sun::star::uno::Any
& a
)
293 SAL_THROW( (::com::sun::star::lang::IllegalArgumentException
) )
295 const enum ::com::sun::star::uno::TypeClass tc
= a
.getValueType().getTypeClass();
297 if ( ::com::sun::star::uno::TypeClass_SHORT
== tc
) {
300 else if ( ::com::sun::star::uno::TypeClass_CHAR
== tc
) {
302 c
= *(sal_Unicode
*) a
.getValue();
305 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN
== tc
) {
307 b
= *((sal_Bool
* )a
.getValue());
310 else if ( ::com::sun::star::uno::TypeClass_BYTE
== tc
) {
313 i
= ( sal_Int16
) i8
;
315 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT
== tc
) {
318 i
= ( sal_Int16
) i16
;
321 throw ::com::sun::star::lang::IllegalArgumentException();
325 inline void SAL_CALL
convertPropertyValue( sal_uInt16
& i
, const ::com::sun::star::uno::Any
& a
)
326 SAL_THROW( (::com::sun::star::lang::IllegalArgumentException
) )
328 const enum ::com::sun::star::uno::TypeClass tc
= a
.getValueType().getTypeClass();
330 if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT
== tc
) {
333 else if ( ::com::sun::star::uno::TypeClass_CHAR
== tc
) {
335 c
= *(sal_Unicode
*) a
.getValue();
338 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN
== tc
) {
340 b
= *((sal_Bool
* )a
.getValue());
343 else if ( ::com::sun::star::uno::TypeClass_BYTE
== tc
) {
346 i
= ( sal_Int16
) i8
;
348 else if ( ::com::sun::star::uno::TypeClass_SHORT
== tc
) {
351 i
= ( sal_Int16
) i16
;
354 throw ::com::sun::star::lang::IllegalArgumentException();
358 inline void SAL_CALL
convertPropertyValue( sal_Int8
& i
, const ::com::sun::star::uno::Any
& a
)
359 SAL_THROW( (::com::sun::star::lang::IllegalArgumentException
) )
361 const enum ::com::sun::star::uno::TypeClass tc
= a
.getValueType().getTypeClass();
363 if ( ::com::sun::star::uno::TypeClass_BYTE
== tc
) {
366 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN
== tc
) {
368 b
= *((sal_Bool
* )a
.getValue());
372 throw ::com::sun::star::lang::IllegalArgumentException();
376 inline void SAL_CALL
convertPropertyValue( float &f
, const ::com::sun::star::uno::Any
&a
)
377 SAL_THROW( (::com::sun::star::lang::IllegalArgumentException
) )
379 const enum ::com::sun::star::uno::TypeClass tc
= a
.getValueType().getTypeClass();
381 if ( ::com::sun::star::uno::TypeClass_FLOAT
== tc
) {
384 else if( ::com::sun::star::uno::TypeClass_DOUBLE
== tc
) {
389 else if( ::com::sun::star::uno::TypeClass_HYPER
== tc
) {
394 // msci 4 does not support this conversion
395 /* else if( ::com::sun::star::uno::TypeClass_UNSIGNED_HYPER == tc ) {
400 */ else if( ::com::sun::star::uno::TypeClass_LONG
== tc
) {
405 else if ( ::com::sun::star::uno::TypeClass_CHAR
== tc
) {
407 c
= *(sal_Unicode
*) a
.getValue();
410 else if ( ::com::sun::star::uno::TypeClass_SHORT
== tc
) {
415 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN
== tc
) {
417 b
= *((sal_Bool
* )a
.getValue());
420 else if ( ::com::sun::star::uno::TypeClass_BYTE
== tc
) {
425 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT
== tc
) {
430 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG
== tc
) {
436 throw ::com::sun::star::lang::IllegalArgumentException();
441 inline void SAL_CALL
convertPropertyValue( double &d
, const ::com::sun::star::uno::Any
&a
)
442 SAL_THROW( (::com::sun::star::lang::IllegalArgumentException
) )
444 const enum ::com::sun::star::uno::TypeClass tc
= a
.getValueType().getTypeClass();
446 if( ::com::sun::star::uno::TypeClass_DOUBLE
== tc
) {
451 else if ( ::com::sun::star::uno::TypeClass_FLOAT
== tc
) {
456 else if( ::com::sun::star::uno::TypeClass_HYPER
== tc
) {
461 // msci 4 does not support this
462 /* else if( ::com::sun::star::uno::TypeClass_UNSIGNED_HYPER == tc ) {
467 */ else if( ::com::sun::star::uno::TypeClass_LONG
== tc
) {
472 else if ( ::com::sun::star::uno::TypeClass_CHAR
== tc
) {
474 c
= *(sal_Unicode
*) a
.getValue();
477 else if ( ::com::sun::star::uno::TypeClass_SHORT
== tc
) {
482 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN
== tc
) {
484 b
= *((sal_Bool
* )a
.getValue());
487 else if ( ::com::sun::star::uno::TypeClass_BYTE
== tc
) {
492 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT
== tc
) {
497 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG
== tc
) {
503 throw ::com::sun::star::lang::IllegalArgumentException();
507 inline void SAL_CALL
convertPropertyValue( ::rtl::OUString
&ow
, const ::com::sun::star::uno::Any
&a
)
508 SAL_THROW( (::com::sun::star::lang::IllegalArgumentException
) )
510 if( ::com::sun::star::uno::TypeClass_STRING
== a
.getValueType().getTypeClass() ) {
514 throw ::com::sun::star::lang::IllegalArgumentException();
518 } // end namespace cppu
523 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */