update credits
[LibreOffice.git] / include / comphelper / types.hxx
blob66c6a9e0bcfb58fbbbdfbf6ec83633b8c6fe0fbb
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 #ifndef _COMPHELPER_TYPES_HXX_
21 #define _COMPHELPER_TYPES_HXX_
23 #include <com/sun/star/uno/Any.hxx>
24 #include <com/sun/star/uno/Reference.hxx>
25 #include <com/sun/star/uno/Sequence.hxx>
26 #include <com/sun/star/lang/IllegalArgumentException.hpp>
27 #include <com/sun/star/lang/XTypeProvider.hpp>
28 #include <com/sun/star/lang/XComponent.hpp>
29 #include <com/sun/star/lang/XUnoTunnel.hpp>
30 #include "comphelper/comphelperdllapi.h"
31 #include "cppu/unotype.hxx"
33 namespace com { namespace sun { namespace star { namespace awt {
34 struct FontDescriptor;
35 } } } }
37 //.........................................................................
38 namespace comphelper
40 //.........................................................................
42 namespace staruno = ::com::sun::star::uno;
43 namespace starawt = ::com::sun::star::awt;
44 namespace starlang = ::com::sun::star::lang;
46 typedef staruno::Reference< staruno::XInterface > InterfaceRef;
47 typedef staruno::Sequence< OUString > StringSequence;
49 //-------------------------------------------------------------------------
50 /** compare the two given Anys
51 The comparison is deep, means if one of the Any's contains an Any which contains an Any ..., this is resolved <br/>
52 Other types recognized currently : FontDescriptor, ::com::sun::star::util::Date/Tim/DateTime, staruno::Sequence<sal_Int8>
54 COMPHELPER_DLLPUBLIC sal_Bool compare(const staruno::Any& rLeft, const staruno::Any& rRight);
56 //-------------------------------------------------------------------------
57 /** compare two FontDescriptor's
59 COMPHELPER_DLLPUBLIC sal_Bool operator ==(const starawt::FontDescriptor& _rLeft, const starawt::FontDescriptor& _rRight);
60 inline sal_Bool operator !=(const starawt::FontDescriptor& _rLeft, const starawt::FontDescriptor& _rRight)
62 return !(_rLeft == _rRight);
65 //-------------------------------------------------------------------------
66 /// returns sal_True if objects of the types given are "compatible"
67 COMPHELPER_DLLPUBLIC sal_Bool isAssignableFrom(const staruno::Type& _rAssignable, const staruno::Type& _rFrom);
69 //-------------------------------------------------------------------------
70 /** just a small shortcut ...
71 check if a type you have at hand at runtime is equal to another type you have at compile time
72 if all our compiler would accept function calls with explicit template arguments (like
73 isA<classFoo>(runtimeType)), we wouldn't need the second parameter. But unfortunally at
74 least the current solaris compiler doesn't allow this ....
75 So this function is nearly senseless ....
77 template <class TYPE>
78 sal_Bool isA(const staruno::Type& _rType, TYPE* pDummy)
80 return _rType.equals(cppu::getTypeFavourUnsigned(pDummy));
83 //-------------------------------------------------------------------------
84 /** check if a type you have at hand at runtime is equal to another type you have at compile time
85 same comment as for the other isA ....
87 template <class TYPE>
88 sal_Bool isA(const staruno::Any& _rVal, TYPE* pDummy)
90 return _rVal.getValueType().equals(
91 cppu::getTypeFavourUnsigned(pDummy));
94 //-------------------------------------------------------------------------
95 /** check if a type you have at hand at runtime is equal to another type you have at compile time
97 template <class TYPE>
98 sal_Bool isAReference(const staruno::Any& _rVal, TYPE*)
100 return _rVal.getValueType().equals(
101 cppu::getTypeFavourUnsigned(
102 static_cast<staruno::Reference<TYPE>*>(NULL)));
105 //-------------------------------------------------------------------------
106 /** ask the given object for an XComponent interface and dispose on it
108 template <class TYPE>
109 void disposeComponent(staruno::Reference<TYPE>& _rxComp)
111 staruno::Reference<starlang::XComponent> xComp(_rxComp, staruno::UNO_QUERY);
112 if (xComp.is())
114 xComp->dispose();
115 _rxComp = NULL;
118 //-------------------------------------------------------------------------
119 template <class TYPE>
120 sal_Bool getImplementation(TYPE*& _pObject, const staruno::Reference< staruno::XInterface >& _rxIFace)
122 _pObject = NULL;
123 staruno::Reference< starlang::XUnoTunnel > xTunnel(_rxIFace, staruno::UNO_QUERY);
124 if (xTunnel.is())
125 _pObject = reinterpret_cast< TYPE* >(xTunnel->getSomething(TYPE::getUnoTunnelImplementationId()));
127 return (_pObject != NULL);
131 //-------------------------------------------------------------------------
132 /** get a com::sun::star::awt::FontDescriptor that is fully initialized with
133 the XXX_DONTKNOW enum values (which isn't the case if you instantiate it
134 via the default constructor)
136 COMPHELPER_DLLPUBLIC starawt::FontDescriptor getDefaultFont();
138 /** examine a sequence for the <type scope="com.sun.star.uno">Type</type> of it's elements.
140 COMPHELPER_DLLPUBLIC staruno::Type getSequenceElementType(const staruno::Type& _rSequenceType);
142 //=========================================================================
143 //= replacement of the former UsrAny.getXXX methods
145 // may be used if you need the return value just as temporary, else it's may be too inefficient ....
147 // no, we don't use templates here. This would lead to a lot of implicit uses of the conversion methods,
148 // which would be difficult to trace ...
150 COMPHELPER_DLLPUBLIC sal_Int64 getINT64(const staruno::Any& _rAny);
151 COMPHELPER_DLLPUBLIC sal_Int32 getINT32(const staruno::Any& _rAny);
152 COMPHELPER_DLLPUBLIC sal_Int16 getINT16(const staruno::Any& _rAny);
153 COMPHELPER_DLLPUBLIC double getDouble(const staruno::Any& _rAny);
154 COMPHELPER_DLLPUBLIC float getFloat(const staruno::Any& _rAny);
155 COMPHELPER_DLLPUBLIC OUString getString(const staruno::Any& _rAny);
156 COMPHELPER_DLLPUBLIC sal_Bool getBOOL(const staruno::Any& _rAny);
158 COMPHELPER_DLLPUBLIC sal_Int32 getEnumAsINT32(const staruno::Any& _rAny) throw(starlang::IllegalArgumentException);
160 //= replacement of some former UsrAny.setXXX methods - can be used with rvalues
161 inline void setBOOL(staruno::Any& _rAny, sal_Bool _b)
162 { _rAny.setValue(&_b, ::getBooleanCppuType()); }
164 //= extension of ::cppu::makeAny()
165 inline staruno::Any makeBoolAny(sal_Bool _b)
166 { return staruno::Any(&_b, ::getBooleanCppuType()); }
168 //.........................................................................
169 } // namespace comphelper
170 //.........................................................................
172 #endif // _COMPHELPER_TYPES_HXX_
174 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */