in black and white
Main menu
Share a book About us Home
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics

Symbian os Expleined effective C++ programming for smartphones - Batchelor D.

Batchelor D. Symbian os Expleined effective C++ programming for smartphones - Wiley publishing , 2005. - 394 p.
ISBN 0-470-02130-6
Download (direct link): symbianosexpltivec++2005.pdf
Previous << 1 .. 117 118 119 120 121 122 < 123 > 124 125 126 127 128 129 .. 151 >> Next

18.7 Compatibility and the Symbian OS Class Types
Chapter 1 describes the main class types, and their characteristics, on Symbian OS. The discussion in this chapter has mainly focused on compatibility issues which apply to C class objects and, to a lesser extent, R classes. T classes, by their very nature, tend to be stack-based and are often simple, taking a similar role to a C struct. T classes frequently
do not have constructors and never have destructors. Once a T class is publicly defined, it is often difficult to make client-compatible changes to it.
18.8 Summary
A change is acceptable if every line of code affected by it can be altered, where necessary, and the code rebuilt against the changes. In effect, this often means that a change must be restricted to the internals of a component rather than its public API. For this reason, you should endeavor to keep private definitions and header files (and anything else which is likely to be subject to change) out of the public domain.
A change is also acceptable if it can be verified as compatible, according to the guidelines I've discussed here. In general, the key compatibility issue for shared library DLLs is backward binary compatibility, with source compatibility an additional aim.
These guidelines derive from those used within Symbian, in accordance with the C++ standard. I am very grateful to David Batchelor, John Forrest and Andrew Thoelke of Symbian who have previously written guides to compatibility best practice on Symbian OS, which I used as the basis of this chapter.
Thin Templates
Now, now, my good man, this is no time to be making enemies
Voltaire on his deathbed, in response to a priest who asked him to renounce Satan
C++ templates are useful for code that can be reused with different types, for example to implement container classes such as dynamic arrays. Templates allow the code to be generic, accepting any type, without forcing the programmer to overload a function.
template<class T>
class CDynamicArray : public CBase {
... // Functions omitted for clarity void Add(const T& aEntry);
T& operator[](TInt alndex);
Prior to the introduction of templates to the C++ standard, generic code tended to be written using void* arguments, to allow the caller to specify any pointer type as an argument. However, the major benefit of using C++ templates for genericity, instead of void*, is that templated code can be checked for type safety at compile time.
However, the problem with template code is that it can lead to a major increase in code size, because for each type used in the template, separate code is generated for each templated function. For example, if your code used the CDynamicArray class above for both an array of HBufC* and an array of TUid values, the object code generated when your code was compiled would contain two copies of the Add() function and two copies of operator[], one for an array of HBufC* and one for an array of TUid. What's more, the template code is generated, at best, once for each DLL or EXE that uses it and, at worst, for every compilation unit. Compiling a templated class into a binary can thus have a significant impact on its size.
The advantage of automatically generated template code is thus a disadvantage when code size matters. Because Symbian OS code is deployed on mobile phones with limited ROM and RAM sizes, it is important to avoid code bloat. Using templated classes, such as CDynamicArray above, expands the code size too significantly. To avoid this, but still reap the benefits of C++ templates, Symbian OS makes use of the thin template pattern. This chapter describes the pattern in more detail, so you have a good idea of how the system code works. If you intend to use C++ templates1 in your code on Symbian OS, you should endeavor to use this pattern to minimize code size. Let's consider the theory first, and then look at a couple of examples of the use of thin templates in Symbian OS code.
The thin template idiom works by implementing the necessary code logic in a generic base class that uses type-unsafe TAny* pointers rather than a templated type. This code is liable to misuse because of the lack of type-checking, so typically these methods will be made protected in the base class to prevent them being called naively. A templated class will then be defined which uses private inheritance2 from this class to take advantage of the generic implementation. The derived class declares the interface to be used by its clients and implements it inline in terms of the base class. I'll show an example of this from Symbian OS code shortly.
Since the derived class uses templates, it can be used with any type required. It is type-safe because it is templated and thus picks up the use of an incorrect type at compile time. There are no additional runtime costs because the interfaces are defined inline, so it will be as if the caller had used the base class directly. And importantly, since the base class is not templated, only a single copy of the code is generated, regardless of the number of types that are used.
Previous << 1 .. 117 118 119 120 121 122 < 123 > 124 125 126 127 128 129 .. 151 >> Next