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