Implementing grammar checker Proofreader in C++

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Implementing grammar checker Proofreader in C++

Artur Trzewik
Hi!

I want to update my grammar checker (Esperantilo) from 3.0 interface to
new 3.1 XProofreader.
It seems that there is some changes about handling XProofreader
instances,  isn't it?

By the old implementation I have used
::cppu::createSingleComponentFactory
to create XGrammarChecker component and it works fine.

No if I use this method, OO tries to create many instances of
XProofreader and it will be overloaded and unusable.
I have looked into Java LanguageTool implementation. It uses own
singleton factory.

What is the right way to do it in C++. Is
::cppu::createSingleComponentFactory not the right method to ensure
that there is only one instance of Proofreader?

In the OO documentation
http://wiki.services.openoffice.org/wiki/Uno/Cpp/Tutorials/Introduction_to_Cpp_Uno
is said not to use own singletons.

Is there any example of Grammar Checker in C++?

Regards
Artur




---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Implementing grammar checker Proofreader in C++

Bugzilla from hatapitk@iki.fi
Hi!

On Saturday 16 May 2009, Artur Trzewik wrote:
> Is there any example of Grammar Checker in C++?

Voikko has grammar checker, and we have a C++ extension for OpenOffice.org
that uses it. I'm not sure if our extension is implemented correctly (probably
not), but at least it works with OOo 3.0.1 and 3.1.

The sources can be found from
  http://voikko.svn.sourceforge.net/viewvc/voikko/trunk/ooovoikko/src/grammar/
And yes, we do have our own singleton factory method there.

Harri

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Implementing grammar checker Proofreader in C++

thomas.lange
In reply to this post by Artur Trzewik

Hi Artur,

> Hi!
>
> I want to update my grammar checker (Esperantilo) from 3.0 interface to
> new 3.1 XProofreader.
> It seems that there is some changes about handling XProofreader
> instances,  isn't it?
>
> By the old implementation I have used
> ::cppu::createSingleComponentFactory
> to create XGrammarChecker component and it works fine.
>
> No if I use this method, OO tries to create many instances of
> XProofreader and it will be overloaded and unusable.
> I have looked into Java LanguageTool implementation. It uses own
> singleton factory.
>
> What is the right way to do it in C++. Is
> ::cppu::createSingleComponentFactory not the right method to ensure
> that there is only one instance of Proofreader?
>
> In the OO documentation
> http://wiki.services.openoffice.org/wiki/Uno/Cpp/Tutorials/Introduction_to_Cpp_Uno
> is said not to use own singletons.
>
> Is there any example of Grammar Checker in C++?
>  

I use the following code:



static ::rtl::OUString MyProofreader_getImplementationName() throw()
{
    return A2OU( GRAMMARCHECKER_IMPLNAME );
}


static uno::Sequence< OUString >
MyProofreader_getSupportedServiceNames(  ) throw()
{
    uno::Sequence< OUString > aSNS( 1 );
    aSNS.getArray()[0] = A2OU( GRAMMARCHECKER_SERVICENAME );
    return aSNS;
}


static uno::Reference< uno::XInterface > SAL_CALL
MyProofreader_createInstance(
    const uno::Reference< lang::XMultiServiceFactory > & rxSMgr )
throw(uno::Exception)
{
    MyProofreader *pObj = new MyProofreader( rxSMgr );
    uno::Reference< uno::XInterface > xRes( dynamic_cast<
linguistic2::XProofreader * >(pObj) );
    pObj->Init();
    return xRes;
}
   

void * SAL_CALL MyProofreader_getFactory(
    const sal_Char *pImplName,
    lang::XMultiServiceFactory *pServiceManager,
    void * /*pRegistryKey*/ )
{
    void * pRet = 0;
    if ( !MyProofreader_getImplementationName().compareToAscii(
pImplName ) )
    {
        uno::Reference< lang::XSingleServiceFactory > xFactory =
            cppu::createOneInstanceFactory(
                pServiceManager,
                MyProofreader_getImplementationName(),
                MyProofreader_createInstance,
                MyProofreader_getSupportedServiceNames());
        // acquire, because we return an interface pointer instead of a
reference
        xFactory->acquire();
        pRet = xFactory.get();
    }
    return pRet;
}


sal_Bool SAL_CALL MyProofreader_writeInfo(
    void * /*pServiceManager*/,
    registry::XRegistryKey * pRegistryKey )
{
    try
    {
        OUString aImpl( '/' );
        aImpl += MyProofreader_getImplementationName().getStr();
        aImpl += A2OU( "/UNO/SERVICES" );
        uno::Reference< registry::XRegistryKey > xNewKey =
pRegistryKey->createKey( aImpl );
        uno::Sequence< OUString > aServices =
MyProofreader_getSupportedServiceNames();
        for( sal_Int32 i = 0; i < aServices.getLength(); i++ )
            xNewKey->createKey( aServices.getConstArray()[i] );

        return sal_True;
    }
    catch (uno::Exception &)
    {
        return sal_False;
    }
}


Maybe createOneInstanceFactory is all that you need.
Unfortunately there was no really wotking create...Component function to
do that. Because of that we used the older and actually deprecated
createOneInstanceFactory, at least that one works just fine.


Thomas



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Implementing grammar checker Proofreader in C++

Mathias Bauer
Thomas Lange - Sun Germany - ham02 - Hamburg wrote:

> Maybe createOneInstanceFactory is all that you need.
> Unfortunately there was no really wotking create...Component function to
> do that. Because of that we used the older and actually deprecated
> createOneInstanceFactory, at least that one works just fine.

The reason why the there is no "createOneInstanceComponentFactory" is
simple: OneInstanceServices are kind of deprecated, as is the old way
using ServiceFactories.

UNO now uses the concept of ComponentFactories that provide their
objects with a ComponentContext. While the factory might be implemented
in a way that there is only one instance per ComponentContext, there is
no control about how many instances of this class will be created as
basically the number of contexts is not limited. Any UNO component can
bootstrap a ComponentContext and create UNO services with it.

If you want to make sure that a particular UNO services can be
instantiated only once, use a Singleton.

But for the time being, using the code Thomas presented here is fine.
Basically this way you are creating an instance with a "default
ComponentContext" that is the backwards compatibility mode of UNO. And
then again your service will be instantiated only once. OTOH it also
means that it can't be created in any other context than the default one.

Regard,
Mathias

--
Mathias Bauer (mba) - Project Lead OpenOffice.org Writer
OpenOffice.org Engineering at Sun: http://blogs.sun.com/GullFOSS
Please don't reply to "[hidden email]".
I use it for the OOo lists and only rarely read other mails sent to it.


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]