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_CPPUHELPER_PROPTYPEHLP_HXX
20 #define INCLUDED_CPPUHELPER_PROPTYPEHLP_HXX
22 #include <cppuhelper/proptypehlp.h>
23 #include <com/sun/star/lang/IllegalArgumentException.hpp>
24 #include <com/sun/star/uno/TypeClass.hpp>
29 /** Converts the value stored in an any to a concrete C++ type.
30 The function does the same as the operator >>= () at the
31 Any class, except that it throws an IllegalArgumentException in case of
32 failures (the value cannot be extracted without data loss )
34 @exception com::sun::star::lang::IllegalArgumentException when the type could not be converted.
36 template < class target
>
37 inline void SAL_CALL
convertPropertyValue( target
&value
, const ::com::sun::star::uno::Any
& a
)
40 if( !( a
>>= value
) ) {
41 throw ::com::sun::star::lang::IllegalArgumentException();
46 // This template is needed at least for msci4 compiler
47 template < class target
>
48 inline void SAL_CALL
convertPropertyValue( target
&value
, ::com::sun::star::uno::Any
& a
)
50 convertPropertyValue( value
, (const ::com::sun::star::uno::Any
& ) a
);
54 conversion of basic types
56 inline void SAL_CALL
convertPropertyValue( sal_Bool
& b
, const ::com::sun::star::uno::Any
& a
)
58 const enum ::com::sun::star::uno::TypeClass tc
= a
.getValueType().getTypeClass();
60 if( ::com::sun::star::uno::TypeClass_LONG
== tc
) {
65 else if ( ::com::sun::star::uno::TypeClass_CHAR
== tc
) {
66 sal_Unicode c
= *static_cast<sal_Unicode
const *>(a
.getValue());
69 else if ( ::com::sun::star::uno::TypeClass_SHORT
== tc
) {
74 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN
== tc
) {
75 b
= *static_cast<sal_Bool
const *>(a
.getValue());
77 else if ( ::com::sun::star::uno::TypeClass_BYTE
== tc
) {
82 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT
== tc
) {
87 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG
== tc
) {
93 throw ::com::sun::star::lang::IllegalArgumentException();
97 inline void SAL_CALL
convertPropertyValue( sal_Int64
& i
, const ::com::sun::star::uno::Any
& a
)
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
= *static_cast<sal_Unicode
const *>(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
= *static_cast<sal_Bool
const *>(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
)
152 const enum ::com::sun::star::uno::TypeClass tc
= a
.getValueType().getTypeClass();
154 if( ::com::sun::star::uno::TypeClass_UNSIGNED_HYPER
== tc
) {
157 if( ::com::sun::star::uno::TypeClass_HYPER
== tc
) {
160 i
= ( sal_uInt64
) i64
;
162 else if( ::com::sun::star::uno::TypeClass_LONG
== tc
) {
165 i
= ( sal_uInt64
)i32
;
167 else if ( ::com::sun::star::uno::TypeClass_CHAR
== tc
) {
169 c
= *static_cast<sal_Unicode
const *>(a
.getValue());
170 i
= ( sal_uInt64
) c
;
172 else if ( ::com::sun::star::uno::TypeClass_SHORT
== tc
) {
175 i
= ( sal_uInt64
) i16
;
177 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN
== tc
) {
179 b
= *static_cast<sal_Bool
const *>(a
.getValue());
180 i
= ( sal_uInt64
) b
;
182 else if ( ::com::sun::star::uno::TypeClass_BYTE
== tc
) {
185 i
= ( sal_uInt64
) i8
;
187 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT
== tc
) {
190 i
= ( sal_uInt64
) i16
;
192 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG
== tc
) {
195 i
= ( sal_uInt64
) i32
;
198 throw ::com::sun::star::lang::IllegalArgumentException();
204 inline void SAL_CALL
convertPropertyValue( sal_Int32
& i
, const ::com::sun::star::uno::Any
& a
)
206 const enum ::com::sun::star::uno::TypeClass tc
= a
.getValueType().getTypeClass();
208 if( ::com::sun::star::uno::TypeClass_LONG
== tc
) {
211 else if ( ::com::sun::star::uno::TypeClass_CHAR
== tc
) {
213 c
= *static_cast<sal_Unicode
const *>(a
.getValue());
216 else if ( ::com::sun::star::uno::TypeClass_SHORT
== tc
) {
219 i
= ( sal_Int32
) i16
;
221 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN
== tc
) {
223 b
= *static_cast<sal_Bool
const *>(a
.getValue());
226 else if ( ::com::sun::star::uno::TypeClass_BYTE
== tc
) {
229 i
= ( sal_Int32
) i8
;
231 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT
== tc
) {
234 i
= ( sal_Int32
) i16
;
236 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG
== tc
) {
239 i
= ( sal_Int32
) i32
;
242 throw ::com::sun::star::lang::IllegalArgumentException();
246 inline void SAL_CALL
convertPropertyValue( sal_uInt32
& i
, const ::com::sun::star::uno::Any
& a
)
248 const enum ::com::sun::star::uno::TypeClass tc
= a
.getValueType().getTypeClass();
250 if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG
== tc
) {
253 else if( ::com::sun::star::uno::TypeClass_LONG
== tc
) {
256 i
= (sal_uInt32
) i32
;
258 else if ( ::com::sun::star::uno::TypeClass_CHAR
== tc
) {
260 c
= *static_cast<sal_Unicode
const *>(a
.getValue());
261 i
= ( sal_uInt32
) c
;
263 else if ( ::com::sun::star::uno::TypeClass_SHORT
== tc
) {
266 i
= ( sal_uInt32
) i16
;
268 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN
== tc
) {
270 b
= *static_cast<sal_Bool
const *>(a
.getValue());
271 i
= ( sal_uInt32
) b
;
273 else if ( ::com::sun::star::uno::TypeClass_BYTE
== tc
) {
276 i
= ( sal_uInt32
) i8
;
278 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT
== tc
) {
281 i
= ( sal_uInt32
) i16
;
284 throw ::com::sun::star::lang::IllegalArgumentException();
289 inline void SAL_CALL
convertPropertyValue( sal_Int16
& i
, const ::com::sun::star::uno::Any
& a
)
291 const enum ::com::sun::star::uno::TypeClass tc
= a
.getValueType().getTypeClass();
293 if ( ::com::sun::star::uno::TypeClass_SHORT
== tc
) {
296 else if ( ::com::sun::star::uno::TypeClass_CHAR
== tc
) {
298 c
= *static_cast<sal_Unicode
const *>(a
.getValue());
301 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN
== tc
) {
303 b
= *static_cast<sal_Bool
const *>(a
.getValue());
306 else if ( ::com::sun::star::uno::TypeClass_BYTE
== tc
) {
309 i
= ( sal_Int16
) i8
;
311 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT
== tc
) {
314 i
= ( sal_Int16
) i16
;
317 throw ::com::sun::star::lang::IllegalArgumentException();
321 inline void SAL_CALL
convertPropertyValue( sal_uInt16
& i
, const ::com::sun::star::uno::Any
& a
)
323 const enum ::com::sun::star::uno::TypeClass tc
= a
.getValueType().getTypeClass();
325 if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT
== tc
) {
328 else if ( ::com::sun::star::uno::TypeClass_CHAR
== tc
) {
330 c
= *static_cast<sal_Unicode
const *>(a
.getValue());
333 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN
== tc
) {
335 b
= *static_cast<sal_Bool
const *>(a
.getValue());
338 else if ( ::com::sun::star::uno::TypeClass_BYTE
== tc
) {
341 i
= ( sal_Int16
) i8
;
343 else if ( ::com::sun::star::uno::TypeClass_SHORT
== tc
) {
346 i
= ( sal_Int16
) i16
;
349 throw ::com::sun::star::lang::IllegalArgumentException();
353 inline void SAL_CALL
convertPropertyValue( sal_Int8
& i
, const ::com::sun::star::uno::Any
& a
)
355 const enum ::com::sun::star::uno::TypeClass tc
= a
.getValueType().getTypeClass();
357 if ( ::com::sun::star::uno::TypeClass_BYTE
== tc
) {
360 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN
== tc
) {
362 b
= *static_cast<sal_Bool
const *>(a
.getValue());
366 throw ::com::sun::star::lang::IllegalArgumentException();
370 inline void SAL_CALL
convertPropertyValue( float &f
, const ::com::sun::star::uno::Any
&a
)
372 const enum ::com::sun::star::uno::TypeClass tc
= a
.getValueType().getTypeClass();
374 if ( ::com::sun::star::uno::TypeClass_FLOAT
== tc
) {
377 else if( ::com::sun::star::uno::TypeClass_DOUBLE
== tc
) {
382 else if( ::com::sun::star::uno::TypeClass_HYPER
== tc
) {
387 // msci 4 does not support this conversion
388 /* else if( ::com::sun::star::uno::TypeClass_UNSIGNED_HYPER == tc ) {
393 */ else if( ::com::sun::star::uno::TypeClass_LONG
== tc
) {
398 else if ( ::com::sun::star::uno::TypeClass_CHAR
== tc
) {
400 c
= *static_cast<sal_Unicode
const *>(a
.getValue());
403 else if ( ::com::sun::star::uno::TypeClass_SHORT
== tc
) {
408 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN
== tc
) {
410 b
= *static_cast<sal_Bool
const *>(a
.getValue());
413 else if ( ::com::sun::star::uno::TypeClass_BYTE
== tc
) {
418 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT
== tc
) {
423 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG
== tc
) {
429 throw ::com::sun::star::lang::IllegalArgumentException();
434 inline void SAL_CALL
convertPropertyValue( double &d
, const ::com::sun::star::uno::Any
&a
)
436 const enum ::com::sun::star::uno::TypeClass tc
= a
.getValueType().getTypeClass();
438 if( ::com::sun::star::uno::TypeClass_DOUBLE
== tc
) {
443 else if ( ::com::sun::star::uno::TypeClass_FLOAT
== tc
) {
448 else if( ::com::sun::star::uno::TypeClass_HYPER
== tc
) {
453 // msci 4 does not support this
454 /* else if( ::com::sun::star::uno::TypeClass_UNSIGNED_HYPER == tc ) {
459 */ else if( ::com::sun::star::uno::TypeClass_LONG
== tc
) {
464 else if ( ::com::sun::star::uno::TypeClass_CHAR
== tc
) {
466 c
= *static_cast<sal_Unicode
const *>(a
.getValue());
469 else if ( ::com::sun::star::uno::TypeClass_SHORT
== tc
) {
474 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN
== tc
) {
476 b
= *static_cast<sal_Bool
const *>(a
.getValue());
479 else if ( ::com::sun::star::uno::TypeClass_BYTE
== tc
) {
484 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT
== tc
) {
489 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG
== tc
) {
495 throw ::com::sun::star::lang::IllegalArgumentException();
499 inline void SAL_CALL
convertPropertyValue( ::rtl::OUString
&ow
, const ::com::sun::star::uno::Any
&a
)
501 if( ::com::sun::star::uno::TypeClass_STRING
== a
.getValueType().getTypeClass() ) {
505 throw ::com::sun::star::lang::IllegalArgumentException();
509 } // end namespace cppu
514 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */