小野 发表于 2013-2-4 09:04:02

三星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:
例如:

[*]result r = E_SUCCESS;
[*] ...
[*]
[*] r = list.Construct(...);
[*] if (r != E_SUCCESS) // identical to 'if (IsFailed(r))'
[*] {
[*] // Process the error condition.
[*] }
b、函数给result赋值或者返回null:
例如:

[*]pObj = list.GetAt(...);
[*] if (GetLastResult() != E_SUCCESS) // or 'if (pObj == null)'
[*] {
[*] // Process the error condition.
[*] }
c、If失败跳到catch:

[*]r = pObj2->Construct(..);
[*] TryCatch(r == E_SUCCESS, , "[%s] Service could not be initialized.",
[*]GetErrorMessage(r));
[*] ...
[*]CATCH:
[*] delete pObj1;
[*] delete pObj2;
[*] return;
B、异常处理:
a、用goto CATCH处理:

[*]result r = E_SUCCESS;
[*] ...
[*]
[*] r = pList->Construct(...);
[*] TryCatch(r == E_SUCCESS, delete pList, "[%s] Propagated.", GetErrorMessage(r));
[*] ...
[*]CATCH:
[*] SetLastResult(r);
[*] return null;
b、尝试放回E_SUCCESS:

[*]r = list.Construct(...);
[*] TryReturn(r == E_SUCCESS, r, "[%s] Propagated.", GetErrorMessage(r);
c、返回一个null:

[*]r = list.Construct(...);
[*] TryReturn(r == E_SUCCESS, null, "[%s] Propagated.", GetErrorMessage(r);
d、转化一个错误的环境到另一个错误的环境:

[*]r = list.indexOf(...);
[*]TryReturn(r == E_SUCCESS, E_INVALID_ARG, "'%s' converted to .",   
[*]GetErrorMessage(r));
4、内存处理:
在bada中内存通过所有权方针管理。所有权有责任删除动态申请的内存并且避免内存泄漏。
独有所有权意味着所有权不能够被分享。得到所有权有两条规定。
1> 新的操作符得到分配空间的所有权。
2> 所有权能够被转移,但是不能被分享。
http://images.51cto.com/files/uploadimg/20110415/1440100.jpg
图1
5、应用程序调试:
为了帮助你调试,bada提供了很多宏指令:
1> Assert 宏指令:
Assert 宏指令是用来测试条件是否成立,如果条件不成立就杀掉进程它们没有被编译到发布版中。
AppAssertion(condition)
这个是用来检查程序是否有逻辑错误的,如果返回错误,那么当前进程就被杀掉。
例如:

[*]result
[*]
[*]MyClass::DoSomething(void)
[*]
[*]{
[*]
[*] result r = E_SUCCESS;   
[*]
[*] r = mutex.Acquire();
[*]
[*] // do something
[*]
[*] r = mutex.Release();
[*]
[*] AppAssertion(r == E_SUCCESS); // Process dies if false.
[*]
[*] return r;
[*]
[*]}
[*]
[*]AppAsserttionf(condition, message)
这个是用来检查程序是否有逻辑错误,如果返回错误,那么当前进程被杀死,一条信息显示在控制台上。
例如:

[*]result
[*]
[*]MyClass::DoSomething(void)
[*]
[*]{
[*]
[*] result r = E_SUCCESS;   
[*]
[*] r = mutex.Acquire();
[*]
[*] // do something
[*]
[*] r = mutex.Release();
[*]
[*] // If false, console prints "Mutex Release Failed"   
[*]
[*] // and the process is killed.   
[*]
[*] AppAssertionf(r == E_SUCCESS, "Mutex Release Failed");
[*]
[*] return r;
[*]
[*]}
在控制台可能显示的信息:
http://images.51cto.com/files/uploadimg/20110415/1440101.jpg
Log宏指令:
AppLog(message)
AppLogDebug(message)
AppLogException(message)
AppLog 可以让你输出任意的信息。AppLogDebug 和AppLogException的工作方式基本相同,在控制台或者文件中显示信息。
例如:

[*]Bool
[*]
[*]MyEngine::Init(int value)
[*]
[*]{
[*]
[*] AppLogDebug("Invoked with value: %d", value);
[*]
[*] // Do initialize.
[*]
[*] if (something_wrong) // You can use Try family macros instead.
[*]
[*] {
[*]
[*]AppLogException("Something terrible happened.");
[*]
[*]Return false;
[*]
[*] }
[*]
[*] AppLog("Initialization successful.");
[*]
[*] AppLogDebug("Exit.");
[*]
[*] return true;
[*]
[*]}
http://images.51cto.com/files/uploadimg/20110415/1440102.jpg
Try宏指令:
Try宏指令是模拟标准C++的try-catch。和Assert不同的事try不杀死进程。
TryCatch(condition,cleanup,message)
TryCatch检测条件,如果失败,打印一条信息,评价一条cleanup表达式,然后gotoCATCH:
例如:

[*]const A*
[*]
[*]MyClass::DoSomething(const mchar* pValue)
[*]
[*]{
[*]
[*] result r = E_SUCCESS;
[*]
[*] // Do something...
[*]
[*] // If pValue is null, print "pValue == null" to the   
[*]
[*] // console and return E_INVALID_ARG.
[*]
[*] TryCatch(pValue != null, r = E_INVALID_ARG, "pValue == null");   
[*]
[*] SetLastResult(E_SUCCESS);
[*]
[*] return _pValue;
[*]
[*]CATCH:
[*]
[*] SetLastResult(r);
[*]
[*] return null;
[*]
[*]}
TryReturn(condition,value,message)
如果条件错误,message输出,value被返回。
TryReturnVoid(conditiong, message)
如果条件错误,打印一条信息。
【编辑推荐】
   
[*]从开发到售卖 三星bada应用程序创建完整流程   
[*]三星bada开发平台概述   
[*]bada开发:OpenGL ES 2.0程序 创建简单3D图形   
[*]三星bada学习笔记:cycle life生命周期   
[*]三星bada学习笔记:基本概念
<DIV align=right>【责任编辑:小野 TEL:(010)68476606】
页: [1]
查看完整版本: 三星bada:Open API的基本风格