VirtualBox

Changeset 73920 in vbox for trunk


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
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/cpp/restbase.h

    r73918 r73920  
    317317
    318318    /**
     319     * Convert from (header) string value.
     320     *
     321     * The default implementation of is a wrapper around deserializeFromJson().
     322     *
     323     * @returns IPRT status code.
     324     * @param   a_rValue    The string value string to parse.
     325     * @param   a_pszName   Field name or similar.
     326     * @param   a_pErrInfo  Where to return additional error info.  Optional.
     327     * @param   a_fFlags    kCollectionFormat_xxx.
     328     */
     329    virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
     330                                  uint32_t a_fFlags = kCollectionFormat_Unspecified);
     331
     332    /**
    319333     * Returns the object type name.
    320334     */
     
    345359    virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_OVERRIDE;
    346360    virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = 0) const RT_OVERRIDE;
     361    virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
     362                           uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
    347363    virtual const char *getType(void) RT_OVERRIDE;
    348364
     
    370386    RTCRestInt64 &operator=(RTCRestInt64 const &a_rThat);
    371387
    372         /* Overridden methods: */
     388    /* Overridden methods: */
    373389    virtual void resetToDefault() RT_OVERRIDE;
    374390    virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_OVERRIDE;
    375391    virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_OVERRIDE;
    376392    virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = 0) const RT_OVERRIDE;
     393    virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
     394                           uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
    377395    virtual const char *getType(void) RT_OVERRIDE;
    378396
     
    405423    virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_OVERRIDE;
    406424    virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = 0) const RT_OVERRIDE;
     425    virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
     426                           uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
    407427    virtual const char *getType(void) RT_OVERRIDE;
    408428
     
    435455    virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_OVERRIDE;
    436456    virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = 0) const RT_OVERRIDE;
     457    virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
     458                           uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
    437459    virtual const char *getType(void) RT_OVERRIDE;
    438460
     
    465487    virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_OVERRIDE;
    466488    virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = 0) const RT_OVERRIDE;
     489    virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
     490                           uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
    467491    virtual const char *getType(void) RT_OVERRIDE;
    468492
     
    495519    virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_OVERRIDE;
    496520    virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = 0) const RT_OVERRIDE;
     521    virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
     522                           uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
    497523    virtual const char *getType(void) RT_OVERRIDE;
    498524};
     
    516542        return VERR_NOT_IMPLEMENTED;
    517543    }
     544    virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
     545                           uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
    518546    virtual const char *getType(void) RT_OVERRIDE;
    519547};
     
    537565        return VERR_NOT_IMPLEMENTED;
    538566    }
     567    virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
     568                           uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
    539569    virtual const char *getType(void) RT_OVERRIDE;
    540570};
     
    606636public:
    607637    /** Default constructor. */
    608     RTCRestClientResponseBase()
    609         : m_rcStatus(VERR_WRONG_ORDER)
    610     {}
    611 
     638    RTCRestClientResponseBase();
    612639    /** Destructor. */
    613     virtual ~RTCRestClientResponseBase()
    614     {}
    615 
     640    virtual ~RTCRestClientResponseBase();
    616641    /** Copy constructor. */
    617     RTCRestClientResponseBase(RTCRestClientResponseBase const &a_rThat)
    618         : m_rcStatus(a_rThat.m_rcStatus)
    619     {}
    620 
     642    RTCRestClientResponseBase(RTCRestClientResponseBase const &a_rThat);
    621643    /** Copy assignment operator. */
    622     RTCRestClientResponseBase &operator=(RTCRestClientResponseBase const &a_rThat)
    623     {
    624         m_rcStatus = a_rThat.m_rcStatus;
    625         return *this;
    626     }
     644    RTCRestClientResponseBase &operator=(RTCRestClientResponseBase const &a_rThat);
    627645
    628646    /**
     
    636654     * @param   a_pppvBody  If a body callback handler is installed, set the value pointed to to NULL.
    637655     */
    638     virtual int receivePrepare(RTHTTP a_hHttp, void ***a_pppvHdr, void ***a_pppvBody)
    639     {
    640         RT_NOREF(a_hHttp, a_pppvHdr, a_pppvBody);
    641         return VINF_SUCCESS;
    642     }
     656    virtual int receivePrepare(RTHTTP a_hHttp, void ***a_pppvHdr, void ***a_pppvBody);
    643657
    644658    /**
     
    652666     * @note    Called before consumeHeaders() and consumeBody().
    653667     */
    654     virtual void receiveComplete(int a_rcStatus, RTHTTP a_hHttp)
    655     {
    656         RT_NOREF_PV(a_hHttp);
    657         m_rcStatus = a_rcStatus;
    658     }
     668    virtual void receiveComplete(int a_rcStatus, RTHTTP a_hHttp);
    659669
    660670    /**
    661671     * Callback that consumes HTTP header data from the server.
    662672     *
    663      * @param   a_pvData   Body data.
     673     * @param   a_pchData  Body data.
    664674     * @param   a_cbData   Amount of body data.
    665675     *
    666676     * @note    Called after receiveComplete()..
    667677     */
    668     virtual void consumeHeaders(const char *a_pvData, size_t a_cbData)
    669     {
    670         RT_NOREF(a_pvData, a_cbData);
    671     }
     678    virtual void consumeHeaders(const char *a_pchData, size_t a_cbData);
    672679
    673680    /**
    674681     * Callback that consumes HTTP body data from the server.
    675682     *
    676      * @param   a_pvData   Body data.
     683     * @param   a_pchData  Body data.
    677684     * @param   a_cbData   Amount of body data.
    678685     *
    679686     * @note    Called after consumeHeaders().
    680687     */
    681     virtual void consumeBody(const char *a_pvData, size_t a_cbData)
    682     {
    683         RT_NOREF(a_pvData, a_cbData);
    684     }
     688    virtual void consumeBody(const char *a_pchData, size_t a_cbData);
    685689
    686690    /**
     
    689693     * @returns IPRT status code.
    690694     */
    691     virtual void receiveFinal()
    692     {
    693     }
     695    virtual void receiveFinal();
    694696
    695697    /**
     
    699701    int getStatus() { return m_rcStatus; }
    700702
    701 private:
     703protected:
    702704    /** Negative numbers are IPRT errors, positive are HTTP status codes. */
    703705    int m_rcStatus;
     706
     707    /**
     708     * Helper that extracts a header field.
     709     *
     710     * @retval  VINF_SUCCESS
     711     * @retval  VERR_NOT_FOUND if not found.
     712     * @retval  VERR_NO_STR_MEMORY
     713     * @param   a_pszField  The header field header name.
     714     * @param   a_cchField  The length of the header field name.
     715     * @param   a_pchData   The header blob to search.
     716     * @param   a_cbData    The size of the header blob to search.
     717     * @param   a_pStrDst   Where to store the header value on successs.
     718     */
     719    int extractHeaderFromBlob(const char *a_pszField, size_t a_cchField, const char *a_pchData, size_t a_cbData,
     720                              RTCString *a_pStrDst);
    704721};
    705722
  • 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