1. Trang chủ
  2. » Công Nghệ Thông Tin

Symbian OS ExplainedEffective C++ Programming for Smartphones phần 1 docx

40 277 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 40
Dung lượng 465,88 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Symbian OS explained effective C++ programming for smartphones / By Jo Stichbury.. Notation and Code Conventions Used in This Book xvIntroduction to Symbian OS xvii... Software engineers

Trang 2

Managing editor

Phil Northam

Project editor

Freddie Gjertsen

Trang 4

Symbian OS Explained

Trang 5

TITLES PUBLISHED BY SYMBIAN PRESS

Trang 6

Managing editor

Phil Northam

Project editor

Freddie Gjertsen

Trang 7

Copyright  2005 by John Wiley & Sons, Ltd

The Atrium, Southern Gate, Chichester, West Sussex PO19 8SQ, England Telephone (+44) 1243 779777 Email (for orders and customer service enquiries): cs-books@wiley.co.uk

Visit our Home Page on www.wileyeurope.com or www.wiley.com

All Rights Reserved No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except under the terms of the Copyright, Designs and Patents Act 1988 or under the terms of

a licence issued by the Copyright Licensing Agency Ltd, 90 Tottenham Court Road, London W1T 4LP,

UK, without the permission in writing of the Publisher, with the exception of any material supplied specifically for the purpose of being entered and executed on a computer system for exclusive use by the purchaser of the publication Requests to the Publisher should be addressed to the Permissions Department, John Wiley & Sons Ltd, The Atrium, Southern Gate, Chichester, West Sussex PO19 8SQ, England, or emailed to permreq@wiley.co.uk, or faxed to (+44) 1243 770620.

Designations used by companies to distinguish their products are often claimed as trademarks All brand names and product names used in this book are trade names, service marks, trademarks or registered trademarks of their respective owners The Publisher is not associated with any product or vendor mentioned in this book.

This publication is designed to provide accurate and authoritative information in regard to the subject matter covered It is sold on the understanding that the Publisher is not engaged in rendering professional services If professional advice or other expert assistance is required, the services of a competent professional should be sought.

Other Wiley Editorial Offices

John Wiley & Sons Inc., 111 River Street, Hoboken, NJ 07030, USA

Jossey-Bass, 989 Market Street, San Francisco, CA 94103-1741, USA

Wiley-VCH Verlag GmbH, Boschstr 12, D-69469 Weinheim, Germany

John Wiley & Sons Australia Ltd, 33 Park Road, Milton, Queensland 4064, Australia

John Wiley & Sons (Asia) Pte Ltd, 2 Clementi Loop #02-01, Jin Xing Distripark, Singapore 129809 John Wiley & Sons Canada Ltd, 22 Worcester Road, Etobicoke, Ontario,

Canada M9W 1L1

Wiley also publishes its books in a variety of electronic formats Some content that

appears in print may not be available in electronic books.

Library of Congress Cataloging-in-Publication Data

Stichbury, Jo.

Symbian OS explained effective C++ programming for smartphones / By Jo Stichbury.

p cm.

Includes bibliographical references and index.

ISBN 0-470-02130-6 (pbk alk paper)

1 Mobile communication systems–Computer programs 2 Operating systems

(Computers) 3 C++ (Computer program language) I Title.

TK6570.M6S745 2004

British Library Cataloguing in Publication Data

A catalogue record for this book is available from the British Library

ISBN 0-470-02130-6

Typeset in 10/12pt Optima by Laserwords Private Limited, Chennai, India

Printed and bound in Great Britain by Biddles Ltd, King’s Lynn

This book is printed on acid-free paper responsibly manufactured from sustainable

forestry in which at least two trees are planted for each one used for paper production.

Trang 8

Notation and Code Conventions Used in This Book xvIntroduction to Symbian OS xvii

Trang 9

3.1 Using the Cleanup Stack 313.2 How Does the Cleanup Stack Work? 353.3 Using the Cleanup Stack with Non-CBase Classes 383.4 Using TCleanupItem for Customized Cleanup 44

6.1 Descriptors as Parameters and Return Types 756.2 Common Descriptor Methods 786.3 The Use of HBufC Heap Descriptors 826.4 Externalizing and Internalizing Descriptors 846.5 The Overuse of TFileName 866.6 Useful Classes for Descriptor Manipulation 87

Trang 10

CONTENTS vii

8.2 Event-Driven Multitasking 1128.3 Working with Active Objects 115

8.5 Threads Without an Active Scheduler 1238.6 Application Code and Active Objects 123

9.1 Active Object Basics 1289.2 Responsibilities of an Active Object 1319.3 Responsibilities of an Asynchronous Service Provider 1339.4 Responsibilities of the Active Scheduler 1349.5 Starting the Active Scheduler 1359.6 Nesting the Active Scheduler 1359.7 Extending the Active Scheduler 136

