Branch libreoffice-5-0-4
[LibreOffice.git] / include / cppuhelper / proptypehlp.hxx
blobfeaa51ba36bc00c30448d7dbabbcc754c16755c2
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_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>
26 namespace cppu
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 );
53 /**
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 ) {
61 sal_Int32 i32 = 0;
62 a >>= i32;
63 b = i32 != 0;
65 else if ( ::com::sun::star::uno::TypeClass_CHAR == tc ) {
66 sal_Unicode c = *static_cast<sal_Unicode const *>(a.getValue());
67 b = c != 0;
69 else if ( ::com::sun::star::uno::TypeClass_SHORT == tc ) {
70 sal_Int16 i16 = 0;
71 a >>= i16;
72 b = i16 != 0;
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 ) {
78 sal_Int8 i8 = 0;
79 a >>= i8;
80 b = i8 != 0;
82 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT == tc ) {
83 sal_uInt16 i16 = 0;
84 a >>= i16;
85 b = i16 != 0;
87 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG == tc ) {
88 sal_uInt32 i32 = 0;
89 a >>= i32;
90 b = i32 != 0;
92 else {
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 ) {
102 a >>= i;
104 else if( ::com::sun::star::uno::TypeClass_UNSIGNED_HYPER == tc ) {
105 sal_uInt64 i64 = 0;
106 a >>= i64;
107 i = ( sal_Int64 ) i64;
109 else if( ::com::sun::star::uno::TypeClass_LONG == tc ) {
110 sal_Int32 i32 = 0;
111 a >>= i32;
112 i = ( sal_Int64 )i32;
114 else if ( ::com::sun::star::uno::TypeClass_CHAR == tc ) {
115 sal_Unicode c;
116 c = *static_cast<sal_Unicode const *>(a.getValue());
117 i = ( sal_Int64 ) c;
119 else if ( ::com::sun::star::uno::TypeClass_SHORT == tc ) {
120 sal_Int16 i16 = 0;
121 a >>= i16;
122 i = ( sal_Int64 ) i16;
124 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN == tc ) {
125 bool b;
126 b = *static_cast<sal_Bool const *>(a.getValue());
127 i = ( sal_Int64 ) b;
129 else if ( ::com::sun::star::uno::TypeClass_BYTE == tc ) {
130 sal_Int8 i8 = 0;
131 a >>= i8;
132 i = ( sal_Int64 ) i8;
134 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT == tc ) {
135 sal_uInt16 i16 = 0;
136 a >>= i16;
137 i = ( sal_Int64 ) i16;
139 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG == tc ) {
140 sal_uInt32 i32 = 0;
141 a >>= i32;
142 i = ( sal_Int64 ) i32;
144 else {
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 ) {
155 a >>= i;
157 if( ::com::sun::star::uno::TypeClass_HYPER == tc ) {
158 sal_Int64 i64;
159 a >>= i64;
160 i = ( sal_uInt64 ) i64;
162 else if( ::com::sun::star::uno::TypeClass_LONG == tc ) {
163 sal_Int32 i32;
164 a >>= i32;
165 i = ( sal_uInt64 )i32;
167 else if ( ::com::sun::star::uno::TypeClass_CHAR == tc ) {
168 sal_Unicode c;
169 c = *static_cast<sal_Unicode const *>(a.getValue());
170 i = ( sal_uInt64 ) c;
172 else if ( ::com::sun::star::uno::TypeClass_SHORT == tc ) {
173 sal_Int16 i16;
174 a >>= i16;
175 i = ( sal_uInt64 ) i16;
177 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN == tc ) {
178 bool b;
179 b = *static_cast<sal_Bool const *>(a.getValue());
180 i = ( sal_uInt64 ) b;
182 else if ( ::com::sun::star::uno::TypeClass_BYTE == tc ) {
183 sal_Int8 i8;
184 a >>= i8;
185 i = ( sal_uInt64 ) i8;
187 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT == tc ) {
188 sal_uInt16 i16;
189 a >>= i16;
190 i = ( sal_uInt64 ) i16;
192 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG == tc ) {
193 sal_uInt32 i32;
194 a >>= i32;
195 i = ( sal_uInt64 ) i32;
197 else {
198 throw ::com::sun::star::lang::IllegalArgumentException();
202 // the basic types
203 // sal_Int32
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 ) {
209 a >>= i;
211 else if ( ::com::sun::star::uno::TypeClass_CHAR == tc ) {
212 sal_Unicode c;
213 c = *static_cast<sal_Unicode const *>(a.getValue());
214 i = ( sal_Int32 ) c;
216 else if ( ::com::sun::star::uno::TypeClass_SHORT == tc ) {
217 sal_Int16 i16 = 0;
218 a >>= i16;
219 i = ( sal_Int32 ) i16;
221 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN == tc ) {
222 bool b;
223 b = *static_cast<sal_Bool const *>(a.getValue());
224 i = ( sal_Int32 ) b;
226 else if ( ::com::sun::star::uno::TypeClass_BYTE == tc ) {
227 sal_Int8 i8 = 0;
228 a >>= i8;
229 i = ( sal_Int32 ) i8;
231 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT == tc ) {
232 sal_uInt16 i16 = 0;
233 a >>= i16;
234 i = ( sal_Int32 ) i16;
236 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG == tc ) {
237 sal_uInt32 i32 = 0;
238 a >>= i32;
239 i = ( sal_Int32 ) i32;
241 else {
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 ) {
251 a >>= i;
253 else if( ::com::sun::star::uno::TypeClass_LONG == tc ) {
254 sal_Int32 i32;
255 a >>= i32;
256 i = (sal_uInt32 ) i32;
258 else if ( ::com::sun::star::uno::TypeClass_CHAR == tc ) {
259 sal_Unicode c;
260 c = *static_cast<sal_Unicode const *>(a.getValue());
261 i = ( sal_uInt32 ) c;
263 else if ( ::com::sun::star::uno::TypeClass_SHORT == tc ) {
264 sal_Int16 i16;
265 a >>= i16;
266 i = ( sal_uInt32 ) i16;
268 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN == tc ) {
269 bool b;
270 b = *static_cast<sal_Bool const *>(a.getValue());
271 i = ( sal_uInt32 ) b;
273 else if ( ::com::sun::star::uno::TypeClass_BYTE == tc ) {
274 sal_Int8 i8;
275 a >>= i8;
276 i = ( sal_uInt32 ) i8;
278 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT == tc ) {
279 sal_uInt16 i16;
280 a >>= i16;
281 i = ( sal_uInt32 ) i16;
283 else {
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 ) {
294 a >>= i;
296 else if ( ::com::sun::star::uno::TypeClass_CHAR == tc ) {
297 sal_Unicode c;
298 c = *static_cast<sal_Unicode const *>(a.getValue());
299 i = ( sal_Int16 ) c;
301 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN == tc ) {
302 bool b;
303 b = *static_cast<sal_Bool const *>(a.getValue());
304 i = ( sal_Int16 ) b;
306 else if ( ::com::sun::star::uno::TypeClass_BYTE == tc ) {
307 sal_Int8 i8 = 0;
308 a >>= i8;
309 i = ( sal_Int16 ) i8;
311 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT == tc ) {
312 sal_uInt16 i16 = 0;
313 a >>= i16;
314 i = ( sal_Int16 ) i16;
316 else {
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 ) {
326 a >>= i;
328 else if ( ::com::sun::star::uno::TypeClass_CHAR == tc ) {
329 sal_Unicode c;
330 c = *static_cast<sal_Unicode const *>(a.getValue());
331 i = ( sal_Int16 ) c;
333 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN == tc ) {
334 bool b;
335 b = *static_cast<sal_Bool const *>(a.getValue());
336 i = ( sal_Int16 ) b;
338 else if ( ::com::sun::star::uno::TypeClass_BYTE == tc ) {
339 sal_Int8 i8 = 0;
340 a >>= i8;
341 i = ( sal_Int16 ) i8;
343 else if ( ::com::sun::star::uno::TypeClass_SHORT == tc ) {
344 sal_Int16 i16 = 0;
345 a >>= i16;
346 i = ( sal_Int16 ) i16;
348 else {
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 ) {
358 a >>= i;
360 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN == tc ) {
361 bool b;
362 b = *static_cast<sal_Bool const *>(a.getValue());
363 i = ( sal_Int8 ) b;
365 else {
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 ) {
375 a >>= f;
377 else if( ::com::sun::star::uno::TypeClass_DOUBLE == tc ) {
378 double d = 0;
379 a >>= d;
380 f = ( float ) d;
382 else if( ::com::sun::star::uno::TypeClass_HYPER == tc ) {
383 sal_Int64 i64 = 0;
384 a >>= i64;
385 f = ( float ) i64;
387 // msci 4 does not support this conversion
388 /* else if( ::com::sun::star::uno::TypeClass_UNSIGNED_HYPER == tc ) {
389 sal_uInt64 i64;
390 a >>= i64;
391 f = ( float ) i64;
393 */ else if( ::com::sun::star::uno::TypeClass_LONG == tc ) {
394 sal_Int32 i32 = 0;
395 a >>= i32;
396 f = ( float )i32;
398 else if ( ::com::sun::star::uno::TypeClass_CHAR == tc ) {
399 sal_Unicode c;
400 c = *static_cast<sal_Unicode const *>(a.getValue());
401 f = ( float ) c;
403 else if ( ::com::sun::star::uno::TypeClass_SHORT == tc ) {
404 sal_Int16 i16 = 0;
405 a >>= i16;
406 f = ( float ) i16;
408 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN == tc ) {
409 bool b;
410 b = *static_cast<sal_Bool const *>(a.getValue());
411 f = ( float ) b;
413 else if ( ::com::sun::star::uno::TypeClass_BYTE == tc ) {
414 sal_Int8 i8 = 0;
415 a >>= i8;
416 f = ( float ) i8;
418 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT == tc ) {
419 sal_uInt16 i16 = 0;
420 a >>= i16;
421 f = ( float ) i16;
423 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG == tc ) {
424 sal_uInt32 i32 = 0;
425 a >>= i32;
426 f = ( float ) i32;
428 else {
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 ) {
439 float f;
440 a >>= f;
441 d = ( double ) f;
443 else if ( ::com::sun::star::uno::TypeClass_FLOAT == tc ) {
444 float f;
445 a >>= f;
446 d = (double) f;
448 else if( ::com::sun::star::uno::TypeClass_HYPER == tc ) {
449 sal_Int64 i64;
450 a >>= i64;
451 d = (double) i64;
453 // msci 4 does not support this
454 /* else if( ::com::sun::star::uno::TypeClass_UNSIGNED_HYPER == tc ) {
455 sal_uInt64 i64;
456 a >>= i64;
457 d = (double) i64;
459 */ else if( ::com::sun::star::uno::TypeClass_LONG == tc ) {
460 sal_Int32 i32;
461 a >>= i32;
462 d = (double)i32;
464 else if ( ::com::sun::star::uno::TypeClass_CHAR == tc ) {
465 sal_Unicode c;
466 c = *static_cast<sal_Unicode const *>(a.getValue());
467 d = (double) c;
469 else if ( ::com::sun::star::uno::TypeClass_SHORT == tc ) {
470 sal_Int16 i16;
471 a >>= i16;
472 d = (double) i16;
474 else if ( ::com::sun::star::uno::TypeClass_BOOLEAN == tc ) {
475 bool b;
476 b = *static_cast<sal_Bool const *>(a.getValue());
477 d = (double) b;
479 else if ( ::com::sun::star::uno::TypeClass_BYTE == tc ) {
480 sal_Int8 i8;
481 a >>= i8;
482 d = (double) i8;
484 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT == tc ) {
485 sal_uInt16 i16;
486 a >>= i16;
487 d = (double) i16;
489 else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG == tc ) {
490 sal_uInt32 i32;
491 a >>= i32;
492 d = (double) i32;
494 else {
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() ) {
502 a >>= ow;
504 else {
505 throw ::com::sun::star::lang::IllegalArgumentException();
509 } // end namespace cppu
511 #endif
514 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */