19877b273SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
39877b273SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
49877b273SAndrew Rist * or more contributor license agreements. See the NOTICE file
59877b273SAndrew Rist * distributed with this work for additional information
69877b273SAndrew Rist * regarding copyright ownership. The ASF licenses this file
79877b273SAndrew Rist * to you under the Apache License, Version 2.0 (the
89877b273SAndrew Rist * "License"); you may not use this file except in compliance
99877b273SAndrew Rist * with the License. You may obtain a copy of the License at
109877b273SAndrew Rist *
119877b273SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
129877b273SAndrew Rist *
139877b273SAndrew Rist * Unless required by applicable law or agreed to in writing,
149877b273SAndrew Rist * software distributed under the License is distributed on an
159877b273SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169877b273SAndrew Rist * KIND, either express or implied. See the License for the
179877b273SAndrew Rist * specific language governing permissions and limitations
189877b273SAndrew Rist * under the License.
199877b273SAndrew Rist *
209877b273SAndrew Rist *************************************************************/
219877b273SAndrew Rist
229877b273SAndrew Rist
23cdf0e10cSrcweir #ifndef _COMPHELPER_EXTRACT_HXX_
24cdf0e10cSrcweir #define _COMPHELPER_EXTRACT_HXX_
25cdf0e10cSrcweir
26cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp>
27cdf0e10cSrcweir #include <com/sun/star/uno/XInterface.hpp>
28cdf0e10cSrcweir #include <com/sun/star/uno/TypeClass.hpp>
29cdf0e10cSrcweir #include <com/sun/star/uno/Type.hxx>
30cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx>
31cdf0e10cSrcweir #include "cppu/unotype.hxx"
32cdf0e10cSrcweir
33cdf0e10cSrcweir namespace cppu
34cdf0e10cSrcweir {
35cdf0e10cSrcweir
36cdf0e10cSrcweir /**
37cdf0e10cSrcweir * Sets enum from int32 value. This function does NOT check for valid enum values!
38cdf0e10cSrcweir *<BR>
39cdf0e10cSrcweir * @param nEnum int32 enum value
40cdf0e10cSrcweir * @param rType enum type
41cdf0e10cSrcweir * @return enum or emoty any.
42cdf0e10cSrcweir */
int2enum(sal_Int32 nEnum,const::com::sun::star::uno::Type & rType)43cdf0e10cSrcweir inline ::com::sun::star::uno::Any SAL_CALL int2enum(
44cdf0e10cSrcweir sal_Int32 nEnum, const ::com::sun::star::uno::Type & rType )
45cdf0e10cSrcweir {
46cdf0e10cSrcweir if (rType.getTypeClass() == ::com::sun::star::uno::TypeClass_ENUM)
47cdf0e10cSrcweir {
48cdf0e10cSrcweir int nVal = nEnum;
49cdf0e10cSrcweir return ::com::sun::star::uno::Any( &nVal, rType );
50cdf0e10cSrcweir }
51cdf0e10cSrcweir return ::com::sun::star::uno::Any();
52cdf0e10cSrcweir }
53cdf0e10cSrcweir
54cdf0e10cSrcweir /**
55cdf0e10cSrcweir * Sets int32 from enum or int in any.
56cdf0e10cSrcweir *<BR>
57cdf0e10cSrcweir * @param rnEnum [out] int32 enum value
58cdf0e10cSrcweir * @param rAny enum or int
59cdf0e10cSrcweir * @param sal_True if enum or int value was set else sal_False.
60cdf0e10cSrcweir */
enum2int(sal_Int32 & rnEnum,const::com::sun::star::uno::Any & rAny)61cdf0e10cSrcweir inline sal_Bool SAL_CALL enum2int( sal_Int32 & rnEnum, const ::com::sun::star::uno::Any & rAny )
62cdf0e10cSrcweir {
63cdf0e10cSrcweir if (rAny.getValueTypeClass() == ::com::sun::star::uno::TypeClass_ENUM)
64cdf0e10cSrcweir {
65cdf0e10cSrcweir rnEnum = * reinterpret_cast< const int * >( rAny.getValue() );
66cdf0e10cSrcweir return sal_True;
67cdf0e10cSrcweir }
68cdf0e10cSrcweir
69cdf0e10cSrcweir return rAny >>= rnEnum;
70cdf0e10cSrcweir }
71cdf0e10cSrcweir
72cdf0e10cSrcweir /**
73cdf0e10cSrcweir * Sets int32 from enum or int in any with additional typecheck
74cdf0e10cSrcweir * <BR>
75cdf0e10cSrcweir * @param rAny enum or int
76cdf0e10cSrcweir * @param eRet the enum value as int. If there is not enum of the given type or
77cdf0e10cSrcweir * a ::com::sun::star::lang::IllegalArgumentException is thrown
78cdf0e10cSrcweir */
79cdf0e10cSrcweir template< typename E >
any2enum(E & eRet,const::com::sun::star::uno::Any & rAny)80cdf0e10cSrcweir inline void SAL_CALL any2enum( E & eRet, const ::com::sun::star::uno::Any & rAny )
81cdf0e10cSrcweir throw( ::com::sun::star::lang::IllegalArgumentException )
82cdf0e10cSrcweir {
83cdf0e10cSrcweir // check for type save enum
84cdf0e10cSrcweir if (! (rAny >>= eRet))
85cdf0e10cSrcweir {
86cdf0e10cSrcweir // if not enum, maybe integer?
87cdf0e10cSrcweir sal_Int32 nValue = 0;
88cdf0e10cSrcweir if (! (rAny >>= nValue))
89cdf0e10cSrcweir throw ::com::sun::star::lang::IllegalArgumentException();
90cdf0e10cSrcweir
91cdf0e10cSrcweir eRet = (E)nValue;
92cdf0e10cSrcweir }
93cdf0e10cSrcweir }
94cdf0e10cSrcweir
95cdf0e10cSrcweir /**
96cdf0e10cSrcweir * Template function to create an uno::Any from an enum
97cdf0e10cSrcweir *
98cdf0e10cSrcweir * @DEPRECATED : use makeAny< E >()
99cdf0e10cSrcweir *
100cdf0e10cSrcweir */
101cdf0e10cSrcweir template< typename E >
enum2any(E eEnum)102cdf0e10cSrcweir inline ::com::sun::star::uno::Any SAL_CALL enum2any( E eEnum )
103cdf0e10cSrcweir {
104cdf0e10cSrcweir return ::com::sun::star::uno::Any( &eEnum, ::cppu::UnoType< E >::get() );
105cdf0e10cSrcweir }
106cdf0e10cSrcweir
107cdf0e10cSrcweir /**
108cdf0e10cSrcweir * Extracts interface from an any. If given any does not hold the demanded interface,
109cdf0e10cSrcweir * it will be queried for it.
110cdf0e10cSrcweir * If no interface is available, the out ref will be cleared.
111cdf0e10cSrcweir *<BR>
112cdf0e10cSrcweir * @param rxOut [out] demanded interface
113cdf0e10cSrcweir * @param rAny interface
114cdf0e10cSrcweir * @return sal_True if any reference (including the null ref) was retrieved from any else sal_False.
115cdf0e10cSrcweir */
116cdf0e10cSrcweir template< class T >
extractInterface(::com::sun::star::uno::Reference<T> & rxOut,const::com::sun::star::uno::Any & rAny)117cdf0e10cSrcweir inline sal_Bool SAL_CALL extractInterface(
118cdf0e10cSrcweir ::com::sun::star::uno::Reference< T > & rxOut,
119cdf0e10cSrcweir const ::com::sun::star::uno::Any & rAny )
120cdf0e10cSrcweir {
121cdf0e10cSrcweir rxOut.clear();
122cdf0e10cSrcweir return (rAny >>= rxOut);
123cdf0e10cSrcweir }
124cdf0e10cSrcweir
125cdf0e10cSrcweir /**
126cdf0e10cSrcweir * extracts a boolean either as a sal_Bool or an integer from
127cdf0e10cSrcweir * an any. If there is no sal_Bool or integer inside the any
128cdf0e10cSrcweir * a ::com::sun::star::lang::IllegalArgumentException is thrown
129cdf0e10cSrcweir *
130cdf0e10cSrcweir */
any2bool(const::com::sun::star::uno::Any & rAny)131cdf0e10cSrcweir inline sal_Bool SAL_CALL any2bool( const ::com::sun::star::uno::Any & rAny )
132cdf0e10cSrcweir throw( ::com::sun::star::lang::IllegalArgumentException )
133cdf0e10cSrcweir {
134*1912a402SJim Jagielski sal_Bool sValue;
135*1912a402SJim Jagielski if ( rAny >>= sValue)
136cdf0e10cSrcweir {
137*1912a402SJim Jagielski return sValue;
138cdf0e10cSrcweir }
139cdf0e10cSrcweir else
140cdf0e10cSrcweir {
141cdf0e10cSrcweir sal_Int32 nValue = 0;
142cdf0e10cSrcweir if (! (rAny >>= nValue))
143cdf0e10cSrcweir throw ::com::sun::star::lang::IllegalArgumentException();
144cdf0e10cSrcweir return nValue != 0;
145cdf0e10cSrcweir }
146cdf0e10cSrcweir }
147cdf0e10cSrcweir
148cdf0e10cSrcweir /**
149cdf0e10cSrcweir * Puts a boolean in an any.
150cdf0e10cSrcweir *
151cdf0e10cSrcweir * @DEPRECATED : use makeAny< sal_Bool >()
152cdf0e10cSrcweir *
153cdf0e10cSrcweir */
bool2any(sal_Bool bBool)154cdf0e10cSrcweir inline ::com::sun::star::uno::Any SAL_CALL bool2any( sal_Bool bBool )
155cdf0e10cSrcweir {
156cdf0e10cSrcweir return ::com::sun::star::uno::Any( &bBool, ::getCppuBooleanType() );
157cdf0e10cSrcweir }
158cdf0e10cSrcweir
159cdf0e10cSrcweir }
160cdf0e10cSrcweir
161cdf0e10cSrcweir #endif
162