xref: /trunk/main/cppu/inc/cppu/unotype.hxx (revision c6ed87c9)
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 INCLUDED_CPPU_UNOTYPE_HXX
25 #define INCLUDED_CPPU_UNOTYPE_HXX
26 
27 #include "sal/config.h"
28 #include "com/sun/star/uno/Type.h"
29 #include "sal/types.h"
30 #include "typelib/typeclass.h"
31 #include "typelib/typedescription.h"
32 
33 namespace com { namespace sun { namespace star { namespace uno {
34     class Any;
35     class Exception;
36     template< typename > class Reference;
37     template< typename > class Sequence;
38     class XInterface;
39 } } } }
40 namespace rtl { class OUString; }
41 
42 namespace cppu {
43 
44 template< typename > class UnoType;
45 
46 /**
47    A unique C++ type representing the UNO type VOID in cppu::UnoType.
48 
49    This type is declared but not defined.  Its only use is as a template
50    argument to cppu::UnoType.
51 
52    @since UDK 3.2.2
53 */
54 struct UnoVoidType;
55 
56 /**
57    A unique C++ type representing the UNO type UNSIGNED SHORT in cppu::UnoType.
58 
59    The UNO types UNSIGNED SHORT and CHAR map to the same C++ type, so this C++
60    type is needed to unambiguously specify UNO types in cppu::UnoType.
61 
62    This type is declared but not defined.  Its only use is as a template
63    argument to cppu::UnoType.
64 
65    @since UDK 3.2.2
66 */
67 struct UnoUnsignedShortType;
68 
69 /**
70    A unique C++ type representing the UNO type UNSIGNED SHORT in cppu::UnoType.
71 
72    The UNO types UNSIGNED SHORT and CHAR map to the same C++ type, so this C++
73    type is needed to unambiguously specify UNO types in cppu::UnoType.
74 
75    This type is declared but not defined.  Its only use is as a template
76    argument to cppu::UnoType.
77 
78    @since UDK 3.2.2
79 */
80 struct UnoCharType;
81 
82 /**
83    A unique C++ type template representing the UNO sequence types in
84    cppu::UnoType.
85 
86    The UNO types UNSIGNED SHORT and CHAR map to the same C++ type, so this C++
87    type is needed to unambiguously specify UNO types in cppu::UnoType.
88 
89    This type is declared but not defined.  Its only use is as a template
90    argument to cppu::UnoType.
91 
92    @since UDK 3.2.2
93 */
94 template< typename > struct UnoSequenceType;
95 
96 namespace detail {
97 
getTypeFromTypeDescriptionReference(::typelib_TypeDescriptionReference * const * tdr)98 inline ::com::sun::star::uno::Type const & getTypeFromTypeDescriptionReference(
99     ::typelib_TypeDescriptionReference * const * tdr)
100 {
101     return *reinterpret_cast< ::com::sun::star::uno::Type const * >(tdr);
102 }
103 
104 inline ::com::sun::star::uno::Type const &
getTypeFromTypeClass(::typelib_TypeClass tc)105 getTypeFromTypeClass(::typelib_TypeClass tc) {
106     return getTypeFromTypeDescriptionReference(
107         ::typelib_static_type_getByTypeClass(tc));
108 }
109 
110 }
111 
112 }
113 
114 // For _MSC_VER 1310, define cppu_detail_getUnoType in the global namespace, to
115 // avoid spurious compiler errors in code that calls cppu_detail_getUnoType:
116 #if !defined _MSC_VER || _MSC_VER > 1310
117 namespace cppu { namespace detail {
118 #endif
119 
120 inline ::com::sun::star::uno::Type const &
cppu_detail_getUnoType(::cppu::UnoVoidType const *)121 cppu_detail_getUnoType(::cppu::UnoVoidType const *) {
122     return ::cppu::detail::getTypeFromTypeClass(::typelib_TypeClass_VOID);
123 }
124 
125 inline ::com::sun::star::uno::Type const &
cppu_detail_getUnoType(bool const *)126 cppu_detail_getUnoType(bool const *) {
127     return ::cppu::detail::getTypeFromTypeClass(::typelib_TypeClass_BOOLEAN);
128 }
129 
130 inline ::com::sun::star::uno::Type const &
cppu_detail_getUnoType(::sal_Bool const *)131 cppu_detail_getUnoType(::sal_Bool const *) {
132     return ::cppu::detail::getTypeFromTypeClass(::typelib_TypeClass_BOOLEAN);
133 }
134 
135 inline ::com::sun::star::uno::Type const &
cppu_detail_getUnoType(::sal_Int8 const *)136 cppu_detail_getUnoType(::sal_Int8 const *) {
137     return ::cppu::detail::getTypeFromTypeClass(::typelib_TypeClass_BYTE);
138 }
139 
140 inline ::com::sun::star::uno::Type const &
cppu_detail_getUnoType(::sal_Int16 const *)141 cppu_detail_getUnoType(::sal_Int16 const *) {
142     return ::cppu::detail::getTypeFromTypeClass(::typelib_TypeClass_SHORT);
143 }
144 
145 inline ::com::sun::star::uno::Type const &
cppu_detail_getUnoType(::cppu::UnoUnsignedShortType const *)146 cppu_detail_getUnoType(::cppu::UnoUnsignedShortType const *) {
147     return ::cppu::detail::getTypeFromTypeClass(
148         ::typelib_TypeClass_UNSIGNED_SHORT);
149 }
150 
151 inline ::com::sun::star::uno::Type const &
cppu_detail_getUnoType(::sal_Int32 const *)152 cppu_detail_getUnoType(::sal_Int32 const *) {
153     return ::cppu::detail::getTypeFromTypeClass(::typelib_TypeClass_LONG);
154 }
155 
156 inline ::com::sun::star::uno::Type const &
cppu_detail_getUnoType(::sal_uInt32 const *)157 cppu_detail_getUnoType(::sal_uInt32 const *) {
158     return ::cppu::detail::getTypeFromTypeClass(
159         ::typelib_TypeClass_UNSIGNED_LONG);
160 }
161 
162 inline ::com::sun::star::uno::Type const &
cppu_detail_getUnoType(::sal_Int64 const *)163 cppu_detail_getUnoType(::sal_Int64 const *) {
164     return ::cppu::detail::getTypeFromTypeClass(::typelib_TypeClass_HYPER);
165 }
166 
167 inline ::com::sun::star::uno::Type const &
cppu_detail_getUnoType(::sal_uInt64 const *)168 cppu_detail_getUnoType(::sal_uInt64 const *) {
169     return ::cppu::detail::getTypeFromTypeClass(
170         ::typelib_TypeClass_UNSIGNED_HYPER);
171 }
172 
173 inline ::com::sun::star::uno::Type const &
cppu_detail_getUnoType(float const *)174 cppu_detail_getUnoType(float const *) {
175     return ::cppu::detail::getTypeFromTypeClass(::typelib_TypeClass_FLOAT);
176 }
177 
178 inline ::com::sun::star::uno::Type const &
cppu_detail_getUnoType(double const *)179 cppu_detail_getUnoType(double const *) {
180     return ::cppu::detail::getTypeFromTypeClass(::typelib_TypeClass_DOUBLE);
181 }
182 
183 inline ::com::sun::star::uno::Type const &
cppu_detail_getUnoType(::cppu::UnoCharType const *)184 cppu_detail_getUnoType(::cppu::UnoCharType const *) {
185     return ::cppu::detail::getTypeFromTypeClass(::typelib_TypeClass_CHAR);
186 }
187 
188 inline ::com::sun::star::uno::Type const &
cppu_detail_getUnoType(::rtl::OUString const *)189 cppu_detail_getUnoType(::rtl::OUString const *) {
190     return ::cppu::detail::getTypeFromTypeClass(::typelib_TypeClass_STRING);
191 }
192 
193 inline ::com::sun::star::uno::Type const &
cppu_detail_getUnoType(::com::sun::star::uno::Type const *)194 cppu_detail_getUnoType(::com::sun::star::uno::Type const *) {
195     return ::cppu::detail::getTypeFromTypeClass(::typelib_TypeClass_TYPE);
196 }
197 
198 inline ::com::sun::star::uno::Type const &
cppu_detail_getUnoType(::com::sun::star::uno::Any const *)199 cppu_detail_getUnoType(::com::sun::star::uno::Any const *) {
200     return ::cppu::detail::getTypeFromTypeClass(::typelib_TypeClass_ANY);
201 }
202 
203 template< typename T > inline ::com::sun::star::uno::Type const &
cppu_detail_getUnoType(::cppu::UnoSequenceType<T> const *)204 cppu_detail_getUnoType(::cppu::UnoSequenceType< T > const *) {
205     //TODO: depending on memory model, the following might not work reliably
206     static typelib_TypeDescriptionReference * p = 0;
207     if (p == 0) {
208         ::typelib_static_sequence_type_init(
209             &p, ::cppu::UnoType< T >::get().getTypeLibType());
210     }
211     return ::cppu::detail::getTypeFromTypeDescriptionReference(&p);
212 }
213 
214 template< typename T > inline ::com::sun::star::uno::Type const &
cppu_detail_getUnoType(::com::sun::star::uno::Sequence<T> const *)215 cppu_detail_getUnoType(::com::sun::star::uno::Sequence< T > const *) {
216     return cppu_detail_getUnoType(
217         static_cast< ::cppu::UnoSequenceType< T > * >(0));
218 }
219 
220 inline ::com::sun::star::uno::Type const &
cppu_detail_getUnoType(::com::sun::star::uno::Exception const *)221 cppu_detail_getUnoType(::com::sun::star::uno::Exception const *) {
222     return ::cppu::detail::getTypeFromTypeClass(::typelib_TypeClass_EXCEPTION);
223 }
224 
225 inline ::com::sun::star::uno::Type const &
cppu_detail_getUnoType(::com::sun::star::uno::XInterface const *)226 cppu_detail_getUnoType(::com::sun::star::uno::XInterface const *) {
227     return ::cppu::detail::getTypeFromTypeClass(::typelib_TypeClass_INTERFACE);
228 }
229 
230 template< typename T > inline ::com::sun::star::uno::Type const &
cppu_detail_getUnoType(::com::sun::star::uno::Reference<T> const *)231 cppu_detail_getUnoType(::com::sun::star::uno::Reference< T > const *) {
232     return ::cppu::UnoType< T >::get();
233 }
234 
235 #if !defined _MSC_VER || _MSC_VER > 1310
236 } }
237 #endif
238 
239 namespace cppu {
240 
241 /**
242    Get the com::sun::star::uno::Type instance representing a certain UNO type.
243 
244    For each C++ type representing a UNO type, the corresponding instantiation of
245    this template has a public static member function get().  (The template is
246    specialized for C++ templates representing polymorphic struct type templates
247    of UNO.  In those cases, it does not work to instantiate UnoType with a C++
248    type that is derived from a C++ type that represents a UNO type, but does not
249    itself represent a UNO type.  In all other cases, UnoType even works for such
250    C++ types that are unambiguously derived from one C++ type that represents a
251    UNO type.)  In addition to those C++ types that are mappings of UNO types
252    (except for sal_uInt16 and sal_Unicode, see below), the following C++ types
253    are appropriate as template arguments: cppu::UnoVoidType, bool,
254    cppu::UnoUnsignedShortType, cppu::UnoCharType, cppu::UnoSequenceType with any
255    appropriate template argument (the latter three to unambiguously specify UNO
256    types, as the UNO types UNSIGNED SHORT and CHAR map to the same C++ type),
257    and com::sun::star::uno::Reference with any appropriate template argument.
258 
259    @since UDK 3.2.2
260 */
261 template< typename T > class UnoType {
262 public:
get()263     static inline ::com::sun::star::uno::Type const & get() {
264         using namespace ::cppu::detail;
265         return cppu_detail_getUnoType(static_cast< T * >(0));
266     }
267 
268 private:
269     UnoType(UnoType &); // not defined
270     ~UnoType(); // not defined
271     void operator =(UnoType &); // not defined
272 };
273 
274 /**
275    A working replacement for getCppuType (see there).
276 
277    There are three overloads of this function that together form the replacement
278    of getCppuType.  The replacement has exactly the same semantics as
279    getCppuType, in that it returns correct results for the UNO type UNSIGNED
280    SHORT but not for the UNO type CHAR.
281 
282    @since UDK 3.2.2
283 */
284 template< typename T > inline ::com::sun::star::uno::Type const &
getTypeFavourUnsigned(T const *)285 getTypeFavourUnsigned(T const *) {
286     return ::cppu::UnoType< T >::get();
287 }
288 
289 /**
290    A working replacement for getCppuType (see there).
291 
292    There are three overloads of this function that together form the replacement
293    of getCppuType.  The replacement has exactly the same semantics as
294    getCppuType, in that it returns correct results for the UNO type UNSIGNED
295    SHORT but not for the UNO type CHAR.
296 
297    @since UDK 3.2.2
298 */
299 inline ::com::sun::star::uno::Type const &
getTypeFavourUnsigned(::sal_uInt16 const *)300 getTypeFavourUnsigned(::sal_uInt16 const *) {
301     return ::cppu::UnoType< ::cppu::UnoUnsignedShortType >::get();
302 }
303 
304 /**
305    A working replacement for getCppuType (see there).
306 
307    There are three overloads of this function that together form the replacement
308    of getCppuType.  The replacement has exactly the same semantics as
309    getCppuType, in that it returns correct results for the UNO type UNSIGNED
310    SHORT but not for the UNO type CHAR.
311 
312    @since UDK 3.2.2
313 */
314 template< typename T > inline ::com::sun::star::uno::Type const &
315 getTypeFavourUnsigned(::com::sun::star::uno::Sequence< T > const *);
316     // defined in com/sun/star/uno/Sequence.hxx
317 
318 /**
319    A working replacement for getCppuType (see there).
320 
321    There are three overloads of this function that together form the replacement
322    of the getCppuType template.  The replacement has exactly the same semantics
323    as the getCppuType template, in that it returns correct results for the UNO
324    type CHAR but not for the UNO type UNSIGNED SHORT.  Additionally, it also
325    returns the intended results for sequence types.
326 
327    @internal
328 
329    @since UDK 3.2.3
330 */
331 template< typename T > inline ::com::sun::star::uno::Type const &
getTypeFavourChar(T const *)332 getTypeFavourChar(T const *) {
333     return ::cppu::UnoType< T >::get();
334 }
335 
336 /**
337    A working replacement for getCppuType (see there).
338 
339    There are three overloads of this function that together form the replacement
340    of the getCppuType template.  The replacement has exactly the same semantics
341    as the getCppuType template, in that it returns correct results for the UNO
342    type CHAR but not for the UNO type UNSIGNED SHORT.  Additionally, it also
343    returns the intended results for sequence types.
344 
345    @internal
346 
347    @since UDK 3.2.3
348 */
349 inline ::com::sun::star::uno::Type const &
getTypeFavourChar(::sal_Unicode const *)350 getTypeFavourChar(::sal_Unicode const *) {
351     return ::cppu::UnoType< ::cppu::UnoCharType >::get();
352 }
353 
354 /**
355    A working replacement for getCppuType (see there).
356 
357    There are three overloads of this function that together form the replacement
358    of the getCppuType template.  The replacement has exactly the same semantics
359    as the getCppuType template, in that it returns correct results for the UNO
360    type CHAR but not for the UNO type UNSIGNED SHORT.  Additionally, it also
361    returns the intended results for sequence types.
362 
363    @internal
364 
365    @since UDK 3.2.3
366 */
367 template< typename T > inline ::com::sun::star::uno::Type const &
368 getTypeFavourChar(::com::sun::star::uno::Sequence< T > const *);
369     // defined in com/sun/star/uno/Sequence.hxx
370 
371 }
372 
373 #endif
374