Programming mobile devices Part II Programming Symbian devices with Symbian C++ Basic Symbian programming.

Slides:



Advertisements
Similar presentations
1 Symbian Dll. 2 Dynamically load libraries during program execution. Only the part that is necessary for current execution is loaded Each DLL module.
Advertisements

Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 16 Exception Handling.
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Lecture 9 Concepts of Programming Languages
More C++ Classes Systems Programming. Systems Programming: C++ Classes 2 Systems Programming: 2 C++ Classes  Preprocessor Wrapper  Time Class Case Study.
Abstract Data Types and Encapsulation Concepts
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Review of C++ Programming Part II Sheng-Fang Huang.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
You gotta be cool. Access Functions and Utility Functions Preprocessor Wrapper Looking Ahead to Composition and Inheritance Object Size Class Scope and.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
1 CSC241: Object Oriented Programming Lecture No 27.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
C++ Memory Overview 4 major memory segments Key differences from Java
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Object-Oriented Programming in C++
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
Digging into the GAT API Comparing C, C++ and Python API‘s Hartmut Kaiser
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
C++ 程序语言设计 Chapter 12: Dynamic Object Creation. Outline  Object creation process  Overloading new & delete.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Learners Support Publications Constructors and Destructors.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Memory Management.
Constructors and Destructors
Creating and Using Objects, Exceptions, Strings
Exceptions David Rabinowitz.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Andy Wang Object Oriented Programming in C++ COP 3330
Dynamically Allocated Memory
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Constructors and Destructors
Objects Managing a Resource
9-10 Classes: A Deeper Look.
COP 3330 Object-oriented Programming in C++
Stacks, Queues, and Deques
CMSC 202 Exceptions.
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
Presentation transcript:

Programming mobile devices Part II Programming Symbian devices with Symbian C++ Basic Symbian programming

Content of this part Basic Data Types Classes Error handling Cleanup Panics Libraries

Basic Data Types TInt, TUintinteger, natural word length TInt8, TInt16, TInt32 integers of 8b,16b... TUint8, TUint16, TUint32 unsigned integers of 8b,16b and 32b TInt64 64 bit integer with operators(+,-,...) TText8, TText16, TText characters TChar A character class with several methods for manipulating

Basic Data Types TBoolETrue / EFalse TReal32, TReal64, TReal floating point numbers (TReal64 and TReal map to double) TAnylike void, used in situations other than return values of functions

Classes 4 main gategories with agreed prefixes T for data type classes (e.g. TInt) C for Heap allocated classes (these inherit from CBase class) R for Resource classes (which reserve resources) M for Interface classes

Data Type Classes Encapsulate basic data types, such as TInt and int Usually have methods for manipulating the data e.g. TChar.loweCase() Comparison usually implemented Used instead of basic data types

Heap Classes All inherit from the CBase class Instantiated on the heap (using the new operation) CBase has a virtual destructor (A heap class hence has to implement it) zero initialization to all members

Resource classes Control resources owned by someone else e.g. client in a client/server communication usually allocated on the stack for example: RFile, RSocket, RThread Pointers to a resource, deleting the class does not usually delete the resource A resource has a reference count (How many pointers point to it)

Interface classes M for Mixin Abstract classes that define interfaces No member variables Methods usually pure virtual Note: Even though multiple inheritance is possible in C++, in Symbian programminig it is usually used only through interfaces. Basically: use interfaces as in Java

Errors and Exceptions Handling errors and exceptions is utterly important in mobile phone apps. Sources of exceptions are vast; out of memory, incoming call, empty battery... Users have high expectations of robustness of the phones and the software on them.

Return Codes Function's return value tells the result of excecution KErrNone -success Others include an error code that tells what went wrong, e.g. KErrNoMemory Used extensively in the APIs Still, not the best possible error handling mechanism. why ?

Leave/Trap Corresponding to Javas try-catch and C++ throw-catch, Symbian has a mechanism called leave-trap leave is like throw in java trap is similar to catch The mechanism is also familiar, a leave "bubbles" up the call stack like in java until it is handled by a trap (if ever)

Leave Programmer can call User::Leave(KErrorCode); which corresponds to throw(Exception) in java The execution of that function is stopped immediately and the control returns to the calling code. Also an API function can (and most do) leave.

Trap Trapping is done with a marco TRAP (or TRAPD). The code is executed onward from the next line after a TRAP (stops "bubbling") The macro takes 2 parameters: a TInt to store the leave code and the name of the function to be executed TInt LeaveResult; TRAP(LeaveResult, func1());

