xref: /trunk/main/comphelper/inc/comphelper/extract.hxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 {
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 {
133     sal_Bool sValue;
134     if ( rAny >>= sValue)
135     {
136         return sValue;
137     }
138     else
139     {
140         sal_Int32 nValue = 0;
141         if (! (rAny >>= nValue))
142             throw ::com::sun::star::lang::IllegalArgumentException();
143         return nValue != 0;
144     }
145 }
146 
147 /**
148  * Puts a boolean in an any.
149  *
150  * @DEPRECATED : use makeAny< sal_Bool >()
151  *
152  */
bool2any(sal_Bool bBool)153 inline ::com::sun::star::uno::Any SAL_CALL bool2any( sal_Bool bBool )
154 {
155     return ::com::sun::star::uno::Any( &bBool, ::getCppuBooleanType() );
156 }
157 
158 }
159 
160 #endif
161