xref: /AOO41X/main/unotools/inc/unotools/unotunnelhelper.hxx (revision bae3752ec30c258ca902793e4eea3c818b0bcaad)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _UNOTOOLS_UNOTUNNELHLP_HXX
25 #define _UNOTOOLS_UNOTUNNELHLP_HXX
26 
27 #include <com/sun/star/uno/Reference.hxx>
28 #include <com/sun/star/uno/Sequence.hxx>
29 #include <com/sun/star/lang/XUnoTunnel.hpp>
30 #include <rtl/uuid.h>
31 #include <rtl/memory.h>
32 #include <comphelper/extract.hxx>
33 
34 namespace utl
35 {
36 namespace staruno = ::com::sun::star::uno;
37 namespace starlang = ::com::sun::star::lang;
38 
39 //-----------------------------------------------------------------------------------------------------------
40 // to use the following, define
41 // sal_Bool getTunneledImplementation(Classname*& pObjImpl, staruno::Reference<starlang::XUnoTunnel> const& xObj);
42 
43 template <class Classname>
getImplementation(Classname * & pImpl,staruno::Reference<starlang::XUnoTunnel> const & xTunnel)44 sal_Bool getImplementation(Classname*& pImpl, staruno::Reference<starlang::XUnoTunnel> const& xTunnel)
45     throw(staruno::RuntimeException)
46 {
47     if (xTunnel.is())
48         return getTunneledImplementation(pImpl, xTunnel);
49 
50     pImpl = 0;
51     return sal_False;
52 }
53 
54 template <class Classname>
getImplementation(Classname * & pImpl,staruno::Reference<staruno::XInterface> const & xObj)55 sal_Bool getImplementation(Classname*& pImpl, staruno::Reference<staruno::XInterface> const& xObj)
56     throw(staruno::RuntimeException)
57 {
58     staruno::Reference<starlang::XUnoTunnel> xTunnel(xObj,staruno::UNO_QUERY);
59     if (xTunnel.is())
60         return getTunneledImplementation(pImpl, xTunnel);
61 
62     pImpl = 0;
63     return sal_False;
64 }
65 
66 template <class Classname>
getImplementation(Classname * & pImpl,staruno::Any const & aObj)67 sal_Bool getImplementation(Classname*& pImpl, staruno::Any const& aObj)
68     throw(staruno::RuntimeException)
69 {
70     staruno::Reference<starlang::XUnoTunnel> xTunnel;
71     if (cppu::extractInterface(xTunnel, aObj))
72         getTunneledImplementation(pImpl, xTunnel);
73 
74     pImpl = 0;
75     return sal_False;
76 }
77 
78 template <class Classname>
getImplementation(Classname * & pImpl,starlang::XUnoTunnel * pObj)79 sal_Bool getImplementation(Classname*& pImpl, starlang::XUnoTunnel* pObj)
80     throw(staruno::RuntimeException)
81 {
82     if (pObj)
83     {
84         staruno::Reference<starlang::XUnoTunnel> xTunnel(pObj);
85         return getTunneledImplementation(pImpl, xTunnel);
86     }
87 
88     pImpl = 0;
89     return sal_False;
90 }
91 
92 //-----------------------------------------------------------------------------------------------------------
93 
94 class UnoTunnelId
95 {
96     sal_Int8 tunnelId[16];
97 public:
UnoTunnelId(sal_Bool bUseMAC=sal_True)98     UnoTunnelId(sal_Bool bUseMAC = sal_True) throw()
99     {
100         rtl_createUuid(reinterpret_cast<sal_uInt8*>(tunnelId),0,bUseMAC);
101     }
102 
getId() const103     staruno::Sequence<sal_Int8> getId() const throw(staruno::RuntimeException)
104     {
105         return staruno::Sequence<sal_Int8>(tunnelId, sizeof(tunnelId));
106     }
107 
equalTo(staruno::Sequence<sal_Int8> const & rIdentifier)108     sal_Bool equalTo(staruno::Sequence<sal_Int8> const& rIdentifier) throw()
109     {
110         return  rIdentifier.getLength() == sizeof(tunnelId) &&
111                 rtl_compareMemory(tunnelId, rIdentifier.getConstArray(), sizeof(tunnelId)) == 0;
112     }
113 
__anon5026f3390102null114     sal_Int8 const (&getIdBytes() const)[16] { return tunnelId; }
115 };
116 
117 //-----------------------------------------------------------------------------------------------------------
118 template<class Classname>
119 class UnoTunnelImplBase
120 {
121 protected:
ThisImplementation()122     Classname* ThisImplementation() throw() { return static_cast<Classname*>(this); }
123 
makeUnoSomething()124     sal_Int64 makeUnoSomething() throw()
125     {
126         return reinterpret_cast<sal_Int64>(static_cast<void*>(ThisImplementation()));
127     }
128 
extractUnoSomething(sal_Int64 nSomething)129     static Classname* extractUnoSomething(sal_Int64 nSomething) throw()
130     {
131         if (nSomething != sal_Int64())
132             return static_cast<Classname*>(reinterpret_cast<void*>(nSomething));
133 
134         return NULL;
135     }
136 #ifdef LINUX
137 public:
138 #endif
139     static Classname*
extractUnoSomething(staruno::Reference<starlang::XUnoTunnel> const & xObj,staruno::Sequence<sal_Int8> const & rMyTunnelId)140         extractUnoSomething(
141             staruno::Reference<starlang::XUnoTunnel> const& xObj,
142             staruno::Sequence<sal_Int8> const& rMyTunnelId
143         )
144         throw(staruno::RuntimeException)
145     {
146         return xObj.is() ? extractUnoSomething(xObj->getSomething(rMyTunnelId)) : NULL;
147     }
148 };
149 //-----------------------------------------------------------------------------------------------------------
150 
151 
152 template<class Classname>
153 class UnoTunnelHelper : public UnoTunnelImplBase<Classname>
154 {
155 protected:
156     static UnoTunnelId s_aTunnelId;
157 
getSomething(staruno::Sequence<sal_Int8> const & rTunnelId)158     sal_Int64 getSomething(staruno::Sequence<sal_Int8> const& rTunnelId) throw()
159     {
160         if (s_aTunnelId.equalTo(rTunnelId))
161             return this->makeUnoSomething();
162         else
163             return sal_Int64();
164     }
165 public:
getImplementationTunnelId()166     static staruno::Sequence<sal_Int8> getImplementationTunnelId()
167         throw(staruno::RuntimeException)
168     {
169         return s_aTunnelId.getId();
170     }
171 #ifndef LINUX
getTunneledImplementation(Classname * & pImpl,staruno::Reference<starlang::XUnoTunnel> const & xObj)172     friend sal_Bool getTunneledImplementation(Classname*& pImpl, staruno::Reference<starlang::XUnoTunnel> const& xObj)
173         throw(staruno::RuntimeException)
174     {
175         pImpl = UnoTunnelHelper<Classname>::UnoTunnelHelper<Classname>::extractUnoSomething( xObj, UnoTunnelHelper<Classname>::getImplementationTunnelId() );
176 
177         return pImpl != 0;
178     }
179 #endif
180 };
181 template<class Classname>
182 UnoTunnelId UnoTunnelHelper<Classname>::s_aTunnelId;
183 
184 
185 //-----------------------------------------------------------------------------------------------------------
186 } // namespace utl
187 
188 #endif // _UNOTOOLS_UNOTUNNELHLP_HXX
189 
190