1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #ifndef _COMPHELPER_EXTRACT_HXX_
28 #define _COMPHELPER_EXTRACT_HXX_
29 
30 #include <com/sun/star/lang/IllegalArgumentException.hpp>
31 #include <com/sun/star/uno/XInterface.hpp>
32 #include <com/sun/star/uno/TypeClass.hpp>
33 #include <com/sun/star/uno/Type.hxx>
34 #include <com/sun/star/uno/Any.hxx>
35 #include "cppu/unotype.hxx"
36 
37 namespace cppu
38 {
39 
40 /**
41  * Sets enum from int32 value.  This function does NOT check for valid enum values!
42  *<BR>
43  * @param nEnum			int32 enum value
44  * @param rType			enum type
45  * @return enum or emoty any.
46  */
47 inline ::com::sun::star::uno::Any SAL_CALL int2enum(
48 	sal_Int32 nEnum, const ::com::sun::star::uno::Type & rType )
49 {
50 	if (rType.getTypeClass() == ::com::sun::star::uno::TypeClass_ENUM)
51 	{
52 		int nVal = nEnum;
53 		return ::com::sun::star::uno::Any( &nVal, rType );
54 	}
55 	return ::com::sun::star::uno::Any();
56 }
57 
58 /**
59  * Sets int32 from enum or int in any.
60  *<BR>
61  * @param rnEnum		[out] int32 enum value
62  * @param rAny			enum or int
63  * @param sal_True if enum or int value was set else sal_False.
64  */
65 inline sal_Bool SAL_CALL enum2int( sal_Int32 & rnEnum, const ::com::sun::star::uno::Any & rAny )
66 {
67 	if (rAny.getValueTypeClass() == ::com::sun::star::uno::TypeClass_ENUM)
68 	{
69 		rnEnum = * reinterpret_cast< const int * >( rAny.getValue() );
70 		return sal_True;
71 	}
72 
73 	return rAny >>= rnEnum;
74 }
75 
76 /**
77  * Sets int32 from enum or int in any with additional typecheck
78  * <BR>
79  * @param rAny			enum or int
80  * @param eRet			the enum value as int. If there is not enum of the given type or
81  *						a ::com::sun::star::lang::IllegalArgumentException is thrown
82  */
83 template< typename E >
84 inline void SAL_CALL any2enum( E & eRet, const ::com::sun::star::uno::Any & rAny )
85 	throw( ::com::sun::star::lang::IllegalArgumentException )
86 {
87 	// check for type save enum
88 	if (! (rAny >>= eRet))
89 	{
90 		// if not enum, maybe integer?
91 		sal_Int32 nValue = 0;
92 		if (! (rAny >>= nValue))
93 			throw ::com::sun::star::lang::IllegalArgumentException();
94 
95 		eRet = (E)nValue;
96 	}
97 }
98 
99 /**
100  * Template function to create an uno::Any from an enum
101  *
102  * @DEPRECATED : use makeAny< E >()
103  *
104  */
105 template< typename E >
106 inline ::com::sun::star::uno::Any SAL_CALL enum2any( E eEnum )
107 {
108 	return ::com::sun::star::uno::Any( &eEnum, ::cppu::UnoType< E >::get() );
109 }
110 
111 /**
112  * Extracts interface from an any.  If given any does not hold the demanded interface,
113  * it will be queried for it.
114  * If no interface is available, the out ref will be cleared.
115  *<BR>
116  * @param rxOut			[out] demanded interface
117  * @param rAny			interface
118  * @return sal_True if any reference (including the null ref) was retrieved from any else sal_False.
119  */
120 template< class T >
121 inline sal_Bool SAL_CALL extractInterface(
122 	::com::sun::star::uno::Reference< T > & rxOut,
123 	const ::com::sun::star::uno::Any & rAny )
124 {
125 	rxOut.clear();
126 	return (rAny >>= rxOut);
127 }
128 
129 /**
130  * extracts a boolean either as a sal_Bool or an integer from
131  * an any. If there is no sal_Bool or integer inside the any
132  * a ::com::sun::star::lang::IllegalArgumentException is thrown
133  *
134  */
135 inline sal_Bool SAL_CALL any2bool( const ::com::sun::star::uno::Any & rAny )
136 	throw( ::com::sun::star::lang::IllegalArgumentException )
137 {
138 	if (rAny.getValueTypeClass() == ::com::sun::star::uno::TypeClass_BOOLEAN)
139 	{
140 		return *(sal_Bool *)rAny.getValue();
141 	}
142 	else
143 	{
144 		sal_Int32 nValue = 0;
145 		if (! (rAny >>= nValue))
146 			throw ::com::sun::star::lang::IllegalArgumentException();
147 		return nValue != 0;
148 	}
149 }
150 
151 /**
152  * Puts a boolean in an any.
153  *
154  * @DEPRECATED : use makeAny< sal_Bool >()
155  *
156  */
157 inline ::com::sun::star::uno::Any SAL_CALL bool2any( sal_Bool bBool )
158 {
159 	return ::com::sun::star::uno::Any( &bBool, ::getCppuBooleanType() );
160 }
161 
162 }
163 
164 #endif
165