Samsung bada: basic Open API Style

Source: Internet
Author: User

1. quadratic structure

In C ++, when the resource allocation fails during object initialization, the object knowledge is partially initialized and the Destructor is not called, which may cause resource leakage. To ensure that resources are not leaked, you can perform secondary construction. Some resources that may fail to be allocated can be placed in a Construct function. Note: this should be a reference for Symbian .)

2. Handling Method

Compared with the standard C ++, bada's processing methods work very differently. To best encapsulate everything, it is done by methods.

In bada, data corruption or device failure caused by data corruption is not possible because direct access to data is not possible.

Data access is restricted to prevent malware from exploiting security vulnerabilities such as buffer overflow.

3. Exception Handling

Bada's error and exception handling are also different from standard C ++. Bada uses error results to replace C ++'s exception handling, because C ++'s exception handling takes a lot of running time and space.

All Exception Handling Methods in bada have a return value of the result type capture, and the result type is unsigned long. The E_SUCCESS result indicates that the method returns successfully, and all other returned results fail.

A. exception detection:

A. The function returns a result:

For example:

 
 
  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. The function assigns a value to the result or returns null:

For example:

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

C. If fails to jump to 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. Exception Handling:

A. Use goto CATCH for processing:

 
 
  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. Try to put back E_SUCCESS:

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

C. Return a null value:

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

D. convert a wrong environment to another wrong environment:

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

4. Memory Processing:

The memory in bada is managed by the ownership policy. It is the responsibility of ownership to delete the dynamically applied memory and avoid Memory leakage.

Proprietary ownership means that ownership cannot be shared. There are two rules for obtaining ownership.

1> the new operator obtains the ownership of the allocated space.

2> ownership can be transferred but cannot be shared.

Figure 1

5. Application debugging:

Bada provides many macro commands to help you Debug:

1> Assert macro commands:

The Assert macro command is used to test whether the conditions are true. If the conditions are not true, kill the process and they are not compiled into the release version.

AppAssertioncondition)

This is used to check whether the program has a logical error. If an error is returned, the current process is killed.

For example:

 
 
  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. AppAsserttionfcondition, message) 

This is used to check whether the program has a logic error. If an error is returned, the current process is killed and a message is displayed on the console.

For example:

 
 
  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.  

Information that may be displayed on the console:

Log macro command:

AppLog (message)

AppLogDebugmessage)

AppLogExceptionmessage)

AppLog allows you to output arbitrary information. AppLogDebug and AppLogException work in the same way. Information is displayed in the console or file.

For example:

 
 
  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.  

Try macro command:

The Try macro command simulates the try-catch of the Standard C ++. Unlike Assert, try not to kill the process.

TryCatchcondition, cleanup, message)

TryCatch detection condition. If it fails, print a piece of information, evaluate a cleanup expression, and then gotoCATCH:

For example:

 
 
  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)

If the condition is incorrect, the message is output, and the value is returned.

TryReturnVoid (conditiong, message)

If the conditions are incorrect, print a message.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.