Week 23 contribution of PDK documentation content. See release notes for details. Fixes bugs Bug 2714, Bug 462.
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
<!-- This component and the accompanying materials are made available under the terms of the License
"Eclipse Public License v1.0" which accompanies this distribution,
and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
<!-- Initial Contributors:
Nokia Corporation - initial contribution.
Contributors:
-->
<!DOCTYPE concept
PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
<concept id="GUID-C73318E7-44E2-5F42-BDB0-AAE9BD72BF5C" xml:lang="en"><title>Using
TCallBack</title><shortdesc>This document describes the TCallBack class.</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody>
<p>The class encapsulates a pointer to a function which takes an argument
of type <codeph>TAny*</codeph> and returns a <codeph>TInt</codeph>. The class
is generally useful but, in particular, simplifies the programming interface
of the <codeph>CIdle</codeph> and <codeph>CPeriodic</codeph> classes.</p>
<p>Given a suitable function and a pointer to an object, a callback is constructed
simply. The function must be a non-member function or a static member of a
class. For example:</p>
<codeblock id="GUID-B70261EB-D95A-52EE-954E-D7062BF8A7EF" xml:space="preserve">TInt Foo(TAny *); // a non-member function
X* pX=new X; // a class X object</codeblock>
<p>or, as a static member of class <codeph>X</codeph>:</p>
<codeblock id="GUID-DA631174-BFA6-5348-9807-5417DCA17523" xml:space="preserve">TInt X::Foo(TAny *); // a static function of class X
X* pX=new X; // a class X object</codeblock>
<p>A callback function returns a true value to indicate whether it should
be called again. This is important when used with the <codeph>CIdle</codeph> and <codeph>CPeriodic</codeph> classes.
The following code fragment shows the programming paradigm:</p>
<codeblock id="GUID-58BF3CFB-D8E3-549B-B30F-568F2C566295" xml:space="preserve">TCallBack cb(Foo,pX); // construction of the callback</codeblock>
<codeblock id="GUID-3B88A62E-0350-53DF-9479-D5749D96E2FD" xml:space="preserve">for (;;)
{
if (!cb.CallBack()) // invoke callback until it returns
{ // a false value
break;
}
}</codeblock>
<p>Calling <codeph>cb.CallBack()</codeph> results in a call to the callback
function <codeph>Foo()</codeph> passing it the pointer <codeph>pX</codeph>.</p>
<p>A common requirement is for the callback function to be a non-static member
of a class. This can be implemented by passing, to the callback function,
a pointer to an instance of the class of which it is a static member. For
example:</p>
<codeblock id="GUID-85DDB62D-FDDC-5B04-9F9C-D5C20C8735DB" xml:space="preserve">class X
{
static X* NewL();
static TInt Foo(TAny* pX);
private:
TInt DoFoo();
...
}</codeblock>
<p>where the static function <codeph>Foo()</codeph> is implemented as:</p>
<codeblock id="GUID-D3FD2FA5-960D-540C-9BCA-0206004CC92C" xml:space="preserve">static TInt X::Foo(TAny* pX)
{
return ((X*)pX)->DoFoo();
}</codeblock>
<p>Typically, create an instance of class <codeph>X</codeph> and, at some
later stage, create the callback:</p>
<codeblock id="GUID-4E685C8E-9185-50FA-9402-A2B4351C3AEA" xml:space="preserve">...
X* pX = X::NewL();
...
TCallBack cb(Foo,pX);
...</codeblock>
</conbody></concept>