Symbian OS Explained - Effective C++ Programming For Smartphones (2005) (779885), страница 70
Текст из файла (страница 70)
. .else statements.• Optimize loops by using the clearest type (do. . .while or while) ineach case.• There should be a break statement for each case of a switchstatement – or a comment if a flow-through to the next case statementis meant.• A switch statement shouldn’t contain case statements that returnfrom the function unless every case in that switch statement contains a return.• A switch statement should be used to indicate state control. Eachcase statement should contain only a small amount of code; largechunks of code should be moved into separate functions to maintainthe readability of the switch statement.CODE CHECKLIST337• There should be a default in every switch statement.• Flow control statements should be used rather than extra booleanvariables to manage program flow.Program Logic• Stack usage should be reasonable, i.e. don’t store large objects onthe stack.• Errors should propagate properly and not be changed or remapped toe.g.
KErrGeneral.• Pointers should be manipulated and accessed correctly, i.e. check forinvalid address access, pointer overflow, etc.• Bitmasks should be used properly (& to read, | to set, ∼ to reset).• Unexpected events and inputs should be handled gracefully, i.e. useerror returns, leaves or panics appropriately.• Hard-coded ”magic” values should not be used.• Logging statements should be informative and in useful places.• Code should be efficient, e.g.
do not copy things unnecessarily, donot create too many temporaries, do not use inefficient algorithms.• Casts should be in C++ style such as static_cast, reinterpret_cast or const_cast rather than in C style.• A cast should only be used where absolutely necessary and should bethe correct type for the expression.Descriptors• Descriptor character size should be correct, i.e. 8 bits for binary dataand ASCII-like text; 16 bits for explicitly Unicode text; no suffix fornormal text.• _L should not be used for literal data: use _LIT instead (except fortest code, etc.).• Assignment to TPtr or TPtrC does not redirect the pointer; Set()is required for that purpose.• HBufC::Des() and TBufC::Des() are not required to use theobject as an unwritable descriptor.338CODE CHECKLIST• HBufC::Des() and TBufC::Des() are not required for assignment: these classes define assignment operators.• Use AppendNum() or Num() to format a number instead ofTDes::AppendFormat() or Format().• Use Append() instead of TDes::AppendFormat() or Format()to concatenate strings.• Arguments to TDes::AppendFormat() or Format() should matchthe format of the string and the descriptor must be passed by pointer.• Use descriptor functions such as Find() and Locate() rather thanre-implementing this functionality.• Iterating over a descriptor using operator[] is expensive; consider using C++ pointer arithmetic and the TDesC::Ptr() function instead.• Descriptors passed to asynchronous operations should persist untilthe operation completes, e.g.
do not use a stack-allocated descriptor.Containers• The least-derived type of container should be used in declarations.• The initial size and granularity of the container should be appropriate.• All owned objects in the container should be destroyed (or ownershippassed) before the container is destroyed.• The appropriate container for the purpose should be used.• Thin templates should be used for any templated code.GlossaryTermDefinitionChapteractive objectResponsible for issuing requests toasynchronous service providers andhandling those requests on completion.Derives from CActive.8, 9active schedulerResponsible for scheduling events to theactive objects in an event-handling program.8, 9asynchronousservice providerA system, component or class, whichprovides a service asynchronously, typicallyto an active object.
Asynchronous requestsare indicated by function calls with aTRequestStatus reference parameter.8, 9backwardcompatibilityUpdating a component in such a way thatclient code which used the original versioncan continue to work with the updatedversion. See also forward compatibility.18binarycompatibilityModifying a library in such a way that codewhich linked against the previous versioncan continue to run with the new versionwithout needing recompilation.18C functionIn the context of Symbian OS, this refers to afunction with a trailing C in its name, e.g.NewLC().
If successful, this type of functionleaves one or more objects on the cleanupstack when it returns.3340GLOSSARYTermDefinitionChapterchunkA unit of memory allocation, where a linearregion of RAM is mapped into contiguouslogical addresses.13clangerThe Clangers are a race of highly-civilized,small, bright pink, long-nosed,mouse-shaped persons who stand uprighton big flappy feet (see www.clangers.co.ukfor more information).throughoutcleanup stackTakes responsibility for cleaning up theobjects pushed onto it if a leaveoccurs.3client interfacefunctionA member function in a client-side session(or subsession) class that sends a specificmessage request to a server, identified by anopcode.11, 12context switchA switch in execution between one threador process and another. This involves savingthe context of the currently executing threador process (i.e.
its address space mappings)and restoring the context of the newlyexecuting thread or process.10, 11, 12descriptorA string, so-called because it isself-describing. All descriptor classes derivefrom TDesC, which describes an area ofmemory used as either a string or binarydata.4, 5descriptorizeTo marshal a flat data object, typically astruct or T class, into a descriptor forinter-thread data transfer.6, 11, 12DLL (dynamiclink library)Dynamic link libraries contain functionsthat are linked and stored separately fromthe programs that use them.13DLL export tableAn area in the DLL which lists the DLLaddresses of exported items. On SymbianOS, these items are indexed byordinal.13E32Collective term for the base components ofSymbian OS.ECOMA generic framework in Symbian OS fromv7.0 for use with plug-in modules.14GLOSSARY341TermDefinitionChapteremulatorAn implementation of Symbian OS hostedon a PC running Microsoft Windows.Compare with target hardware.13entry pointA function called when a DLL is loaded orattached by a thread or process.
In SymbianOS, the function E32Dll() is usuallycoded as the entry point.13EPOC, EPOC32Earlier names for Symbian OS. Rumors thatthis stood for ‘Electronic Piece of Cheese’are unconfirmed.exec callA call to the kernel via a software interrupt(see this useful paper by John Pagonis formore details www.symbian.com/developer/techlib/papers/userland/userland.pdf)F32Collective term for the components makingup the Symbian OS file server.FEP (Front-EndProcessor)Allows the input of characters by a user, forexample, by handwriting recognition orvoice, and converts the input transparentlyinto text.13file serverThe server thread that mediates all filesystem operations. All application programsthat use files are clients of this thread.11flat bufferA dynamic buffer using flat storage, i.e.
asingle segment of memory (see alsosegmented buffer).7forwardcompatibilityUpdating a component in such a way thatcode which works with the new version alsoworks with the original version. See alsobackward compatibility.18frameworkA component that allows its functionality tobe extended by plug-in modules, sometimesknown as framework extensions. Theframework loads the extensions as requiredat runtime.13, 14frameworkextensionProvides plug-in functionality to aframework, typically by implementinginterfaces defined by that framework. OnSymbian OS, a framework extension isimplemented as a polymorphic DLL.1410342GLOSSARYTermDefinitionChapterfreezeThe process of fixing the associationbetween name and ordinal, for functionsexported from a DLL. It is a colloquialreference to a .def file (see moduledefinition file).13import library(.lib)A static library that an application programuses at link time to resolve references tosymbols defined in a corresponding DLL.Information from this library concerning thelocation of export functions in the DLL isincluded in the application’s executablefile.13IPC(Inter-ProcessCommunication)Used by clients to communicate without-of-process servers on Symbian OS.11, 12just-in-time (JIT)Just-in-time debugging is used to attach adebugger to a process at the point at whichit is about to terminate.15kernelThe core of the operating system.
It managesmemory, loads processes and libraries andschedules threads for execution.10L functionA function with a trailing L in its name, e.g.NewL(), which indicates that it may leave.2leaveSymbian OS exception handling. A functionleaves when User::Leave()is called. Itcauses a return to the current trap harness.2mixinAn interface class for ”mixing in” withprimary base classes, and the only form ofmultiple inheritance encouraged onSymbian OS.1moduledefinition file(.def)A file that specifies an ordinal value for eachnamed function or data item to be exportedfrom a DLL.
It is used to insert ordinalinformation into the DLL export table,allowing exports to be accessed by ordinalfrom clients of the DLL.13, 18null threadThe lowest-priority thread in the system,belonging to the kernel. It only runs whenthere is no higher priority thread to run andusually puts the phone into power-savingmode.10GLOSSARY343TermDefinitionChapterOOM(out-of-memory)An error caused when the available RAMhas been exhausted, preventing further heapallocation.throughoutpanicA run-time exception on Symbian OS thatterminates the current thread. Panics tend tobe caused when assertion statementsfail.15, 16plug-inA polymorphic interface DLL used to extenda framework application.13, 14polymorphicDLLA DLL that provides plug-in functionality toanother program by implementing a definedinterface.