11.1 Why Have a Client–Server Framework? 16811.2 How Do the Client and Server Fit Together? 16811.3 How Do the Client and Server Communicate? 17011.4 What Classes Does the Client–Server Framework Use? 17011.5 How Do Synchronous and Asynchronous Requests

11.6 How Is a Server Started? 17911.7 How Many Connections Can a Client Have? 18011.8 What Happens When a Client Disconnects? 18011.9 What Happens If a Client Dies? 18111.10 What Happens If a Server Dies? 181

Trang 11

12.1 Client–Server Request Codes 19012.2 Client Boilerplate Code 19112.3 Starting the Server and Connecting to It from the Client 19812.4 Server Startup Code 203

Trang 12

CONTENTS ix

15.3 Symbian OS Panic Categories 25015.4 Panicking Another Thread 25115.5 Faults, Leaves and Panics 253

18.1 Forward and Backward Compatibility 27818.2 Source Compatibility 27918.3 Binary Compatibility 28018.4 Preventing Compatibility Breaks 28118.5 What Can I Change Without Breaking Binary

21.1 Reduce the Size of Program Code 31721.2 Use Heap Memory Carefully 32021.3 Use Stack Memory Carefully 32521.4 Eliminate Sub-Expressions to Maximize Code

Trang 14

Charles Davies, Chief Technical Officer, Symbian

Software engineers live in interesting times: software is becoming sive We all increasingly rely on our personal computers and use theirsoftware as an essential tool in organizing our lives But what we ”see” isjust the tip of the iceberg Most software exists beneath the surface within

perva-a vperva-ariety of embedded systems such perva-as electronic consumer devices,motor cars, and aircraft Symbian OS is targeted at mobile phones – aclass of embedded system that exists in massive volume and which isused by the entire developed world

The amount of software built into a mobile phone is expanding rapidly

In recent times it has outpaced Moore’s law: in the past three years theamount of embedded software in high-end phones has jumped from

Trang 15

xii FOREWORD

about 2 MB to 20 MB This is partly required by the sophistication ofnew 3G networks, but it is mainly due to mobile phones subsumingthe functionality of other portable consumer devices, such as digitalcameras and camcorders, digital audio players, video players, electronicorganizers, mobile gaming consoles, portable radios, portable TVs, emailterminals, cordless phones and even electronic payment cards Themobile phone is becomingthekey portable lifestyle support system – anelectronic Swiss army knife

Symbian OS is needed because this explosion in software-supportedfunctionality requires a capable operating system that is designed forsophisticated, always-on, battery-powered mobile devices

The object-oriented programming paradigm of Symbian OS helpsmanage system complexity and permeates the architecture of Symbian

OS This architecture uses many advanced, but classical, constructsfound in other multitasking operating systems Examples include pre-emptive multitasking threads, processes, asynchronous services andinternal servers for serializing access to shared resources Symbian OShas some particular features that also need to be understood if one is

to become an effective Symbian OS programmer These distinct featureshave been designed to cope with the rigorous discipline of mobile deviceprogramming, for example, in the handling of asynchronous events anderrors, avoiding memory leakages and other dangling resources

Software engineers moving from the embedded space will probablyhave to make a transition from C to the object-oriented world of C++.Software engineers moving from the PC space will probably be used toC++, but will not be used to the tougher disciplines of programming formobile phones where robustness, code size, memory usage, performanceand battery life is important, where there are no regular reboots to mop

up memory leaks and where the radio increases and enriches the number

of events to which applications have to respond

Whatever your background and level of experience, your effectivenesswill be much improved if you read this book and understand the essentialSymbian OS concepts

Trang 16

About this Book

Developing good C++ code on Symbian OS requires a clear ing of the underlying concepts and the nature of the operating system.This book explains the key features of Symbian OS and shows how youcan use this knowledge most effectively It also focuses on some aspects

understand-of good C++ style that particularly apply to Symbian OS With standing and practice, the expertise required to write high quality C++code on Symbian OS should become second nature

under-The book is divided into a series of self-contained chapters, eachdiscussing specific and important features of Symbian OS Besides thefundamentals, the chapters illustrate best practice and describe any com-mon mistakes to avoid The chapters are concise enough to impart theinsight you need without being so brief as to leave you needing more infor-mation Each chapter delivers simple and straightforward explanationswithout omitting the important facts

This book doesn’t teach you how to write C++ It assumes you’realready familiar with the most important concepts of the language Neitherdoes it walk through Symbian OS from the perspective of developing aparticular application Instead, I try to impart an understanding of thecentral concepts and key features of Symbian OS and good techniques inC++ To quote from Scott Meyers,1whose book inspired me to write thisone, ”In this book you’ll find advice on what you should do, and why,and what you should not do, and why not”

1 Scott Meyers, Effective C++: 50 specific ways to improve your programs and designs ,

1997 See the Bibliography for further details.

Trang 17

xiv ABOUT THIS BOOK

Who Is It For?

The book assumes a reasonable understanding of programming in C++

