zoukankan      html  css  js  c++  java
  • [Bada开发]API官方学习2-风格

    三星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.  r = list.Construct(...);
    4.  if (r != E_SUCCESS) // identical to 'if (IsFailed(r))'
    5.  {
    6.  // Process the error condition.
    7.  }

    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.  r = pList->Construct(...);
    4.  TryCatch(r == E_SUCCESS, delete pList, "[%s] Propagated.", GetErrorMessage(r));
    5.  ...
    6. CATCH:
    7.  SetLastResult(r);
    8.  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. MyClass::DoSomething(void)
    3. {
    4.  result r = E_SUCCESS;
    5.  r = mutex.Acquire();
    6.  // do something
    7.  r = mutex.Release();
    8.  AppAssertion(r == E_SUCCESS); // Process dies if false.
    9.  return r;
    10. }
    11. AppAsserttionf(condition, message)

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

    例如:

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

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

    图2

    Log宏指令:

    AppLog(message)

    AppLogDebug(message)

    AppLogException(message)

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

    例如:

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

    图3

    Try宏指令:

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

    TryCatch(condition,cleanup,message)

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

    例如:

    1. const A*
    2. MyClass::DoSomething(const mchar* pValue)
    3. {
    4.  result r = E_SUCCESS;
    5.  // Do something...
    6.  // If pValue is null, print "pValue == null" to the
    7.  // console and return E_INVALID_ARG.
    8.  TryCatch(pValue != null, r = E_INVALID_ARG, "pValue == null");
    9.  SetLastResult(E_SUCCESS);
    10.  return _pValue;
    11. CATCH:
    12.  SetLastResult(r);
    13.  return null;
    14. }

    TryReturn(condition,value,message)

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

    TryReturnVoid(conditiong, message)

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

  • 相关阅读:
    Windows下好用的git客户端--GitExtentions
    高分辨率下放大netbeans中的小图标
    小书匠使用手册
    win8 telnet VirtualBox中的redhat9
    win8安装新字体
    netbeans设置字体
    win7下Chrome有两个图标的解决方法
    【转】HDU-6035-Colorful Tree
    POJ1703--Find them, Catch them(种类并查集)
    KMP的妙用(利用next数组寻找字符串的循环节)
  • 原文地址:https://www.cnblogs.com/webapplee/p/3767836.html
Copyright © 2011-2022 走看看