LibreOffice Module comphelper (master) 1
types.cxx
Go to the documentation of this file.
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
20#include <comphelper/types.hxx>
22#include <com/sun/star/awt/FontUnderline.hpp>
23#include <com/sun/star/awt/FontStrikeout.hpp>
24#include <com/sun/star/awt/FontDescriptor.hpp>
25#include <o3tl/any.hxx>
26#include <osl/diagnose.h>
27#include <typelib/typedescription.hxx>
28#include <sal/log.hxx>
29
30namespace comphelper
31{
32using namespace ::com::sun::star::uno;
33using namespace ::com::sun::star::awt;
34using namespace ::com::sun::star::lang;
35
36sal_Int64 getINT64(const Any& _rAny)
37{
38 sal_Int64 nReturn = 0;
39 if (!(_rAny >>= nReturn))
40 SAL_WARN("comphelper", "conversion from Any to sal_Int64 failed");
41 return nReturn;
42}
43
44sal_Int32 getINT32(const Any& _rAny)
45{
46 sal_Int32 nReturn = 0;
47 if (!(_rAny >>= nReturn))
48 SAL_WARN("comphelper", "conversion from Any to sal_Int32 failed");
49 return nReturn;
50}
51
52sal_Int16 getINT16(const Any& _rAny)
53{
54 sal_Int16 nReturn = 0;
55 if (!(_rAny >>= nReturn))
56 SAL_WARN("comphelper", "conversion from Any to sal_Int16 failed");
57 return nReturn;
58}
59
60double getDouble(const Any& _rAny)
61{
62 double nReturn = 0.0;
63 if (!(_rAny >>= nReturn))
64 SAL_WARN("comphelper", "conversion from Any to double failed");
65 return nReturn;
66}
67
68float getFloat(const Any& _rAny)
69{
70 float nReturn = 0.0;
71 if (!(_rAny >>= nReturn))
72 SAL_WARN("comphelper", "conversion from Any to float failed");
73 return nReturn;
74}
75
76OUString getString(const Any& _rAny)
77{
78 OUString nReturn;
79 if (!(_rAny >>= nReturn))
80 SAL_WARN("comphelper", "conversion from Any to OUString failed");
81 return nReturn;
82}
83
84bool getBOOL(const Any& _rAny)
85{
86 bool bReturn = false;
87 if (auto b = o3tl::tryAccess<bool>(_rAny))
88 bReturn = *b;
89 else
90 OSL_FAIL("comphelper::getBOOL : invalid argument !");
91 return bReturn;
92}
93
94sal_Int32 getEnumAsINT32(const Any& _rAny)
95{
96 sal_Int32 nReturn = 0;
97 if (!::cppu::enum2int(nReturn, _rAny))
98 throw IllegalArgumentException("enum2int failed",
99 css::uno::Reference<css::uno::XInterface>(), -1);
100 return nReturn;
101}
102
103FontDescriptor getDefaultFont()
104{
105 FontDescriptor aReturn;
106 aReturn.Slant = FontSlant_DONTKNOW;
107 aReturn.Underline = FontUnderline::DONTKNOW;
108 aReturn.Strikeout = com::sun::star::awt::FontStrikeout::DONTKNOW;
109 return aReturn;
110}
111
112bool isAssignableFrom(const Type& _rAssignable, const Type& _rFrom)
113{
114 // get the type lib descriptions
115 typelib_TypeDescription* pAssignable = nullptr;
116 _rAssignable.getDescription(&pAssignable);
117
118 typelib_TypeDescription* pFrom = nullptr;
119 _rFrom.getDescription(&pFrom);
120
121 // and ask the type lib
122 return typelib_typedescription_isAssignableFrom(pAssignable, pFrom);
123}
124
125Type getSequenceElementType(const Type& _rSequenceType)
126{
127 OSL_ENSURE(_rSequenceType.getTypeClass() == TypeClass_SEQUENCE,
128 "getSequenceElementType: must be called with a sequence type!");
129
130 if (_rSequenceType.getTypeClass() != TypeClass_SEQUENCE)
131 return Type();
132
133 TypeDescription aTD(_rSequenceType);
134 typelib_IndirectTypeDescription* pSequenceTD
135 = reinterpret_cast<typelib_IndirectTypeDescription*>(aTD.get());
136
137 OSL_ASSERT(pSequenceTD && pSequenceTD->pType);
138 if (pSequenceTD && pSequenceTD->pType)
139 return Type(pSequenceTD->pType);
140
141 return Type();
142}
143
144} // namespace comphelper
145
146/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define SAL_WARN(area, stream)
struct _typelib_TypeDescription typelib_TypeDescription
sal_Int32 getEnumAsINT32(const Any &_rAny)
Definition: types.cxx:94
bool getBOOL(const Any &_rAny)
Definition: types.cxx:84
sal_Int64 getINT64(const Any &_rAny)
Definition: types.cxx:36
Type getSequenceElementType(const Type &_rSequenceType)
Definition: types.cxx:125
FontDescriptor getDefaultFont()
get a css::awt::FontDescriptor that is fully initialized with the XXX_DONTKNOW enum values (which isn...
Definition: types.cxx:103
sal_Int16 getINT16(const Any &_rAny)
Definition: types.cxx:52
double getDouble(const Any &_rAny)
Definition: types.cxx:60
sal_Int32 getINT32(const Any &_rAny)
Definition: types.cxx:44
bool isAssignableFrom(const Type &_rAssignable, const Type &_rFrom)
Definition: types.cxx:112
float getFloat(const Any &_rAny)
Definition: types.cxx:68
OUString getString(const Any &_rAny)
Definition: types.cxx:76
Type
bool enum2int(sal_Int32 &rnEnum, const css::uno::Any &rAny)
Sets int32 from enum or int in any.
Definition: extract.hxx:54
detail::Optional< bool >::type tryAccess< bool >(css::uno::Any const &any)
sal_Bool SAL_CALL typelib_typedescription_isAssignableFrom(typelib_TypeDescription *pAssignable, typelib_TypeDescription *pFrom) SAL_THROW_EXTERN_C()