Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / comphelper / extract.hxx
blob4f4072145e7119be25822b187b0e48c509745793
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #ifndef INCLUDED_COMPHELPER_EXTRACT_HXX
20 #define INCLUDED_COMPHELPER_EXTRACT_HXX
22 #include <sal/config.h>
24 #include <cassert>
26 #include <com/sun/star/lang/IllegalArgumentException.hpp>
27 #include <com/sun/star/uno/TypeClass.hpp>
28 #include <com/sun/star/uno/Type.hxx>
29 #include <com/sun/star/uno/Any.hxx>
30 #include <cppu/unotype.hxx>
32 namespace cppu
35 /**
36 * Sets enum from int32 value. This function does NOT check for valid enum values!
37 *<BR>
38 * @param nEnum int32 enum value
39 * @param rType enum type
40 * @return enum or empty any.
42 inline css::uno::Any SAL_CALL int2enum(
43 sal_Int32 nEnum, const css::uno::Type & rType )
45 assert(rType.getTypeClass() == css::uno::TypeClass_ENUM);
46 return css::uno::Any( &nEnum, rType );
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.
56 inline bool enum2int( sal_Int32 & rnEnum, const css::uno::Any & rAny )
58 if (rAny.getValueTypeClass() == css::uno::TypeClass_ENUM)
60 rnEnum = * static_cast< const sal_Int32 * >( rAny.getValue() );
61 return true;
64 return rAny >>= rnEnum;
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 css::lang::IllegalArgumentException is thrown
73 * @throws css::lang::IllegalArgumentException
75 template< typename E >
76 inline void any2enum( E & eRet, const css::uno::Any & rAny )
78 // check for typesafe enum
79 if (! (rAny >>= eRet))
81 // if not enum, maybe integer?
82 sal_Int32 nValue = 0;
83 if (! (rAny >>= nValue))
84 throw css::lang::IllegalArgumentException();
86 eRet = static_cast<E>(nValue);
90 /**
91 * extracts a boolean either as a bool or an integer from
92 * an any. If there is no bool or integer inside the any
93 * a css::lang::IllegalArgumentException is thrown
95 * @throws css::lang::IllegalArgumentException
97 inline bool any2bool( const css::uno::Any & rAny )
99 bool b;
100 if (rAny >>= b)
102 return b;
104 else
106 sal_Int32 nValue = 0;
107 if (! (rAny >>= nValue))
108 throw css::lang::IllegalArgumentException();
109 return nValue != 0;
115 #endif
117 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */