LibreOffice
LibreOffice 24.2 SDK C/C++ API Reference
proptypehlp.hxx
Go to the documentation of this file.
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 
20 /*
21  * This file is part of LibreOffice published API.
22  */
23 
24 #ifndef INCLUDED_CPPUHELPER_PROPTYPEHLP_HXX
25 #define INCLUDED_CPPUHELPER_PROPTYPEHLP_HXX
26 
27 #include "cppuhelper/proptypehlp.h"
28 #include "com/sun/star/lang/IllegalArgumentException.hpp"
29 #include "com/sun/star/uno/TypeClass.hpp"
30 
31 namespace cppu
32 {
33 
34 template < class target >
35 inline void SAL_CALL convertPropertyValue( target &value , const css::uno::Any & a)
36 {
37 
38  if( !( a >>= value ) ) {
39  throw css::lang::IllegalArgumentException();
40  }
41 }
42 
43 void convertPropertyValue(bool & b, const css::uno::Any & a)
44 {
45  if( a >>= b )
46  return;
47 
48  switch( a.getValueType().getTypeClass() ) {
49  case css::uno::TypeClass_BYTE:
50  b = a.get<sal_Int8>() != 0;
51  break;
52  case css::uno::TypeClass_SHORT:
53  b = a.get<sal_Int16>() != 0;
54  break;
55  case css::uno::TypeClass_UNSIGNED_SHORT:
56  {
57  sal_uInt16 i16 = 0;
58  a >>= i16;
59  b = i16 != 0;
60  break;
61  }
62  case css::uno::TypeClass_LONG:
63  b = a.get<sal_Int32>() != 0;
64  break;
65  case css::uno::TypeClass_UNSIGNED_LONG:
66  b = a.get<sal_uInt32>() != 0;
67  break;
68  case css::uno::TypeClass_CHAR:
69  {
70  sal_Unicode c = *static_cast<sal_Unicode const *>(a.getValue());
71  b = c != 0;
72  break;
73  }
74  default:
75  throw css::lang::IllegalArgumentException();
76  }
77 }
78 
79 void convertPropertyValue(sal_Bool & target, css::uno::Any const & source) {
80  bool b;
81  convertPropertyValue(b, source);
82  target = b;
83 }
84 
85 inline void SAL_CALL convertPropertyValue( sal_Int64 & i , const css::uno::Any & a )
86 {
87  if( !(a >>= i) ) {
88  switch( a.getValueType().getTypeClass() ) {
89  case css::uno::TypeClass_BOOLEAN:
90  i = static_cast<sal_Int64>(a.get<bool>());
91  break;
92  case css::uno::TypeClass_CHAR:
93  {
94  sal_Unicode c;
95  c = *static_cast<sal_Unicode const *>(a.getValue());
96  i = static_cast<sal_Int64>(c);
97  break;
98  }
99  default:
100  throw css::lang::IllegalArgumentException();
101  }
102  }
103 }
104 
105 
106 inline void SAL_CALL convertPropertyValue( sal_uInt64 & i , const css::uno::Any & a )
107 {
108  if( !(a >>= i) ) {
109  switch( a.getValueType().getTypeClass() ) {
110  case css::uno::TypeClass_BOOLEAN:
111  i = static_cast<sal_uInt64>(a.get<bool>());
112  break;
113  case css::uno::TypeClass_CHAR:
114  {
115  sal_Unicode c;
116  c = *static_cast<sal_Unicode const *>(a.getValue());
117  i = static_cast<sal_uInt64>(c);
118  break;
119  }
120  default:
121  throw css::lang::IllegalArgumentException();
122  }
123  }
124 }
125 
126 inline void SAL_CALL convertPropertyValue( sal_Int32 & i , const css::uno::Any & a )
127 {
128  if( !(a >>= i) ) {
129  switch( a.getValueType().getTypeClass() ) {
130  case css::uno::TypeClass_BOOLEAN:
131  i = static_cast<sal_Int32>(a.get<bool>());
132  break;
133  case css::uno::TypeClass_CHAR:
134  {
135  sal_Unicode c;
136  c = *static_cast<sal_Unicode const *>(a.getValue());
137  i = static_cast<sal_Int32>(c);
138  break;
139  }
140  default:
141  throw css::lang::IllegalArgumentException();
142  }
143  }
144 }
145 
146 inline void SAL_CALL convertPropertyValue( sal_uInt32 & i , const css::uno::Any & a )
147 {
148  if( !(a >>= i) ) {
149  switch( a.getValueType().getTypeClass() ) {
150  case css::uno::TypeClass_BOOLEAN:
151  i = static_cast<sal_uInt32>(a.get<bool>());
152  break;
153  case css::uno::TypeClass_CHAR:
154  {
155  sal_Unicode c;
156  c = *static_cast<sal_Unicode const *>(a.getValue());
157  i = static_cast<sal_uInt32>(c);
158  break;
159  }
160  default:
161  throw css::lang::IllegalArgumentException();
162  }
163  }
164 }
165 
166 inline void SAL_CALL convertPropertyValue( sal_Int16 & i , const css::uno::Any & a )
167 {
168  if( !(a >>= i) ) {
169  switch( a.getValueType().getTypeClass() ) {
170  case css::uno::TypeClass_BOOLEAN:
171  i = static_cast<sal_Int16>(a.get<bool>());
172  break;
173  case css::uno::TypeClass_CHAR:
174  {
175  sal_Unicode c;
176  c = *static_cast<sal_Unicode const *>(a.getValue());
177  i = static_cast<sal_Int16>(c);
178  break;
179  }
180  default:
181  throw css::lang::IllegalArgumentException();
182  }
183  }
184 }
185 
186 inline void SAL_CALL convertPropertyValue( sal_uInt16 & i , const css::uno::Any & a )
187 {
188  if( !(a >>= i) ) {
189  switch( a.getValueType().getTypeClass() ) {
190  case css::uno::TypeClass_BOOLEAN:
191  i = static_cast<sal_uInt16>(a.get<bool>());
192  break;
193  case css::uno::TypeClass_CHAR:
194  {
195  sal_Unicode c;
196  c = *static_cast<sal_Unicode const *>(a.getValue());
197  i = static_cast<sal_Int16>(c);
198  break;
199  }
200  default:
201  throw css::lang::IllegalArgumentException();
202  }
203  }
204 }
205 
206 inline void SAL_CALL convertPropertyValue( sal_Int8 & i , const css::uno::Any & a )
207 {
208  if( !(a >>= i) ) {
209  switch( a.getValueType().getTypeClass() ) {
210  case css::uno::TypeClass_BOOLEAN:
211  i = static_cast<sal_Int8>(a.get<bool>());
212  break;
213  default:
214  throw css::lang::IllegalArgumentException();
215  }
216  }
217 }
218 
219 inline void SAL_CALL convertPropertyValue( float &f , const css::uno::Any &a )
220 {
221  if( a >>= f )
222  return;
223 
224  switch( a.getValueType().getTypeClass() ) {
225  case css::uno::TypeClass_BOOLEAN:
226  f = static_cast<float>(a.get<bool>());
227  break;
228  case css::uno::TypeClass_LONG:
229  f = static_cast<float>(a.get<sal_Int32>());
230  break;
231  case css::uno::TypeClass_UNSIGNED_LONG:
232  f = static_cast<float>(a.get<sal_uInt32>());
233  break;
234  case css::uno::TypeClass_HYPER:
235  f = static_cast<float>(a.get<sal_Int64>());
236  break;
237  case css::uno::TypeClass_UNSIGNED_HYPER:
238  f = static_cast<float>(a.get<sal_uInt64>());
239  break;
240  case css::uno::TypeClass_DOUBLE:
241  f = static_cast<float>(a.get<double>());
242  break;
243  case css::uno::TypeClass_CHAR:
244  {
245  sal_Unicode c;
246  c = *static_cast<sal_Unicode const *>(a.getValue());
247  f = static_cast<float>(c);
248  break;
249  }
250  default:
251  throw css::lang::IllegalArgumentException();
252  }
253 }
254 
255 inline void SAL_CALL convertPropertyValue( double &d , const css::uno::Any &a )
256 {
257  if( a >>= d )
258  return;
259 
260  switch( a.getValueType().getTypeClass() ) {
261  case css::uno::TypeClass_BOOLEAN:
262  d = static_cast<double>(a.get<bool>());
263  break;
264  case css::uno::TypeClass_HYPER:
265  d = static_cast<double>(a.get<sal_Int64>());
266  break;
267  case css::uno::TypeClass_UNSIGNED_HYPER:
268  d = static_cast<double>(a.get<sal_uInt64>());
269  break;
270  case css::uno::TypeClass_CHAR:
271  {
272  sal_Unicode c;
273  c = *static_cast<sal_Unicode const *>(a.getValue());
274  d = static_cast<double>(c);
275  break;
276  }
277  default:
278  throw css::lang::IllegalArgumentException();
279  }
280 }
281 
282 } // end namespace cppu
283 
284 #endif
285 
286 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void convertPropertyValue(target &value, const css::uno::Any &a)
Converts the value stored in an any to a concrete C++ type.
Definition: proptypehlp.hxx:35
signed char sal_Int8
Definition: types.h:43
sal_uInt16 sal_Unicode
Definition: types.h:123
unsigned char sal_Bool
Definition: types.h:38
Definition: Enterable.hxx:30