LibreOffice Module comphelper (master) 1
servicehelper.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#ifndef INCLUDED_COMPHELPER_SERVICEHELPER_HXX
21#define INCLUDED_COMPHELPER_SERVICEHELPER_HXX
22
23#include <rtl/uuid.h>
24#include <com/sun/star/lang/XUnoTunnel.hpp>
25#include <com/sun/star/uno/Sequence.hxx>
26
27namespace comphelper {
28
29 // Class incapsulating UIDs used as e.g. tunnel IDs for css::lang::XUnoTunnel,
30 // or implementation IDs for css::lang::XTypeProvider
32 {
33 private:
34 css::uno::Sequence< sal_Int8 > m_aSeq;
35 public:
37 {
38 rtl_createUuid(reinterpret_cast<sal_uInt8*>(m_aSeq.getArray()), nullptr, true);
39 }
40 const css::uno::Sequence< sal_Int8 >& getSeq() const { return m_aSeq; }
41 };
42
43 inline sal_Int64 getSomething_cast(void* p)
44 {
45 return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_IntPtr>(p));
46 }
47
48 template<class T> inline T* getSomething_cast(sal_Int64 n)
49 {
50 return reinterpret_cast<T*>(sal::static_int_cast<sal_IntPtr>(n));
51 }
52
53 template <class T> T* getFromUnoTunnel(const css::uno::Reference<css::lang::XUnoTunnel>& xUT)
54 {
55 if (!xUT.is())
56 return nullptr;
57
58 return getSomething_cast<T>(xUT->getSomething(T::getUnoTunnelId()));
59 }
60
61 // Takes an interface
62 template <class T> T* getFromUnoTunnel(const css::uno::Reference<css::uno::XInterface>& xIface)
63 {
64 return getFromUnoTunnel<T>(
65 css::uno::Reference<css::lang::XUnoTunnel>{ xIface, css::uno::UNO_QUERY });
66 }
67
68 // Takes an Any
69 template <class T> T* getFromUnoTunnel(const css::uno::Any& rAny)
70 {
71 // For unclear reason, using a Reference ctor taking an Any
72 // gives different results compared to use of operator >>=.
73 css::uno::Reference<css::lang::XUnoTunnel> xUnoTunnel;
74 rAny >>= xUnoTunnel;
75 return getFromUnoTunnel<T>(xUnoTunnel);
76 }
77
78 template <typename T>
79 inline bool isUnoTunnelId(const css::uno::Sequence< sal_Int8 >& rId)
80 {
81 return rId.getLength() == 16
82 && memcmp(T::getUnoTunnelId().getConstArray(), rId.getConstArray(), 16) == 0;
83 }
84
85 template <class Base> struct FallbackToGetSomethingOf
86 {
87 static sal_Int64 get(const css::uno::Sequence<sal_Int8>& rId, Base* p)
88 {
89 return p->Base::getSomething(rId);
90 }
91 };
92
93 template <> struct FallbackToGetSomethingOf<void>
94 {
95 static sal_Int64 get(const css::uno::Sequence<sal_Int8>&, void*) { return 0; }
96 };
97
98 template <class T, class Base = void>
99 sal_Int64 getSomethingImpl(const css::uno::Sequence<sal_Int8>& rId, T* pThis,
101 {
102 if (isUnoTunnelId<T>(rId))
103 return getSomething_cast(pThis);
104
105 return FallbackToGetSomethingOf<Base>::get(rId, pThis);
106 }
107
108}
109
124#define UNO3_GETIMPLEMENTATION_DECL( classname ) \
125 static const css::uno::Sequence< sal_Int8 > & getUnoTunnelId() noexcept; \
126 virtual sal_Int64 SAL_CALL getSomething( const css::uno::Sequence< sal_Int8 >& aIdentifier ) override;
127
128#define UNO3_GETIMPLEMENTATION_BASE_IMPL( classname ) \
129const css::uno::Sequence< sal_Int8 > & classname::getUnoTunnelId() noexcept \
130{ \
131 static const comphelper::UnoIdInit aId; \
132 return aId.getSeq(); \
133}
134
135#define UNO3_GETIMPLEMENTATION_IMPL( classname )\
136UNO3_GETIMPLEMENTATION_BASE_IMPL(classname)\
137sal_Int64 SAL_CALL classname::getSomething( const css::uno::Sequence< sal_Int8 >& rId ) \
138{ \
139 return comphelper::getSomethingImpl(rId, this); \
140}
141
142#define UNO3_GETIMPLEMENTATION2_IMPL( classname, baseclass )\
143UNO3_GETIMPLEMENTATION_BASE_IMPL(classname)\
144sal_Int64 SAL_CALL classname::getSomething( const css::uno::Sequence< sal_Int8 >& rId ) \
145{ \
146 return comphelper::getSomethingImpl(rId, this, comphelper::FallbackToGetSomethingOf<baseclass>{}); \
147}
148
149
150#endif // INCLUDED_COMPHELPER_SERVICEHELPER_HXX
151
152/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Sequence< sal_Int8 > m_aSeq
const css::uno::Sequence< sal_Int8 > & getSeq() const
void * p
sal_Int64 n
T * getFromUnoTunnel(const css::uno::Reference< css::lang::XUnoTunnel > &xUT)
sal_Int64 getSomethingImpl(const css::uno::Sequence< sal_Int8 > &rId, T *pThis, FallbackToGetSomethingOf< Base >={})
sal_Int64 getSomething_cast(void *p)
bool isUnoTunnelId(const css::uno::Sequence< sal_Int8 > &rId)
static sal_Int64 get(const css::uno::Sequence< sal_Int8 > &, void *)
static sal_Int64 get(const css::uno::Sequence< sal_Int8 > &rId, Base *p)
unsigned char sal_uInt8