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 template <typename T>
69 inline bool isUnoTunnelId(const css::uno::Sequence< sal_Int8 >& rId)
70 {
71 return rId.getLength() == 16
72 && memcmp(T::getUnoTunnelId().getConstArray(), rId.getConstArray(), 16) == 0;
73 }
74
75 template <class Base> struct FallbackToGetSomethingOf
76 {
77 static sal_Int64 get(const css::uno::Sequence<sal_Int8>& rId, Base* p)
78 {
79 return p->Base::getSomething(rId);
80 }
81 };
82
83 template <> struct FallbackToGetSomethingOf<void>
84 {
85 static sal_Int64 get(const css::uno::Sequence<sal_Int8>&, void*) { return 0; }
86 };
87
88 template <class T, class Base = void>
89 sal_Int64 getSomethingImpl(const css::uno::Sequence<sal_Int8>& rId, T* pThis,
91 {
92 if (isUnoTunnelId<T>(rId))
93 return getSomething_cast(pThis);
94
95 return FallbackToGetSomethingOf<Base>::get(rId, pThis);
96 }
97
98}
99
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;
117
118#define UNO3_GETIMPLEMENTATION_BASE_IMPL( classname ) \
119const css::uno::Sequence< sal_Int8 > & classname::getUnoTunnelId() noexcept \
120{ \
121 static const comphelper::UnoIdInit aId; \
122 return aId.getSeq(); \
123}
124
125#define UNO3_GETIMPLEMENTATION_IMPL( classname )\
126UNO3_GETIMPLEMENTATION_BASE_IMPL(classname)\
127sal_Int64 SAL_CALL classname::getSomething( const css::uno::Sequence< sal_Int8 >& rId ) \
128{ \
129 return comphelper::getSomethingImpl(rId, this); \
130}
131
132#define UNO3_GETIMPLEMENTATION2_IMPL( classname, baseclass )\
133UNO3_GETIMPLEMENTATION_BASE_IMPL(classname)\
134sal_Int64 SAL_CALL classname::getSomething( const css::uno::Sequence< sal_Int8 >& rId ) \
135{ \
136 return comphelper::getSomethingImpl(rId, this, comphelper::FallbackToGetSomethingOf<baseclass>{}); \
137}
138
139
140#endif // INCLUDED_COMPHELPER_SERVICEHELPER_HXX
141
142/* 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