Difference between revisions of "HelperNotHelper"

From Apache OpenOffice Wiki
Jump to: navigation, search
 
(12 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 +
{{Documentation/Candidate}}
 +
{{NeedsRework|EN}}
 
It's important to figure out the difference beween class implementing component when you use or not an helper.  
 
It's important to figure out the difference beween class implementing component when you use or not an helper.  
 
= Class without Helper =
 
= Class without Helper =
 
Here is the C++ code
 
Here is the C++ code
−
<source lang="cpp">
+
<syntaxhighlight lang="cpp">
−
class MyCounterImpl
+
class MyCounterImpl : public XCountable, public XServiceInfo
−
: public XCountable
 
−
, public XServiceInfo
 
 
{
 
{
 
sal_Int32 m_nRefCount;
 
sal_Int32 m_nRefCount;
 
sal_Int32 m_nCount;
 
sal_Int32 m_nCount;
 
public:
 
public:
−
MyCounterImpl( const Reference< XMultiServiceFactory > & xServiceManager )
+
MyCounterImpl( const Reference< XMultiServiceFactory > & xServiceManager );
−
: m_xServiceManager( xServiceManager ), m_nRefCount( 0 )
+
~MyCounterImpl();
−
{ printf( "< MyCounterImpl ctor called >\n" ); }
 
−
~MyCounterImpl()
 
−
{ printf( "< MyCounterImpl dtor called >\n" ); }
 
  
 
// XInterface implementation
 
// XInterface implementation
−
virtual void SAL_CALL acquire() throw ()
+
virtual void SAL_CALL acquire() throw ();
−
{ ++m_nRefCount; }
+
virtual void SAL_CALL release() throw ();
−
virtual void SAL_CALL release() throw ()
+
virtual Any SAL_CALL queryInterface( const Type & rType ) throw (RuntimeException);
−
{ if (! --m_nRefCount) delete this; }
+
−
virtual Any SAL_CALL queryInterface( const Type & rType ) throw (RuntimeException)
 
−
{ return cppu::queryInterface(rType, static_cast< XInterface* >
 
−
                                          ( static_cast< XServiceInfo* >( this ) ),
 
−
            static_cast< XCountable* >( this ),
 
−
                                      static_cast< XServiceInfo* >( this ) ); }
 
−
 
 
 
// XServiceInfo implementation
 
// XServiceInfo implementation
 
     virtual OUString SAL_CALL getImplementationName(  ) throw(RuntimeException);
 
     virtual OUString SAL_CALL getImplementationName(  ) throw(RuntimeException);
Line 34: Line 25:
  
 
// XCountable implementation
 
// XCountable implementation
−
virtual sal_Int32 SAL_CALL getCount() throw (RuntimeException)
+
virtual sal_Int32 SAL_CALL getCount() throw (RuntimeException);
−
{ return m_nCount; }
+
virtual void SAL_CALL setCount( sal_Int32 nCount ) throw (RuntimeException);
−
virtual void SAL_CALL setCount( sal_Int32 nCount ) throw (RuntimeException)
+
virtual sal_Int32 SAL_CALL increment() throw (RuntimeException);
−
{ m_nCount = nCount; }
+
virtual sal_Int32 SAL_CALL decrement() throw (RuntimeException);
−
virtual sal_Int32 SAL_CALL increment() throw (RuntimeException)
 
−
{ return (++m_nCount); }
 
−
virtual sal_Int32 SAL_CALL decrement() throw (RuntimeException)
 
−
{ return (--m_nCount); }
 
 
};
 
};
−
</source>
+
</syntaxhighlight>
 +
{{Note|As you can see, the class "MyCounterImpl" is derived from XCountable and XServiceInfo, but also from XInterface even if not shown in inheritance part.}}
  
 
= Class with an Helper =
 
= Class with an Helper =
Line 49: Line 37:
 
Here is the code
 
Here is the code
  
−
<source lang="cpp">
+
<syntaxhighlight lang="cpp">
−
class MyCounterImpl : public ::cppu::WeakImplHelper2<
+
class MyCounterImpl : public ::cppu::WeakImplHelper2< XCountable, XServiceInfo >
−
      XCountable, XServiceInfo >
 
 
{
 
{
 
// to obtain other services if needed
 
// to obtain other services if needed
 
Reference< XMultiServiceFactory > m_xServiceManager;
 
Reference< XMultiServiceFactory > m_xServiceManager;
−
 
 
sal_Int32 m_nRefCount;
 
sal_Int32 m_nRefCount;
 
sal_Int32 m_nCount;
 
sal_Int32 m_nCount;
Line 67: Line 53:
  
 
// XCountable implementation
 
// XCountable implementation
−
virtual sal_Int32 SAL_CALL getCount() throw (RuntimeException)
+
virtual sal_Int32 SAL_CALL getCount() throw (RuntimeException);
−
{ return m_nCount; }
+
virtual void SAL_CALL setCount( sal_Int32 nCount ) throw (RuntimeException);
−
virtual void SAL_CALL setCount( sal_Int32 nCount ) throw (RuntimeException)
+
virtual sal_Int32 SAL_CALL increment() throw (RuntimeException);
−
{ m_nCount = nCount; }
+
virtual sal_Int32 SAL_CALL decrement() throw (RuntimeException);
−
virtual sal_Int32 SAL_CALL increment() throw (RuntimeException)
 
−
{ return (++m_nCount); }
 
−
virtual sal_Int32 SAL_CALL decrement() throw (RuntimeException)
 
−
{ return (--m_nCount); }
 
 
};
 
};
−
</source>
+
</syntaxhighlight>
 +
 
 +
Go back to [[Constructing_Components#Results_2|Constructing Components]].

Latest revision as of 12:21, 5 June 2026


Edit-find-replace.png This article should be checked for accuracy and conformity to style.

It's important to figure out the difference beween class implementing component when you use or not an helper.

Class without Helper

Here is the C++ code

class MyCounterImpl : public XCountable, public XServiceInfo
{
	sal_Int32 m_nRefCount;
	sal_Int32 m_nCount;	
public:
	MyCounterImpl( const Reference< XMultiServiceFactory > & xServiceManager );
	~MyCounterImpl();

// XInterface implementation
	virtual void SAL_CALL acquire() throw ();
	virtual void SAL_CALL release() throw ();
	virtual Any SAL_CALL queryInterface( const Type & rType ) throw (RuntimeException);
		
// XServiceInfo	implementation
    virtual OUString SAL_CALL getImplementationName(  ) throw(RuntimeException);
    virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(RuntimeException);
    virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(  ) throw(RuntimeException);
    static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static(  );

// XCountable implementation
	virtual sal_Int32 SAL_CALL getCount() throw (RuntimeException);
	virtual void SAL_CALL setCount( sal_Int32 nCount ) throw (RuntimeException);
	virtual sal_Int32 SAL_CALL increment() throw (RuntimeException);
	virtual sal_Int32 SAL_CALL decrement() throw (RuntimeException);
};
Documentation note.png As you can see, the class "MyCounterImpl" is derived from XCountable and XServiceInfo, but also from XInterface even if not shown in inheritance part.

Class with an Helper

Here is the code

class MyCounterImpl : public ::cppu::WeakImplHelper2< XCountable, XServiceInfo >
{
	// to obtain other services if needed
	Reference< XMultiServiceFactory > m_xServiceManager;
	sal_Int32 m_nRefCount;
	sal_Int32 m_nCount;
	
public:
// XServiceInfo	implementation
    virtual OUString SAL_CALL getImplementationName(  ) throw(RuntimeException);
    virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(RuntimeException);
    virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(  ) throw(RuntimeException);
    static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static(  );

// XCountable implementation
	virtual sal_Int32 SAL_CALL getCount() throw (RuntimeException);
	virtual void SAL_CALL setCount( sal_Int32 nCount ) throw (RuntimeException);
	virtual sal_Int32 SAL_CALL increment() throw (RuntimeException);
	virtual sal_Int32 SAL_CALL decrement() throw (RuntimeException);
};

Go back to Constructing Components.

Personal tools