|
1 <?xml version="1.0" encoding="utf-8"?> |
|
2 <!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. --> |
|
3 <!-- This component and the accompanying materials are made available under the terms of the License |
|
4 "Eclipse Public License v1.0" which accompanies this distribution, |
|
5 and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". --> |
|
6 <!-- Initial Contributors: |
|
7 Nokia Corporation - initial contribution. |
|
8 Contributors: |
|
9 --> |
|
10 <!DOCTYPE concept |
|
11 PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd"> |
|
12 <concept id="GUID-C73318E7-44E2-5F42-BDB0-AAE9BD72BF5C" xml:lang="en"><title>Using |
|
13 TCallBack</title><shortdesc>This document describes the TCallBack class.</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody> |
|
14 <p>The class encapsulates a pointer to a function which takes an argument |
|
15 of type <codeph>TAny*</codeph> and returns a <codeph>TInt</codeph>. The class |
|
16 is generally useful but, in particular, simplifies the programming interface |
|
17 of the <codeph>CIdle</codeph> and <codeph>CPeriodic</codeph> classes.</p> |
|
18 <p>Given a suitable function and a pointer to an object, a callback is constructed |
|
19 simply. The function must be a non-member function or a static member of a |
|
20 class. For example:</p> |
|
21 <codeblock id="GUID-B70261EB-D95A-52EE-954E-D7062BF8A7EF" xml:space="preserve">TInt Foo(TAny *); // a non-member function |
|
22 X* pX=new X; // a class X object</codeblock> |
|
23 <p>or, as a static member of class <codeph>X</codeph>:</p> |
|
24 <codeblock id="GUID-DA631174-BFA6-5348-9807-5417DCA17523" xml:space="preserve">TInt X::Foo(TAny *); // a static function of class X |
|
25 X* pX=new X; // a class X object</codeblock> |
|
26 <p>A callback function returns a true value to indicate whether it should |
|
27 be called again. This is important when used with the <codeph>CIdle</codeph> and <codeph>CPeriodic</codeph> classes. |
|
28 The following code fragment shows the programming paradigm:</p> |
|
29 <codeblock id="GUID-58BF3CFB-D8E3-549B-B30F-568F2C566295" xml:space="preserve">TCallBack cb(Foo,pX); // construction of the callback</codeblock> |
|
30 <codeblock id="GUID-3B88A62E-0350-53DF-9479-D5749D96E2FD" xml:space="preserve">for (;;) |
|
31 { |
|
32 if (!cb.CallBack()) // invoke callback until it returns |
|
33 { // a false value |
|
34 break; |
|
35 } |
|
36 }</codeblock> |
|
37 <p>Calling <codeph>cb.CallBack()</codeph> results in a call to the callback |
|
38 function <codeph>Foo()</codeph> passing it the pointer <codeph>pX</codeph>.</p> |
|
39 <p>A common requirement is for the callback function to be a non-static member |
|
40 of a class. This can be implemented by passing, to the callback function, |
|
41 a pointer to an instance of the class of which it is a static member. For |
|
42 example:</p> |
|
43 <codeblock id="GUID-85DDB62D-FDDC-5B04-9F9C-D5C20C8735DB" xml:space="preserve">class X |
|
44 { |
|
45 static X* NewL(); |
|
46 static TInt Foo(TAny* pX); |
|
47 private: |
|
48 TInt DoFoo(); |
|
49 ... |
|
50 }</codeblock> |
|
51 <p>where the static function <codeph>Foo()</codeph> is implemented as:</p> |
|
52 <codeblock id="GUID-D3FD2FA5-960D-540C-9BCA-0206004CC92C" xml:space="preserve">static TInt X::Foo(TAny* pX) |
|
53 { |
|
54 return ((X*)pX)->DoFoo(); |
|
55 }</codeblock> |
|
56 <p>Typically, create an instance of class <codeph>X</codeph> and, at some |
|
57 later stage, create the callback:</p> |
|
58 <codeblock id="GUID-4E685C8E-9185-50FA-9402-A2B4351C3AEA" xml:space="preserve">... |
|
59 X* pX = X::NewL(); |
|
60 ... |
|
61 TCallBack cb(Foo,pX); |
|
62 ...</codeblock> |
|
63 </conbody></concept> |