Is BREW an object oriented environment ?
Header files and sample applications in BREW SDK are coded in C. But BREW may also be coded in C++.
SophiaFramework : BREW C++ Class Library & GUI Framework & XML Middleware
Merits and demerits of C
The merit of C is that the processing speed is faster than C++. But, with proper understanding of compiler techniques, C++ code may be made to run as fast as C code.
The demerit of C is that encapsulation is difficult. As applications get bigger, the complexity of the code gets worse. Under such circumstance, module encapsulation is required to decrease the dependency between modules. ( see List 1)
* List 1 : Incomplete encapsulation IFile* file; // Type casting to IAStream* is required, but when encapsulation // is perfect, casting will not be used. // Type casting in C is a very dangerous // Because it depends on programmers to manage the type IASTREAM_Read((IAStream*)file, buffer, sizeof(buffer));
Developers can naturally encapsulate modules in C++ with encapsulation functions.
* List 2 : Perfect encapsulation SFBFile* file; SFBAStream* stream; // Up casting done automatically stream = file; // API is encapsulated stream->Read(buffer, sizeof(buffer));
Encapsulation prevents tangled source code, while promoting both reusability and maintainability.
C++ is better suited to more advanced applications such as media players or 3D games.
How to use C++ in BREW
The first step is creating C++ wrappers for the default BREW interfaces provided by Qualcomm, Inc. SophiaFramework provides C++ wrappers for all BREW interfaces. See list 3 for example.
* List 3 : Coding BREW with C++ Using Interface void function(SInt16 index) { SFXAnsiString name; //String Class SFBFileMgrSmp filemgr; //Wrapper for IFileMgr (smart pointer) SFBFileSmp file; //Wrapper for IFile (smart pointer) ... name = "config/" + SFXAnsiString::Format("%02d", index) + ".txt"; file = filemgr->OpenFile(name); return; }
By using SophiaFramework's string class, string operations can be coded simply. Buffer accesses during string operations will be hidden, freeing developers from such details.
Here is a sample of developing BREW application in C++ without using SophiaFramework.
"HelloWorld" application coded in C++ (Comments are written in Japanese)
The "new" and "delete" operators
The new and delete operators are not available in Qualcomm BREW. (The BREW SDK does not allow applications to use ANSI standard libraries) But in order to implement constructors and destructors in C++, these operators are necessary.
C++ allows developers to implement their own new and delete operators. These operators need to be customized to fit the BREW environment. (See list 4)
The implementation is as follows: define global functions (See list 5), then overload the operators. Header files should be referred to at the top of all source files.
* List 4 : Implementing sample void* operator new(size_t size) throw() { return MALLOC(size); } void* operator new(size_t /*size*/, void* address) throw() { return address; } void* operator new [](size_t size) throw() { return MALLOC(size); } void* operator new [](size_t /*size*/, void* address) throw() { return address; } void operator delete(void* address) throw() { FREE(address); return; } void operator delete(void* /*address*/, void* /*dummy*/) throw() { return; } void operator delete [](void* address) throw() { FREE(address); return; } void operator delete [](void* /*address*/, void* /*dummy*/) throw() { return; }
* List 5 : Declaration sample extern void* operator new(size_t size) throw(); extern void* operator new(size_t size, void* address) throw(); extern void* operator new [](size_t size) throw(); extern void* operator new [](size_t size, void* address) throw(); extern void operator delete(void* address) throw(); extern void operator delete(void* address, void* dummy) throw(); extern void operator delete [](void* address) throw(); extern void operator delete [](void* address, void* dummy) throw();
Defining operators as inline functions will cause errors when used with the RVCTB compiler by ARM Ltd. Therefore, new and delete operators must be defined as standard functions (Not inline).
Compiler and linker options
IDE extension tools will be installed as part of the BREW SDK. Included in these extensions, is a tool to generate make files for the ARM compiler.
When using the RVCTB linker, special attention needs to be paid to the linker options. The options generated by the linker are displayed in list 6.
* List 6 : Linker option sample ( Before modification ) # the -entry flag is not really needed, # but it keeps the linker from reporting # warning L6305W (no entry point). The address LFLAGS = $(ROPILINK) -rwpi -entry 0x8000#
The -entry point option can have a significant impact on code size, since it is used by the linker during optimization.
For greater optimization, the options must be changed as listed in list 7.
* List 7 : Linker option sample ( After modification ) # the -entry flag is not really needed, # but it keeps the linker from reporting # warning L6305W (no entry point). The address LFLAGS = $(ROPILINK) -rwpi -entry AEEMod_Load
If this step is omitted, the size of the C++ application after being linked will be abnormally large.