VirtualBox

Changeset 74023 in vbox for trunk/include/iprt


Ignore:
Timestamp:
Sep 2, 2018 1:43:59 PM (6 years ago)
Author:
vboxsync
Message:

IPRT/rest: Implemented header collection (x-obmcs-header-collection) and multi-query array mapping. Renamed RTCRestObjectBase::getType() to typeName() to avoid clashing with generated code. bugref:9167

Location:
trunk/include/iprt
Files:
2 edited

Legend:

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

    r74020 r74023  
    3333#include <iprt/json.h>
    3434#include <iprt/list.h>
     35#include <iprt/string.h>
    3536#include <iprt/stdarg.h>
    3637#include <iprt/cpp/ministring.h>
     
    314315     * @note When adding collection format types, make sure to also
    315316     *       update RTCRestArrayBase::toString().
     317     * @note Bit 24 is reserved (for kHdrField_MapCollection).
    316318     */
    317319    enum
     
    360362                           uint32_t a_fFlags = kCollectionFormat_Unspecified);
    361363
     364    /** Type classification */
     365    typedef enum kTypeClass
     366    {
     367        kTypeClass_Invalid = 0,
     368        kTypeClass_Bool,                /**< Primitive: bool. */
     369        kTypeClass_Int64,               /**< Primitive: bool. */
     370        kTypeClass_Int32,               /**< Primitive: bool. */
     371        kTypeClass_Int16,               /**< Primitive: bool. */
     372        kTypeClass_Double,              /**< Primitive: bool. */
     373        kTypeClass_String,              /**< Primitive: bool. */
     374        kTypeClass_Object,              /**< Object (any kind of data model object). */
     375        kTypeClass_Array,               /**< Array (containing any kind of object). */
     376        kTypeClass_StringMap            /**< String map (containing any kind of object). */
     377    } kTypeClass;
     378
     379    /**
     380     * Returns the object type class.
     381     */
     382    virtual kTypeClass typeClass(void) const;
     383
    362384    /**
    363385     * Returns the object type name.
    364386     */
    365     virtual const char *getType(void) = 0;
     387    virtual const char *typeName(void) const = 0;
    366388
    367389    /**
     
    414436    virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
    415437                           uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
    416     virtual const char *getType(void) RT_OVERRIDE;
     438    virtual kTypeClass typeClass(void) const RT_OVERRIDE;
     439    virtual const char *typeName(void) const RT_OVERRIDE;
    417440
    418441    /** Factory method. */
     
    453476    virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
    454477                           uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
    455     virtual const char *getType(void) RT_OVERRIDE;
     478    virtual kTypeClass typeClass(void) const RT_OVERRIDE;
     479    virtual const char *typeName(void) const RT_OVERRIDE;
    456480
    457481    /** Factory method. */
     
    492516    virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
    493517                           uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
    494     virtual const char *getType(void) RT_OVERRIDE;
     518    virtual kTypeClass typeClass(void) const RT_OVERRIDE;
     519    virtual const char *typeName(void) const RT_OVERRIDE;
    495520
    496521    /** Factory method. */
     
    531556    virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
    532557                           uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
    533     virtual const char *getType(void) RT_OVERRIDE;
     558    virtual kTypeClass typeClass(void) const RT_OVERRIDE;
     559    virtual const char *typeName(void) const RT_OVERRIDE;
    534560
    535561    /** Factory method. */
     
    570596    virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
    571597                           uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
    572     virtual const char *getType(void) RT_OVERRIDE;
     598    virtual kTypeClass typeClass(void) const RT_OVERRIDE;
     599    virtual const char *typeName(void) const RT_OVERRIDE;
    573600
    574601    /** Factory method. */
     
    613640    virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
    614641                           uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
    615     virtual const char *getType(void) RT_OVERRIDE;
     642    virtual kTypeClass typeClass(void) const RT_OVERRIDE;
     643    virtual const char *typeName(void) const RT_OVERRIDE;
    616644
    617645    /** Factory method. */
     
    638666    virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
    639667                           uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
     668    virtual kTypeClass typeClass(void) const RT_OVERRIDE;
     669    virtual const char *typeName(void) const RT_OVERRIDE;
    640670
    641671    /**
     
    660690    {
    661691        return m_cElements;
     692    }
     693
     694    /**
     695     * Returns the base object pointer at a given index.
     696     *
     697     * @returns The base object at @a a_idx, NULL if out of range.
     698     * @param   a_idx           The array index.
     699     */
     700    RTCRestObjectBase *atBase(size_t a_idx)
     701    {
     702        if (a_idx < m_cElements)
     703            return m_papElements[a_idx];
     704        return NULL;
     705    }
     706
     707    /**
     708     * Returns the const base object pointer at a given index.
     709     *
     710     * @returns The base object at @a a_idx, NULL if out of range.
     711     * @param   a_idx           The array index.
     712     */
     713    RTCRestObjectBase const *atBase(size_t a_idx) const
     714    {
     715        if (a_idx < m_cElements)
     716            return m_papElements[a_idx];
     717        return NULL;
    662718    }
    663719
     
    669725     */
    670726    bool removeAt(size_t a_idx);
    671 
    672727
    673728    /**
     
    782837    {
    783838        return copyArrayWorker(a_rThat, false /*fThrow*/);
    784     }
    785 
    786     virtual const char *getType(void) RT_OVERRIDE
    787     {
    788         return "RTCRestArray<ElementType>";
    789839    }
    790840
     
    10091059    //virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
    10101060    //                       uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
     1061    virtual kTypeClass typeClass(void) const RT_OVERRIDE;
     1062    virtual const char *typeName(void) const RT_OVERRIDE;
    10111063
    10121064    /**
     
    10481100    bool remove(RTCString const &a_rStrKey);
    10491101
     1102    /**
     1103     * Creates a new value and inserts it under the given key, returning the new value.
     1104     *
     1105     * @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
     1106     *          VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
     1107     * @param   a_ppValue   Where to return the pointer to the value.
     1108     * @param   a_pszKey    The key to put it under.
     1109     * @param   a_cchKey    The length of the key.  Default is the entire string.
     1110     * @param   a_fReplace  Whether to replace or fail on key collision.
     1111     */
     1112    int putNewValue(RTCRestObjectBase **a_ppValue, const char *a_pszKey, size_t a_cchKey = RTSTR_MAX, bool a_fReplace = false);
     1113
     1114    /**
     1115     * Creates a new value and inserts it under the given key, returning the new value.
     1116     *
     1117     * @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
     1118     *          VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
     1119     * @param   a_ppValue   Where to return the pointer to the value.
     1120     * @param   a_pszKey    The key to put it under.
     1121     * @param   a_fReplace  Whether to replace or fail on key collision.
     1122     */
     1123    int putNewValue(RTCRestObjectBase **a_ppValue, RTCString const &a_rStrKey, bool a_fReplace = false);
    10501124
    10511125protected:
     
    10701144    size_t              m_cEntries;
    10711145
     1146public:
     1147    /** @name Map Iteration
     1148     * @{  */
     1149    /** Const iterator. */
     1150    class ConstIterator
     1151    {
     1152    private:
     1153        MapEntry            *m_pCur;
     1154        ConstIterator();
     1155    protected:
     1156        ConstIterator(MapEntry *a_pEntry) : m_pCur(a_pEntry) { }
     1157    public:
     1158        ConstIterator(ConstIterator const &a_rThat) : m_pCur(a_rThat.m_pCur) { }
     1159
     1160        /** Gets the key string. */
     1161        RTCString const         &getKey()   { return m_pCur->strKey; }
     1162        /** Gets poitner to the value object. */
     1163        RTCRestObjectBase const *getValue() { return m_pCur->pValue; }
     1164
     1165        /** Advance to the next map entry. */
     1166        ConstIterator &operator++()
     1167        {
     1168            m_pCur = RTListNodeGetNextCpp(&m_pCur->ListEntry, MapEntry, ListEntry);
     1169            return *this;
     1170        }
     1171
     1172        /** Advance to the previous map entry. */
     1173        ConstIterator &operator--()
     1174        {
     1175            m_pCur = RTListNodeGetPrevCpp(&m_pCur->ListEntry, MapEntry, ListEntry);
     1176            return *this;
     1177        }
     1178
     1179        /** Compare equal. */
     1180        bool operator==(ConstIterator const &a_rThat) { return m_pCur == a_rThat.m_pCur; }
     1181        /** Compare not equal. */
     1182        bool operator!=(ConstIterator const &a_rThat) { return m_pCur != a_rThat.m_pCur; }
     1183
     1184        /* Map class must be friend so it can use the MapEntry constructor. */
     1185        friend RTCRestStringMapBase;
     1186    };
     1187
     1188    /** Returns iterator for the first map entry (unless it's empty and it's also the end). */
     1189    ConstIterator begin() const { return ConstIterator(RTListGetFirstCpp(&m_ListHead, MapEntry, ListEntry)); }
     1190    /** Returns iterator for the last map entry (unless it's empty and it's also the end). */
     1191    ConstIterator last() const  { return ConstIterator(RTListGetLastCpp(&m_ListHead, MapEntry, ListEntry)); }
     1192    /** Returns the end iterator.  This does not ever refer to an actual map entry. */
     1193    ConstIterator end() const   { return ConstIterator(RT_FROM_CPP_MEMBER(&m_ListHead, MapEntry, ListEntry)); }
     1194    /** @} */
     1195
     1196
    10721197protected:
    10731198    /**
     
    11061231     * @param   a_pValue        The value to insert.  Ownership is transferred to the map on success.
    11071232     * @param   a_fReplace      Whether to replace existing key-value pair with matching key.
    1108      */
    1109     int putWorker(const char *a_pszKey, RTCRestObjectBase *a_pValue, bool a_fReplace);
     1233     * @param   a_cchKey        The key length, the whole string by default.
     1234     */
     1235    int putWorker(const char *a_pszKey, RTCRestObjectBase *a_pValue, bool a_fReplace, size_t a_cchKey = RTSTR_MAX);
    11101236
    11111237    /**
     
    11171243     * @param   a_rValue        The value to copy into the map.
    11181244     * @param   a_fReplace      Whether to replace existing key-value pair with matching key.
    1119      */
    1120     int putCopyWorker(const char *a_pszKey, RTCRestObjectBase const &a_rValue, bool a_fReplace);
     1245     * @param   a_cchKey        The key length, the whole string by default.
     1246     */
     1247    int putCopyWorker(const char *a_pszKey, RTCRestObjectBase const &a_rValue, bool a_fReplace, size_t a_cchKey = RTSTR_MAX);
    11211248
    11221249    /**
     
    11761303    {
    11771304        return copyMapWorker(a_rThat, false /*a_fThrow*/);
    1178     }
    1179 
    1180     virtual const char *getType(void) RT_OVERRIDE
    1181     {
    1182         return "RTCRestStringMap<ValueType>";
    11831305    }
    11841306
     
    12201342    int put(RTCString const &a_rStrKey, ValueType *a_pValue, bool a_fReplace = false)
    12211343    {
    1222         return putWorker(a_rStrKey.c_str(), a_pValue, a_fReplace);
     1344        return putWorker(a_rStrKey.c_str(), a_pValue, a_fReplace, a_rStrKey.length());
    12231345    }
    12241346
     
    12481370    int putCopy(RTCString const &a_rStrKey, const ValueType &a_rValue, bool a_fReplace = false)
    12491371    {
    1250         return putCopyWorker(a_rStrKey.c_str(), a_rValue, a_fReplace);
     1372        return putCopyWorker(a_rStrKey.c_str(), a_rValue, a_fReplace, a_rStrKey.length());
    12511373    }
    12521374
     
    13471469    virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
    13481470                           uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
    1349     virtual const char *getType(void) RT_OVERRIDE;
     1471    virtual kTypeClass typeClass(void) const RT_OVERRIDE;
     1472    virtual const char *typeName(void) const RT_OVERRIDE;
    13501473
    13511474    /** Factory method. */
     
    14521575    typedef struct
    14531576    {
    1454         const char                 *pszName;    /**< The parameter name. */
    1455         uint32_t                    fFlags;     /**< The toString flags. */
    1456         bool                        fRequired;  /**< Required or not. */
    1457         uint8_t                     iBitNo;     /**< The parameter bit number. */
     1577        const char *pszName;            /**< The parameter name. */
     1578        uint32_t    fFlags;             /**< The toString flags. */
     1579        bool        fRequired;          /**< Required or not. */
     1580        uint8_t     iBitNo;             /**< The parameter bit number. */
    14581581    } QUERYPARAMDESC;
    14591582
     
    14731596    typedef struct
    14741597    {
    1475         const char                 *pszName;    /**< The parameter name. */
    1476         uint32_t                    fFlags;     /**< The toString flags. */
    1477         bool                        fRequired;  /**< Required or not. */
    1478         uint8_t                     iBitNo;     /**< The parameter bit number. */
     1598        const char *pszName;            /**< The parameter name. */
     1599        uint32_t    fFlags;             /**< The toString flags. */
     1600        bool        fRequired;          /**< Required or not. */
     1601        uint8_t     iBitNo;             /**< The parameter bit number. */
     1602        bool        fMapCollection;     /**< Collect headers starting with pszName into a map. */
    14791603    } HEADERPARAMDESC;
    14801604
     
    16191743        /** Collection map, name is a prefix followed by '*'. */
    16201744        kHdrField_MapCollection   = RT_BIT_32(24),
    1621         /** Array collection, i.e. the heade field may appear more than once. */
    1622         kHdrField_ArrayCollection = RT_BIT_32(25)
    16231745    };
    16241746
  • trunk/include/iprt/err.h

    r73978 r74023  
    32183218/** Server response contains unexpected repetitive header field. */
    32193219#define VERR_REST_RESPONSE_REPEAT_HEADER_FIELD          (-25703)
     3220
     3221/** Internal error \#1. */
     3222#define VERR_REST_INTERAL_ERROR_1                       (-25791)
     3223/** Internal error \#2. */
     3224#define VERR_REST_INTERAL_ERROR_2                       (-25792)
     3225/** Internal error \#3. */
     3226#define VERR_REST_INTERAL_ERROR_3                       (-25793)
     3227/** Internal error \#4. */
     3228#define VERR_REST_INTERAL_ERROR_4                       (-25794)
     3229/** Internal error \#5. */
     3230#define VERR_REST_INTERAL_ERROR_5                       (-25795)
     3231/** Internal error \#6. */
     3232#define VERR_REST_INTERAL_ERROR_6                       (-25796)
     3233/** Internal error \#7. */
     3234#define VERR_REST_INTERAL_ERROR_7                       (-25797)
     3235/** Internal error \#8. */
     3236#define VERR_REST_INTERAL_ERROR_8                       (-25798)
     3237/** Internal error \#9. */
     3238#define VERR_REST_INTERAL_ERROR_9                       (-25799)
    32203239/** @} */
    32213240
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