VirtualBox

Changeset 73803 in vbox


Ignore:
Timestamp:
Aug 21, 2018 3:00:15 PM (6 years ago)
Author:
vboxsync
Message:

iprt/cpp/restbase.h: Some serialization and deserialization ideas. bugref:9167

File:
1 edited

Legend:

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

    r73802 r73803  
    2828
    2929#include <iprt/types.h>
     30#include <iprt/assert.h>
    3031#include <iprt/err.h>
    3132#include <iprt/http.h>
     33#include <iprt/json.h>
     34#include <iprt/stdarg.h>
     35#include <iprt/cpp/ministring.h>
    3236#include <iprt/cpp/utils.h>
    3337
     
    4044
    4145/**
    42  * Base class for REST data objects.
     46 * Abstract base class for serializing data objects.
     47 */
     48class RTCRestOutputBase
     49{
     50public:
     51    RTCRestOutputBase();
     52    virtual ~RTCRestOutputBase();
     53
     54    /**
     55     * RTStrPrintf like function (see @rel pg_rt_str_format).
     56     *
     57     * @returns Number of bytes outputted.
     58     * @param   uDepth      The indentation level.
     59     * @param   pszFormat   The format string.
     60     * @param   ...         Argument specfied in @a pszFormat.
     61     */
     62    size_t         printf(unsigned uIndent, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(2, 3)
     63    {
     64        va_list va;
     65        va_start(va, pszFormat);
     66        size_t cchWritten = this->vprintf(uIndent, pszFormat, va);
     67        va_end(va);
     68        return cchWritten;
     69    }
     70
     71    /**
     72     * RTStrPrintfV like function (see @rel pg_rt_str_format).
     73     *
     74     * @returns Number of bytes outputted.
     75     * @param   uDepth      The indentation level.
     76     * @param   pszFormat   The format string.
     77     * @param   va          Argument specfied in @a pszFormat.
     78     */
     79    virtual size_t vprintf(unsigned uIndent, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(2, 0) = 0;
     80};
     81
     82
     83/**
     84 * Serialize to a string object.
     85 */
     86class RTCRestOutputToString : public RTCRestOutputBase, public RTCNonCopyable
     87{
     88public:
     89    /**
     90     * Creates an instance that appends to @a a_pDst.
     91     * @param   a_pDst      Pointer to the destination string object.
     92     *                      NULL is not accepted and will assert.
     93     */
     94    RTCRestOutputToString(RTCString *a_pDst);
     95    virtual ~RTCRestOutputToString();
     96
     97    size_t vprintf(unsigned uIndent, const char *pszFormat, va_list va);
     98
     99    /**
     100     * Finalizes the output and releases the string object to the caller.
     101     *
     102     * @returns The released string object.  NULL if we ran out of memory or if
     103     *          called already.
     104     *
     105     * @remark  This sets m_pDst to NULL and the object cannot be use for any
     106     *          more output afterwards.
     107     */
     108    virtual RTCString *finalize(void);
     109
     110
     111protected:
     112    /** Pointer to the destination string.  NULL after finalize().   */
     113    RTCString  *m_pDst;
     114    /** Set if we ran out of memory and should ignore subsequent calls. */
     115    bool        m_fOutOfMemory;
     116};
     117
     118
     119/**
     120 * Abstract base class for REST data objects.
    43121 */
    44122class RTCSRestObjectBase
     
    47125    RTCSRestObjectBase() {}
    48126    virtual ~RTCSRestObjectBase() {}
     127
    49128    /** @todo Add some kind of state? */
     129
     130    /**
     131     * Serialize the object as JSON.
     132     *
     133     * @returns a_rDst
     134     * @param   a_rDst      The destination for the serialization.
     135     * @param   uIndent     The indentation level.  Increment by 1 for child
     136     *                      objects.
     137     */
     138    virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst, unsigned uIndent) = 0;
     139
     140    /**
     141     * Deserialize object from the given JSON iterator.
     142     *
     143     * @returns IPRT status code.
     144     * @param   hJsonIt     The JSON iterator for this object.
     145     * @param   pErrInfo    Where to return additional error information.
     146     *                      Optional.
     147     *
     148     * @todo Take a RTJSONVAL?
     149     */
     150    virtual int deserializeFromJson(RTJSONIT hJsonIt, PRTERRINFO pErrInfo) = 0;
    50151};
    51152
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