VirtualBox

Changeset 73949 in vbox


Ignore:
Timestamp:
Aug 29, 2018 12:53:23 PM (6 years ago)
Author:
vboxsync
Message:

IPRT/rest: Basic string map implementation, sans enumeration. bugref:9167

Location:
trunk
Files:
1 added
2 edited

Legend:

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

    r73936 r73949  
    3232#include <iprt/http.h>
    3333#include <iprt/json.h>
     34#include <iprt/list.h>
    3435#include <iprt/stdarg.h>
    3536#include <iprt/cpp/ministring.h>
     
    593594    static DECLCALLBACK(RTCRestObjectBase *) createInstance(void)
    594595    {
    595         return new RTCRestArray<ElementType>();
     596        return new (std::nothrow) RTCRestArray<ElementType>();
    596597    }
    597598
     
    599600    static DECLCALLBACK(RTCRestObjectBase *) createElementInstance(void)
    600601    {
    601         return new ElementType();
    602     }
     602        return new (std::nothrow) ElementType();
     603    }
     604};
     605
     606
     607/**
     608 * Abstract base class for the RTCRestStringMap template.
     609 */
     610class RTCRestStringMapBase : public RTCRestObjectBase
     611{
     612public:
     613    /** Default destructor. */
     614    RTCRestStringMapBase();
     615    /** Copy constructor. */
     616    RTCRestStringMapBase(RTCRestStringMapBase const &a_rThat);
     617    /** Destructor. */
     618    virtual ~RTCRestStringMapBase();
     619    /** Copy assignment operator. */
     620    RTCRestStringMapBase &operator=(RTCRestStringMapBase const &a_rThat);
     621
     622    /* Overridden methods: */
     623    virtual void resetToDefault() RT_OVERRIDE;
     624    virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_OVERRIDE;
     625    virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_OVERRIDE;
     626    // later?
     627    //virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = 0) const RT_OVERRIDE;
     628    //virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
     629    //                       uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
     630
     631    /**
     632     * Clear the content of the map.
     633     */
     634    void clear();
     635
     636    /**
     637     * Gets the number of entries in the map.
     638     */
     639    size_t size() const;
     640
     641    /**
     642     * Checks if the map contains the given key.
     643     * @returns true if key found, false if not.
     644     * @param   a_pszKey   The key to check fo.
     645     */
     646    bool constainsKey(const char *a_pszKey) const;
     647
     648    /**
     649     * Checks if the map contains the given key.
     650     * @returns true if key found, false if not.
     651     * @param   a_rStrKey   The key to check fo.
     652     */
     653    bool constainsKey(RTCString const &a_rStrKey) const;
     654
     655    /**
     656     * Remove any key-value pair with the given key.
     657     * @returns true if anything was removed, false if not found.
     658     * @param   a_pszKey    The key to remove.
     659     */
     660    bool remove(const char *a_pszKey);
     661
     662    /**
     663     * Remove any key-value pair with the given key.
     664     * @returns true if anything was removed, false if not found.
     665     * @param   a_rStrKey   The key to remove.
     666     */
     667    bool remove(RTCString const &a_rStrKey);
     668
     669
     670protected:
     671    /** Map entry. */
     672    typedef struct MapEntry
     673    {
     674        /** String space core. */
     675        RTSTRSPACECORE      Core;
     676        /** List node for enumeration. */
     677        RTLISTNODE          ListEntry;
     678        /** The key.
     679         * @remarks Core.pszString points to the value of this object.  So, consider it const. */
     680        RTCString           strKey;
     681        /** The value. */
     682        RTCRestObjectBase  *pValue;
     683    } MapEntry;
     684    /** The map tree. */
     685    RTSTRSPACE          m_Map;
     686    /** The enumeration list head (MapEntry). */
     687    RTLISTANCHOR        m_ListHead;
     688    /** Number of map entries. */
     689    size_t              m_cEntries;
     690
     691protected:
     692    /**
     693     * Wrapper around the value constructor.
     694     *
     695     * @returns Pointer to new value object on success, NULL if out of memory.
     696     */
     697    virtual RTCRestObjectBase *createValue(void) = 0;
     698
     699    /**
     700     * Wrapper around the value copy constructor.
     701     *
     702     * @returns Pointer to copy on success, NULL if out of memory.
     703     * @param   a_pSrc      The value to copy.
     704     */
     705    virtual RTCRestObjectBase *createValueCopy(RTCRestObjectBase const *a_pSrc) = 0;
     706
     707    /**
     708     * Worker for the copy constructor and the assignment operator.
     709     *
     710     * This will use createEntryCopy to do the copying.
     711     *
     712     * @returns VINF_SUCCESS on success, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
     713     * @param   a_rThat     The map to copy.  Caller makes 100% sure the it has
     714     *                      the same type as the destination.
     715     * @param   a_fThrow    Whether to throw error.
     716     */
     717    int copyMapWorker(RTCRestStringMapBase const &a_rThat, bool fThrow);
     718
     719    /**
     720     * Worker for performing inserts.
     721     *
     722     * @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
     723     *          VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
     724     * @param   a_pszKey        The key.
     725     * @param   a_pValue        The value to insert.  Ownership is transferred to the map on success.
     726     * @param   a_fReplace      Whether to replace existing key-value pair with matching key.
     727     */
     728    int putWorker(const char *a_pszKey, RTCRestObjectBase *a_pValue, bool a_fReplace);
     729
     730    /**
     731     * Worker for performing inserts.
     732     *
     733     * @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
     734     *          VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
     735     * @param   a_pszKey        The key.
     736     * @param   a_rValue        The value to copy into the map.
     737     * @param   a_fReplace      Whether to replace existing key-value pair with matching key.
     738     */
     739    int putCopyWorker(const char *a_pszKey, RTCRestObjectBase const &a_rValue, bool a_fReplace);
     740
     741    /**
     742     * Worker for getting the value corresponding to the given key.
     743     *
     744     * @returns Pointer to the value object if found, NULL if key not in the map.
     745     * @param   a_pszKey        The key which value to look up.
     746     */
     747    RTCRestObjectBase *getWorker(const char *a_pszKey);
     748
     749    /**
     750     * Worker for getting the value corresponding to the given key, const variant.
     751     *
     752     * @returns Pointer to the value object if found, NULL if key not in the map.
     753     * @param   a_pszKey        The key which value to look up.
     754     */
     755    RTCRestObjectBase const *getWorker(const char *a_pszKey) const;
     756
     757private:
     758    static DECLCALLBACK(int) stringSpaceDestructorCallback(PRTSTRSPACECORE pStr, void *pvUser);
    603759};
    604760
     
    607763 * Limited map class.
    608764 */
    609 template<class ElementType> class RTCRestStringMap : public RTCRestObjectBase
    610 {
    611 public:
    612     RTCRestStringMap() {};
    613     ~RTCRestStringMap() {};
    614 /** @todo more later. */
    615 
    616     virtual void resetToDefault() RT_OVERRIDE
    617     {
    618     }
    619 
    620     virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_OVERRIDE
    621     {
    622         RT_NOREF(a_rDst);
    623         return a_rDst;
    624     }
    625 
    626     virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_OVERRIDE
    627     {
    628         RT_NOREF(a_rCursor);
    629         return VERR_NOT_IMPLEMENTED;
    630     }
    631 
    632     virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
    633                            uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE
    634     {
    635         RT_NOREF(a_rValue, a_pszName, a_pErrInfo, a_fFlags);
    636         return VERR_NOT_IMPLEMENTED;
     765template<class ValueType> class RTCRestStringMap : public RTCRestStringMapBase
     766{
     767public:
     768    /** Default constructor, creates emtpy map. */
     769    RTCRestStringMap()
     770        : RTCRestStringMapBase()
     771    {}
     772
     773    /** Copy constructor. */
     774    RTCRestStringMap(RTCRestStringMap const &a_rThat)
     775        : RTCRestStringMapBase()
     776    {
     777        copyMapWorker(a_rThat, true /*a_fThrow*/);
     778    }
     779
     780    /** Destructor. */
     781    virtual ~RTCRestStringMap()
     782    {
     783       /* nothing to do here. */
     784    }
     785
     786    /** Copy assignment operator. */
     787    RTCRestStringMap &operator=(RTCRestStringMap const &a_rThat)
     788    {
     789        copyMapWorker(a_rThat, true /*a_fThrow*/);
     790        return *this;
    637791    }
    638792
    639793    virtual const char *getType(void) RT_OVERRIDE
    640794    {
    641         return "RTCRestStringMap<ElementType>";
     795        return "RTCRestStringMap<ValueType>";
    642796    }
    643797
     
    645799    static DECLCALLBACK(RTCRestObjectBase *) createInstance(void)
    646800    {
    647         return new RTCRestStringMap<ElementType>();
    648     }
    649 
    650     /** Factory method for elements. */
    651     static DECLCALLBACK(RTCRestObjectBase *) createElementInstance(void)
    652     {
    653         return new ElementType();
    654     }
     801        return new (std::nothrow) RTCRestStringMap<ValueType>();
     802    }
     803
     804    /** Factory method for values. */
     805    static DECLCALLBACK(RTCRestObjectBase *) createValueInstance(void)
     806    {
     807        return new (std::nothrow) ValueType();
     808    }
     809
     810    /**
     811     * Inserts the given object into the map.
     812     *
     813     * @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
     814     *          VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
     815     * @param   a_pszKey        The key.
     816     * @param   a_pValue        The value to insert.  Ownership is transferred to the map on success.
     817     * @param   a_fReplace      Whether to replace existing key-value pair with matching key.
     818     */
     819    int put(const char *a_pszKey, ValueType *a_pValue, bool a_fReplace = false)
     820    {
     821        return putWorker(a_pszKey, a_pValue, a_fReplace);
     822    }
     823
     824    /**
     825     * Inserts the given object into the map.
     826     *
     827     * @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
     828     *          VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
     829     * @param   a_rStrKey       The key.
     830     * @param   a_pValue        The value to insert.  Ownership is transferred to the map on success.
     831     * @param   a_fReplace      Whether to replace existing key-value pair with matching key.
     832     */
     833    int put(RTCString const &a_rStrKey, ValueType *a_pValue, bool a_fReplace = false)
     834    {
     835        return putWorker(a_rStrKey.c_str(), a_pValue, a_fReplace);
     836    }
     837
     838    /**
     839     * Inserts a copy of the given object into the map.
     840     *
     841     * @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
     842     *          VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
     843     * @param   a_pszKey        The key.
     844     * @param   a_rValue        The value to insert a copy of.
     845     * @param   a_fReplace      Whether to replace existing key-value pair with matching key.
     846     */
     847    int putCopy(const char *a_pszKey, const ValueType &a_rValue, bool a_fReplace = false)
     848    {
     849        return putCopyWorker(a_pszKey, a_rValue, a_fReplace);
     850    }
     851
     852    /**
     853     * Inserts a copy of the given object into the map.
     854     *
     855     * @returns VINF_SUCCESS or VWRN_ALREADY_EXISTS on success.
     856     *          VERR_ALREADY_EXISTS, VERR_NO_MEMORY or VERR_NO_STR_MEMORY on failure.
     857     * @param   a_rStrKey       The key.
     858     * @param   a_rValue        The value to insert a copy of.
     859     * @param   a_fReplace      Whether to replace existing key-value pair with matching key.
     860     */
     861    int putCopy(RTCString const &a_rStrKey, const ValueType &a_rValue, bool a_fReplace = false)
     862    {
     863        return putCopyWorker(a_rStrKey.c_str(), a_rValue, a_fReplace);
     864    }
     865
     866    /**
     867     * Gets the value corresponding to the given key.
     868     *
     869     * @returns Pointer to the value object if found, NULL if key not in the map.
     870     * @param   a_pszKey        The key which value to look up.
     871     */
     872    ValueType *get(const char *a_pszKey)
     873    {
     874        return (ValueType *)getWorker(a_pszKey);
     875    }
     876
     877    /**
     878     * Gets the value corresponding to the given key.
     879     *
     880     * @returns Pointer to the value object if found, NULL if key not in the map.
     881     * @param   a_rStrKey       The key which value to look up.
     882     */
     883    ValueType *get(RTCString const &a_rStrKey)
     884    {
     885        return (ValueType *)getWorker(a_pszKey.c_str());
     886    }
     887
     888    /**
     889     * Gets the const value corresponding to the given key.
     890     *
     891     * @returns Pointer to the value object if found, NULL if key not in the map.
     892     * @param   a_pszKey        The key which value to look up.
     893     */
     894    ValueType const *get(const char *a_pszKey) const
     895    {
     896        return (ValueType const *)getWorker(a_pszKey);
     897    }
     898
     899    /**
     900     * Gets the const value corresponding to the given key.
     901     *
     902     * @returns Pointer to the value object if found, NULL if key not in the map.
     903     * @param   a_rStrKey       The key which value to look up.
     904     */
     905    ValueType const *get(RTCString const &a_rStrKey) const
     906    {
     907        return (ValueType const *)getWorker(a_pszKey.c_str());
     908    }
     909
     910    /** @todo enumerator*/
     911
     912protected:
     913    virtual RTCRestObjectBase *createValue(void) RT_OVERRIDE
     914    {
     915        return new (std::nothrow) ValueType();
     916    }
     917
     918    virtual RTCRestObjectBase *createValueCopy(RTCRestObjectBase const *a_pSrc) RT_OVERRIDE
     919    {
     920        return new (std::nothrow) ValueType(*(ValueType const *)a_pSrc);
     921    }
     922};
     923
     924
     925/**
     926 * Dynamic REST object.
     927 *
     928 * @todo figure this one out. it's possible this is only used in maps and
     929 *       could be a specialized map implementation.
     930 */
     931class RTCRestObject : public RTCRestObjectBase
     932{
     933public:
     934    /** Default destructor. */
     935    RTCRestObject();
     936    /** Copy constructor. */
     937    RTCRestObject(RTCRestBool const &a_rThat);
     938    /** Destructor. */
     939    virtual ~RTCRestObject();
     940    /** Copy assignment operator. */
     941    RTCRestBool &operator=(RTCRestObject const &a_rThat);
     942
     943    /* Overridden methods: */
     944    virtual void resetToDefault() RT_OVERRIDE;
     945    virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_OVERRIDE;
     946    virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_OVERRIDE;
     947    virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = 0) const RT_OVERRIDE;
     948    virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
     949                           uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
     950    virtual const char *getType(void) RT_OVERRIDE;
     951
     952    /** Factory method. */
     953    static DECLCALLBACK(RTCRestObjectBase *) createInstance(void);
     954
     955protected:
     956    /** @todo figure out the value stuff here later... */
    655957};
    656958
  • trunk/src/VBox/Runtime/Makefile.kmk

    r73920 r73949  
    16571657        common/rest/RTCRestClientResponseBase.cpp \
    16581658        common/rest/RTCRestJsonPrimaryCursor.cpp \
     1659        common/rest/RTCRestStringMapBase.cpp \
    16591660        common/rest/RTCRestOutputToString.cpp
    16601661endif
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