1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _COMPHELPER_TYPES_HXX_
25 #define _COMPHELPER_TYPES_HXX_
26 
27 #include <com/sun/star/uno/Any.hxx>
28 #include <com/sun/star/uno/Reference.hxx>
29 #include <com/sun/star/uno/Sequence.hxx>
30 #include <com/sun/star/lang/IllegalArgumentException.hpp>
31 #include <com/sun/star/lang/XTypeProvider.hpp>
32 #include <com/sun/star/lang/XComponent.hpp>
33 #include <com/sun/star/lang/XUnoTunnel.hpp>
34 #include "comphelper/comphelperdllapi.h"
35 #include "cppu/unotype.hxx"
36 
37 namespace com { namespace sun { namespace star { namespace awt {
38 	struct FontDescriptor;
39 } } } }
40 
41 //.........................................................................
42 namespace comphelper
43 {
44 //.........................................................................
45 
46 	namespace staruno		= ::com::sun::star::uno;
47 	namespace starawt		= ::com::sun::star::awt;
48 	namespace starlang		= ::com::sun::star::lang;
49 
50 	typedef staruno::Reference< staruno::XInterface >			InterfaceRef;
51 	typedef staruno::Sequence< ::rtl::OUString >				StringSequence;
52 
53 	//-------------------------------------------------------------------------
54 	/** compare the two given Anys
55 		The comparison is deep, means if one of the Any's contains an Any which contains an Any ..., this is resolved <br/>
56 		Other types recognized currently : FontDescriptor, ::com::sun::star::util::Date/Tim/DateTime, staruno::Sequence<sal_Int8>
57 	*/
58 	COMPHELPER_DLLPUBLIC sal_Bool compare(const staruno::Any& rLeft, const staruno::Any& rRight);
59 
60 	//-------------------------------------------------------------------------
61 	/** compare two FontDescriptor's
62 	*/
63 	COMPHELPER_DLLPUBLIC sal_Bool	operator ==(const starawt::FontDescriptor& _rLeft, const starawt::FontDescriptor& _rRight);
operator !=(const starawt::FontDescriptor & _rLeft,const starawt::FontDescriptor & _rRight)64 	inline	sal_Bool	operator !=(const starawt::FontDescriptor& _rLeft, const starawt::FontDescriptor& _rRight)
65 	{
66 		return !(_rLeft == _rRight);
67 	}
68 
69 	//-------------------------------------------------------------------------
70 	/// returns sal_True if objects of the types given are "compatible"
71 	COMPHELPER_DLLPUBLIC sal_Bool isAssignableFrom(const staruno::Type& _rAssignable, const staruno::Type& _rFrom);
72 
73 	//-------------------------------------------------------------------------
74 	/** just a small shortcut ...
75 		check if a type you have at hand at runtime is equal to another type you have at compile time
76 		if all our compiler would accept function calls with explicit template arguments (like
77 		isA<classFoo>(runtimeType)), we wouldn't need the second parameter. But unfortunally at
78 		least the current solaris compiler doesn't allow this ....
79 		So this function is nearly senseless ....
80 	*/
81 	template <class TYPE>
isA(const staruno::Type & _rType,TYPE * pDummy)82 	sal_Bool isA(const staruno::Type& _rType, TYPE* pDummy)
83 	{
84 		return  _rType.equals(cppu::getTypeFavourUnsigned(pDummy));
85 	}
86 
87 	//-------------------------------------------------------------------------
88 	/** check if a type you have at hand at runtime is equal to another type you have at compile time
89 		same comment as for the other isA ....
90 	*/
91 	template <class TYPE>
isA(const staruno::Any & _rVal,TYPE * pDummy)92 	sal_Bool isA(const staruno::Any& _rVal, TYPE* pDummy)
93 	{
94 		return  _rVal.getValueType().equals(
95             cppu::getTypeFavourUnsigned(pDummy));
96 	}
97 
98 	//-------------------------------------------------------------------------
99 	/** check if a type you have at hand at runtime is equal to another type you have at compile time
100 	*/
101 	template <class TYPE>
isAReference(const staruno::Any & _rVal,TYPE * pDummy)102 	sal_Bool isAReference(const staruno::Any& _rVal, TYPE* pDummy)
103 	{
104 		return  _rVal.getValueType().equals(
105             cppu::getTypeFavourUnsigned(
106                 static_cast<staruno::Reference<TYPE>*>(NULL)));
107 	}
108 
109 	//-------------------------------------------------------------------------
110 	/** ask the given object for an XComponent interface and dispose on it
111 	*/
112 	template <class TYPE>
disposeComponent(staruno::Reference<TYPE> & _rxComp)113 	void disposeComponent(staruno::Reference<TYPE>& _rxComp)
114 	{
115 		staruno::Reference<starlang::XComponent> xComp(_rxComp, staruno::UNO_QUERY);
116 		if (xComp.is())
117 		{
118 			xComp->dispose();
119 			_rxComp = NULL;
120 		}
121 	}
122 	//-------------------------------------------------------------------------
123 	template <class TYPE>
getImplementation(TYPE * & _pObject,const staruno::Reference<staruno::XInterface> & _rxIFace)124 	sal_Bool getImplementation(TYPE*& _pObject, const staruno::Reference< staruno::XInterface >& _rxIFace)
125 	{
126 		_pObject = NULL;
127 		staruno::Reference< starlang::XUnoTunnel > xTunnel(_rxIFace, staruno::UNO_QUERY);
128 		if (xTunnel.is())
129 			_pObject = reinterpret_cast< TYPE* >(xTunnel->getSomething(TYPE::getUnoTunnelImplementationId()));
130 
131 		return (_pObject != NULL);
132 	}
133 
134 
135 	//-------------------------------------------------------------------------
136 	/**	get a com::sun::star::awt::FontDescriptor that is fully initialized with
137 		the XXX_DONTKNOW enum values (which isn't the case if you instantiate it
138 		via the default constructor)
139 	*/
140 	COMPHELPER_DLLPUBLIC starawt::FontDescriptor	getDefaultFont();
141 
142 	/** examine a sequence for the <type scope="com.sun.star.uno">Type</type> of it's elements.
143 	*/
144 	COMPHELPER_DLLPUBLIC staruno::Type getSequenceElementType(const staruno::Type& _rSequenceType);
145 
146 //=========================================================================
147 //= replacement of the former UsrAny.getXXX methods
148 
149 	// may be used if you need the return value just as temporary, else it's may be too inefficient ....
150 
151 	// no, we don't use templates here. This would lead to a lot of implicit uses of the conversion methods,
152 	// which would be difficult to trace ...
153 
154 	COMPHELPER_DLLPUBLIC sal_Int32		getINT32(const staruno::Any& _rAny);
155 	COMPHELPER_DLLPUBLIC sal_Int16		getINT16(const staruno::Any& _rAny);
156 	COMPHELPER_DLLPUBLIC double			getDouble(const staruno::Any& _rAny);
157 	COMPHELPER_DLLPUBLIC float			getFloat(const staruno::Any& _rAny);
158 	COMPHELPER_DLLPUBLIC ::rtl::OUString	getString(const staruno::Any& _rAny);
159 	COMPHELPER_DLLPUBLIC sal_Bool		getBOOL(const staruno::Any& _rAny);
160 
161 	COMPHELPER_DLLPUBLIC sal_Int32		getEnumAsINT32(const staruno::Any& _rAny) throw(starlang::IllegalArgumentException);
162 
163 //= replacement of some former UsrAny.setXXX methods - can be used with rvalues
setBOOL(staruno::Any & _rAny,sal_Bool _b)164 	inline void setBOOL(staruno::Any& _rAny, sal_Bool _b)
165 	{ _rAny.setValue(&_b, ::getBooleanCppuType()); }
166 
167 //= extension of ::cppu::makeAny()
makeBoolAny(sal_Bool _b)168 	inline staruno::Any makeBoolAny(sal_Bool _b)
169 	{ return staruno::Any(&_b, ::getBooleanCppuType()); }
170 
171 //.........................................................................
172 }	// namespace comphelper
173 //.........................................................................
174 
175 #endif // _COMPHELPER_TYPES_HXX_
176 
177