It does not assume in-depth knowledge of Symbian OS and coversthe basics such as descriptors (Chapters 5 and 6) and active objects(Chapters 8 and 9) as well as more complex features such as the Sym-bian OS client–server architecture (Chapters 11 and 12) and ECOM(Chapter 14)

The book focuses upon the core of the operating system, which should

be equally applicable to all versions of Symbian OS, and user interfaces,such as UIQ and Series 60 If there are any important differences betweenplatforms, they are highlighted At the time of writing (Spring 2004),Symbian is preparing to release a new version of the operating system,Symbian OS v8.0 This book explicitly indicates any changes that thenew version will introduce, where the author is aware of them

If you are a developer targeting, or thinking of targeting, Symbian

OS, this book will show you how to write the most effective C++.You will benefit from a greater understanding of the characteristic fea-tures and design of the operating system, and confidence in how touse them However experienced you are with Symbian OS, there arealways new tricks to learn, which is why this book will appeal to alllevels of developer It reflects the combined wisdom of the many expe-rienced Symbian OS developers I have worked with Over the years,they have taught me a great deal, and when I looked more closely atparts of the OS whilst writing this book, I learnt even more I hope youwill too

How to Use This Book

As I’ve already mentioned, the book is split into a number of chapters,where each chapter acts as a guide to a particular feature of Symbian OS.The title of each, a detailed table of contents and the index, glossary andbibliography sections are all designed to help you find the informationyou need

The chapters do not necessarily need to be read in sequence Rather,the book can be dipped into for revision, reference or to provide a handy

”tip for the day”; it does not necessarily need to be read from cover tocover The chapters cross-reference each other where there is overlapbetween them, to indicate which other areas are particularly relevant tothe discussion

For clarification and explanation, the chapters also use example codewhich has been designed to illustrate good coding style and the conven-tions of Symbian OS code and code layout

Trang 18

ABOUT THIS BOOK xv

Notation and Code Conventions Used in This Book

The textual layout of this book is self-explanatory, but the code layoutneeds some introduction Where I use example code, it will be highlighted

as follows:

This is example code;

C++ code for Symbian OS uses an established naming convention whichyou should try to stick to in order for your own code to communicate itsintent most clearly Besides communication, the main benefit of adhering

to the convention is that it is chosen to reflect clearly object cleanupand ownership

The best way to get used to it is to look at code examples such as thosefound in your chosen SDK as well as in this book The main features ofthe naming conventions are described here; in Chapter 1, I discuss theclass name prefix conventions, while Chapter 2 covers the reason for thetrailing L on some function names and Chapter 3 discusses the use oftrailing C

be lower case, including acronyms:

void SortFunction();

TInt myLocalVariable;

CMemberVariable* iDevilsHaircut;

class CActiveScheduler;

class CBbc;//Acronyms are not usually written in upper case

Global variables tend to be discouraged, but typically start either with aninitial capital letter or are prefixed with a lower case ”g”

Prefixes

Member variables are prefixed with a lower case ”i” which stands for

”instance”

Trang 19

xvi ABOUT THIS BOOK

TInt iCount;

CPhilosopher* iThinker;

Parameters are prefixed with a lower case ”a” which stands for ment” Do not use ”an” for arguments that start with a vowel

”argu-void ExampleFunction(TBool aExampleBool, const TDesC& aName);

Note TBool aExampleBool rather than TBool anExampleBool.Automatic variables have no prefix and the first letter is lower case

TInt index;

CMyClass* ptr = NULL;

Class names should be prefixed with an appropriate letter (”C”, ”R”, ”T”

or ”M” as described fully in Chapter 1)

class CActive;

class TParse;

Constants should be prefixed with ”K”

const TInt KMaxFilenameLength = 256;

#define KMaxFilenameLength 256

Enumeration members are prefixed with ”E” Enumerations are types, and

so are prefixed with ”T” (you can find more information about T classes

Trang 20

ABOUT THIS BOOK xvii

A trailing ”D” on a function name means that it will result in the deletion

of the object referred to by the function

TInt ExecuteLD(TInt aResourceId);

Underscores

Underscores should be avoided except in macros ( ASSERT_DEBUG)

or resource files (MENU_ITEM)

Code layout

You’ll notice that the curly bracket layout in Symbian OS code, usedthroughout this book, is to indent the bracket as well as the followingstatement I’m not a big fan of this, and I don’t think it makes thatmuch difference if you use an alternative layout However, if you want

to stick to writing code the ”Symbian way”, you should adhere to thefollowing convention:

void EatChilliL(CChilliPepper* aChilli)

{

if (!aChilli) {

User::Leave(KErrArgument);

} TChilliType type = aChilli->Type();

if (EScotchBonnet==type) {

User::Leave(KErrNotSupported);

} DoEatChilliL(aChilli);

Ngày đăng: 14/08/2014, 12:20

TỪ KHÓA LIÊN QUAN