Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 : #ifndef _COMPHELPER_EXTRACT_HXX_
20 : #define _COMPHELPER_EXTRACT_HXX_
21 :
22 : #include <com/sun/star/lang/IllegalArgumentException.hpp>
23 : #include <com/sun/star/uno/XInterface.hpp>
24 : #include <com/sun/star/uno/TypeClass.hpp>
25 : #include <com/sun/star/uno/Type.hxx>
26 : #include <com/sun/star/uno/Any.hxx>
27 : #include "cppu/unotype.hxx"
28 :
29 : namespace cppu
30 : {
31 :
32 : /**
33 : * Sets enum from int32 value. This function does NOT check for valid enum values!
34 : *<BR>
35 : * @param nEnum int32 enum value
36 : * @param rType enum type
37 : * @return enum or emoty any.
38 : */
39 705 : inline ::com::sun::star::uno::Any SAL_CALL int2enum(
40 : sal_Int32 nEnum, const ::com::sun::star::uno::Type & rType )
41 : {
42 705 : if (rType.getTypeClass() == ::com::sun::star::uno::TypeClass_ENUM)
43 : {
44 705 : int nVal = nEnum;
45 705 : return ::com::sun::star::uno::Any( &nVal, rType );
46 : }
47 0 : return ::com::sun::star::uno::Any();
48 : }
49 :
50 : /**
51 : * Sets int32 from enum or int in any.
52 : *<BR>
53 : * @param rnEnum [out] int32 enum value
54 : * @param rAny enum or int
55 : * @param sal_True if enum or int value was set else sal_False.
56 : */
57 13648 : inline sal_Bool SAL_CALL enum2int( sal_Int32 & rnEnum, const ::com::sun::star::uno::Any & rAny )
58 : {
59 13648 : if (rAny.getValueTypeClass() == ::com::sun::star::uno::TypeClass_ENUM)
60 : {
61 11466 : rnEnum = * reinterpret_cast< const int * >( rAny.getValue() );
62 11466 : return sal_True;
63 : }
64 :
65 2182 : return rAny >>= rnEnum;
66 : }
67 :
68 : /**
69 : * Sets int32 from enum or int in any with additional typecheck
70 : * <BR>
71 : * @param rAny enum or int
72 : * @param eRet the enum value as int. If there is not enum of the given type or
73 : * a ::com::sun::star::lang::IllegalArgumentException is thrown
74 : */
75 : template< typename E >
76 157 : inline void SAL_CALL any2enum( E & eRet, const ::com::sun::star::uno::Any & rAny )
77 : throw( ::com::sun::star::lang::IllegalArgumentException )
78 : {
79 : // check for type save enum
80 157 : if (! (rAny >>= eRet))
81 : {
82 : // if not enum, maybe integer?
83 6 : sal_Int32 nValue = 0;
84 6 : if (! (rAny >>= nValue))
85 0 : throw ::com::sun::star::lang::IllegalArgumentException();
86 :
87 6 : eRet = (E)nValue;
88 : }
89 157 : }
90 :
91 : /**
92 : * Template function to create an uno::Any from an enum
93 : *
94 : * @DEPRECATED : use makeAny< E >()
95 : *
96 : */
97 : template< typename E >
98 0 : inline ::com::sun::star::uno::Any SAL_CALL enum2any( E eEnum )
99 : {
100 0 : return ::com::sun::star::uno::Any( &eEnum, ::cppu::UnoType< E >::get() );
101 : }
102 :
103 : /**
104 : * Extracts interface from an any. If given any does not hold the demanded interface,
105 : * it will be queried for it.
106 : * If no interface is available, the out ref will be cleared.
107 : *<BR>
108 : * @param rxOut [out] demanded interface
109 : * @param rAny interface
110 : * @return sal_True if any reference (including the null ref) was retrieved from any else sal_False.
111 : */
112 : template< class T >
113 1936 : inline sal_Bool SAL_CALL extractInterface(
114 : ::com::sun::star::uno::Reference< T > & rxOut,
115 : const ::com::sun::star::uno::Any & rAny )
116 : {
117 1936 : rxOut.clear();
118 1936 : return (rAny >>= rxOut);
119 : }
120 :
121 : /**
122 : * extracts a boolean either as a sal_Bool or an integer from
123 : * an any. If there is no sal_Bool or integer inside the any
124 : * a ::com::sun::star::lang::IllegalArgumentException is thrown
125 : *
126 : */
127 11335 : inline sal_Bool SAL_CALL any2bool( const ::com::sun::star::uno::Any & rAny )
128 : throw( ::com::sun::star::lang::IllegalArgumentException )
129 : {
130 11335 : if (rAny.getValueTypeClass() == ::com::sun::star::uno::TypeClass_BOOLEAN)
131 : {
132 11335 : return *(sal_Bool *)rAny.getValue();
133 : }
134 : else
135 : {
136 0 : sal_Int32 nValue = 0;
137 0 : if (! (rAny >>= nValue))
138 0 : throw ::com::sun::star::lang::IllegalArgumentException();
139 0 : return nValue != 0;
140 : }
141 : }
142 :
143 : /**
144 : * Puts a boolean in an any.
145 : *
146 : * @DEPRECATED : use makeAny< sal_Bool >()
147 : *
148 : */
149 1337 : inline ::com::sun::star::uno::Any SAL_CALL bool2any( sal_Bool bBool )
150 : {
151 1337 : return ::com::sun::star::uno::Any( &bBool, ::getCppuBooleanType() );
152 : }
153 :
154 : }
155 :
156 : #endif
157 :
158 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|