1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 INCLUDED_COMPHELPER_SERVICEHELPER_HXX
21 #define INCLUDED_COMPHELPER_SERVICEHELPER_HXX
24 #include <com/sun/star/lang/XUnoTunnel.hpp>
25 #include <com/sun/star/uno/Sequence.hxx>
27 #include <type_traits>
29 namespace comphelper
{
31 // Class incapsulating UIDs used as e.g. tunnel IDs for css::lang::XUnoTunnel,
32 // or implementation IDs for css::lang::XTypeProvider
36 css::uno::Sequence
< sal_Int8
> m_aSeq
;
38 UnoIdInit() : m_aSeq(16)
40 rtl_createUuid(reinterpret_cast<sal_uInt8
*>(m_aSeq
.getArray()), nullptr, true);
42 const css::uno::Sequence
< sal_Int8
>& getSeq() const { return m_aSeq
; }
45 inline sal_Int64
getSomething_cast(void* p
)
47 return sal::static_int_cast
<sal_Int64
>(reinterpret_cast<sal_IntPtr
>(p
));
50 template<class T
> inline T
* getSomething_cast(sal_Int64 n
)
52 return reinterpret_cast<T
*>(sal::static_int_cast
<sal_IntPtr
>(n
));
55 template <class T
> T
* getFromUnoTunnel(const css::uno::Reference
<css::lang::XUnoTunnel
>& xUT
)
60 return getSomething_cast
<T
>(xUT
->getSomething(T::getUnoTunnelId()));
64 template <class T
> T
* getFromUnoTunnel(const css::uno::Reference
<css::uno::XInterface
>& xIface
)
66 return getFromUnoTunnel
<T
>(
67 css::uno::Reference
<css::lang::XUnoTunnel
>{ xIface
, css::uno::UNO_QUERY
});
71 template <class T
> T
* getFromUnoTunnel(const css::uno::Any
& rAny
)
73 // For unclear reason, using a Reference ctor taking an Any
74 // gives different results compared to use of operator >>=.
75 css::uno::Reference
<css::lang::XUnoTunnel
> xUnoTunnel
;
77 return getFromUnoTunnel
<T
>(xUnoTunnel
);
81 inline bool isUnoTunnelId(const css::uno::Sequence
< sal_Int8
>& rId
)
83 return rId
.getLength() == 16
84 && memcmp(T::getUnoTunnelId().getConstArray(), rId
.getConstArray(), 16) == 0;
87 template <class Base
> struct FallbackToGetSomethingOf
89 static sal_Int64
get(const css::uno::Sequence
<sal_Int8
>& rId
, Base
* p
)
91 return p
->Base::getSomething(rId
);
95 template <> struct FallbackToGetSomethingOf
<void>
97 static sal_Int64
get(const css::uno::Sequence
<sal_Int8
>&, void*) { return 0; }
100 template <class T
, class Base
= void>
101 sal_Int64
getSomethingImpl(const css::uno::Sequence
<sal_Int8
>& rId
, T
* pThis
,
102 FallbackToGetSomethingOf
<Base
> = {})
104 if (isUnoTunnelId
<T
>(rId
))
105 return getSomething_cast(pThis
);
107 return FallbackToGetSomethingOf
<Base
>::get(rId
, pThis
);
112 /** the UNO3_GETIMPLEMENTATION_* macros implement a static helper function
113 that gives access to your implementation for a given interface reference,
117 MyClass* pClass = comphelper::getFromUnoTunnel<MyClass>( xRef );
120 Put a UNO3_GETIMPLEMENTATION_DECL( classname ) inside your class
121 definition and UNO3_GETIMPLEMENTATION_IMPL( classname ) inside
122 your cxx file. Your class must inherit css::lang::XUnoTunnel
123 and export it with queryInterface. Implementation of XUnoTunnel is
126 #define UNO3_GETIMPLEMENTATION_DECL( classname ) \
127 static const css::uno::Sequence< sal_Int8 > & getUnoTunnelId() noexcept; \
128 virtual sal_Int64 SAL_CALL getSomething( const css::uno::Sequence< sal_Int8 >& aIdentifier ) override;
130 #define UNO3_GETIMPLEMENTATION_BASE_IMPL( classname ) \
131 const css::uno::Sequence< sal_Int8 > & classname::getUnoTunnelId() noexcept \
133 static const comphelper::UnoIdInit aId; \
134 return aId.getSeq(); \
137 #define UNO3_GETIMPLEMENTATION_IMPL( classname )\
138 UNO3_GETIMPLEMENTATION_BASE_IMPL(classname)\
139 sal_Int64 SAL_CALL classname::getSomething( const css::uno::Sequence< sal_Int8 >& rId ) \
141 return comphelper::getSomethingImpl(rId, this); \
144 #define UNO3_GETIMPLEMENTATION2_IMPL( classname, baseclass )\
145 UNO3_GETIMPLEMENTATION_BASE_IMPL(classname)\
146 sal_Int64 SAL_CALL classname::getSomething( const css::uno::Sequence< sal_Int8 >& rId ) \
148 return comphelper::getSomethingImpl(rId, this, comphelper::FallbackToGetSomethingOf<baseclass>{}); \
152 #endif // INCLUDED_COMPHELPER_SERVICEHELPER_HXX
154 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */