VirtualBox

Changeset 56118 in vbox for trunk/include/VBox/com


Ignore:
Timestamp:
May 27, 2015 7:49:50 PM (10 years ago)
Author:
vboxsync
Message:

VBoxManage: A quick command handler return-code cleanup that turned out to be rather tedious.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/VBox/com/errorprint.h

    r45125 r56118  
    5353
    5454/**
     55 * Extended macro that implements all the other CHECK_ERROR2XXX macros.
     56 *
     57 * Calls the method of the given interface and checks the return status code.
     58 * If the status indicates failure, as much information as possible is reported
     59 * about the error, including current source file and line.
     60 *
     61 * After reporting an error, the statement |stmtError| is executed.
     62 *
     63 * This macro family is intended for command line tools like VBoxManage, but
     64 * could also be handy for debugging.
     65 *
     66 * @param   type        For defining @a hrc locally inside the the macro
     67 *                      expansion, pass |HRESULT| otherwise |RT_NOTHING|.
     68 * @param   hrc         Name of the HRESULT variable to assign the result of the
     69 *                      method call to.
     70 * @param   iface       The interface pointer (can be a smart pointer object).
     71 * @param   method      The method to invoke together with the parameters.
     72 * @param   stmtError   Statement to be executed after reporting failures.  This
     73 *                      can be a |break| or |return| statement, if so desired.
     74 *
     75 * @remarks Unlike CHECK_ERROR, CHECK_ERROR_RET and family, this macro family
     76 *          does not presuppose a |rc| variable but instead either let the user
     77 *          specify the variable to use or employs  a local variable |hrcCheck|
     78 *          within its own scope.
     79 *
     80 * @sa      CHECK_ERROR2, CHECK_ERROR2I, CHECK_ERROR2_STMT, CHECK_ERROR2I_STMT,
     81 *          CHECK_ERROR2_BREAK, CHECK_ERROR2I_BREAK, CHECK_ERROR2_RET,
     82 *          CHECK_ERROR2I_RET
     83 */
     84#define CHECK_ERROR2_EX(type, hrc, iface, method, stmtError) \
     85    if (1) { \
     86        type hrc = iface->method; \
     87        if (SUCCEEDED(hrc)) \
     88        { /*likely*/ } \
     89        else \
     90        { \
     91            com::GlueHandleComError(iface, #method, (hrc), __FILE__, __LINE__); \
     92            stmtError; \
     93        } \
     94    } else do { /* nothing */ } while (0)
     95
     96
     97/**
    5598 *  Calls the given method of the given interface and then checks if the return
    5699 *  value (COM result code) indicates a failure. If so, prints the failed
     
    61104 *  Used by command line tools or for debugging and assumes the |HRESULT rc|
    62105 *  variable is accessible for assigning in the current scope.
     106 * @sa CHECK_ERROR2, CHECK_ERROR2I
    63107 */
    64108#define CHECK_ERROR(iface, method) \
     
    68112            com::GlueHandleComError(iface, #method, rc, __FILE__, __LINE__); \
    69113    } while (0)
     114/**
     115 * Simplified version of CHECK_ERROR2_EX, no error statement or type necessary.
     116 *
     117 * @param   hrc         Name of the HRESULT variable to assign the result of the
     118 *                      method call to.
     119 * @param   iface       The interface pointer (can be a smart pointer object).
     120 * @param   method      The method to invoke together with the parameters.
     121 * @sa CHECK_ERROR2I, CHECK_ERROR2_EX
     122 */
     123#define CHECK_ERROR2(hrc, iface, method)            CHECK_ERROR2_EX(RT_NOTHING, hrc, iface, method, (void)1)
     124/**
     125 * Simplified version of CHECK_ERROR2_EX that uses an internal variable
     126 * |hrcCheck| for holding the result and have no error statement.
     127 *
     128 * @param   iface       The interface pointer (can be a smart pointer object).
     129 * @param   method      The method to invoke together with the parameters.
     130 * @sa CHECK_ERROR2, CHECK_ERROR2_EX
     131 */
     132#define CHECK_ERROR2I(iface, method)                CHECK_ERROR2_EX(HRESULT, hrcCheck, iface, method, (void)1)
     133
    70134
    71135/**
    72136 * Same as CHECK_ERROR except that it also executes the statement |stmt| on
    73137 * failure.
     138 * @sa CHECK_ERROR2_STMT, CHECK_ERROR2I_STMT
    74139 */
    75140#define CHECK_ERROR_STMT(iface, method, stmt) \
     
    82147        } \
    83148    } while (0)
    84 
    85 /**
    86  * Same as CHECK_ERROR_STMT except that it uses an internal variable |hrcCheck|
    87  * for holding the result.
    88  */
    89 #define CHECK_ERROR2_STMT(iface, method, stmt) \
    90     do { \
    91         HRESULT hrcCheck = iface->method; \
    92         if (FAILED(hrcCheck)) \
    93         { \
    94             com::GlueHandleComError(iface, #method, hrcCheck, __FILE__, __LINE__); \
    95             stmt; \
    96         } \
    97     } while (0)
     149/**
     150 * Simplified version of CHECK_ERROR2_EX (no @a hrc type).
     151 *
     152 * @param   hrc         Name of the HRESULT variable to assign the result of the
     153 *                      method call to.
     154 * @param   iface       The interface pointer (can be a smart pointer object).
     155 * @param   method      The method to invoke together with the parameters.
     156 * @param   stmt        Statement to be executed after reporting failures.
     157 * @sa CHECK_ERROR2I_STMT, CHECK_ERROR2_EX
     158 */
     159#define CHECK_ERROR2_STMT(hrc, iface, method, stmt) CHECK_ERROR2_EX(RT_NOTHING, hrc, iface, method, stmt)
     160/**
     161 * Simplified version of CHECK_ERROR2_EX that uses an internal variable
     162 * |hrcCheck| for holding the result.
     163 *
     164 * @param   iface       The interface pointer (can be a smart pointer object).
     165 * @param   method      The method to invoke together with the parameters.
     166 * @param   stmt        Statement to be executed after reporting failures.
     167 * @sa CHECK_ERROR2_STMT, CHECK_ERROR2_EX
     168 */
     169#define CHECK_ERROR2I_STMT(iface, method, stmt)     CHECK_ERROR2_EX(HRESULT, hrcCheck, iface, method, stmt)
    98170
    99171
     
    101173 *  Does the same as CHECK_ERROR(), but executes the |break| statement on
    102174 *  failure.
     175 * @sa CHECK_ERROR2_BREAK, CHECK_ERROR2I_BREAK
    103176 */
    104177#ifdef __GNUC__
     
    126199    else do {} while (0)
    127200#endif
    128 
    129 /**
    130  *  Does the same as CHECK_ERROR(), but executes the |return ret| statement on
    131  *  failure.
     201/**
     202 * Simplified version of CHECK_ERROR2_EX that executes the |break| statement
     203 * after error reporting (no @a hrc type).
     204 *
     205 * @param   hrc         The result variable (type HRESULT).
     206 * @param   iface       The interface pointer (can be a smart pointer object).
     207 * @param   method      The method to invoke together with the parameters.
     208 * @sa CHECK_ERROR2I_BREAK, CHECK_ERROR2_EX
     209 */
     210#define CHECK_ERROR2_BREAK(hrc, iface, method)      CHECK_ERROR2_EX(RT_NOTHING, hrc, iface, method, break)
     211/**
     212 * Simplified version of CHECK_ERROR2_EX that executes the |break| statement
     213 * after error reporting and that uses an internal variable |hrcCheck| for
     214 * holding the result.
     215 *
     216 * @param   iface       The interface pointer (can be a smart pointer object).
     217 * @param   method      The method to invoke together with the parameters.
     218 * @sa CHECK_ERROR2_BREAK, CHECK_ERROR2_EX
     219 */
     220#define CHECK_ERROR2I_BREAK(iface, method)          CHECK_ERROR2_EX(HRESULT, hrcCheck, iface, method, break)
     221
     222
     223/**
     224 * Does the same as CHECK_ERROR(), but executes the |return ret| statement on
     225 * failure.
     226 * @sa CHECK_ERROR2_RET, CHECK_ERROR2I_RET
    132227 */
    133228#define CHECK_ERROR_RET(iface, method, ret) \
     
    140235        } \
    141236    } while (0)
    142 
    143 /**
    144  * Does the same as CHECK_ERROR(), but returns @a ret on failure.
    145  *
    146  * Unlike CHECK_ERROR and CHECK_ERROR_RET, this macro does not presuppose a
    147  * |rc| variable but instead employs a local variable |hrcCheck| in its own
    148  * scope.  This |hrcCheck| variable can be referenced by the @a rcRet
    149  * parameter.
     237/**
     238 * Simplified version of CHECK_ERROR2_EX that executes the |return (rcRet)|
     239 * statement after error reporting.
     240 *
     241 * @param   iface       The interface pointer (can be a smart pointer object).
     242 * @param   method      The method to invoke together with the parameters.
     243 * @param   rcRet       What to return on failure.
     244 */
     245#define CHECK_ERROR2_RET(hrc, iface, method, rcRet) CHECK_ERROR2_EX(RT_NOTHING, hrc, iface, method, return (rcRet))
     246/**
     247 * Simplified version of CHECK_ERROR2_EX that executes the |return (rcRet)|
     248 * statement after error reporting and that uses an internal variable |hrcCheck|
     249 * for holding the result.
    150250 *
    151251 * @param   iface       The interface pointer (can be a smart pointer object).
     
    154254 *                      the status code of the method call.
    155255 */
    156 #define CHECK_ERROR2_RET(iface, method, rcRet) \
    157     do { \
    158         HRESULT hrcCheck = iface->method; \
    159         if (FAILED(hrcCheck)) \
    160         { \
    161             com::GlueHandleComError(iface, #method, hrcCheck, __FILE__, __LINE__); \
    162             return (rcRet); \
    163         } \
    164     } while (0)
     256#define CHECK_ERROR2I_RET(iface, method, rcRet)     CHECK_ERROR2_EX(HRESULT, hrcCheck, iface, method, return (rcRet))
    165257
    166258
     
    168260 * Check the progress object for an error and if there is one print out the
    169261 * extended error information.
     262 * @remarks Requires HRESULT variable named @a rc.
    170263 */
    171264#define CHECK_PROGRESS_ERROR(progress, msg) \
     
    173266        LONG iRc; \
    174267        rc = progress->COMGETTER(ResultCode)(&iRc); \
    175         if (FAILED(iRc)) \
    176         { \
    177             rc = iRc; \
     268        if (FAILED(rc) || FAILED(iRc)) \
     269        { \
     270            if (SUCCEEDED(rc)) rc = iRc; else iRc = rc; \
    178271            RTMsgError msg; \
    179272            com::GlueHandleComErrorProgress(progress, __PRETTY_FUNCTION__, iRc, __FILE__, __LINE__); \
     
    182275
    183276/**
    184  *  Does the same as CHECK_PROGRESS_ERROR(), but executes the |break| statement
    185  *  on failure.
     277 * Does the same as CHECK_PROGRESS_ERROR(), but executes the |break| statement
     278 * on failure.
     279 * @remarks Requires HRESULT variable named @a rc.
    186280 */
    187281#ifdef __GNUC__
     
    191285        LONG iRc; \
    192286        rc = progress->COMGETTER(ResultCode)(&iRc); \
    193         if (FAILED(iRc)) \
    194         { \
    195             rc = iRc; \
     287        if (FAILED(rc) || FAILED(iRc)) \
     288        { \
     289            if (SUCCEEDED(rc)) rc = iRc; else iRc = rc; \
    196290            RTMsgError msg; \
    197291            com::GlueHandleComErrorProgress(progress, __PRETTY_FUNCTION__, iRc, __FILE__, __LINE__); \
     
    200294    })
    201295#else
    202 # define CHECK_PROGRESS_ERROR_BREAK(progress, msg) \
     296#define CHECK_PROGRESS_ERROR_BREAK(progress, msg) \
    203297    if (1) \
    204298    { \
    205299        LONG iRc; \
    206300        rc = progress->COMGETTER(ResultCode)(&iRc); \
    207         if (FAILED(iRc)) \
    208         { \
    209             rc = iRc; \
     301        if (FAILED(rc) || FAILED(iRc)) \
     302        { \
     303            if (SUCCEEDED(rc)) rc = iRc; else iRc = rc; \
    210304            RTMsgError msg; \
    211305            com::GlueHandleComErrorProgress(progress, __PRETTY_FUNCTION__, iRc, __FILE__, __LINE__); \
     
    217311
    218312/**
    219  *  Does the same as CHECK_PROGRESS_ERROR(), but executes the |return ret|
    220  *  statement on failure.
     313 * Does the same as CHECK_PROGRESS_ERROR(), but executes the |return ret|
     314 * statement on failure.
    221315 */
    222316#define CHECK_PROGRESS_ERROR_RET(progress, msg, ret) \
    223317    do { \
    224318        LONG iRc; \
    225         progress->COMGETTER(ResultCode)(&iRc); \
    226         if (FAILED(iRc)) \
     319        HRESULT hrcCheck = progress->COMGETTER(ResultCode)(&iRc); \
     320        if (SUCCEEDED(hrcCheck) && SUCCEEDED(iRc)) \
     321        { /* likely */ } \
     322        else \
    227323        { \
    228324            RTMsgError msg; \
    229             com::GlueHandleComErrorProgress(progress, __PRETTY_FUNCTION__, iRc, __FILE__, __LINE__); \
     325            com::GlueHandleComErrorProgress(progress, __PRETTY_FUNCTION__, \
     326                                            SUCCEEDED(hrcCheck) ? iRc : hrcCheck, __FILE__, __LINE__); \
    230327            return (ret); \
    231328        } \
     
    248345
    249346/**
    250  *  Does the same as ASSERT(), but executes the |return ret| statement if the
    251  *  expression to assert is false;
     347 * Does the same as ASSERT(), but executes the |return ret| statement if the
     348 * expression to assert is false;
     349 * @remarks WARNING! @a expr is evalutated TWICE!
    252350 */
    253351#define ASSERT_RET(expr, ret) \
     
    255353
    256354/**
    257  *  Does the same as ASSERT(), but executes the |break| statement if the
    258  *  expression to assert is false;
     355 * Does the same as ASSERT(), but executes the |break| statement if the
     356 * expression to assert is false;
     357 * @remarks WARNING! @a expr is evalutated TWICE!
    259358 */
    260359#define ASSERT_BREAK(expr, ret) \
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette