1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef COMPHELPER_COMPONENTCONTEXT_HXX
21 #define COMPHELPER_COMPONENTCONTEXT_HXX
23 #include <comphelper/comphelperdllapi.h>
25 #include <com/sun/star/uno/XComponentContext.hpp>
26 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28 //........................................................................
31 //........................................................................
33 //====================================================================
35 //====================================================================
36 /** a helper class for working with a component context
38 class COMPHELPER_DLLPUBLIC ComponentContext
41 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
> m_xContext
;
42 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiComponentFactory
> m_xORB
;
45 /** constructs an instance
47 the component context to manage
48 @throws ::com::sun::star::lang::NullPointerException
49 if the given context, or its component factory, are <NULL/>
51 ComponentContext( const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& _rxContext
);
53 /** constructs an instance
54 @param _rxLegacyFactory
55 the legacy service factor to obtain the <type scope="com::sun::star::uno">XComponentContext</type> from
56 @throws ::com::sun::star::uno::RuntimeException
57 if the given factory or does not have a DefaultContext property to obtain
59 @throws ::com::sun::star::lang::NullPointerException
60 if the given factory is <NULL/>, or provides a component context being <NULL/>, or provides
61 a component context whose component factory is <NULL/>
63 ComponentContext( const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& _rxLegacyFactory
);
65 /** returns the ->XComponentContext interface
67 inline ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>
68 getUNOContext() const { return m_xContext
; }
70 /** determines whether the context is not <NULL/>
72 inline sal_Bool
is() const
74 return m_xContext
.is();
77 /** creates a component using our component factory/context
78 @throws ::com::sun::star::uno::Exception
80 <TRUE/> if and only if the component could be successfully created
82 template < typename INTERFACE
>
83 bool createComponent( const OUString
& _rServiceName
, ::com::sun::star::uno::Reference
< INTERFACE
>& _out_rxComponent
) const
85 _out_rxComponent
.clear();
86 _out_rxComponent
= _out_rxComponent
.query(
87 m_xORB
->createInstanceWithContext( _rServiceName
, m_xContext
)
89 return _out_rxComponent
.is();
92 /** creates a component using our component factory/context
93 @throws ::com::sun::star::uno::Exception
95 <TRUE/> if and only if the component could be successfully created
97 template < typename INTERFACE
>
98 bool createComponent( const sal_Char
* _pAsciiServiceName
, ::com::sun::star::uno::Reference
< INTERFACE
>& _out_rxComponent
) const
100 return createComponent( OUString::createFromAscii( _pAsciiServiceName
), _out_rxComponent
);
103 /** creates a component using our component factory/context, passing creation arguments
104 @throws ::com::sun::star::uno::Exception
106 <TRUE/> if and only if the component could be successfully created
108 template < typename INTERFACE
>
109 bool createComponentWithArguments( const OUString
& _rServiceName
, const ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
>& _rArguments
, ::com::sun::star::uno::Reference
< INTERFACE
>& _out_rxComponent
) const
111 _out_rxComponent
.clear();
112 _out_rxComponent
= _out_rxComponent
.query(
113 m_xORB
->createInstanceWithArgumentsAndContext( _rServiceName
, _rArguments
, m_xContext
)
115 return _out_rxComponent
.is();
118 /** creates a component using our component factory/context, passing creation arguments
119 @throws ::com::sun::star::uno::Exception
121 <TRUE/> if and only if the component could be successfully created
123 template < typename INTERFACE
>
124 bool createComponentWithArguments( const sal_Char
* _pAsciiServiceName
, const ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
>& _rArguments
, ::com::sun::star::uno::Reference
< INTERFACE
>& _out_rxComponent
) const
126 return createComponentWithArguments( OUString::createFromAscii( _pAsciiServiceName
), _rArguments
, _out_rxComponent
);
129 /** creates a component using our component factory/context
131 @throws ::com::sun::star::lang::ServiceNotRegisteredException
132 if the given service is not registered
134 if an exception occurred during creating the component
136 the newly created component. Is never <NULL/>.
138 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
> createComponent( const OUString
& _rServiceName
) const;
140 /** creates a component using our component factory/context
142 @throws ::com::sun::star::lang::ServiceNotRegisteredException
143 if the given service is not registered
145 if an exception occurred during creating the component
147 the newly created component. Is never <NULL/>.
149 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
> createComponent( const sal_Char
* _pAsciiServiceName
) const
151 return createComponent( OUString::createFromAscii( _pAsciiServiceName
) );
154 /** creates a component using our component factory/context, passing creation arguments
156 @throws ::com::sun::star::lang::ServiceNotRegisteredException
157 if the given service is not registered
159 if an exception occurred during creating the component
161 the newly created component. Is never <NULL/>.
163 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
> createComponentWithArguments(
164 const OUString
& _rServiceName
,
165 const ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
>& _rArguments
168 /** creates a component using our component factory/context, passing creation arguments
170 @throws ::com::sun::star::lang::ServiceNotRegisteredException
171 if the given service is not registered
173 if an exception occurred during creating the component
175 the newly created component. Is never <NULL/>.
177 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
> createComponentWithArguments(
178 const sal_Char
* _pAsciiServiceName
,
179 const ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
>& _rArguments
182 return createComponentWithArguments( OUString::createFromAscii( _pAsciiServiceName
), _rArguments
);
185 /** retrieves a singleton instance from the context
187 Singletons are collected below the <code>/singletons</code> key in a component context,
188 so accessing them means retrieving the value under <code>/singletons/<instance_name></code>.
190 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
> getSingleton( const OUString
& _rInstanceName
) const;
192 /** retrieves a singleton instance from the context
194 Singletons are collected below the <code>/singletons</code> key in a component context,
195 so accessing them means retrieving the value under <code>/singletons/<instance_name></code>.
197 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
> getSingleton( const sal_Char
* _pAsciiInstanceName
) const
199 return getSingleton( OUString::createFromAscii( _pAsciiInstanceName
) );
202 /** returns the ->XMultiServiceFactory interface of ->m_xORB, for passing to
203 older code which does not yet support ->XMultiComponentFactory
204 @throws ::com::sun::star::uno::RuntimeException
205 if our our component factory does not support this interface
207 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>
208 getLegacyServiceFactory() const;
210 /** retrieves a value from our component context
212 the name of the value to retrieve
214 the context value with the given name
215 @seealso XComponentContext::getValueByName
216 @seealso getContextValueByAsciiName
218 ::com::sun::star::uno::Any
219 getContextValueByName( const OUString
& _rName
) const;
221 /** retrieves a value from our component context, specified by 8-bit ASCII string
223 the name of the value to retrieve, as ASCII character string
225 the context value with the given name
226 @seealso XComponentContext::getValueByName
227 @seealso getContextValueByName
229 inline ::com::sun::star::uno::Any
230 getContextValueByAsciiName( const sal_Char
* _pAsciiName
) const
232 return getContextValueByName( OUString::createFromAscii( _pAsciiName
) );
237 //........................................................................
238 } // namespace comphelper
239 //........................................................................
241 #endif // COMPHELPER_COMPONENTCONTEXT_HXX
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */