tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / include / comphelper / servicehelper.hxx
blob03e7a322c340dfd25aed24b1e65561bc7730337d
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 INCLUDED_COMPHELPER_SERVICEHELPER_HXX
21 #define INCLUDED_COMPHELPER_SERVICEHELPER_HXX
23 #include <rtl/uuid.h>
24 #include <com/sun/star/lang/XUnoTunnel.hpp>
25 #include <com/sun/star/uno/Sequence.hxx>
27 namespace comphelper {
29 // Class incapsulating UIDs used as e.g. tunnel IDs for css::lang::XUnoTunnel,
30 // or implementation IDs for css::lang::XTypeProvider
31 class UnoIdInit
33 private:
34 css::uno::Sequence< sal_Int8 > m_aSeq;
35 public:
36 UnoIdInit() : m_aSeq(16)
38 rtl_createUuid(reinterpret_cast<sal_uInt8*>(m_aSeq.getArray()), nullptr, true);
40 const css::uno::Sequence< sal_Int8 >& getSeq() const { return m_aSeq; }
43 inline sal_Int64 getSomething_cast(void* p)
45 return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_IntPtr>(p));
48 template<class T> inline T* getSomething_cast(sal_Int64 n)
50 return reinterpret_cast<T*>(sal::static_int_cast<sal_IntPtr>(n));
53 template <class T> T* getFromUnoTunnel(const css::uno::Reference<css::lang::XUnoTunnel>& xUT)
55 if (!xUT.is())
56 return nullptr;
58 return getSomething_cast<T>(xUT->getSomething(T::getUnoTunnelId()));
61 // Takes an interface
62 template <class T> T* getFromUnoTunnel(const css::uno::Reference<css::uno::XInterface>& xIface)
64 return getFromUnoTunnel<T>(
65 css::uno::Reference<css::lang::XUnoTunnel>{ xIface, css::uno::UNO_QUERY });
68 template <typename T>
69 inline bool isUnoTunnelId(const css::uno::Sequence< sal_Int8 >& rId)
71 return rId.getLength() == 16
72 && memcmp(T::getUnoTunnelId().getConstArray(), rId.getConstArray(), 16) == 0;
75 template <class Base> struct FallbackToGetSomethingOf
77 static sal_Int64 get(const css::uno::Sequence<sal_Int8>& rId, Base* p)
79 return p->Base::getSomething(rId);
83 template <> struct FallbackToGetSomethingOf<void>
85 static sal_Int64 get(const css::uno::Sequence<sal_Int8>&, void*) { return 0; }
88 template <class T, class Base = void>
89 sal_Int64 getSomethingImpl(const css::uno::Sequence<sal_Int8>& rId, T* pThis,
90 FallbackToGetSomethingOf<Base> = {})
92 if (isUnoTunnelId<T>(rId))
93 return getSomething_cast(pThis);
95 return FallbackToGetSomethingOf<Base>::get(rId, pThis);
100 /** the UNO3_GETIMPLEMENTATION_* macros implement a static helper function
101 that gives access to your implementation for a given interface reference,
102 if possible.
104 Example:
105 MyClass* pClass = comphelper::getFromUnoTunnel<MyClass>( xRef );
107 Usage:
108 Put a UNO3_GETIMPLEMENTATION_DECL( classname ) inside your class
109 definition and UNO3_GETIMPLEMENTATION_IMPL( classname ) inside
110 your cxx file. Your class must inherit css::lang::XUnoTunnel
111 and export it with queryInterface. Implementation of XUnoTunnel is
112 done by this macro.
114 #define UNO3_GETIMPLEMENTATION_DECL( classname ) \
115 static const css::uno::Sequence< sal_Int8 > & getUnoTunnelId() noexcept; \
116 virtual sal_Int64 SAL_CALL getSomething( const css::uno::Sequence< sal_Int8 >& aIdentifier ) override;
118 #define UNO3_GETIMPLEMENTATION_BASE_IMPL( classname ) \
119 const css::uno::Sequence< sal_Int8 > & classname::getUnoTunnelId() noexcept \
121 static const comphelper::UnoIdInit aId; \
122 return aId.getSeq(); \
125 #define UNO3_GETIMPLEMENTATION_IMPL( classname )\
126 UNO3_GETIMPLEMENTATION_BASE_IMPL(classname)\
127 sal_Int64 SAL_CALL classname::getSomething( const css::uno::Sequence< sal_Int8 >& rId ) \
129 return comphelper::getSomethingImpl(rId, this); \
132 #define UNO3_GETIMPLEMENTATION2_IMPL( classname, baseclass )\
133 UNO3_GETIMPLEMENTATION_BASE_IMPL(classname)\
134 sal_Int64 SAL_CALL classname::getSomething( const css::uno::Sequence< sal_Int8 >& rId ) \
136 return comphelper::getSomethingImpl(rId, this, comphelper::FallbackToGetSomethingOf<baseclass>{}); \
140 #endif // INCLUDED_COMPHELPER_SERVICEHELPER_HXX
142 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */