Editing Documentation/Maemo For Symbian Developers Guide/Maemo and Symbian OS Concepts

Warning: You are not logged in. Your IP address will be recorded in this page's edit history.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 1: Line 1:
 +
= 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.
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.
Line 5: Line 7:
The following topics show the main programming differences for both environments.
The following topics show the main programming differences for both environments.
-
== Programming languages and libraries ==
+
= 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 (<code>GObject</code>). 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.
+
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 (<span><font face="monospace">GObject</font></span>). 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 others C/C++ libraries.
-
== Symbian OS essential concepts ==
+
= 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.
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.
-
=== Naming guidelines ===
+
== Naming guidelines ==
-
Symbian OS naming conventions like class names prefix (<code>C</code>, <code>M</code>, <code>R</code>, or <code>T</code>), function leave indication (<code>L</code> 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:
+
Symbian OS naming conventions like class names prefix (<span><font face="monospace">C</font></span>, <span><font face="monospace">M</font></span>, <span><font face="monospace">R</font></span>, or <span><font face="monospace">T</font></span>), function leave indication (<span><font face="monospace">L</font></span> 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 <code>gtk_widget_</code>.
+
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, e.g. <span><font face="monospace">gtk_widget_</font></span>.
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.
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.
+
Local variables should always be favoured over global variables, i.e. 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.
-
=== Runtime type information ===
+
== 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 [http://www.cplusplus.com/doc/tutorial/typecasting/ dynamic_cast] operator is used to retrieve RTTI.
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 [http://www.cplusplus.com/doc/tutorial/typecasting/ dynamic_cast] operator is used to retrieve RTTI.
-
=== Exceptions ===
+
== Exceptions ==
-
Symbian/C++ developers are familiar with concepts like ''leaving'' and ''trap harness'' because standard [http://www.cplusplus.com/doc/tutorial/exceptions/ 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 (<code>try</code>, <code>catch</code>, <code>throw</code>) are available.
+
Symbian/C++ developers are familiar with concepts like ''leaving'' and ''trap harness'' because standard [http://www.cplusplus.com/doc/tutorial/exceptions/ 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.
-
=== Cleanup stack ===
+
== Cleanup stack ==
-
A Symbian/C++ specific concept that is not available in the Maemo platform. Similar functionality can be achieved by using [[:wikipedia:Auto_ptr|auto_ptr]] class which is also available for Symbian v9 onwards. <code>auto_ptr</code> is a [[:wikipedia:Smart_pointer|smart pointer]] implementation in [[:wikipedia:Standard_Template_Library |C++ Standard Template Library]], and it is a good way to prevent memory leaks in case of exceptions ([http://maemo4mobile.garage.maemo.org/snippet_autoptr.html view example]).
+
A Symbian/C++ specific concept that is not available in the Maemo platform. Similar functionality can be achieved by using [http://en.wikipedia.org/wiki/Auto_ptr auto_ptr] class which is also available for Symbian v9 onwards. <span><font face="monospace">auto_ptr</font></span> is a [http://en.wikipedia.org/wiki/Smart_pointer smart pointer] implementation in [http://en.wikipedia.org/wiki/Standard_Template_Library C++ Standard Template Library], and it is a good way to prevent memory leaks in case of exceptions ([http://maemo4mobile.garage.maemo.org/snippet_autoptr.html view example]).
-
For more information on using the <code>auto_ptr</code>, see the article [http://www.gotw.ca/publications/using_auto_ptr_effectively.htm Using auto_ptr Effectively].
+
For more information on using the <span><font face="monospace">auto_ptr</font></span>, see the article [http://www.gotw.ca/publications/using_auto_ptr_effectively.htm Using auto_ptr Effectively].
-
== Class construction ==
+
= Class construction =
-
The Symbian OS documentation states that if a class has to allocate memory, it should be derived from <code>CBase</code>. In Maemo, there is no such base class. This design causes some differences.
+
The Symbian OS documentation states that if a class has to allocate memory, it should be derived from <span><font face="monospace">CBase</font></span>. In Maemo, there is no such base class. This design causes some differences.
-
=== Class inheritance ===
+
== Class inheritance ==
-
In Symbian/C++ multiple inheritances are only allowed when combining multiple M-classes and one <code>CBase</code> 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.
+
In Symbian/C++ multiple inheritances are only allowed when combining multiple M-classes and one <span><font face="monospace">CBase</font></span> 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.
-
=== Two-phased construction ===
+
== 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:
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:
-
<source lang="cpp">class MyInnerClass
+
<code>class MyInnerClass
{
{
public:
public:
MyInnerClass(){}
MyInnerClass(){}
-
};
+
}<nowiki>;
/* ----------------------------------------------- */
/* ----------------------------------------------- */
class MyClass
class MyClass
-
{
+
</nowiki>{
public:
public:
MyClass();
MyClass();
private:
private:
-
auto_ptr<MyInnerClass> m_inner;
+
auto_ptr&lt;MyInnerClass&gt; m_inner;
-
};
+
}<nowiki>;
MyClass::MyClass() : m_inner (new MyInnerClass())
MyClass::MyClass() : m_inner (new MyInnerClass())
-
{
+
</nowiki>{
}
}
Line 72: Line 74:
void functionFoo()
void functionFoo()
{
{
-
auto_ptr<MyClass> myAutoObject(new MyClass());
+
auto_ptr&lt;MyClass&gt; myAutoObject(new MyClass());
/* IF THROW HERE ....*/
/* IF THROW HERE ....*/
-
}
+
}</code>
-
</source>
+
-
In the example above, exceptions may be thrown after the instantiation of <code>myAutoObject</code> or in the construction of <code>MyInnerClass</code>. In both cases the objects do not leak memory due to the use of the <code>auto_ptr</code> 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 [http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.4 in the exceptions section of the C++ FAQ].
+
In the example above, exceptions may be thrown after the instantiation of <span><font face="monospace">myAutoObject</font></span> or in the construction of <span><font face="monospace">MyInnerClass</font></span>. In both cases the objects do not leak memory due to the use of <span><font face="monospace">auto_ptr</font></span> class that holds the pointer of the objects and call the destructor in case of leave and end of scope. A good explanation can be found [http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.4 here].
-
== Descriptors ==
+
= Descriptors =
-
The <code>Descriptors</code> class was specifically designed for Symbian OS and does not exist as such in the Maemo platform.
+
Descriptors class was specifically designed for Symbian OS and does not exist as such in the Maemo platform.
-
== Strings ==
+
= Strings =
-
When working with strings, instead of descriptors you can use the C-style <code>NULL</code>-terminated character arrays and methods.
+
When working with strings, instead of descriptors you can use the C-style NULL-terminated character arrays and methods.
-
=== NULL-terminated character arrays using standard C ===
+
'''Null-terminated character arrays using standard C'''
-
[[:wikipedia:String.h|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.
+
[http://en.wikipedia.org/wiki/String.h 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.
-
=== NULL-terminated character arrays using GLib ===
+
'''NULL-terminated character arrays using Glib'''
-
GLib is extensively used in the Maemo platform, so it is good practice to use the [http://library.gnome.org/devel/glib/stable/glib-String-Utility-Functions.html GLib-provided types and methods if using character arrays], although nothing prevents the developer from using other types.
+
<span><font face="monospace">GLib</font></span> is extensively used in the Maemo platform, so it is good practice to use the [http://library.gnome.org/devel/glib/stable/glib-String-Utility-Functions.html GLib-provided types and methods if using character arrays], although nothing prevents the developer from using other types.
-
See this [http://maemo4mobile.garage.maemo.org/snippet_gchar.html gchar example].
+
See the [http://maemo4mobile.garage.maemo.org/snippet_gchar.html example].
=== GString ===
=== GString ===
-
[http://library.gnome.org/devel/glib/stable/glib-Strings.html 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 <code>NULL</code> values.
+
[http://library.gnome.org/devel/glib/stable/glib-Strings.html GString] from the <span><font face="monospace">Glib</font></span>. 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 <span><font face="monospace">NULL</font></span> values.
-
See this [http://maemo4mobile.garage.maemo.org/snippet_gstring.html GString example].
+
See the [http://maemo4mobile.garage.maemo.org/snippet_gstring.html example].
-
=== String class ===
+
== String class ==
-
[[:wikipedia:String_(C++)|String class]] from the [[:wikipedia:Standard_Template_Library|C++ Standard Template Library]]. The <code>string</code> class resizes automatically when necessary.
+
[http://en.wikipedia.org/wiki/String_(C++) String class] from the [http://en.wikipedia.org/wiki/Standard_Template_Library C++ Standard Template Library]. The <span><font face="monospace">string</font></span> class resizes automatically when necessary.
-
See the [http://maemo4mobile.garage.maemo.org/snippet_stlstring.html STL string example].
+
See the [http://maemo4mobile.garage.maemo.org/snippet_stlstring.html example].
== Binary data ==
== Binary data ==
Line 113: Line 114:
If you are working with binary data, you can use:
If you are working with binary data, you can use:
-
* Low-level, C-language memory allocation and [[:wikipedia:String.h|handling]].
+
* Low-level, C-language memory allocation and [http://en.wikipedia.org/wiki/String.h handling].
-
* <code>GArray</code> from GLib - arrays of arbitrary elements which grow automatically as elements are added.
+
* GArray from GLib - arrays of arbitrary elements which grow automatically as elements are added.
-
* <code>GString</code> that works also with binary data, as it stores the data length.
+
* GString that works also with binary data, as it stores the data length.
-
* [[:wikipedia:Standard_Template_Library#Containers|C++ containers]] (for example, <code>vector</code>s and <code>list</code>s).
+
* [http://en.wikipedia.org/wiki/Standard_Template_Library#Containers C++ containers] (for example, vectors and lists).
-
== Active objects and threads ==
+
= 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.
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.
Line 126: Line 127:
Below you see a timeout example function with event loop (based on [http://w00d5t0ck.info/gnome_tutorial/gnome_tutorial.html#AEN53 Gnome developers' tutorial]).
Below you see a timeout example function with event loop (based on [http://w00d5t0ck.info/gnome_tutorial/gnome_tutorial.html#AEN53 Gnome developers' tutorial]).
-
<source lang="c">
+
<code><nowiki>
-
#include <glib.h>
+
#include
-
#define COUNT_UNTIL 2
+
#define COUNT_UNTIL 2
-
#define TIMEOUT 2
+
#define TIMEOUT 2
-
static int counter;
+
static int counter;
-
GMainLoop *main_loop;
+
GMainLoop *main_loop;
-
gboolean timeout_func(gpointer data)
+
gboolean
-
{
+
-
  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[])
+
timeout_func(gpointer data)
-
{
+
{
-
  counter = 0;
+
counter++;
-
  main_loop = g_main_loop_new(NULL, FALSE);
+
g_printf("Running for the %d. time.\n", counter);
-
  g_timeout_add_seconds(TIMEOUT, timeout_func, NULL);
+
if (counter == COUNT_UNTIL)
-
  g_main_loop_run(main_loop);
+
{
-
  return 0;
+
g_main_loop_quit(main_loop);
-
}
+
return FALSE;
-
</source>
+
}
 +
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;
 +
}
 +
</nowiki></code>
In case of dealing with threads you can use:
In case of dealing with threads you can use:
-
* [[:wikipedia:POSIX_Threads|POSIX threads]] (the traditional Unix-style operating system type)
+
* [http://en.wikipedia.org/wiki/POSIX_Threads POSIX threads] (the traditional Unix-style operating system type)
-
* <code>GThread</code> from GLib
+
* GThread from GLib
-
== Static data in DLLs ==
+
= 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.
+
In Symbian OS version before v9 static data in DLLs was not supported. From v9 onwards static data is supported but it consumes big amounts of RAM 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.
-
== Platform security ==
+
= 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.
+
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. The 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.
-
== Client-server model ==
+
= 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.
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.

Learn more about Contributing to the wiki.


Please note that all contributions to maemo.org wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see maemo.org wiki:Copyrights for details). Do not submit copyrighted work without permission!


Cancel | Editing help (opens in new window)