VirtualBox

Changeset 22488 in vbox for trunk/include/iprt


Ignore:
Timestamp:
Aug 26, 2009 8:56:21 PM (16 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
51508
Message:

iprt/types.h,iprt/err.h: Strict type checks on what's passed to RT_SUCCESS, RT_FAILURE and their _NP variants.

Location:
trunk/include/iprt
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/err.h

    r21784 r22488  
    4646 */
    4747
     48#ifdef __cplusplus
     49/**
     50 * Strict type validation class.
     51 *
     52 * This is only really useful for type checking the arguments to RT_SUCCESS,
     53 * RT_SUCCESS_NP, RT_FAILURE and RT_FAILURE_NP.  The RTErrStrictType2
     54 * constructor is for integration with external status code strictness regimes.
     55 */
     56class RTErrStrictType
     57{
     58protected:
     59    int32_t m_rc;
     60
     61public:
     62    /**
     63     * Constructor for interaction with external status code strictness regimes.
     64     *
     65     * This is a special constructor for helping external return code validator
     66     * classes interact cleanly with RT_SUCCESS, RT_SUCCESS_NP, RT_FAILURE and
     67     * RT_FAILURE_NP while barring automatic cast to integer.
     68     *
     69     * @param   rcObj       IPRT status code object from an automatic cast.
     70     */
     71    RTErrStrictType(RTErrStrictType2 const rcObj)
     72        : m_rc(rcObj.getValue())
     73    {
     74    }
     75
     76    /**
     77     * Integer constructor used by RT_SUCCESS_NP.
     78     *
     79     * @param   rc          IPRT style status code.
     80     */
     81    RTErrStrictType(int32_t rc)
     82        : m_rc(rc)
     83    {
     84    }
     85
     86#if 0 /** @todo figure where int32_t is long instead of int. */
     87    /**
     88     * Integer constructor used by RT_SUCCESS_NP.
     89     *
     90     * @param   rc          IPRT style status code.
     91     */
     92    RTErrStrictType(signed int rc)
     93        : m_rc(rc)
     94    {
     95    }
     96#endif
     97
     98    /**
     99     * Test for success.
     100     */
     101    bool success() const
     102    {
     103        return m_rc >= 0;
     104    }
     105
     106private:
     107    /** @name Try ban a number of wrong types.
     108     * @{ */
     109    RTErrStrictType(uint8_t rc)         : m_rc(-999) { NOREF(rc); }
     110    RTErrStrictType(uint16_t rc)        : m_rc(-999) { NOREF(rc); }
     111    RTErrStrictType(uint32_t rc)        : m_rc(-999) { NOREF(rc); }
     112    RTErrStrictType(uint64_t rc)        : m_rc(-999) { NOREF(rc); }
     113    RTErrStrictType(int8_t rc)          : m_rc(-999) { NOREF(rc); }
     114    RTErrStrictType(int16_t rc)         : m_rc(-999) { NOREF(rc); }
     115    RTErrStrictType(int64_t rc)         : m_rc(-999) { NOREF(rc); }
     116    /** @todo fight long here - clashes with int32_t/int64_t on some platforms. */
     117    /** @} */
     118};
     119#endif /* __cplusplus */
     120
     121
     122/** @def RTERR_STRICT_RC
     123 * Indicates that RT_SUCCESS_NP, RT_SUCCESS, RT_FAILURE_NP and RT_FAILURE should
     124 * make type enforcing at compile time.
     125 *
     126 * @remarks     Only define this for C++ code.
     127 */
     128#if defined(__cplusplus) \
     129 && !defined(RTERR_STRICT_RC) \
     130 && (   defined(DOXYGEN_RUNNING) \
     131     || defined(DEBUG) \
     132     || defined(RT_STRICT) )
     133# define RTERR_STRICT_RC        1
     134#endif
     135
     136
    48137/** @def RT_SUCCESS
    49138 * Check for success. We expect success in normal cases, that is the code path depending on
     
    55144 * @param   rc  The iprt status code to test.
    56145 */
    57 #define RT_SUCCESS(rc)      ( RT_LIKELY((int)(rc) >= VINF_SUCCESS) )
     146#define RT_SUCCESS(rc)      ( RT_LIKELY(RT_SUCCESS_NP(rc)) )
    58147
    59148/** @def RT_SUCCESS_NP
     
    65154 * @param   rc  The iprt status code to test.
    66155 */
    67 #define RT_SUCCESS_NP(rc)   ( (int)(rc) >= VINF_SUCCESS )
     156#ifdef RTERR_STRICT_RC
     157# define RT_SUCCESS_NP(rc)   ( RTErrStrictType(rc).success() )
     158#else
     159# define RT_SUCCESS_NP(rc)   ( (int)(rc) >= VINF_SUCCESS )
     160#endif
    68161
    69162/** @def RT_FAILURE
  • trunk/include/iprt/types.h

    r22457 r22488  
    14521452typedef const RTMAC *PCRTMAC;
    14531453
     1454
     1455#ifdef __cplusplus
     1456/**
     1457 * Strict type validation helper class.
     1458 *
     1459 * See RTErrStrictType and RT_SUCCESS_NP.
     1460 */
     1461class RTErrStrictType2
     1462{
     1463protected:
     1464    /** The status code.  */
     1465    int32_t m_rc;
     1466
     1467public:
     1468    /**
     1469     * Constructor.
     1470     * @param   rc      IPRT style status code.
     1471     */
     1472    RTErrStrictType2(int32_t rc) : m_rc(rc)
     1473    {
     1474    }
     1475
     1476    /**
     1477     * Get the status code.
     1478     * @returns IPRT style status code.
     1479     */
     1480    int32_t getValue() const
     1481    {
     1482        return m_rc;
     1483    }
     1484};
     1485#endif /* __cplusplus */
    14541486/** @} */
    14551487
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