xref: /trunk/main/framework/inc/macros/xserviceinfo.hxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 __FRAMEWORK_MACROS_XSERVICEINFO_HXX_
25 #define __FRAMEWORK_MACROS_XSERVICEINFO_HXX_
26 
27 //_________________________________________________________________________________________________________________
28 //  my own includes
29 //_________________________________________________________________________________________________________________
30 
31 #include <general.h>
32 
33 //_________________________________________________________________________________________________________________
34 //  interface includes
35 //_________________________________________________________________________________________________________________
36 #include <com/sun/star/uno/Exception.hpp>
37 #include <com/sun/star/uno/RuntimeException.hpp>
38 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
39 #include <com/sun/star/lang/XServiceInfo.hpp>
40 
41 //_________________________________________________________________________________________________________________
42 //  other includes
43 //_________________________________________________________________________________________________________________
44 #include <com/sun/star/uno/Any.hxx>
45 #include <com/sun/star/uno/Reference.hxx>
46 #include <com/sun/star/uno/Sequence.hxx>
47 #include <com/sun/star/uno/Type.hxx>
48 #include <com/sun/star/uno/XComponentContext.hpp>
49 #include <com/sun/star/beans/XPropertySet.hpp>
50 #include <cppuhelper/factory.hxx>
51 #include <comphelper/sequence.hxx>
52 #include <rtl/ustring.hxx>
53 #include <rtl/logfile.hxx>
54 
55 //_________________________________________________________________________________________________________________
56 //  namespace
57 //_________________________________________________________________________________________________________________
58 
59 namespace framework{
60 
61 /*_________________________________________________________________________________________________________________
62 
63     macros for declaration and definition of XServiceInfo
64     Please use follow public macros only!
65 
66     1)  DECLARE_XSERVICEINFO                                                                                => use it to declare XServiceInfo in your header
67     2)  DEFINE_XSERVICEINFO_MULTISERVICE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME )          => use it to define XServiceInfo for multi service mode
68     3)  DEFINE_XSERVICEINFO_ONEINSTANCESERVICE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME )    => use it to define XServiceInfo for one instance service mode
69     4)  DEFINE_INIT_SERVICE( CLASS )                                                                        => use it to implement your own impl_initService() method, which is necessary for initializing object by using his own reference!
70 
71 _________________________________________________________________________________________________________________*/
72 
73 //*****************************************************************************************************************
74 //  private
75 //  implementation of XServiceInfo and helper functions
76 //*****************************************************************************************************************
77 #define PRIVATE_DEFINE_XSERVICEINFO_BASE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME )                                                  \
78     /*===========================================================================================================*/                                 \
79     /* XServiceInfo                                                                                              */                                 \
80     /*===========================================================================================================*/                                 \
81     ::rtl::OUString SAL_CALL CLASS::getImplementationName()                                                     \
82     {                                                                                                                                               \
83         return impl_getStaticImplementationName();                                                                                                  \
84     }                                                                                                                                               \
85                                                                                                                                                     \
86     /*===========================================================================================================*/                                 \
87     /* XServiceInfo                                                                                              */                                 \
88     /*===========================================================================================================*/                                 \
89     sal_Bool SAL_CALL CLASS::supportsService( const ::rtl::OUString& sServiceName )                             \
90     {                                                                                                                                               \
91         return ::comphelper::findValue(getSupportedServiceNames(), sServiceName, sal_True).getLength() != 0;                                        \
92     }                                                                                                                                               \
93                                                                                                                                                     \
94     /*===========================================================================================================*/                                 \
95     /* XServiceInfo                                                                                              */                                 \
96     /*===========================================================================================================*/                                 \
97     css::uno::Sequence< ::rtl::OUString > SAL_CALL CLASS::getSupportedServiceNames()                            \
98     {                                                                                                                                               \
99         return impl_getStaticSupportedServiceNames();                                                                                               \
100     }                                                                                                                                               \
101                                                                                                                                                     \
102     /*===========================================================================================================*/                                 \
103     /* Helper for XServiceInfo                                                                                   */                                 \
104     /*===========================================================================================================*/                                 \
105     css::uno::Sequence< ::rtl::OUString > CLASS::impl_getStaticSupportedServiceNames()                                                              \
106     {                                                                                                                                               \
107         css::uno::Sequence< ::rtl::OUString > seqServiceNames( 1 );                                                                                 \
108         seqServiceNames.getArray() [0] = SERVICENAME ;                                                                                              \
109         return seqServiceNames;                                                                                                                     \
110     }                                                                                                                                               \
111                                                                                                                                                     \
112     /*===========================================================================================================*/                                 \
113     /* Helper for XServiceInfo                                                                                   */                                 \
114     /*===========================================================================================================*/                                 \
115     ::rtl::OUString CLASS::impl_getStaticImplementationName()                                                                                       \
116     {                                                                                                                                               \
117         return IMPLEMENTATIONNAME ;                                                                                                                 \
118     }
119 
120 #define PRIVATE_DEFINE_XSERVICEINFO_OLDSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME )                                              \
121     PRIVATE_DEFINE_XSERVICEINFO_BASE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME )                                                      \
122     /*===========================================================================================================*/                                 \
123     /* Helper for registry                                                                                       */                                 \
124     /* Attention: To avoid against wrong ref counts during our own initialize procedure, we must                 */                                 \
125     /*            use right EXTERNAL handling of them. That's why you should do nothing in your ctor, which could*/                                 \
126     /*            work on your ref count! All other things are allowed. Do work with your own reference - please */                                 \
127     /*            use "impl_initService()" method.                                                               */                                 \
128     /*===========================================================================================================*/                                 \
129     css::uno::Reference< css::uno::XInterface > SAL_CALL CLASS::impl_createInstance( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager )  \
130     {                                                                                                                                                                                              \
131         RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework","Ocke.Janssen@sun.com",U2B(IMPLEMENTATIONNAME).getStr());                                                                                                               \
132         /* create new instance of service */                                                                                                                                                       \
133         CLASS* pClass = new CLASS( xServiceManager );                                                                                                                                              \
134         /* hold it alive by increasing his ref count!!! */                                                                                                                                         \
135         css::uno::Reference< css::uno::XInterface > xService( static_cast< XINTERFACECAST* >(pClass), css::uno::UNO_QUERY );                                                                       \
136         /* initialize new service instance ... he can use his own refcount ... we hold it! */                                                                                                      \
137         pClass->impl_initService();                                                                                                                                                                \
138         /* return new created service as reference */                                                                                                                                              \
139         return xService;                                                                                                                                                                           \
140     }
141 
142 #define PRIVATE_DEFINE_XSERVICEINFO_NEWSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME )                                              \
143     PRIVATE_DEFINE_XSERVICEINFO_BASE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME )                                                      \
144     /*===========================================================================================================*/                                 \
145     /* Helper for registry                                                                                       */                                 \
146     /* Attention: To avoid against wrong ref counts during our own initialize procedure, we must                 */                                 \
147     /*            use right EXTERNAL handling of them. That's why you should do nothing in your ctor, which could*/                                 \
148     /*            work on your ref count! All other things are allowed. Do work with your own reference - please */                                 \
149     /*            use "impl_initService()" method.                                                               */                                 \
150     /*===========================================================================================================*/                                 \
151     css::uno::Reference< css::uno::XInterface > SAL_CALL CLASS::impl_createInstance( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager )\
152     {                                                                                                                                                               \
153         /* retrieve component context from the given service manager */                                                                                             \
154         static const ::rtl::OUString PROP_DEFAULTCONTEXT = ::rtl::OUString::createFromAscii("DefaultContext");                                                      \
155         css::uno::Reference< css::beans::XPropertySet >    xSMGRProps(xServiceManager, css::uno::UNO_QUERY_THROW);                                                  \
156         css::uno::Reference< css::uno::XComponentContext > xComponentContext;                                                                                       \
157         xSMGRProps->getPropertyValue( PROP_DEFAULTCONTEXT ) >>= xComponentContext;                                                                                  \
158         /* create new instance of service */                                                                                                                        \
159         CLASS* pClass = new CLASS( xComponentContext );                                                                                                             \
160         /* hold it alive by increasing his ref count!!! */                                                                                                          \
161         css::uno::Reference< css::uno::XInterface > xService( static_cast< XINTERFACECAST* >(pClass), css::uno::UNO_QUERY );                                        \
162         /* initialize new service instance ... he can use his own refcount ... we hold it! */                                                                       \
163         pClass->impl_initService();                                                                                                                                 \
164         /* return new created service as reference */                                                                                                               \
165         return xService;                                                                                                                                            \
166     }
167 
168 //*****************************************************************************************************************
169 //  private
170 //  definition of helper function createFactory() for multiple services
171 //*****************************************************************************************************************
172 #define PRIVATE_DEFINE_SINGLEFACTORY( CLASS )                                                                                                                           \
173     css::uno::Reference< css::lang::XSingleServiceFactory > CLASS::impl_createFactory( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager )  \
174     {                                                                                                                                                                   \
175         css::uno::Reference< css::lang::XSingleServiceFactory > xReturn ( cppu::createSingleFactory (   xServiceManager                             ,                   \
176                                                                                                         CLASS::impl_getStaticImplementationName()   ,                   \
177                                                                                                         CLASS::impl_createInstance                  ,                   \
178                                                                                                         CLASS::impl_getStaticSupportedServiceNames()                    \
179                                                                                                     )                                                                   \
180                                                                         );                                                                                              \
181         return xReturn;                                                                                                                                                 \
182     }
183 
184 //*****************************************************************************************************************
185 //  private
186 //  definition of helper function createFactory() for one instance services
187 //*****************************************************************************************************************
188 #define PRIVATE_DEFINE_ONEINSTANCEFACTORY( CLASS )                                                                                                                      \
189     css::uno::Reference< css::lang::XSingleServiceFactory > CLASS::impl_createFactory( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager )  \
190     {                                                                                                                                                                   \
191         css::uno::Reference< css::lang::XSingleServiceFactory > xReturn ( cppu::createOneInstanceFactory    (   xServiceManager                             ,           \
192                                                                                                                 CLASS::impl_getStaticImplementationName()   ,           \
193                                                                                                                 CLASS::impl_createInstance                  ,           \
194                                                                                                                 CLASS::impl_getStaticSupportedServiceNames()            \
195                                                                                                             )                                                           \
196                                                                         );                                                                                              \
197         return xReturn;                                                                                                                                                 \
198     }
199 
200 //*****************************************************************************************************************
201 //  public
202 //  declaration of XServiceInfo and helper functions
203 //*****************************************************************************************************************
204 #define DECLARE_XSERVICEINFO                                                                                                                                                                                                            \
205     /* interface XServiceInfo */                                                                                                                                                                                                        \
206     virtual ::rtl::OUString                                        SAL_CALL getImplementationName              (                                                                               );   \
207     virtual sal_Bool                                               SAL_CALL supportsService                    ( const ::rtl::OUString&                                        sServiceName    );   \
208     virtual css::uno::Sequence< ::rtl::OUString >                  SAL_CALL getSupportedServiceNames           (                                                                               );   \
209     /* Helper for XServiceInfo */                                                                                                                                                                                                       \
210     static css::uno::Sequence< ::rtl::OUString >                   SAL_CALL impl_getStaticSupportedServiceNames(                                                                               );                                       \
211     static ::rtl::OUString                                         SAL_CALL impl_getStaticImplementationName   (                                                                               );                                       \
212     /* Helper for registry */                                                                                                                                                                                                           \
213     static css::uno::Reference< css::uno::XInterface >             SAL_CALL impl_createInstance                ( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager );          \
214     static css::uno::Reference< css::lang::XSingleServiceFactory > SAL_CALL impl_createFactory                 ( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager );                                       \
215     /* Helper for initialization of service by using own reference! */                                                                                                                                                                  \
216     virtual void                                                   SAL_CALL impl_initService                   (                                                                               );                                       \
217 
218 //*****************************************************************************************************************
219 //  public
220 //  implementation of XServiceInfo
221 //*****************************************************************************************************************
222 #define DEFINE_XSERVICEINFO_MULTISERVICE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME )              \
223     PRIVATE_DEFINE_XSERVICEINFO_OLDSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME )              \
224     PRIVATE_DEFINE_SINGLEFACTORY( CLASS )
225 
226 #define DEFINE_XSERVICEINFO_ONEINSTANCESERVICE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME )        \
227     PRIVATE_DEFINE_XSERVICEINFO_OLDSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME )              \
228     PRIVATE_DEFINE_ONEINSTANCEFACTORY( CLASS )
229 
230 #define DEFINE_XSERVICEINFO_MULTISERVICE_2( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME )            \
231     PRIVATE_DEFINE_XSERVICEINFO_NEWSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME )              \
232     PRIVATE_DEFINE_SINGLEFACTORY( CLASS )
233 
234 #define DEFINE_XSERVICEINFO_ONEINSTANCESERVICE_2( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME )      \
235     PRIVATE_DEFINE_XSERVICEINFO_NEWSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME )              \
236     PRIVATE_DEFINE_ONEINSTANCEFACTORY( CLASS )
237 
238 //*****************************************************************************************************************
239 //  public
240 //  implementation of service initialize!
241 //  example of using:   DEFINE_INIT_SERVICE( MyClassName,
242 //                          {
243 //                              ...
244 //                              Reference< XInterface > xThis( this, UNO_QUERY );
245 //                              myMember* pMember = new myMember( xThis );
246 //                              ...
247 //                          }
248 //                      )
249 //*****************************************************************************************************************
250 #define DEFINE_INIT_SERVICE( CLASS, FUNCTIONBODY )                                                              \
251     void SAL_CALL CLASS::impl_initService()                                                                     \
252     {                                                                                                           \
253         FUNCTIONBODY                                                                                            \
254     }
255 
256 #define DEFINE_INIT_SERVICE_WITH_BASECLASS( CLASS, BASECLASS, FUNCTIONBODY )                                    \
257     void SAL_CALL CLASS::impl_initService()                                                                     \
258     {                                                                                                           \
259         BASECLASS::impl_initService();                                                                          \
260         {                                                                                                       \
261             FUNCTIONBODY                                                                                        \
262         }                                                                                                       \
263     }
264 
265 }       //  namespace framework
266 
267 #endif  //  #ifndef __FRAMEWORK_MACROS_XSERVICEINFO_HXX_
268