三星bada:Open API的基本风格

移动开发
三星bada是一个新的手机平台,它允许开发者开发丰富的应用程序用来提升用户在移动空间中的体验。本文介绍了三星bada平台Open API的基本风格,以及在开发过程中可能会遇到的问题和处理方法。

1、二次构造

在C++中当在对象初始化时分配资源失败,那么对象知识部分初始化并且析构函数并没有被调用,这样会导致资源泄露。保证资源不被泄露可以在进行二次构造,即将一些可能分配资源失败的放在一个Construct()函数里面。(注:这个应该是借鉴的Symbian。)

2、处理方法

与标准C++相比bada的处理方法工作起来很不相同。为了最好的封装任何事情都是由方法进行处理。

在bada中数据损坏或者由于数据损坏导致的设备故障时不可能的,因为直接访问数据时不可能的。

限制数据的访问是为了阻止恶意软件利用一些安全漏洞例如缓冲区溢出。

3、异常处理

bada的错误和异常处理与标准C++也是不同的。bada利用错误的结果代替C++的异常处理,因为C++的异常处理会占用很多的运行时间和空间。

所有的异常处理在bada中有一个返回值result类型捕捉,result类型就是unsigned long。E_SUCCESS结果表示方法返回成功,其余的所有的返回结果都是失败的。

A、异常的侦测:

a、函数返回一个result:

例如:

  1. result r = E_SUCCESS;  
  2.  ...  
  3.  
  4.  r = list.Construct(...);  
  5.  if (r != E_SUCCESS) // identical to 'if (IsFailed(r))'  
  6.  {  
  7.  // Process the error condition.  
  8.  } 

b、函数给result赋值或者返回null:

例如:

  1. pObj = list.GetAt(...);  
  2.  if (GetLastResult() != E_SUCCESS) // or 'if (pObj == null)'  
  3.  {  
  4.  // Process the error condition.  
  5.  } 

c、If失败跳到catch:

  1. r = pObj2->Construct(..);  
  2.  TryCatch(r == E_SUCCESS, , "[%s] Service could not be initialized.",  
  3.   GetErrorMessage(r));  
  4.  ...  
  5. CATCH:  
  6.  delete pObj1;  
  7.  delete pObj2;  
  8.  return

B、异常处理:

