New-style
service constructors in OpenOffice.org Basic
Starting with OpenOffice.org 3.2
OpenOffice.org Basic allows to use UNO new-style service constructors
(for more details please see
http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/ProUNO/Services).
To achieve this UNO services are now
mapped to OpenOffice.org Basic objects. They have to be addressed by
using the complete UNO namespace path. Let's take the example from
the "Service Constructors" section in the wiki page
mentioned above completed by a dummy module:
module com { module sun { module star { module foo {
service SomeService: XSomeInterface {
create1();
create2([in] long arg1, [in] string arg2);
create3([in] any... rest);
};
}; }; }; };
Then the service object can be addressed like this:
Dim oSomeServiceObj
oSomeServiceObj = com.sun.star.foo.SomeService
All constructors defined for a new-style service are available as methods of its corresponding
OpenOffice.org Basic object, e.g.:
Dim oSomeInstance As Object
oSomeInstance = oSomeServiceObj.create1()
' or
oSomeInstance = oSomeServiceObj.create2( 42, "Hello" )
' or
oSomeInstance = oSomeServiceObj.create3( aVarOfAnyType )
Internally the parameters are passed to the createInstanceWithArgumentsAndContext
method of com.sun.star.lang.XMultiComponentFactory as Arguments
sequence. The UNO default context is used both to obtain the Multi
Component Factory and as Context parameter.
This is the simple version. To give
more control to the user there's also another way to call service
constructors similar to the one used in the C++
mapping of service constructors. In this version the UNO context
can be passed explicitly to the constructor method. Then the code
would look like this:
Dim oMyContext As Object
oMyContext = GetContextFromSomewhere()
Dim oSomeInstance As Object
oSomeInstance = oSomeServiceObj.create1( oMyContext )
' or
oSomeInstance = oSomeServiceObj.create2( oMyContext, 42, "Hello" )
' or
oSomeInstance = oSomeServiceObj.create3( oMyContext, aVarOfAnyType )
In this case the passed context is used internally instead of the UNO default context.
The OpenOffice.org Basic runtime
automatically chooses the appropriate version by checking the first
parameter's type. If the first parameter supports
com.sun.star.uno.XComponentContext
and the parameter count exceeds the one of the used constructor the
second version is used.
This can become a problem in one
special case: If a constructor has a rest parameter and expects an
object supporting XComponentContext as argument it becomes ambiguous
which version should be used. It could be a call to the constructor
with the intention to pass the context as an argument to the service
(case 1) or with the intention to pass the context to
createInstanceWithArgumentsAndContext with no argument for the
service (case 2).
The solution: In case 1 it doesn't
matter if no or one context is passed as parameter:
oSomeInstance = oSomeServiceObj.create3()
oSomeInstance = oSomeServiceObj.create3( oMyContext )
oMyContext is only used for createInstanceWithArgumentsAndContext and not passed as argument to
the service.
For case 2 the context has to be
passed twice
oSomeInstance = oSomeServiceObj.create3( oMyContext, oMyContext )
Then the second context parameter is
passed to the service. This may look a little bit strange, but it's a
rather exotic scenario anyway.
If a new-style service only has an
implicit constructor it's mapped to a method "create"
without parameters in OpenOffice.org Basic.