Symbian OS Explained - Effective C++ Programming For Smartphones (2005) (779885), страница 2
Текст из файла (страница 2)
Symbian OShas some particular features that also need to be understood if one isto 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 mopup memory leaks and where the radio increases and enriches the numberof 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.About this BookDeveloping good C++ code on Symbian OS requires a clear understanding 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 aspectsof good C++ style that particularly apply to Symbian OS. With understanding and practice, the expertise required to write high quality C++code on Symbian OS should become second nature.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 common mistakes to avoid. The chapters are concise enough to impart theinsight you need without being so brief as to leave you needing more information.
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,1 whose 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”.1Scott Meyers, Effective C++: 50 specific ways to improve your programs and designs,1997. See the Bibliography for further details.xivABOUT THIS BOOKWho 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 Symbian OS client–server architecture (Chapters 11 and 12) and ECOM(Chapter 14).The book focuses upon the core of the operating system, which shouldbe 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, SymbianOS, this book will show you how to write the most effective C++.You will benefit from a greater understanding of the characteristic features 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 experienced 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 BookAs 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 conventions of Symbian OS code and code layout.ABOUT THIS BOOKxvNotation and Code Conventions Used in This BookThe textual layout of this book is self-explanatory, but the code layoutneeds some introduction.
Where I use example code, it will be highlightedas 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 adheringto 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.CapitalizationThe first letter of class names should be capitalized:class TClanger;The words making up variable, class or function names should beadjoining, with the first letter of each word capitalized where appropriate (parameters, automatic, global and member variables, and functionparameters have a lower case first letter).
The rest of each word shouldbe lower case, including acronyms:void SortFunction();TInt myLocalVariable;CMemberVariable* iDevilsHaircut;class CActiveScheduler;class CBbc;//Acronyms are not usually written in upper caseGlobal variables tend to be discouraged, but typically start either with aninitial capital letter or are prefixed with a lower case ”g”.PrefixesMember variables are prefixed with a lower case ”i” which stands for”instance”.xviABOUT THIS BOOKTInt iCount;CPhilosopher* iThinker;Parameters are prefixed with a lower case ”a” which stands for ”argument”.
Do not use ”an” for arguments that start with a vowel.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 256Enumeration members are prefixed with ”E”. Enumerations are types, andso are prefixed with ”T” (you can find more information about T classesin Chapter 1).enum TChilliPeppers {EScotchBonnet, EJalapeno, ECayenne};SuffixesA trailing ”L” on a function name indicates that the function may leave.void ConstructL();A trailing ”C” on a function name indicates that the function returns apointer that has been pushed onto the cleanup stack.CPhilosopher* NewLC();ABOUT THIS BOOKxviiA trailing ”D” on a function name means that it will result in the deletionof the object referred to by the function.TInt ExecuteLD(TInt aResourceId);UnderscoresUnderscores should be avoided except in macros (__ASSERT_DEBUG)or resource files (MENU_ITEM).Code layoutYou’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 wantto 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);}TipsThroughout the book, tips and advice are signified by a symbol in themargin.These tips complement each section, providing reminders of keyelements of the discussion.Introduction to Symbian OSA key characteristic of a mobile phone is that it is small – as small and aslight as possible.xviiiABOUT THIS BOOKIt’s with you all the time, ready to send and receive phone callsor messages, play alarms to wake you, connect to the phone networkor other devices, organize your personal information or play games.Whatever you want to do, you want access to your phone to be instant.
Itshould be ready without the long boot-up you expect from a PC. It shouldreact to user input from the moment you pick it up. And it should bereliable. As a mobile phone owner, how many of your contacts’ phonenumbers do you know? Your phone keeps your personal data for you andit’s essential that it doesn’t lose it.Let’s examine the consequences of these features on the design of amobile operating system, such as Symbian OS. The phone may be smalland light, but, as users, we still demand it to have a reasonable batterylife.