a、用goto CATCH处理:

  1. result r = E_SUCCESS;  
  2.  ...  
  3.  
  4.  r = pList->Construct(...);  
  5.  TryCatch(r == E_SUCCESS, delete pList, "[%s] Propagated.", GetErrorMessage(r));  
  6.  ...  
  7. CATCH:  
  8.  SetLastResult(r);  
  9.  return null; 

 b、尝试放回E_SUCCESS:

  1. r = list.Construct(...);  
  2.  TryReturn(r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r); 

 c、返回一个null:

  1. r = list.Construct(...);  
  2.  TryReturn(r == E_SUCCESS, null, "[%s] Propagated.", GetErrorMessage(r); 

 d、转化一个错误的环境到另一个错误的环境:

  1. r = list.indexOf(...);  
  2. TryReturn(r == E_SUCCESS, E_INVALID_ARG, "'%s' converted to [E_INVALID_ARG].",   
  3. GetErrorMessage(r)); 

 4、内存处理:

在bada中内存通过所有权方针管理。所有权有责任删除动态申请的内存并且避免内存泄漏。

独有所有权意味着所有权不能够被分享。得到所有权有两条规定。

1> 新的操作符得到分配空间的所有权。

2> 所有权能够被转移,但是不能被分享。

图1

图1

5、应用程序调试:

为了帮助你调试,bada提供了很多宏指令:

1> Assert 宏指令:

Assert 宏指令是用来测试条件是否成立,如果条件不成立就杀掉进程它们没有被编译到发布版中。

AppAssertion(condition) 

这个是用来检查程序是否有逻辑错误的,如果返回错误,那么当前进程就被杀掉。

例如:

  1. result  
  2.  
  3. MyClass::DoSomething(void)  
  4.  
  5. {  
  6.  
  7.  result r = E_SUCCESS;   
  8.  
  9.  r = mutex.Acquire();  
  10.  
  11.  // do something  
  12.  
  13.  r = mutex.Release();  
  14.  
  15.  AppAssertion(r == E_SUCCESS); // Process dies if false.  
  16.  
  17.  return r;  
  18.  
  19. }  
  20.  
  21. AppAsserttionf(condition, message) 

 这个是用来检查程序是否有逻辑错误,如果返回错误,那么当前进程被杀死,一条信息显示在控制台上。

例如:

  1. result  
  2.  
  3. MyClass::DoSomething(void)  
  4.  
  5. {  
  6.  
  7.  result r = E_SUCCESS;   
  8.  
  9.  r = mutex.Acquire();  
  10.  
  11.  // do something  
  12.  
  13.  r = mutex.Release();  
  14.  
  15.  // If false, console prints "Mutex Release Failed"   
  16.  
  17.  // and the process is killed.   
  18.  
  19.  AppAssertionf(r == E_SUCCESS, "Mutex Release Failed");  
  20.  
  21.  return r;  
  22.  

 在控制台可能显示的信息:

图2

Log宏指令:

AppLog(message)

AppLogDebug(message)

AppLogException(message)

AppLog 可以让你输出任意的信息。AppLogDebug 和AppLogException的工作方式基本相同,在控制台或者文件中显示信息。

例如:

  1. Bool  
  2.  
  3. MyEngine::Init(int value)  
  4.  
  5. {  
  6.  
  7.  AppLogDebug("Invoked with value: %d", value);  
  8.  
  9.  // Do initialize.  
  10.  
  11.  if (something_wrong) // You can use Try family macros instead.  
  12.  
  13.  {  
  14.  
  15.   AppLogException("Something terrible happened.");  
  16.  
  17.   Return false;  
  18.  
  19.  }  
  20.  
  21.  AppLog("Initialization successful.");  
  22.  
  23.  AppLogDebug("Exit.");  
  24.  
  25.  return true;  
  26.  

图3

Try宏指令:

Try宏指令是模拟标准C++的try-catch。和Assert不同的事try不杀死进程。

TryCatch(condition,cleanup,message)

TryCatch检测条件,如果失败,打印一条信息,评价一条cleanup表达式,然后gotoCATCH:

例如:

  1. const A*  
  2.  
  3. MyClass::DoSomething(const mchar* pValue)  
  4.  
  5. {  
  6.  
  7.  result r = E_SUCCESS;  
  8.  
  9.  // Do something...  
  10.  
  11.  // If pValue is null, print "pValue == null" to the   
  12.  
  13.  // console and return E_INVALID_ARG.  
  14.  
  15.  TryCatch(pValue != null, r = E_INVALID_ARG, "pValue == null");   
  16.  
  17.  SetLastResult(E_SUCCESS);  
  18.  
  19.  return _pValue;  
  20.  
  21. CATCH:  
  22.  
  23.  SetLastResult(r);  
  24.  
  25.  return null;  
  26.  

TryReturn(condition,value,message)

如果条件错误,message输出,value被返回。

TryReturnVoid(conditiong, message)

如果条件错误,打印一条信息。

【编辑推荐】

  1. 从开发到售卖 三星bada应用程序创建完整流程
  2. 三星bada开发平台概述
  3. bada开发:OpenGL ES 2.0程序 创建简单3D图形
  4. 三星bada学习笔记:cycle life生命周期
  5. 三星bada学习笔记:基本概念
责任编辑:佚名 来源: 博客园
相关推荐

2010-07-21 17:00:58

bada接口

2010-02-07 14:55:06

bada三星

2011-09-22 10:10:56

2010-09-05 17:03:01

bada 1.0bada三星

2010-07-28 11:19:55

HelloWorldbada

2009-11-20 14:25:29

badaUI三星

2011-04-29 11:24:06

2012-05-17 09:25:18

三星BadaAndroid

2010-04-12 17:59:05

bada开发

2011-06-01 14:00:09

UIbada 2.01bada

2011-04-22 09:57:36

bada三星

2011-03-10 16:57:29

三星Symbianbada

2010-08-25 09:52:22

bada SDK 1.更新bada

2011-02-16 21:36:30

bada 2.0bada三星

2010-07-17 16:31:15

cycle lifebada

2011-04-20 09:30:58

bada 2.0bada三星

2012-08-24 10:46:23

三星BadaTizen

2012-01-16 09:15:08

三星BadaTizen

2012-01-18 09:22:40

三星Bada

2009-12-09 22:23:12

bada三星
点赞
收藏

51CTO技术栈公众号