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>
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>
29 
30 namespace comphelper
31 {
32 using namespace ::com::sun::star::uno;
33 using namespace ::com::sun::star::awt;
34 using namespace ::com::sun::star::lang;
35 
36 sal_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 
44 sal_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 
52 sal_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 
60 double 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 
68 float 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 
76 OUString 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 
84 bool 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 
94 sal_Int32 getEnumAsINT32(const Any& _rAny)
95 {
96  sal_Int32 nReturn = 0;
97  if (!::cppu::enum2int(nReturn, _rAny))
98  throw IllegalArgumentException();
99  return nReturn;
100 }
101 
102 FontDescriptor getDefaultFont()
103 {
104  FontDescriptor aReturn;
105  aReturn.Slant = FontSlant_DONTKNOW;
106  aReturn.Underline = FontUnderline::DONTKNOW;
107  aReturn.Strikeout = com::sun::star::awt::FontStrikeout::DONTKNOW;
108  return aReturn;
109 }
110 
111 bool isAssignableFrom(const Type& _rAssignable, const Type& _rFrom)
112 {
113  // get the type lib descriptions
114  typelib_TypeDescription* pAssignable = nullptr;
115  _rAssignable.getDescription(&pAssignable);
116 
117  typelib_TypeDescription* pFrom = nullptr;
118  _rFrom.getDescription(&pFrom);
119 
120  // and ask the type lib
121  return typelib_typedescription_isAssignableFrom(pAssignable, pFrom);
122 }
123 
124 Type getSequenceElementType(const Type& _rSequenceType)
125 {
126  OSL_ENSURE(_rSequenceType.getTypeClass() == TypeClass_SEQUENCE,
127  "getSequenceElementType: must be called with a sequence type!");
128 
129  if (_rSequenceType.getTypeClass() != TypeClass_SEQUENCE)
130  return Type();
131 
132  TypeDescription aTD(_rSequenceType);
133  typelib_IndirectTypeDescription* pSequenceTD
134  = reinterpret_cast<typelib_IndirectTypeDescription*>(aTD.get());
135 
136  OSL_ASSERT(pSequenceTD && pSequenceTD->pType);
137  if (pSequenceTD && pSequenceTD->pType)
138  return Type(pSequenceTD->pType);
139 
140  return Type();
141 }
142 
143 } // namespace comphelper
144 
145 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type getSequenceElementType(const Type &_rSequenceType)
Definition: types.cxx:124
Type
float getFloat(const Any &_rAny)
Definition: types.cxx:68
sal_Int64 getINT64(const Any &_rAny)
Definition: types.cxx:36
bool getBOOL(const Any &_rAny)
Definition: types.cxx:84
bool isAssignableFrom(const Type &_rAssignable, const Type &_rFrom)
Definition: types.cxx:111
bool enum2int(sal_Int32 &rnEnum, const css::uno::Any &rAny)
Sets int32 from enum or int in any.
Definition: extract.hxx:54
sal_Int16 getINT16(const Any &_rAny)
Definition: types.cxx:52
sal_Bool SAL_CALL typelib_typedescription_isAssignableFrom(typelib_TypeDescription *pAssignable, typelib_TypeDescription *pFrom) SAL_THROW_EXTERN_C()
sal_Int32 getEnumAsINT32(const Any &_rAny)
Definition: types.cxx:94
struct _typelib_TypeDescription typelib_TypeDescription
sal_Int32 getINT32(const Any &_rAny)
Definition: types.cxx:44
double getDouble(const Any &_rAny)
Definition: types.cxx:60
FontDescriptor getDefaultFont()
get a css::awt::FontDescriptor that is fully initialized with the XXX_DONTKNOW enum values (which isn...
Definition: types.cxx:102
#define SAL_WARN(area, stream)
detail::Optional< bool >::type tryAccess< bool >(css::uno::Any const &any)
OUString getString(const Any &_rAny)
Definition: types.cxx:76