VirtualBox

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


Ignore:
Timestamp:
Aug 27, 2018 7:38:55 PM (6 years ago)
Author:
vboxsync
Message:

iprt/rest: More work on the API code generation. bugref:9167

Location:
trunk/src/VBox/Runtime
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/Makefile.kmk

    r73918 r73920  
    16551655        common/rest/RTCRestClientApiBase.cpp \
    16561656        common/rest/RTCRestClientRequestBase.cpp \
     1657        common/rest/RTCRestClientResponseBase.cpp \
    16571658        common/rest/RTCRestJsonPrimaryCursor.cpp \
    16581659        common/rest/RTCRestOutputToString.cpp
  • trunk/src/VBox/Runtime/common/rest/rest-primary-object-types.cpp

    r73914 r73920  
    3535
    3636#include <stdio.h>
     37#include <stdlib.h>
     38#include <errno.h>
    3739
    3840
     
    5557
    5658
    57 int RTCRestObjectBase::toString(RTCString *a_pDst, uint32_t a_fFlags /*= 0*/) const
    58 {
    59     Assert(a_fFlags == 0); RT_NOREF(a_fFlags);
     59int RTCRestObjectBase::toString(RTCString *a_pDst, uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) const
     60{
     61    Assert(a_fFlags == kCollectionFormat_Unspecified); RT_NOREF(a_fFlags);
    6062
    6163    /*
     
    7678
    7779
     80int RTCRestObjectBase::fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo /*= NULL*/,
     81                                  uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/)
     82{
     83    Assert(a_fFlags == kCollectionFormat_Unspecified); RT_NOREF(a_fFlags);
     84
     85    /*
     86     * Just wrap the JSON serialization method.
     87     */
     88    RTJSONVAL hValue = NIL_RTJSONVAL;
     89    int rc = RTJsonParseFromString(&hValue, a_rValue.c_str(), a_pErrInfo);
     90    if (RT_SUCCESS(rc))
     91    {
     92        RTCRestJsonPrimaryCursor PrimaryCursor(hValue, a_pszName, a_pErrInfo);
     93        rc = deserializeFromJson(PrimaryCursor.m_Cursor);
     94    }
     95    return rc;
     96}
     97
    7898
    7999/*********************************************************************************************************************************
     
    156176
    157177
    158 int RTCRestBool::toString(RTCString *a_pDst, uint32_t a_fFlags /*= 0*/) const
    159 {
    160     Assert(a_fFlags == 0); RT_NOREF(a_fFlags);
    161 
    162     /* Be a little careful here to avoid throwing anything. */
    163     int rc = a_pDst->reserveNoThrow(m_fValue ? sizeof("true") : sizeof("false"));
    164     if (RT_SUCCESS(rc))
    165     {
    166         if (m_fValue)
    167             a_pDst->assign(RT_STR_TUPLE("true"));
    168         else
    169             a_pDst->assign(RT_STR_TUPLE("false"));
    170     }
    171     return rc;
     178int RTCRestBool::toString(RTCString *a_pDst, uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) const
     179{
     180    Assert(a_fFlags == 0); RT_NOREF(a_fFlags);
     181
     182    if (m_fValue)
     183        return a_pDst->assignNoThrow(RT_STR_TUPLE("true"));
     184    return a_pDst->assignNoThrow(RT_STR_TUPLE("false"));
     185}
     186
     187
     188int RTCRestBool::fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo /*= NULL*/,
     189                            uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/)
     190{
     191    Assert(a_fFlags == 0); RT_NOREF(a_fFlags);
     192    if (a_rValue.startsWithWord("true", RTCString::CaseInsensitive))
     193        m_fValue = true;
     194    else if (a_rValue.startsWithWord("false", RTCString::CaseInsensitive))
     195        m_fValue = false;
     196    else
     197        return RTErrInfoSetF(a_pErrInfo, VERR_INVALID_PARAMETER, "%s: unable to parse '%s' as bool", a_pszName, a_rValue.c_str());
     198    return VINF_SUCCESS;
    172199}
    173200
     
    255282
    256283
    257 int RTCRestInt64::toString(RTCString *a_pDst, uint32_t a_fFlags /*= 0*/) const
    258 {
    259     Assert(a_fFlags == 0); RT_NOREF(a_fFlags);
    260 
    261     /* Be a little careful here to avoid throwing anything. */
    262     char   szValue[64];
    263     size_t cchValue = RTStrPrintf(szValue, sizeof(szValue), "%RI64", m_iValue);
    264     int rc = a_pDst->reserveNoThrow(cchValue + 1);
    265     if (RT_SUCCESS(rc))
    266         a_pDst->assign(szValue, cchValue);
    267     return rc;
     284int RTCRestInt64::toString(RTCString *a_pDst, uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) const
     285{
     286    Assert(a_fFlags == 0); RT_NOREF(a_fFlags);
     287
     288    return a_pDst->printfNoThrow("%RI64", m_iValue);
     289}
     290
     291
     292int RTCRestInt64::fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo /*= NULL*/,
     293                             uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/)
     294{
     295    Assert(a_fFlags == 0); RT_NOREF(a_fFlags);
     296
     297    int rc = RTStrToInt64Full(RTStrStripL(a_rValue.c_str()), 10, &m_iValue);
     298    if (rc == VINF_SUCCESS || rc == VERR_TRAILING_SPACES)
     299        return VINF_SUCCESS;
     300    return RTErrInfoSetF(a_pErrInfo, rc, "%s: error %Rrc parsing '%s' as int64_t", a_pszName, rc, a_rValue.c_str());
    268301}
    269302
     
    358391
    359392
    360 int RTCRestInt32::toString(RTCString *a_pDst, uint32_t a_fFlags /*= 0*/) const
    361 {
    362     Assert(a_fFlags == 0); RT_NOREF(a_fFlags);
    363 
    364     /* Be a little careful here to avoid throwing anything. */
    365     char   szValue[16];
    366     size_t cchValue = RTStrPrintf(szValue, sizeof(szValue), "%RI32", m_iValue);
    367     int rc = a_pDst->reserveNoThrow(cchValue + 1);
    368     if (RT_SUCCESS(rc))
    369         a_pDst->assign(szValue, cchValue);
    370     return rc;
     393int RTCRestInt32::toString(RTCString *a_pDst, uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) const
     394{
     395    Assert(a_fFlags == 0); RT_NOREF(a_fFlags);
     396
     397    return a_pDst->printfNoThrow("%RI32", m_iValue);
     398}
     399
     400
     401int RTCRestInt32::fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo /*= NULL*/,
     402                             uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/)
     403{
     404    Assert(a_fFlags == 0); RT_NOREF(a_fFlags);
     405
     406    int rc = RTStrToInt32Full(RTStrStripL(a_rValue.c_str()), 10, &m_iValue);
     407    if (rc == VINF_SUCCESS || rc == VERR_TRAILING_SPACES)
     408        return VINF_SUCCESS;
     409    return RTErrInfoSetF(a_pErrInfo, rc, "%s: error %Rrc parsing '%s' as int32_t", a_pszName, rc, a_rValue.c_str());
    371410}
    372411
     
    461500
    462501
    463 int RTCRestInt16::toString(RTCString *a_pDst, uint32_t a_fFlags /*= 0*/) const
    464 {
    465     Assert(a_fFlags == 0); RT_NOREF(a_fFlags);
    466 
    467     /* Be a little careful here to avoid throwing anything. */
    468     char   szValue[8];
    469     size_t cchValue = RTStrPrintf(szValue, sizeof(szValue), "%RI16", m_iValue);
    470     int rc = a_pDst->reserveNoThrow(cchValue + 1);
    471     if (RT_SUCCESS(rc))
    472         a_pDst->assign(szValue, cchValue);
    473     return rc;
     502int RTCRestInt16::toString(RTCString *a_pDst, uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) const
     503{
     504    Assert(a_fFlags == 0); RT_NOREF(a_fFlags);
     505
     506    return a_pDst->printfNoThrow("%RI16", m_iValue);
     507}
     508
     509
     510int RTCRestInt16::fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo /*= NULL*/,
     511                             uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/)
     512{
     513    Assert(a_fFlags == 0); RT_NOREF(a_fFlags);
     514
     515    int rc = RTStrToInt16Full(RTStrStripL(a_rValue.c_str()), 10, &m_iValue);
     516    if (rc == VINF_SUCCESS || rc == VERR_TRAILING_SPACES)
     517        return VINF_SUCCESS;
     518    return RTErrInfoSetF(a_pErrInfo, rc, "%s: error %Rrc parsing '%s' as int16_t", a_pszName, rc, a_rValue.c_str());
    474519}
    475520
     
    544589
    545590
    546 int RTCRestDouble::toString(RTCString *a_pDst, uint32_t a_fFlags /*= 0*/) const
     591int RTCRestDouble::toString(RTCString *a_pDst, uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) const
    547592{
    548593    Assert(a_fFlags == 0); RT_NOREF(a_fFlags);
     
    558603    size_t const cchValue = strlen(szValue);
    559604
    560     int rc = a_pDst->reserveNoThrow(cchValue + 1);
    561     if (RT_SUCCESS(rc))
    562         a_pDst->assign(szValue, cchValue);
    563     return rc;
     605    return a_pDst->assignNoThrow(szValue, cchValue);
     606}
     607
     608
     609int RTCRestDouble::fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo /*= NULL*/,
     610                              uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/)
     611{
     612    Assert(a_fFlags == 0); RT_NOREF(a_fFlags);
     613
     614    const char *pszValue = RTStrStripL(a_rValue.c_str());
     615    errno = 0;
     616    char *pszNext = NULL;
     617    m_rdValue = strtod(pszValue, &pszNext);
     618    if (errno == 0)
     619        return VINF_SUCCESS;
     620    int rc = RTErrConvertFromErrno(errno);
     621    return RTErrInfoSetF(a_pErrInfo, rc, "%s: error %Rrc parsing '%s' as double", a_pszName, rc, a_rValue.c_str());
    564622}
    565623
     
    632690        const char  *pszValue = RTJsonValueGetString(a_rCursor.m_hValue);
    633691        const size_t cchValue = strlen(pszValue);
    634         int rc = reserveNoThrow(cchValue + 1);
     692        int rc = assignNoThrow(pszValue, cchValue);
    635693        if (RT_SUCCESS(rc))
    636         {
    637             assign(pszValue, cchValue);
    638694            return VINF_SUCCESS;
    639         }
    640695        return a_rCursor.m_pPrimary->addError(a_rCursor, rc, "no memory for %zu char long string", cchValue);
    641696    }
     
    652707
    653708
    654 int RTCRestString::toString(RTCString *a_pDst, uint32_t a_fFlags /*= 0*/) const
    655 {
    656     Assert(a_fFlags == 0); RT_NOREF(a_fFlags);
    657 
    658     /* Careful as always. */
    659     if (m_cch)
    660     {
    661         int rc = a_pDst->reserveNoThrow(m_cch + 1);
    662         if (RT_SUCCESS(rc))
    663         { /* likely */ }
    664         else
    665             return rc;
    666     }
    667     a_pDst->assign(*this);
    668     return VINF_SUCCESS;
     709int RTCRestString::toString(RTCString *a_pDst, uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/) const
     710{
     711    Assert(a_fFlags == 0); RT_NOREF(a_fFlags);
     712
     713    return a_pDst->assignNoThrow(*this);
     714}
     715
     716
     717int RTCRestString::fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo /*= NULL*/,
     718                              uint32_t a_fFlags /*= kCollectionFormat_Unspecified*/)
     719{
     720    Assert(a_fFlags == 0); RT_NOREF(a_fFlags);
     721    RT_NOREF(a_pszName); RT_NOREF(a_pErrInfo);
     722
     723    return assignNoThrow(a_rValue);
    669724}
    670725
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