Example code void func1() { TInt result; result = somefunc(); if(result) { User::Leave(KSomeError); } void TestFunc() { TInt LeaveResult; TRAP(LeaveResult, func1()); if(LeaveResult) { // Leave happened in func1() that has to be handled }

Example code void func1() { TInt result; result = somefunc(); if(result) { User::Leave(KSomeError); } void TestFunc() { TRAPD(LeaveResult, func1()); // Defines LeaveResult if(LeaveResult) { // Leave happened in func1() that has to be handled }

Trap return values If leave does not occur KErrNone, integer 0 is returned. In trap, the return value of the function can also be saved: TRAPD(LeaveResult, resval = func1());

Leave functions User:: namespace has a set of static API leave functions: User::Leave(leavecode); User::LeaveNoMemory(); //KErrNoMemory User::LeaveIfError(leavecode); // If leavecode is negatice, leave with error as the reason. User::LeaveIfNull(TAny *pointer); //KErrNoMemory as the reason

The L suffix Symbian uses a convention to use a suffix L in functions that may leave This is to signal the programmer that it may be necessary to trap such a function Unless trapped, the code to follow might not be executed

A couple words about memory In contrast to Java, in C++ you must actively free any memory you have allocated. Not doing so will result in leaking of memory. Usually you use new to generate new objects and delete to destroy them Also, it is a good practice to set pointers to null after they no longer point to an object

Leaving and memory void func1() { CSomeObject *testObj = new CSomeObject; TInt result = testObj.somefunc(); // which may leave delete testObj; testObj = null; } void TestFunc() { TRAPD(LeaveResult, func1()); // Defines LeaveResult if(LeaveResult) { // Leave happened in func1() that has to be handled } What if?

Cleanup Obviously, we have a problem Since leave can result in skipping some code, we must be careful with allocated memory not to have code that leaks In previous example testObj is an automatic variable that goes out of scope when func1 exits Result: the created object is left in the memory and cannot be deleted

Cleanup stack The solution to our problem is something called a cleanup stack It can be thought of a storage of pointers to created objects (which might otherwise be lost) Think of it as a memory accounting device push pop

Cleanup stack Items that must be cleaned are pushed into the stack If leave occurs, these items are automatically popped from the stack and deleted If no leave occurs, you must pop the items yourself Watch out for double deletion

An example void myTestfunc() { CTestObj *obj = new CTestObj(); CleanupStack::PushL(obj); MyOtherFuncL(); // may leave CleanupStack::Pop(); delete obj; }

An example void myTestfunc() { CTestObj *obj = new CTestObj(); CleanupStack::PushL(obj); MyOtherFuncL(); // may leave CleanupStack::PopAndDestroy(); delete obj; // WRONG! obj already deleted }

Creating a cleanup stack Cleanup stack is automatically created for GUI applications For your own threads you have to create the cleanup stac yourself CTrapCleanup *clstack = CTrapCleanup::New() remember to delete the stac when finished delete clstack;

Object types PushL(CBase *) –delete is called when object is popped –calls the destructor of the derived class PushL(TAny *) –User::Free() is called when object is popped –destructor is not called

What if PushL leaves? What if there is no space in the stack? Symbian always keeps at least one free spot on the cleanup stack. If the PushL leaves, the pushed item will still be pushed to the stack Allocating the next slot may fail resulting PushL leaving

Complex cleanup Sometimes we have objects that require more complex cleanup than just delete PushL(TCleanupItem userCleanup) TCleanupItem is a wrapper class for a function that handles cleanup

Other cleanup functions CleanupClosePushL (T& obj) Pop calls obj.Close() used mainly for R classes, which require Close() to be called CleanupReleasePushL (T& obj) calls Release()

Other cleanup functions CleanupDeletePushL (T& obj) Using templates, the pop function calls the actual destructor of the object regardless of the type of the object Hence, the object doesn't have to be derived from CBase you should use this for all non CBase derived classes that have a destructor.

Templates Were created to make function code re- use possible (instead of just overloading functions) Templates basically achieve the same as a superclass in Java

LC functions LC suffix denotes functions that automatically push a reference of a created object to the cleanup stack For example: User::AllocLC(100) allocates memory and pushes a pointer to the cleanupstack No need for User::PushL(...)

Leave in object creation The new operation may fail due to inadequate memory. You can use a ELeave when calling the constructor to assure that leave occurs if object creation (new operation) fails CMyObject *obj = new (ELeave) CMyObject; Constructor itself should never leave (why?)

Two phase constructors After creating the object with new we call a constructor function which may leave This call can be trapped Often we name this function as ConstructL

ConstructL void testL() { CTestObj *o = new (ELeave) CTestObj; CleanupStack::PushL(o); o->ConstructL();... }

Panics Don' t panic The operating system may do that. An unrecoverable error that causes the thread to finish User::panic(const TDes &Category, TInt reason) On the device the execution ends and the device shows a dialog box

Libraries static (linked at build) DLLs (Dynamic Link Library) –loaded and linked at runtime –one copy is shared by multiple programs