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 INCLUDED_COMPHELPER_EXTRACT_HXX
20 : #define INCLUDED_COMPHELPER_EXTRACT_HXX
21 :
22 : #include <com/sun/star/lang/IllegalArgumentException.hpp>
23 : #include <com/sun/star/uno/TypeClass.hpp>
24 : #include <com/sun/star/uno/Type.hxx>
25 : #include <com/sun/star/uno/Any.hxx>
26 : #include <cppu/unotype.hxx>
27 :
28 : namespace cppu
29 : {
30 :
31 : /**
32 : * Sets enum from int32 value. This function does NOT check for valid enum values!
33 : *<BR>
34 : * @param nEnum int32 enum value
35 : * @param rType enum type
36 : * @return enum or emoty any.
37 : */
38 0 : inline ::com::sun::star::uno::Any SAL_CALL int2enum(
39 : sal_Int32 nEnum, const ::com::sun::star::uno::Type & rType )
40 : {
41 0 : if (rType.getTypeClass() == ::com::sun::star::uno::TypeClass_ENUM)
42 : {
43 0 : int nVal = nEnum;
44 0 : return ::com::sun::star::uno::Any( &nVal, rType );
45 : }
46 0 : return ::com::sun::star::uno::Any();
47 : }
48 :
49 : /**
50 : * Sets int32 from enum or int in any.
51 : *<BR>
52 : * @param rnEnum [out] int32 enum value
53 : * @param rAny enum or int
54 : * @param sal_True if enum or int value was set else sal_False.
55 : */
56 0 : inline bool SAL_CALL enum2int( sal_Int32 & rnEnum, const ::com::sun::star::uno::Any & rAny )
57 : {
58 0 : if (rAny.getValueTypeClass() == ::com::sun::star::uno::TypeClass_ENUM)
59 : {
60 0 : rnEnum = * reinterpret_cast< const int * >( rAny.getValue() );
61 0 : return true;
62 : }
63 :
64 0 : return rAny >>= rnEnum;
65 : }
66 :
67 : /**
68 : * Sets int32 from enum or int in any with additional typecheck
69 : * <BR>
70 : * @param rAny enum or int
71 : * @param eRet the enum value as int. If there is not enum of the given type or
72 : * a ::com::sun::star::lang::IllegalArgumentException is thrown
73 : */
74 : template< typename E >
75 0 : inline void SAL_CALL any2enum( E & eRet, const ::com::sun::star::uno::Any & rAny )
76 : throw( ::com::sun::star::lang::IllegalArgumentException )
77 : {
78 : // check for type save enum
79 0 : if (! (rAny >>= eRet))
80 : {
81 : // if not enum, maybe integer?
82 0 : sal_Int32 nValue = 0;
83 0 : if (! (rAny >>= nValue))
84 0 : throw ::com::sun::star::lang::IllegalArgumentException();
85 :
86 0 : eRet = (E)nValue;
87 : }
88 0 : }
89 :
90 : /**
91 : * Template function to create an uno::Any from an enum
92 : *
93 : * @DEPRECATED : use makeAny< E >()
94 : *
95 : */
96 : template< typename E >
97 0 : inline ::com::sun::star::uno::Any SAL_CALL enum2any( E eEnum )
98 : {
99 0 : return ::com::sun::star::uno::Any( &eEnum, ::cppu::UnoType< E >::get() );
100 : }
101 :
102 : /**
103 : * extracts a boolean either as a sal_Bool or an integer from
104 : * an any. If there is no sal_Bool or integer inside the any
105 : * a ::com::sun::star::lang::IllegalArgumentException is thrown
106 : *
107 : */
108 0 : inline bool SAL_CALL any2bool( const ::com::sun::star::uno::Any & rAny )
109 : throw( ::com::sun::star::lang::IllegalArgumentException )
110 : {
111 0 : if (rAny.getValueTypeClass() == ::com::sun::star::uno::TypeClass_BOOLEAN)
112 : {
113 0 : return *(sal_Bool *)rAny.getValue();
114 : }
115 : else
116 : {
117 0 : sal_Int32 nValue = 0;
118 0 : if (! (rAny >>= nValue))
119 0 : throw ::com::sun::star::lang::IllegalArgumentException();
120 0 : return nValue != 0;
121 : }
122 : }
123 :
124 : }
125 :
126 : #endif
127 :
128 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|