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 _COMPHELPER_EXTRACT_HXX_
24 #define _COMPHELPER_EXTRACT_HXX_
25
26 #include <com/sun/star/lang/IllegalArgumentException.hpp>
27 #include <com/sun/star/uno/XInterface.hpp>
28 #include <com/sun/star/uno/TypeClass.hpp>
29 #include <com/sun/star/uno/Type.hxx>
30 #include <com/sun/star/uno/Any.hxx>
31 #include "cppu/unotype.hxx"
32
33 namespace cppu
34 {
35
36 /**
37 * Sets enum from int32 value. This function does NOT check for valid enum values!
38 *<BR>
39 * @param nEnum int32 enum value
40 * @param rType enum type
41 * @return enum or emoty any.
42 */
int2enum(sal_Int32 nEnum,const::com::sun::star::uno::Type & rType)43 inline ::com::sun::star::uno::Any SAL_CALL int2enum(
44 sal_Int32 nEnum, const ::com::sun::star::uno::Type & rType )
45 {
46 if (rType.getTypeClass() == ::com::sun::star::uno::TypeClass_ENUM)
47 {
48 int nVal = nEnum;
49 return ::com::sun::star::uno::Any( &nVal, rType );
50 }
51 return ::com::sun::star::uno::Any();
52 }
53
54 /**
55 * Sets int32 from enum or int in any.
56 *<BR>
57 * @param rnEnum [out] int32 enum value
58 * @param rAny enum or int
59 * @param sal_True if enum or int value was set else sal_False.
60 */
enum2int(sal_Int32 & rnEnum,const::com::sun::star::uno::Any & rAny)61 inline sal_Bool SAL_CALL enum2int( sal_Int32 & rnEnum, const ::com::sun::star::uno::Any & rAny )
62 {
63 if (rAny.getValueTypeClass() == ::com::sun::star::uno::TypeClass_ENUM)
64 {
65 rnEnum = * reinterpret_cast< const int * >( rAny.getValue() );
66 return sal_True;
67 }
68
69 return rAny >>= rnEnum;
70 }
71
72 /**
73 * Sets int32 from enum or int in any with additional typecheck
74 * <BR>
75 * @param rAny enum or int
76 * @param eRet the enum value as int. If there is not enum of the given type or
77 * a ::com::sun::star::lang::IllegalArgumentException is thrown
78 */
79 template< typename E >
any2enum(E & eRet,const::com::sun::star::uno::Any & rAny)80 inline void SAL_CALL any2enum( E & eRet, const ::com::sun::star::uno::Any & rAny )
81 throw( ::com::sun::star::lang::IllegalArgumentException )
82 {
83 // check for type save enum
84 if (! (rAny >>= eRet))
85 {
86 // if not enum, maybe integer?
87 sal_Int32 nValue = 0;
88 if (! (rAny >>= nValue))
89 throw ::com::sun::star::lang::IllegalArgumentException();
90
91 eRet = (E)nValue;
92 }
93 }
94
95 /**
96 * Template function to create an uno::Any from an enum
97 *
98 * @DEPRECATED : use makeAny< E >()
99 *
100 */
101 template< typename E >
enum2any(E eEnum)102 inline ::com::sun::star::uno::Any SAL_CALL enum2any( E eEnum )
103 {
104 return ::com::sun::star::uno::Any( &eEnum, ::cppu::UnoType< E >::get() );
105 }
106
107 /**
108 * Extracts interface from an any. If given any does not hold the demanded interface,
109 * it will be queried for it.
110 * If no interface is available, the out ref will be cleared.
111 *<BR>
112 * @param rxOut [out] demanded interface
113 * @param rAny interface
114 * @return sal_True if any reference (including the null ref) was retrieved from any else sal_False.
115 */
116 template< class T >
extractInterface(::com::sun::star::uno::Reference<T> & rxOut,const::com::sun::star::uno::Any & rAny)117 inline sal_Bool SAL_CALL extractInterface(
118 ::com::sun::star::uno::Reference< T > & rxOut,
119 const ::com::sun::star::uno::Any & rAny )
120 {
121 rxOut.clear();
122 return (rAny >>= rxOut);
123 }
124
125 /**
126 * extracts a boolean either as a sal_Bool or an integer from
127 * an any. If there is no sal_Bool or integer inside the any
128 * a ::com::sun::star::lang::IllegalArgumentException is thrown
129 *
130 */
any2bool(const::com::sun::star::uno::Any & rAny)131 inline sal_Bool SAL_CALL any2bool( const ::com::sun::star::uno::Any & rAny )
132 throw( ::com::sun::star::lang::IllegalArgumentException )
133 {
134 sal_Bool sValue;
135 if ( rAny >>= sValue)
136 {
137 return sValue;
138 }
139 else
140 {
141 sal_Int32 nValue = 0;
142 if (! (rAny >>= nValue))
143 throw ::com::sun::star::lang::IllegalArgumentException();
144 return nValue != 0;
145 }
146 }
147
148 /**
149 * Puts a boolean in an any.
150 *
151 * @DEPRECATED : use makeAny< sal_Bool >()
152 *
153 */
bool2any(sal_Bool bBool)154 inline ::com::sun::star::uno::Any SAL_CALL bool2any( sal_Bool bBool )
155 {
156 return ::com::sun::star::uno::Any( &bBool, ::getCppuBooleanType() );
157 }
158
159 }
160
161 #endif
162