自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

三星bada:Open API的基本風格

移動開發(fā)
三星bada是一個新的手機平臺,它允許開發(fā)者開發(fā)豐富的應用程序用來提升用戶在移動空間中的體驗。本文介紹了三星bada平臺Open API的基本風格,以及在開發(fā)過程中可能會遇到的問題和處理方法。

1、二次構(gòu)造

在C++中當在對象初始化時分配資源失敗,那么對象知識部分初始化并且析構(gòu)函數(shù)并沒有被調(diào)用,這樣會導致資源泄露。保證資源不被泄露可以在進行二次構(gòu)造,即將一些可能分配資源失敗的放在一個Construct()函數(shù)里面。(注:這個應該是借鑒的Symbian。)

2、處理方法

與標準C++相比bada的處理方法工作起來很不相同。為了最好的封裝任何事情都是由方法進行處理。

在bada中數(shù)據(jù)損壞或者由于數(shù)據(jù)損壞導致的設備故障時不可能的,因為直接訪問數(shù)據(jù)時不可能的。

限制數(shù)據(jù)的訪問是為了阻止惡意軟件利用一些安全漏洞例如緩沖區(qū)溢出。

3、異常處理

bada的錯誤和異常處理與標準C++也是不同的。bada利用錯誤的結(jié)果代替C++的異常處理,因為C++的異常處理會占用很多的運行時間和空間。

所有的異常處理在bada中有一個返回值result類型捕捉,result類型就是unsigned long。E_SUCCESS結(jié)果表示方法返回成功,其余的所有的返回結(jié)果都是失敗的。

A、異常的偵測:

a、函數(shù)返回一個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、函數(shù)給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、轉(zhuǎn)化一個錯誤的環(huán)境到另一個錯誤的環(huán)境:

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

 4、內(nèi)存處理:

在bada中內(nèi)存通過所有權(quán)方針管理。所有權(quán)有責任刪除動態(tài)申請的內(nèi)存并且避免內(nèi)存泄漏。

獨有所有權(quán)意味著所有權(quán)不能夠被分享。得到所有權(quán)有兩條規(guī)定。

1> 新的操作符得到分配空間的所有權(quán)。

2> 所有權(quán)能夠被轉(zhuǎn)移,但是不能被分享。

圖1

圖1

5、應用程序調(diào)試:

為了幫助你調(diào)試,bada提供了很多宏指令:

1> Assert 宏指令:

Assert 宏指令是用來測試條件是否成立,如果條件不成立就殺掉進程它們沒有被編譯到發(fā)布版中。

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. 從開發(fā)到售賣 三星bada應用程序創(chuàng)建完整流程
  2. 三星bada開發(fā)平臺概述
  3. bada開發(fā):OpenGL ES 2.0程序 創(chuàng)建簡單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-07-28 11:19:55

HelloWorldbada

2010-09-05 17:03:01

bada 1.0bada三星

2011-04-29 11:24:06

2009-11-20 14:25:29

badaUI三星

2010-04-12 17:59:05

bada開發(fā)

2012-05-17 09:25:18

三星BadaAndroid

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

2010-07-17 16:31:15

cycle lifebada

2011-04-20 09:30:58

bada 2.0bada三星

2011-02-16 21:36:30

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技術(shù)棧公眾號