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 
27 #include <type_traits>
28 
29 namespace comphelper {
30 
31  // Class incapsulating UIDs used as e.g. tunnel IDs for css::lang::XUnoTunnel,
32  // or implementation IDs for css::lang::XTypeProvider
33  class UnoIdInit
34  {
35  private:
36  css::uno::Sequence< sal_Int8 > m_aSeq;
37  public:
38  UnoIdInit() : m_aSeq(16)
39  {
40  rtl_createUuid(reinterpret_cast<sal_uInt8*>(m_aSeq.getArray()), nullptr, true);
41  }
42  const css::uno::Sequence< sal_Int8 >& getSeq() const { return m_aSeq; }
43  };
44 
45  inline sal_Int64 getSomething_cast(void* p)
46  {
47  return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_IntPtr>(p));
48  }
49 
50  template<class T> inline T* getSomething_cast(sal_Int64 n)
51  {
52  return reinterpret_cast<T*>(sal::static_int_cast<sal_IntPtr>(n));
53  }
54 
55  template <class T> T* getFromUnoTunnel(const css::uno::Reference<css::lang::XUnoTunnel>& xUT)
56  {
57  if (!xUT.is())
58  return nullptr;
59 
60  return getSomething_cast<T>(xUT->getSomething(T::getUnoTunnelId()));
61  }
62 
63  // Takes an interface
64  template <class T> T* getFromUnoTunnel(const css::uno::Reference<css::uno::XInterface>& xIface)
65  {
66  return getFromUnoTunnel<T>(
67  css::uno::Reference<css::lang::XUnoTunnel>{ xIface, css::uno::UNO_QUERY });
68  }
69 
70  // Takes an Any
71  template <class T> T* getFromUnoTunnel(const css::uno::Any& rAny)
72  {
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;
76  rAny >>= xUnoTunnel;
77  return getFromUnoTunnel<T>(xUnoTunnel);
78  }
79 
80  template <typename T>
81  inline bool isUnoTunnelId(const css::uno::Sequence< sal_Int8 >& rId)
82  {
83  return rId.getLength() == 16
84  && memcmp(T::getUnoTunnelId().getConstArray(), rId.getConstArray(), 16) == 0;
85  }
86 
87  template <class Base> struct FallbackToGetSomethingOf
88  {
89  static sal_Int64 get(const css::uno::Sequence<sal_Int8>& rId, Base* p)
90  {
91  return p->Base::getSomething(rId);
92  }
93  };
94 
95  template <> struct FallbackToGetSomethingOf<void>
96  {
97  static sal_Int64 get(const css::uno::Sequence<sal_Int8>&, void*) { return 0; }
98  };
99 
100  template <class T, class Base = void>
101  sal_Int64 getSomethingImpl(const css::uno::Sequence<sal_Int8>& rId, T* pThis,
103  {
104  if (isUnoTunnelId<T>(rId))
105  return getSomething_cast(pThis);
106 
107  return FallbackToGetSomethingOf<Base>::get(rId, pThis);
108  }
109 
110 }
111 
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;
129 
130 #define UNO3_GETIMPLEMENTATION_BASE_IMPL( classname ) \
131 const css::uno::Sequence< sal_Int8 > & classname::getUnoTunnelId() noexcept \
132 { \
133  static const comphelper::UnoIdInit aId; \
134  return aId.getSeq(); \
135 }
136 
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 ) \
140 { \
141  return comphelper::getSomethingImpl(rId, this); \
142 }
143 
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 ) \
147 { \
148  return comphelper::getSomethingImpl(rId, this, comphelper::FallbackToGetSomethingOf<baseclass>{}); \
149 }
150 
151 
152 #endif // INCLUDED_COMPHELPER_SERVICEHELPER_HXX
153 
154 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
T * getFromUnoTunnel(const css::uno::Reference< css::lang::XUnoTunnel > &xUT)
css::uno::Sequence< sal_Int8 > m_aSeq
sal_Int64 getSomething_cast(void *p)
const css::uno::Sequence< sal_Int8 > & getSeq() const
bool isUnoTunnelId(const css::uno::Sequence< sal_Int8 > &rId)
Base
sal_Int64 getSomethingImpl(const css::uno::Sequence< sal_Int8 > &rId, T *pThis, FallbackToGetSomethingOf< Base >={})
void * p
static sal_Int64 get(const css::uno::Sequence< sal_Int8 > &rId, Base *p)
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo