VirtualBox

Changeset 73875 in vbox


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

IPRT: More REST work. bugref:9167

Location:
trunk
Files:
5 added
2 edited

Legend:

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

    r73866 r73875  
    149149
    150150
     151/* forward decl: */
     152class RTCRestJsonPrimaryCursor;
     153
     154/**
     155 * JSON cursor structure.
     156 *
     157 * This reduces the number of parameters passed around when deserializing JSON
     158 * input and also helps constructing full object name for logging and error reporting.
     159 */
     160struct RTCRestJsonCursor
     161{
     162    /** Handle to the value being parsed. */
     163    RTJSONVAL                           m_hValue;
     164    /** Name of the value. */
     165    const char                         *m_pszName;
     166    /** Pointer to the parent, NULL if primary. */
     167    struct RTCRestJsonCursor const     *m_pParent;
     168    /** Pointer to the primary cursor structure. */
     169    RTCRestJsonPrimaryCursor           *m_pPrimary;
     170
     171    RTCRestJsonCursor(struct RTCRestJsonCursor const &a_rParent)
     172        : m_hValue(NIL_RTJSONVAL), m_pszName(NULL), m_pParent(&a_rParent), m_pPrimary(a_rParent.m_pPrimary)
     173    { }
     174
     175    RTCRestJsonCursor(RTJSONVAL hValue, const char *pszName, struct RTCRestJsonCursor *pParent)
     176        : m_hValue(hValue), m_pszName(pszName), m_pParent(pParent), m_pPrimary(pParent->m_pPrimary)
     177    { }
     178
     179    RTCRestJsonCursor(RTJSONVAL hValue, const char *pszName)
     180        : m_hValue(hValue), m_pszName(pszName), m_pParent(NULL), m_pPrimary(NULL)
     181    { }
     182
     183    ~RTCRestJsonCursor()
     184    {
     185        if (m_hValue != NIL_RTJSONVAL)
     186        {
     187            RTJsonValueRelease(m_hValue);
     188            m_hValue = NIL_RTJSONVAL;
     189        }
     190    }
     191};
     192
     193
     194/**
     195 * The primary JSON cursor class.
     196 */
     197class RTCRestJsonPrimaryCursor
     198{
     199public:
     200    /** The cursor for the first level. */
     201    RTCRestJsonCursor   m_Cursor;
     202    /** Error info keeper. */
     203    PRTERRINFO          m_pErrInfo;
     204
     205    /** Creates a primary json cursor with optiona error info. */
     206    RTCRestJsonPrimaryCursor(RTJSONVAL hValue, const char *pszName, PRTERRINFO pErrInfo = NULL)
     207        : m_Cursor(hValue, pszName)
     208        , m_pErrInfo(pErrInfo)
     209    {
     210        m_Cursor.m_pPrimary = this;
     211    }
     212
     213    virtual ~RTCRestJsonPrimaryCursor()
     214    {  }
     215
     216    /**
     217     * Add an error message.
     218     *
     219     * @returns a_rc
     220     * @param   a_rCursor       The cursor reporting the error.
     221     * @param   a_rc            The status code.
     222     * @param   a_pszFormat     Format string.
     223     * @param   ...             Format string arguments.
     224     */
     225    virtual int addError(RTCRestJsonCursor const &a_rCursor, int a_rc, const char *a_pszFormat, ...);
     226
     227    /**
     228     * Reports that the current field is not known.
     229     *
     230     * @returns Status to propagate.
     231     * @param   a_rCursor       The cursor for the field.
     232     */
     233    virtual int unknownField(RTCRestJsonCursor const &a_rCursor);
     234
     235    /**
     236     * Copies the full path into pszDst.
     237     *
     238     * @returns pszDst
     239     * @param   a_rCursor       The cursor to start walking at.
     240     * @param   a_pszDst        Where to put the path.
     241     * @param   a_cbDst         Size of the destination buffer.
     242     */
     243    virtual char *getPath(RTCRestJsonCursor const &a_rCursor, char *pszDst, size_t cbDst) const;
     244};
     245
     246
    151247/**
    152248 * Abstract base class for REST data objects.
     
    177273     *
    178274     * @returns IPRT status code.
    179      * @param   hJsonIt     The JSON iterator for this object.
    180      * @param   pErrInfo    Where to return additional error information.
    181      *                      Optional.
    182      *
    183      * @todo Take a RTJSONVAL?
    184      */
    185     virtual int deserializeFromJson(RTJSONIT hJsonIt, PRTERRINFO pErrInfo) = 0;
     275     * @parm    pCursor     JSON cursor.
     276     */
     277    virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) = 0;
     278
     279    /** @name Helpers for deserializing primitive types and strings.
     280     * @{ */
     281    static int deserialize_RTCString_FromJson(RTCRestJsonCursor const &a_rCursor, RTCString *a_pDst);
     282    static int deserialize_int64_t_FromJson(RTCRestJsonCursor const &a_rCursor, int64_t *a_piDst);
     283    static int deserialize_int32_t_FromJson(RTCRestJsonCursor const &a_rCursor, int32_t *a_piDst);
     284    static int deserialize_int16_t_FromJson(RTCRestJsonCursor const &a_rCursor, int16_t *a_piDst);
     285    static int deserialize_bool_FromJson(RTCRestJsonCursor const &a_rCursor, bool *a_pfDst);
     286    static int deserialize_double_FromJson(RTCRestJsonCursor const &a_rCursor, double *a_prdDst);
     287    /** @} */
     288
     289    /** @name Helpers for serializing floating point types.
     290     * @{ */
     291    static RTCRestOutputBase &serialize_double_AsJson(RTCRestOutputBase &a_rDst, double const *a_prdValue);
     292    /** @} */
    186293};
    187294
     
    199306    virtual void resetToDefaults();
    200307    virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst);
    201     virtual int deserializeFromJson(RTJSONIT hJsonIt, PRTERRINFO pErrInfo);
     308    virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor)
     309    {
     310        RT_NOREF(a_rCursor);
     311        return VERR_NOT_IMPLEMENTED;
     312    }
    202313};
    203314
     
    215326    virtual void resetToDefaults();
    216327    virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst);
    217     virtual int deserializeFromJson(RTJSONIT hJsonIt, PRTERRINFO pErrInfo);
     328    virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor)
     329    {
     330        RT_NOREF(a_rCursor);
     331        return VERR_NOT_IMPLEMENTED;
     332    }
    218333};
    219334
  • trunk/src/VBox/Runtime/Makefile.kmk

    r73761 r73875  
    543543        common/rand/randadv.cpp \
    544544        common/rand/randparkmiller.cpp \
     545        common/rest/RTCRestJsonPrimaryCursor.cpp \
     546        common/rest/RTCRestObjectBase.cpp \
    545547        common/sort/RTSortIsSorted.cpp \
    546548        common/sort/RTSortApvIsSorted.cpp \
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