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