Documentation/Maemo For Symbian Developers Guide/Maemo and Symbian OS Concepts
The Symbian OS is famous for its design, concepts, and coding conventions. The Maemo platform gives the developer more freedom, but it also requires the developer to take more responsibility. The Maemo platform does not enforce any specific design patterns, so the developer is responsible for preventing memory leaks, buffer overflows, and other security problems.
Programming in Maemo is closer to desktop programming than Symbian programming is. This is because it is based on Linux, which was created for desktop environments, whereas Symbian OS was specifically developed for resource constrained systems. For this reason, the Maemo developer has more freedom than the Symbian developer.
The following topics show the main programming differences for both environments.
Contents |
[edit] Programming languages and libraries
Symbian developers must obey a lot of coding conventions to write code correctly. In Maemo this is no longer necessary because Maemo uses standard C and C++. Most software developed for the Maemo platform is written in C. This software widely uses GLib utility library that provides the object model adopted for Maemo (GObject
). However, C++ applications can also be written since there are several C++ bindings available for different libraries in the Maemo platform. Despite these bindings, C library calls should be made in code written for the Maemo platform sooner or later due to the fact that most part of the platform uses standard C and GLib. Due to Open C/C++, the Symbian developer should already be aware of the use of these libraries as well as STL, boost, and other C/C++ libraries.
[edit] Symbian OS essential concepts
Some differences between Symbian C++ and C++ used in Maemo are listed below. These differences are more about the conventions, concepts, and software design. Some replacements for missing Symbian/C++ specific concepts are also introduced.
[edit] Naming guidelines
Symbian OS naming conventions like class names prefix (C
, M
, R
, or T
), function leave indication (L
suffix) are left behind when programming for Maemo platform. In Maemo, the developer is given more freedom and there are no mandatory prefixes in class names. Although there are no strict rules to be followed, Maemo has some programming guidelines that should be considered. Some examples are given below:
Descriptive, lower-case names should be used for variables and functions. Underscores (_) can be used to indicate word boundaries. Non-static and unnecessary global names should be avoided. If it is absolutely necessary to employ a global variable, type or function, one should use a prefix specific to the library or module where the name is, for example gtk_widget_
.
Static memory (variables) should be preferred to dynamic memory, when the needed memory size is not large and/or is used regularly in the program. Static memory does not leak, a pointer to static memory is less likely to cause a segmentation fault, and using static memory keeps the memory usage more constant.
Local variables should always be favoured over global variables, that is the visibility of a variable should be kept at a minimum. Global variables should not be used to pass values between functions: this can be performed with arguments. Note: Global variables require extra care when programming with threads. Thus, these should be avoided.
[edit] Runtime type information
Symbian OS supports runtime type information (RTTI) only from Symbian OS v9 onwards. In Maemo, run time type information was available since the beginning. In both cases, the dynamic_cast operator is used to retrieve RTTI.
[edit] Exceptions
Symbian/C++ developers are familiar with concepts like leaving and trap harness because standard C++ exceptions did not exist when Symbian OS was originally developed. Only from Symbian OS v9 onwards C++ were exception mechanisms supported, although they still should not be mixed with Symbian/C++ code. In Maemo the standard C++ exceptions (try
, catch
, throw
) are available.
[edit] Cleanup stack
A Symbian/C++ specific concept that is not available in the Maemo platform. Similar functionality can be achieved by using auto_ptr class which is also available for Symbian v9 onwards. auto_ptr
is a smart pointer implementation in C++ Standard Template Library, and it is a good way to prevent memory leaks in case of exceptions (view example).
For more information on using the auto_ptr
, see the article Using auto_ptr Effectively.
[edit] Class construction
The Symbian OS documentation states that if a class has to allocate memory, it should be derived from CBase
. In Maemo, there is no such base class. This design causes some differences.
[edit] Class inheritance
In Symbian/C++ multiple inheritances are only allowed when combining multiple M-classes and one CBase
derived class. This is the same approach adopted by Java that allows multiple interfaces implementations and only one class derivation per class. The reason is that multiple inheritances can become too complicated. Maemo does not impose any limits to the inheritance mechanism, so the developer has more freedom as well as more responsibility.
[edit] Two-phased construction
Since the full C++ exception mechanism is used in Maemo (which includes stack unwinding), the two-phased construction of objects is not needed any more. The developer should use smart pointers to store pointers which may leak in case of exceptions. See the brief example from listing below:
class MyInnerClass { public: MyInnerClass(){} }; /* ----------------------------------------------- */ class MyClass { public: MyClass(); private: auto_ptr<MyInnerClass> m_inner; }; MyClass::MyClass() : m_inner (new MyInnerClass()) { } /* ----------------------------------------------- */ void functionFoo() { auto_ptr<MyClass> myAutoObject(new MyClass()); /* IF THROW HERE ....*/ }
In the example above, exceptions may be thrown after the instantiation of myAutoObject
or in the construction of MyInnerClass
. In both cases the objects do not leak memory due to the use of the auto_ptr
class that holds the pointer of the objects and calls the destructor in case of leave the end of scope. A good explanation can be found in the exceptions section of the C++ FAQ.
[edit] Descriptors
The Descriptors
class was specifically designed for Symbian OS and does not exist as such in the Maemo platform.
[edit] Strings
When working with strings, instead of descriptors you can use the C-style NULL
-terminated character arrays and methods.
[edit] NULL-terminated character arrays using standard C
The C-style NULL-terminated character arrays and methods can be used to store strings only. They are not as safe because they can cause buffer overflow. They can only be used with character set based on ASCII. One advantage is that it is guaranteed to work on any platform which supports C.
[edit] NULL-terminated character arrays using GLib
GLib is extensively used in the Maemo platform, so it is good practice to use the GLib-provided types and methods if using character arrays, although nothing prevents the developer from using other types.
See this gchar example.
[edit] GString
GString from GLib. Similar to a standard C string, except that it grows automatically when text is appended or inserted. Also, it stores the length of the string, so it can be used for binary data which contains bytes with NULL
values.
See this GString example.
[edit] String class
String class from the C++ Standard Template Library. The string
class resizes automatically when necessary.
See the STL string example.
[edit] Binary data
If you are working with binary data, you can use:
- Low-level, C-language memory allocation and handling.
-
GArray
from GLib - arrays of arbitrary elements which grow automatically as elements are added. -
GString
that works also with binary data, as it stores the data length. - C++ containers (for example,
vector
s andlist
s).
[edit] Active objects and threads
Symbian OS discourages the usage of threads because context switching is considered an expensive operation which degrades battery life. Basically, this is true in the Maemo platform too, but avoiding threads at all cost is not required.
Using the main event loop of GLib it is possible to run several asynchronous operations in a single thread, without blocking. This is fairly similar to using Active Objects. Examples of sources of events are file descriptors, sockets, even timeouts, and so on.
Below you see a timeout example function with event loop (based on Gnome developers' tutorial).
#include <glib.h> #define COUNT_UNTIL 2 #define TIMEOUT 2 static int counter; GMainLoop *main_loop; gboolean timeout_func(gpointer data) { counter++; g_printf("Running for the %d. time.\n", counter); if (counter == COUNT_UNTIL) { g_main_loop_quit(main_loop); return FALSE; } return TRUE; } int main(int argc, char *argv[]) { counter = 0; main_loop = g_main_loop_new(NULL, FALSE); g_timeout_add_seconds(TIMEOUT, timeout_func, NULL); g_main_loop_run(main_loop); return 0; }
In case of dealing with threads you can use:
- POSIX threads (the traditional Unix-style operating system type)
-
GThread
from GLib
[edit] Static data in DLLs
In Symbian OS version before v9 static data in DLLs was not supported. From v9 onwards static data is supported but it consumes large amounts of memory. The Maemo platform does not have this limitation. You can use, for example, global variables in your shared libraries. Note, however, that the static data is not shared between the processes.
[edit] Platform security
Platform security does not exist in the Maemo platform. When installing software with the Application Manager, it only warns if the software is NOT being downloaded from the official Maemo repositories. Maemo does not have a specific scheme to enforce platform security as Symbian has. One benefit is that installing software in Maemo is more developer-friendly than in Symbian.
[edit] Client-server model
Whereas Symbian OS extensively uses a client/server approach with asynchronous method calls, the Maemo platform uses direct, synchronous API calls. Usually this means that called functions block until they finish, with some exceptions.
Using sockets is a good example of the differences between the Symbian OS and Maemo platforms, regarding the client-server model vs. direct API calls. See networking example.
- This page was last modified on 4 November 2010, at 10:45.
- This page has been accessed 13,369 times.