1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef _CPPUHELPER_PROPTYPEHLP_HXX
24 #define _CPPUHELPER_PROPTYPEHLP_HXX
25 
26 #include <cppuhelper/proptypehlp.h>
27 
28 namespace cppu
29 {
30 
31 /** Converts the value stored in an any to a concrete C++ type.
32     The function does the same as the operator >>= () at the
33     Any class, except that it throws an IllegalArgumentException in case of
34     failures (the value cannot be extracted without data loss )
35 
36    @exception com::sun::star::lang::IllegalArgumentException when the type could not be converted.
37  */
38 template < class target >
convertPropertyValue(target & value,const::com::sun::star::uno::Any & a)39 inline void SAL_CALL convertPropertyValue( target &value , const  ::com::sun::star::uno::Any & a)
40 {
41 
42 	if( !( a >>= value ) ) {
43 		throw ::com::sun::star::lang::IllegalArgumentException();
44 	}
45 }
46 
47 
48 // This template is needed at least for msci4 compiler
49 template < class target >
convertPropertyValue(target & value,::com::sun::star::uno::Any & a)50 inline void SAL_CALL convertPropertyValue( target &value ,  ::com::sun::star::uno::Any & a)
51 {
52 	convertPropertyValue( value ,  (const ::com::sun::star::uno::Any & )  a );
53 }
54 
55 /**
56   conversion of basic types
57 */
convertPropertyValue(sal_Bool & b,const::com::sun::star::uno::Any & a)58 inline void SAL_CALL convertPropertyValue( sal_Bool & b   , const ::com::sun::star::uno::Any & a )
59 	SAL_THROW( (::com::sun::star::lang::IllegalArgumentException) )
60 {
61 	const enum ::com::sun::star::uno::TypeClass tc = a.getValueType().getTypeClass();
62 
63 	if( ::com::sun::star::uno::TypeClass_LONG == tc ) {
64 		sal_Int32 i32 = 0;
65 		a >>= i32;
66 		b = ( sal_Bool )i32;
67 	}
68 	else if ( ::com::sun::star::uno::TypeClass_CHAR  == tc ) {
69 		sal_Unicode c = *(sal_Unicode*) a.getValue();
70 		b = ( sal_Bool ) c;
71 	}
72 	else if ( ::com::sun::star::uno::TypeClass_SHORT == tc ) {
73 		sal_Int16 i16 = 0;
74 		a >>= i16;
75 		b = ( sal_Bool ) i16;
76 	}
77 	else if ( ::com::sun::star::uno::TypeClass_BOOLEAN == tc ) {
78 		b = *((sal_Bool*)a.getValue());
79 	}
80 	else if ( ::com::sun::star::uno::TypeClass_BYTE == tc ) {
81 		sal_Int8 i8 = 0;
82 		a >>= i8;
83 		b = ( sal_Bool ) i8;
84 	}
85 	else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT == tc ) {
86 		sal_uInt16 i16 = 0;
87 		a >>= i16;
88 		b = ( sal_Bool	) i16;
89 	}
90 	else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG == tc ) {
91 		sal_uInt32 i32 = 0;
92 		a >>= i32;
93 		b = ( sal_Bool ) i32;
94 	}
95 	else {
96 		throw ::com::sun::star::lang::IllegalArgumentException();
97 	}
98 }
99 
convertPropertyValue(sal_Int64 & i,const::com::sun::star::uno::Any & a)100 inline void SAL_CALL convertPropertyValue( sal_Int64 & i  , const ::com::sun::star::uno::Any & a )
101 	SAL_THROW( (::com::sun::star::lang::IllegalArgumentException) )
102 {
103 	const enum ::com::sun::star::uno::TypeClass tc = a.getValueType().getTypeClass();
104 
105 	if( ::com::sun::star::uno::TypeClass_HYPER == tc ) {
106 		a >>= i;
107 	}
108 	else if( ::com::sun::star::uno::TypeClass_UNSIGNED_HYPER == tc ) {
109 		sal_uInt64 i64 = 0;
110 		a >>= i64;
111 		i = ( sal_Int64 ) i64;
112 	}
113 	else if( ::com::sun::star::uno::TypeClass_LONG == tc ) {
114 		sal_Int32 i32 = 0;
115 		a >>= i32;
116 		i = ( sal_Int64 )i32;
117 	}
118 	else if ( ::com::sun::star::uno::TypeClass_CHAR  == tc ) {
119 		sal_Unicode c;
120 		c = *(sal_Unicode *)a.getValue();
121 		i = ( sal_Int64 ) c;
122 	}
123 	else if ( ::com::sun::star::uno::TypeClass_SHORT == tc ) {
124 		sal_Int16 i16 = 0;
125 		a >>= i16;
126 		i = ( sal_Int64 ) i16;
127 	}
128 	else if ( ::com::sun::star::uno::TypeClass_BOOLEAN == tc ) {
129 		sal_Bool b;
130 		b =  *((sal_Bool * )a.getValue());
131 		i = ( sal_Int64 ) b;
132 	}
133 	else if ( ::com::sun::star::uno::TypeClass_BYTE == tc ) {
134 		sal_Int8 i8 = 0;
135 		a >>= i8;
136 		i = ( sal_Int64 ) i8;
137 	}
138 	else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT == tc ) {
139 		sal_uInt16 i16 = 0;
140 		a >>= i16;
141 		i = ( sal_Int64	) i16;
142 	}
143 	else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG == tc ) {
144 		sal_uInt32 i32 = 0;
145 		a >>= i32;
146 		i = ( sal_Int64 ) i32;
147 	}
148 	else {
149 		throw ::com::sun::star::lang::IllegalArgumentException();
150 	}
151 }
152 
153 
convertPropertyValue(sal_uInt64 & i,const::com::sun::star::uno::Any & a)154 inline void SAL_CALL convertPropertyValue( sal_uInt64 & i  , const ::com::sun::star::uno::Any & a )
155 	SAL_THROW( (::com::sun::star::lang::IllegalArgumentException) )
156 {
157 	const enum ::com::sun::star::uno::TypeClass tc = a.getValueType().getTypeClass();
158 
159 	if( ::com::sun::star::uno::TypeClass_UNSIGNED_HYPER == tc ) {
160 		a >>= i;
161 	}
162 	if( ::com::sun::star::uno::TypeClass_HYPER == tc ) {
163 		sal_Int64 i64;
164 		a >>= i64;
165 		i = ( sal_uInt64 ) i64;
166 	}
167 	else if( ::com::sun::star::uno::TypeClass_LONG == tc ) {
168 		sal_Int32 i32;
169 		a >>= i32;
170 		i = ( sal_uInt64 )i32;
171 	}
172 	else if ( ::com::sun::star::uno::TypeClass_CHAR  == tc ) {
173 		sal_Unicode c;
174 		c = *( sal_Unicode * ) a.getValue() ;
175 		i = ( sal_uInt64 ) c;
176 	}
177 	else if ( ::com::sun::star::uno::TypeClass_SHORT == tc ) {
178 		sal_Int16 i16;
179 		a >>= i16;
180 		i = ( sal_uInt64 ) i16;
181 	}
182 	else if ( ::com::sun::star::uno::TypeClass_BOOLEAN == tc ) {
183 		sal_Bool b;
184 		b =  *((sal_Bool * )a.getValue());
185 		i = ( sal_uInt64 ) b;
186 	}
187 	else if ( ::com::sun::star::uno::TypeClass_BYTE == tc ) {
188 		sal_Int8 i8;
189 		a >>= i8;
190 		i = ( sal_uInt64 ) i8;
191 	}
192 	else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT == tc ) {
193 		sal_uInt16 i16;
194 		a >>= i16;
195 		i = ( sal_uInt64	) i16;
196 	}
197 	else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG == tc ) {
198 		sal_uInt32 i32;
199 		a >>= i32;
200 		i = ( sal_uInt64 ) i32;
201 	}
202 	else {
203 		throw ::com::sun::star::lang::IllegalArgumentException();
204 	}
205 }
206 
207 // the basic types
208 // sal_Int32
convertPropertyValue(sal_Int32 & i,const::com::sun::star::uno::Any & a)209 inline void SAL_CALL convertPropertyValue( sal_Int32 & i  , const ::com::sun::star::uno::Any & a )
210 	SAL_THROW( (::com::sun::star::lang::IllegalArgumentException) )
211 {
212 	const enum ::com::sun::star::uno::TypeClass tc = a.getValueType().getTypeClass();
213 
214 	if( ::com::sun::star::uno::TypeClass_LONG == tc ) {
215 		a >>= i;
216 	}
217 	else if ( ::com::sun::star::uno::TypeClass_CHAR  == tc ) {
218 		sal_Unicode c;
219 		c = *(sal_Unicode*) a.getValue();
220 		i = ( sal_Int32 ) c;
221 	}
222 	else if ( ::com::sun::star::uno::TypeClass_SHORT == tc ) {
223 		sal_Int16 i16 = 0;
224 		a >>= i16;
225 		i = ( sal_Int32 ) i16;
226 	}
227 	else if ( ::com::sun::star::uno::TypeClass_BOOLEAN == tc ) {
228 		sal_Bool b;
229 		b =  *((sal_Bool * )a.getValue());
230 		i = ( sal_Int32 ) b;
231 	}
232 	else if ( ::com::sun::star::uno::TypeClass_BYTE == tc ) {
233 		sal_Int8 i8 = 0;
234 		a >>= i8;
235 		i = ( sal_Int32 ) i8;
236 	}
237 	else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT == tc ) {
238 		sal_uInt16 i16 = 0;
239 		a >>= i16;
240 		i = ( sal_Int32	) i16;
241 	}
242 	else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG == tc ) {
243 		sal_uInt32 i32 = 0;
244 		a >>= i32;
245 		i = ( sal_Int32 ) i32;
246 	}
247 	else {
248 		throw ::com::sun::star::lang::IllegalArgumentException();
249 	}
250 }
251 
convertPropertyValue(sal_uInt32 & i,const::com::sun::star::uno::Any & a)252 inline void SAL_CALL convertPropertyValue( sal_uInt32 & i  , const ::com::sun::star::uno::Any & a )
253 	SAL_THROW( (::com::sun::star::lang::IllegalArgumentException) )
254 {
255 	const enum ::com::sun::star::uno::TypeClass tc = a.getValueType().getTypeClass();
256 
257 	if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG == tc ) {
258 		a >>= i;
259 	}
260 	else if( ::com::sun::star::uno::TypeClass_LONG == tc ) {
261 		sal_Int32 i32;
262 		a >>= i32;
263 		i = (sal_uInt32 ) i32;
264 	}
265 	else if ( ::com::sun::star::uno::TypeClass_CHAR  == tc ) {
266 		sal_Unicode c;
267 		c = *(sal_Unicode*) a.getValue();
268 		i = ( sal_uInt32 ) c;
269 	}
270 	else if ( ::com::sun::star::uno::TypeClass_SHORT == tc ) {
271 		sal_Int16 i16;
272 		a >>= i16;
273 		i = ( sal_uInt32 ) i16;
274 	}
275 	else if ( ::com::sun::star::uno::TypeClass_BOOLEAN == tc ) {
276 		sal_Bool b;
277 		b =  *((sal_Bool * )a.getValue());
278 		i = ( sal_uInt32 ) b;
279 	}
280 	else if ( ::com::sun::star::uno::TypeClass_BYTE == tc ) {
281 		sal_Int8 i8;
282 		a >>= i8;
283 		i = ( sal_uInt32 ) i8;
284 	}
285 	else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT == tc ) {
286 		sal_uInt16 i16;
287 		a >>= i16;
288 		i = ( sal_uInt32	) i16;
289 	}
290 	else {
291 		throw ::com::sun::star::lang::IllegalArgumentException();
292 	}
293 }
294 
295 
convertPropertyValue(sal_Int16 & i,const::com::sun::star::uno::Any & a)296 inline void SAL_CALL convertPropertyValue( sal_Int16 & i  , const ::com::sun::star::uno::Any & a )
297 	SAL_THROW( (::com::sun::star::lang::IllegalArgumentException) )
298 {
299 	const enum ::com::sun::star::uno::TypeClass tc = a.getValueType().getTypeClass();
300 
301 	if ( ::com::sun::star::uno::TypeClass_SHORT == tc ) {
302 		a >>= i;
303 	}
304 	else if ( ::com::sun::star::uno::TypeClass_CHAR  == tc ) {
305 		sal_Unicode c;
306 		c = *(sal_Unicode*) a.getValue();
307 		i = ( sal_Int16 ) c;
308 	}
309 	else if ( ::com::sun::star::uno::TypeClass_BOOLEAN == tc ) {
310 		sal_Bool b;
311 		b =  *((sal_Bool * )a.getValue());
312 		i = ( sal_Int16 ) b;
313 	}
314 	else if ( ::com::sun::star::uno::TypeClass_BYTE == tc ) {
315 		sal_Int8 i8 = 0;
316 		a >>= i8;
317 		i = ( sal_Int16 ) i8;
318 	}
319 	else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT == tc ) {
320 		sal_uInt16 i16 = 0;
321 		a >>= i16;
322 		i = ( sal_Int16	) i16;
323 	}
324 	else {
325 		throw ::com::sun::star::lang::IllegalArgumentException();
326 	}
327 }
328 
convertPropertyValue(sal_uInt16 & i,const::com::sun::star::uno::Any & a)329 inline void SAL_CALL convertPropertyValue( sal_uInt16 & i  , const ::com::sun::star::uno::Any & a )
330 	SAL_THROW( (::com::sun::star::lang::IllegalArgumentException) )
331 {
332 	const enum ::com::sun::star::uno::TypeClass tc = a.getValueType().getTypeClass();
333 
334 	if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT == tc ) {
335 		a >>= i;
336 	}
337 	else if ( ::com::sun::star::uno::TypeClass_CHAR  == tc ) {
338 		sal_Unicode c;
339 		c = *(sal_Unicode *) a.getValue();
340 		i = ( sal_Int16 ) c;
341 	}
342 	else if ( ::com::sun::star::uno::TypeClass_BOOLEAN == tc ) {
343 		sal_Bool b;
344 		b =  *((sal_Bool * )a.getValue());
345 		i = ( sal_Int16 ) b;
346 	}
347 	else if ( ::com::sun::star::uno::TypeClass_BYTE == tc ) {
348 		sal_Int8 i8 = 0;
349 		a >>= i8;
350 		i = ( sal_Int16 ) i8;
351 	}
352 	else if ( ::com::sun::star::uno::TypeClass_SHORT == tc ) {
353 		sal_Int16 i16 = 0;
354 		a >>= i16;
355 		i = ( sal_Int16 ) i16;
356 	}
357 	else {
358 		throw ::com::sun::star::lang::IllegalArgumentException();
359 	}
360 }
361 
convertPropertyValue(sal_Int8 & i,const::com::sun::star::uno::Any & a)362 inline void SAL_CALL convertPropertyValue( sal_Int8 & i  , const ::com::sun::star::uno::Any & a )
363 	SAL_THROW( (::com::sun::star::lang::IllegalArgumentException) )
364 {
365 	const enum ::com::sun::star::uno::TypeClass tc = a.getValueType().getTypeClass();
366 
367 	if ( ::com::sun::star::uno::TypeClass_BYTE == tc ) {
368 		a >>= i;
369 	}
370 	else if ( ::com::sun::star::uno::TypeClass_BOOLEAN == tc ) {
371 		sal_Bool b;
372 		b =  *((sal_Bool * )a.getValue());
373 		i = ( sal_Int8 ) b;
374 	}
375 	else {
376 		throw ::com::sun::star::lang::IllegalArgumentException();
377 	}
378 }
379 
convertPropertyValue(float & f,const::com::sun::star::uno::Any & a)380 inline void SAL_CALL convertPropertyValue( float &f , const ::com::sun::star::uno::Any &a )
381 	SAL_THROW( (::com::sun::star::lang::IllegalArgumentException) )
382 {
383 	const enum ::com::sun::star::uno::TypeClass tc = a.getValueType().getTypeClass();
384 
385 	if ( ::com::sun::star::uno::TypeClass_FLOAT == tc ) {
386 		a >>= f;
387 	}
388 	else if( ::com::sun::star::uno::TypeClass_DOUBLE == tc ) {
389 	 	double d = 0;
390 	 	a >>= d;
391 	 	f = ( float ) d;
392 	}
393 	else if( ::com::sun::star::uno::TypeClass_HYPER == tc ) {
394 		sal_Int64 i64 = 0;
395 		a >>= i64;
396 		f = ( float ) i64;
397 	}
398 	// msci 4 does not support this conversion
399 /*	else if( ::com::sun::star::uno::TypeClass_UNSIGNED_HYPER == tc ) {
400 		sal_uInt64 i64;
401 		a >>= i64;
402 		f = ( float ) i64;
403 	}
404 */	else if( ::com::sun::star::uno::TypeClass_LONG == tc ) {
405 		sal_Int32 i32 = 0;
406 		a >>= i32;
407 		f = ( float )i32;
408 	}
409 	else if ( ::com::sun::star::uno::TypeClass_CHAR  == tc ) {
410 		sal_Unicode c;
411 		c = *(sal_Unicode*) a.getValue();
412 		f = ( float ) c;
413 	}
414 	else if ( ::com::sun::star::uno::TypeClass_SHORT == tc ) {
415 		sal_Int16 i16 = 0;
416 		a >>= i16;
417 		f = ( float ) i16;
418 	}
419 	else if ( ::com::sun::star::uno::TypeClass_BOOLEAN == tc ) {
420 		sal_Bool b;
421 		b =  *((sal_Bool * )a.getValue());
422 		f = ( float ) b;
423 	}
424 	else if ( ::com::sun::star::uno::TypeClass_BYTE == tc ) {
425 		sal_Int8 i8 = 0;
426 		a >>= i8;
427 		f = ( float ) i8;
428 	}
429 	else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT == tc ) {
430 		sal_uInt16 i16 = 0;
431 		a >>= i16;
432 		f = ( float	) i16;
433 	}
434 	else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG == tc ) {
435 		sal_uInt32 i32 = 0;
436 		a >>= i32;
437 		f = ( float ) i32;
438 	}
439 	else {
440 		throw ::com::sun::star::lang::IllegalArgumentException();
441 	}
442 }
443 
444 
convertPropertyValue(double & d,const::com::sun::star::uno::Any & a)445 inline void SAL_CALL convertPropertyValue( double &d , const ::com::sun::star::uno::Any &a )
446 	SAL_THROW( (::com::sun::star::lang::IllegalArgumentException) )
447 {
448 	const enum ::com::sun::star::uno::TypeClass tc = a.getValueType().getTypeClass();
449 
450 	if( ::com::sun::star::uno::TypeClass_DOUBLE == tc ) {
451 	 	float f;
452 	 	a >>= f;
453 	 	d = ( double ) f;
454 	}
455 	else if ( ::com::sun::star::uno::TypeClass_FLOAT == tc ) {
456 		float f;
457 		a >>= f;
458 		d = (double) f;
459 	}
460 	else if( ::com::sun::star::uno::TypeClass_HYPER == tc ) {
461 		sal_Int64 i64;
462 		a >>= i64;
463 		d = (double) i64;
464 	}
465 	// msci 4 does not support this
466 /*	else if( ::com::sun::star::uno::TypeClass_UNSIGNED_HYPER == tc ) {
467 		sal_uInt64 i64;
468 		a >>= i64;
469 		d = (double) i64;
470 	}
471 */	else if( ::com::sun::star::uno::TypeClass_LONG == tc ) {
472 		sal_Int32 i32;
473 		a >>= i32;
474 		d = (double)i32;
475 	}
476 	else if ( ::com::sun::star::uno::TypeClass_CHAR  == tc ) {
477 		sal_Unicode c;
478 		c = *(sal_Unicode*) a.getValue();
479 		d = (double) c;
480 	}
481 	else if ( ::com::sun::star::uno::TypeClass_SHORT == tc ) {
482 		sal_Int16 i16;
483 		a >>= i16;
484 		d = (double) i16;
485 	}
486 	else if ( ::com::sun::star::uno::TypeClass_BOOLEAN == tc ) {
487 		sal_Bool b;
488 		b =  *((sal_Bool * )a.getValue());
489 		d = (double) b;
490 	}
491 	else if ( ::com::sun::star::uno::TypeClass_BYTE == tc ) {
492 		sal_Int8 i8;
493 		a >>= i8;
494 		d = (double) i8;
495 	}
496 	else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT == tc ) {
497 		sal_uInt16 i16;
498 		a >>= i16;
499 		d = (double) i16;
500 	}
501 	else if ( ::com::sun::star::uno::TypeClass_UNSIGNED_LONG == tc ) {
502 		sal_uInt32 i32;
503 		a >>= i32;
504 		d = (double) i32;
505 	}
506 	else {
507 		throw ::com::sun::star::lang::IllegalArgumentException();
508 	}
509 }
510 
convertPropertyValue(::rtl::OUString & ow,const::com::sun::star::uno::Any & a)511 inline void SAL_CALL convertPropertyValue( ::rtl::OUString &ow , const ::com::sun::star::uno::Any &a )
512 	SAL_THROW( (::com::sun::star::lang::IllegalArgumentException) )
513 {
514 	if( ::com::sun::star::uno::TypeClass_STRING == a.getValueType().getTypeClass() ) {
515 		a >>= ow;
516 	}
517 	else {
518 		throw ::com::sun::star::lang::IllegalArgumentException();
519 	}
520 }
521 
522 }     // end namespace cppu
523 
524 #endif
525 
526 
527