android: Update app-specific/MIME type icons
[LibreOffice.git] / comphelper / source / misc / types.cxx
blob8887d5b5ac928091d622909602777bf54c25944e
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 .
20 #include <comphelper/types.hxx>
21 #include <comphelper/extract.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>
30 namespace comphelper
32 using namespace ::com::sun::star::uno;
33 using namespace ::com::sun::star::awt;
34 using namespace ::com::sun::star::lang;
36 sal_Int64 getINT64(const Any& _rAny)
38 sal_Int64 nReturn = 0;
39 if (!(_rAny >>= nReturn))
40 SAL_WARN("comphelper", "conversion from Any to sal_Int64 failed");
41 return nReturn;
44 sal_Int32 getINT32(const Any& _rAny)
46 sal_Int32 nReturn = 0;
47 if (!(_rAny >>= nReturn))
48 SAL_WARN("comphelper", "conversion from Any to sal_Int32 failed");
49 return nReturn;
52 sal_Int16 getINT16(const Any& _rAny)
54 sal_Int16 nReturn = 0;
55 if (!(_rAny >>= nReturn))
56 SAL_WARN("comphelper", "conversion from Any to sal_Int16 failed");
57 return nReturn;
60 double getDouble(const Any& _rAny)
62 double nReturn = 0.0;
63 if (!(_rAny >>= nReturn))
64 SAL_WARN("comphelper", "conversion from Any to double failed");
65 return nReturn;
68 float getFloat(const Any& _rAny)
70 float nReturn = 0.0;
71 if (!(_rAny >>= nReturn))
72 SAL_WARN("comphelper", "conversion from Any to float failed");
73 return nReturn;
76 OUString getString(const Any& _rAny)
78 OUString nReturn;
79 if (!(_rAny >>= nReturn))
80 SAL_WARN("comphelper", "conversion from Any to OUString failed");
81 return nReturn;
84 bool getBOOL(const Any& _rAny)
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;
94 sal_Int32 getEnumAsINT32(const Any& _rAny)
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;
103 FontDescriptor getDefaultFont()
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;
112 bool isAssignableFrom(const Type& _rAssignable, const Type& _rFrom)
114 // get the type lib descriptions
115 typelib_TypeDescription* pAssignable = nullptr;
116 _rAssignable.getDescription(&pAssignable);
118 typelib_TypeDescription* pFrom = nullptr;
119 _rFrom.getDescription(&pFrom);
121 // and ask the type lib
122 return typelib_typedescription_isAssignableFrom(pAssignable, pFrom);
125 Type getSequenceElementType(const Type& _rSequenceType)
127 OSL_ENSURE(_rSequenceType.getTypeClass() == TypeClass_SEQUENCE,
128 "getSequenceElementType: must be called with a sequence type!");
130 if (_rSequenceType.getTypeClass() != TypeClass_SEQUENCE)
131 return Type();
133 TypeDescription aTD(_rSequenceType);
134 typelib_IndirectTypeDescription* pSequenceTD
135 = reinterpret_cast<typelib_IndirectTypeDescription*>(aTD.get());
137 OSL_ASSERT(pSequenceTD && pSequenceTD->pType);
138 if (pSequenceTD && pSequenceTD->pType)
139 return Type(pSequenceTD->pType);
141 return Type();
144 } // namespace comphelper
146 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */