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 <rtl/instance.hxx>
25 #include <com/sun/star/lang/XUnoTunnel.hpp>
26 #include <com/sun/star/uno/Sequence.hxx>
27 
29 {
30 private:
31  css::uno::Sequence< sal_Int8 > m_aSeq;
32 public:
33  UnoTunnelIdInit() : m_aSeq(16)
34  {
35  rtl_createUuid( reinterpret_cast<sal_uInt8*>(m_aSeq.getArray()), nullptr, true );
36  }
37  const css::uno::Sequence< sal_Int8 >& getSeq() const { return m_aSeq; }
38 };
39 
40 namespace comphelper {
41 
42  template<class T>
43  T* getUnoTunnelImplementation( const css::uno::Reference< css::uno::XInterface >& xIface )
44  {
45  css::uno::Reference< css::lang::XUnoTunnel > xUT( xIface, css::uno::UNO_QUERY );
46  if (!xUT.is())
47  return nullptr;
48 
49  return reinterpret_cast<T*>(sal::static_int_cast<sal_IntPtr>(xUT->getSomething( T::getUnoTunnelId() )));
50  }
51 
52 }
53 
54 template <typename T>
55 inline bool isUnoTunnelId(const css::uno::Sequence< sal_Int8 >& rId)
56 {
57  return rId.getLength() == 16
58  && memcmp( T::getUnoTunnelId().getConstArray(), rId.getConstArray(), 16 ) == 0;
59 }
60 
75 #define UNO3_GETIMPLEMENTATION_DECL( classname ) \
76  static const css::uno::Sequence< sal_Int8 > & getUnoTunnelId() throw(); \
77  virtual sal_Int64 SAL_CALL getSomething( const css::uno::Sequence< sal_Int8 >& aIdentifier ) override;
78 
79 #define UNO3_GETIMPLEMENTATION_BASE_IMPL( classname ) \
80 namespace \
81 { \
82  class the##classname##UnoTunnelId : public rtl::Static< UnoTunnelIdInit, the##classname##UnoTunnelId> {}; \
83 } \
84 const css::uno::Sequence< sal_Int8 > & classname::getUnoTunnelId() throw() \
85 { \
86  return the##classname##UnoTunnelId::get().getSeq(); \
87 }
88 
89 #define UNO3_GETIMPLEMENTATION_IMPL( classname )\
90 UNO3_GETIMPLEMENTATION_BASE_IMPL(classname)\
91 sal_Int64 SAL_CALL classname::getSomething( const css::uno::Sequence< sal_Int8 >& rId ) \
92 { \
93  if( isUnoTunnelId<classname>(rId) ) \
94  { \
95  return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_IntPtr>(this)); \
96  } \
97  return 0; \
98 }
99 
100 #define UNO3_GETIMPLEMENTATION2_IMPL( classname, baseclass )\
101 UNO3_GETIMPLEMENTATION_BASE_IMPL(classname)\
102 sal_Int64 SAL_CALL classname::getSomething( const css::uno::Sequence< sal_Int8 >& rId ) \
103 { \
104  if( isUnoTunnelId<classname>(rId) ) \
105  { \
106  return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_IntPtr>(this)); \
107  } \
108  else \
109  { \
110  return baseclass::getSomething( rId ); \
111  } \
112 }
113 
114 
115 #endif // INCLUDED_COMPHELPER_SERVICEHELPER_HXX
116 
117 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool isUnoTunnelId(const css::uno::Sequence< sal_Int8 > &rId)
T * getUnoTunnelImplementation(const css::uno::Reference< css::uno::XInterface > &xIface)
const css::uno::Sequence< sal_Int8 > & getSeq() const
css::uno::Sequence< sal_Int8 > m_aSeq