VirtualBox

Changeset 73874 in vbox for trunk/src/VBox/Runtime


Ignore:
Timestamp:
Aug 24, 2018 3:36:01 PM (6 years ago)
Author:
vboxsync
Message:

RTJson: Added RTJsonValueTypeName and did some internal macro cleanups in the implementation. bugref:9167

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/misc/json.cpp

    r73616 r73874  
    12981298
    12991299
    1300 #define AssertJsonTypeReturn(pJson, enmExpectedType, ret) do {          \
    1301         AssertPtrReturn((pJson), (ret));                                \
    1302         AssertReturn((pJson) != NIL_RTJSONVAL, (ret));                  \
    1303         AssertReturn((pJson)->enmType == (enmExpectedType), (ret));     \
     1300RTDECL(const char *) RTJsonValueTypeName(RTJSONVALTYPE enmType)
     1301{
     1302    switch (enmType)
     1303    {
     1304        case RTJSONVALTYPE_INVALID: return "invalid";
     1305        case RTJSONVALTYPE_OBJECT:  return "object";
     1306        case RTJSONVALTYPE_ARRAY:   return "array";
     1307        case RTJSONVALTYPE_STRING:  return "string";
     1308        case RTJSONVALTYPE_NUMBER:  return "number";
     1309        case RTJSONVALTYPE_NULL:    return "null";
     1310        case RTJSONVALTYPE_TRUE:    return "true";
     1311        case RTJSONVALTYPE_FALSE:   return "false";
     1312        default:                    return "???";
     1313    }
     1314}
     1315
     1316
     1317#define RTJSON_ASSERT_TYPE_RETURN(pJson, enmExpectedType, ret) do { \
     1318        AssertPtrReturn((pJson), (ret));  \
     1319        AssertReturn((pJson) != NIL_RTJSONVAL, (ret)); \
     1320        AssertReturn((pJson)->enmType == (enmExpectedType), (ret)); \
    13041321    } while (0)
    13051322
    1306 #define RTJSON_TYPECHECK(pJson, enmExpectedType) do {   \
    1307         if (RT_UNLIKELY((pJson) == NIL_RTJSONVAL))      \
    1308             return VERR_INVALID_HANDLE;                \
    1309         if ((pJson)->enmType != (enmExpectedType))      \
    1310             return VERR_JSON_VALUE_INVALID_TYPE;        \
     1323#define RTJSON_TYPECHECK_RETURN(pJson, enmExpectedType) do {\
     1324        if (RT_LIKELY((pJson) != NIL_RTJSONVAL)) { /*likely*/ } \
     1325        else return VERR_INVALID_HANDLE; \
     1326        if ((pJson)->enmType == (enmExpectedType)) { /*likely*/ } \
     1327        else return VERR_JSON_VALUE_INVALID_TYPE; \
    13111328    } while (0)
    13121329
    13131330
    1314 #define RTJSON_TYPECHECK_CONTAINER(pJson) do {                  \
    1315         if (RT_UNLIKELY((pJson) == NIL_RTJSONVAL))              \
    1316             return VERR_INVALID_HANDLE;                         \
    1317         if (   (pJson)->enmType != RTJSONVALTYPE_ARRAY          \
    1318             && (pJson)->enmType != RTJSONVALTYPE_OBJECT)        \
    1319             return VERR_JSON_VALUE_INVALID_TYPE;                \
     1331#define RTJSON_TYPECHECK_CONTAINER_RETURN(pJson) do { \
     1332        if (RT_LIKELY((pJson) != NIL_RTJSONVAL)) { /* likely */ } \
     1333        else return VERR_INVALID_HANDLE; \
     1334        if (   (pJson)->enmType == RTJSONVALTYPE_ARRAY  \
     1335            || (pJson)->enmType == RTJSONVALTYPE_OBJECT) \
     1336        { /* likely */ } \
     1337        else return VERR_JSON_VALUE_INVALID_TYPE; \
    13201338    } while (0)
    13211339
     
    13241342{
    13251343    PRTJSONVALINT pThis = hJsonVal;
    1326     AssertJsonTypeReturn(pThis, RTJSONVALTYPE_STRING, NULL);
     1344    RTJSON_ASSERT_TYPE_RETURN(pThis, RTJSONVALTYPE_STRING, NULL);
    13271345
    13281346    return pThis->Type.String.pszStr;
     
    13361354    AssertPtrReturn(ppszStr, VERR_INVALID_POINTER);
    13371355
    1338     RTJSON_TYPECHECK(pThis, RTJSONVALTYPE_STRING);
     1356    RTJSON_TYPECHECK_RETURN(pThis, RTJSONVALTYPE_STRING);
    13391357    *ppszStr = pThis->Type.String.pszStr;
    13401358    return VINF_SUCCESS;
     
    13471365    AssertPtrReturn(pi64Num, VERR_INVALID_POINTER);
    13481366
    1349     RTJSON_TYPECHECK(pThis, RTJSONVALTYPE_NUMBER);
     1367    RTJSON_TYPECHECK_RETURN(pThis, RTJSONVALTYPE_NUMBER);
    13501368    *pi64Num = pThis->Type.Number.i64Num;
    13511369    return VINF_SUCCESS;
     
    13591377    AssertPtrReturn(phJsonVal, VERR_INVALID_POINTER);
    13601378
    1361     RTJSON_TYPECHECK(pThis, RTJSONVALTYPE_OBJECT);
     1379    RTJSON_TYPECHECK_RETURN(pThis, RTJSONVALTYPE_OBJECT);
    13621380
    13631381    int rc = VERR_NOT_FOUND;
     
    14331451{
    14341452    PRTJSONVALINT pThis = hJsonVal;
    1435     AssertJsonTypeReturn(pThis, RTJSONVALTYPE_ARRAY, 0);
     1453    RTJSON_ASSERT_TYPE_RETURN(pThis, RTJSONVALTYPE_ARRAY, 0);
    14361454
    14371455    return pThis->Type.Array.cItems;
     
    14441462    AssertPtrReturn(pcItems, VERR_INVALID_POINTER);
    14451463
    1446     RTJSON_TYPECHECK(pThis, RTJSONVALTYPE_ARRAY);
     1464    RTJSON_TYPECHECK_RETURN(pThis, RTJSONVALTYPE_ARRAY);
    14471465    *pcItems = pThis->Type.Array.cItems;
    14481466    return VINF_SUCCESS;
     
    14551473    AssertPtrReturn(phJsonVal, VERR_INVALID_POINTER);
    14561474
    1457     RTJSON_TYPECHECK(pThis, RTJSONVALTYPE_ARRAY);
     1475    RTJSON_TYPECHECK_RETURN(pThis, RTJSONVALTYPE_ARRAY);
    14581476    if (RT_UNLIKELY(idx >= pThis->Type.Array.cItems))
    14591477        return VERR_OUT_OF_RANGE;
     
    14701488    AssertPtrReturn(phJsonIt, VERR_INVALID_POINTER);
    14711489
    1472     RTJSON_TYPECHECK_CONTAINER(pThis);
     1490    RTJSON_TYPECHECK_CONTAINER_RETURN(pThis);
    14731491
    14741492    PRTJSONITINT pIt = (PRTJSONITINT)RTMemTmpAllocZ(sizeof(RTJSONITINT));
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