三星bada:Open API的基本風格
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:
例如:
- result r = E_SUCCESS;
- ...
- r = list.Construct(...);
- if (r != E_SUCCESS) // identical to 'if (IsFailed(r))'
- {
- // Process the error condition.
- }
b、函數(shù)給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、轉(zhuǎn)化一個錯誤的環(huán)境到另一個錯誤的環(huán)境:
- r = list.indexOf(...);
- TryReturn(r == E_SUCCESS, E_INVALID_ARG, "'%s' converted to [E_INVALID_ARG].",
- 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
5、應用程序調(diào)試:
為了幫助你調(diào)試,bada提供了很多宏指令:
1> Assert 宏指令:
Assert 宏指令是用來測試條件是否成立,如果條件不成立就殺掉進程它們沒有被編譯到發(fā)布版中。
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;
- }
在控制臺可能顯示的信息:
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;
- }
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)
如果條件錯誤,打印一條信息。
【編輯推薦】