Wiley.Symbian.OS.C.plus.plus.for.Mobile.Phones.Aug.2007 (779890), страница 15
Текст из файла (страница 15)
These are now deprecated – current compilers canuse the standard C++ definitions.3.7 ClassesAs you’d expect, classes are used to represent objects, abstractions andinterfaces. Relationships between classes are used to represent relationships between objects or abstractions. The most important relationshipsbetween classes are:• uses-a: if class A ‘uses-a’ class B, then A has a member of type B, B&,const B&, B* or const B*, or a function that can easily return a Bin one of these guises; A can then use B’s member functions and data• has-a: ‘has-a’ is like ‘uses-a’, except that A takes responsibility forconstructing and destroying the B as well as using it during its lifetime• is-a: if class A ‘is-a’ class B, then B should be an abstraction of A; ‘is-a’relationships are usually represented in C++ using public derivation• implements: if class A implements an interface M, then it implementsall M’s pure virtual functions; this is the only circumstance in whichmultiple inheritance is used in Symbian OS.Sometimes, the distinction between abstract and concrete notionsis blurred.
CEikDialog is concrete as far as its implementation ofCCoeControl is concerned but CEikDialog is an essentially abstractbase class for user-specified dialogs or Uikon standard dialogs suchas a CEikInfoDialog. Some classes (such as CCoeControl andCEikDialog) contain no pure virtual functions, but are still intended tobe derived from. They provide fall-back functionality for general cases,but it is likely that one or two functions must be overridden to provide auseful class.58SYMBIAN OS C++InterfacesSymbian OS makes quite extensive use of interface classes (originallycalled mixins). An interface is an abstract base class with no data andonly pure virtual functions.APIs that have both library and framework aspects often define theirlibrary aspect by means of a concrete class and their framework by meansof an interface class.
To use such an API, you need to use the concreteclass and implement the interface.The Symbian OS Print API provides an example. In addition to libraryclasses to start the print job, there are framework classes for printing partof a page and for notifying the progress of the job to an application.MPrintProcessObserver is the interface for notifying progress:class MPrintProcessObserver{public:virtual void NotifyPrintStarted(TPrintParameters aPrintParams) = 0;virtual void NotifyBandPrinted(TInt aPercentageOfPagePrinted,TInt aCurrentPageNum, TInt aCurrentCopyNum) = 0;virtual void NotifyPrintEnded(TInt aErrorCode) = 0;};This interface definition includes functions for reporting the beginningand end of a print job and its progress at intervals throughout.
The printpreview image is designed to be owned by a control in a print-previewdialog. It provides a standard print-preview image and its definition starts:class CPrintPreviewImage : public CBase, private MPrintProcessObserver,private MPageRegionPrinter{}You can see that this uses C++ multiple inheritance, in this caseinheriting from CBase and two interface classes. When using multipleinheritance in this manner, place the CBase (or CBase-derived) classfirst in the list.
Otherwise, subtle details of the operating system will causeproblems.The only encouraged use of multiple inheritance in Symbian OS is toimplement interfaces. Any other use is unnecessary and is strongly discouraged. Standard classes are not designed with multiple inheritancein mind.DESIGN PATTERNS59Bad PracticesMany C++ features that look attractive at first sight are not used inSymbian OS – or, at least, they’re not encouraged in anything other thanvery specific situations:• private inheritance: inheritance should only be used for ‘is-a’ relationships; private inheritance (the default in C++) is used to mean‘has-a’, so that the private base class effectively becomes a privatedata member• multiple inheritance: except in the case of interfaces, full-blown C++multiple inheritance is more confusing than useful• overriding non-trivial virtual functions: base classes with virtual functions should either specify trivial behavior (doing nothing, for example)or leave them pure virtual.
This helps you to be clear about the purposeof the virtual function.• ‘just-in-case’ tactics: making functions virtual ‘just in case’ they shouldbe overridden or protected ‘just in case’ a derived class wishes to usethem is an excuse for unclear thinking.There may be times when these practices can be used for good reason.The thin-template pattern is really a C++ technical trick, so it’s fair gameto use C++ technical tricks such as private inheritance to help implementit. But if you want your C++ to be a straightforward implementation ofgood object-oriented system design, you should use the object-orientedfeatures of C++ rather than such technical tricks.3.8 Design PatternsObject orientation supports good design using the ‘uses-a’, ‘has-a’, ‘is-a’and ‘implements’ relationships. Through good design, object orientationalso supports good code re-use.
That’s particularly attractive for SymbianOS, since minimizing the amount of code you require to implement aparticular system is a very important design goal.But code re-use isn’t the only form of re-use. Often, you find yourselfdoing the same thing again and again, but somehow you can’t abstract itinto an API – even using the full power of object orientation and templatesin C++. Or, you succeed in abstracting an API, but it’s more difficult touse the API than it was to write the repeated code in the first place.This is a good time to think in terms of re-using design patterns.
Designpatterns are ways of designing things, rather than objects or APIs that youcan re-use or glue together.60SYMBIAN OS C++Symbian OS contains many frequently used design patterns. The mostcharacteristic design patterns relate to cleanup (see Chapter 4) and activeobjects (see Chapter 6). However, most of the patterns used in SymbianOS are standard patterns used elsewhere in the software industry.SummaryIn this chapter, we have looked at the features of C++ that Symbian OSuses, those it avoids and those it augments.
We’ve also seen the codingstandards that are used in Symbian OS. Specifically, we’ve seen:• the use of Symbian OS fundamental types to guarantee consistentbehavior between compilers and platforms• naming conventions – the core ones help with cleanup: T, C andR distinguish between different cleanup requirements for classes; irefers to member variables and L to leaving functions that may fail forreasons beyond the control of the programmer; M prefixes interfaces(consisting of pure virtual functions)• good design of function prototypes – how to code input and outputparameters and suggested use of references and pointers in SymbianOS• the difference between library and framework DLLs – the former provide functions that you call, the latter functions for you to implementthat the framework calls• exporting non-virtual, non-inline functions using the IMPORT_C andEXPORT_C macros• how to handle virtual functions• templates and the thin-template pattern to keep code size to a minimum• the four relationships between classes• the use of mixins to provide interfaces and the only use of multipleinheritance in Symbian OS.4Objects – Memory Management,Cleanup and Error HandlingBefore we head into the deeper aspects of Symbian OS, we need to spendsome time looking at some of the basic operations, programming patternsand classes that are common to all applications, and indeed, to almostall code that runs in the system.Object creation and destruction are intimately tied up with the issue ofcleanup.
It is important to make sure that your applications are coded insuch a way that they do not leak memory even if a coding error occurs – areal issue for systems that may not be rebooted for long periods of time, ifat all. This is why memory management (object creation and destruction),cleanup and error handling are discussed as a single subject.
This chapterpresents the basic patterns that are used over and over again: buildingblocks that allow you to build safe and efficient code.Symbian OS uses object orientation, and is written in C++, with bitsof Assembler thrown in at the lowest levels. This means that the vastmajority of applications are written in C++. However, as discussed inChapter 2, Symbian OS C++ is not exactly the same as C++ in otherenvironments.• C++ does more than Symbian OS requires – for example, full-blownmultiple inheritance.• C++ does less than Symbian OS requires – for example, it doesn’tinsist on the number of bits used to represent the basic types, and itknows nothing about DLLs.• Different C++ communities do things differently because their requirements are different.
In Symbian OS, large-scale system design is62OBJECTS – MEMORY MANAGEMENT, CLEANUP AND ERROR HANDLINGcombined with a focus on error handling and cleanup, and efficiencyin terms of ROM and RAM budgets.• Symbian OS is particularly concerned with the handling of errorsthat may occur during memory allocation. Because of the deviceconstraints, it is important that these errors are handled in such a waythat no memory remains allocated or orphaned afterwards.4.1 Object Creation and DestructionOne of the fundamental characteristics of object-oriented systems is thecreation and destruction of objects.