VirtualBox

Changeset 73933 in vbox for trunk/include/iprt


Ignore:
Timestamp:
Aug 28, 2018 8:57:23 PM (6 years ago)
Author:
vboxsync
Message:

IPRT/rest: Adding genric factor methods for every class and use it to generalize response header field parsing. bugref:9167

Location:
trunk/include/iprt
Files:
2 edited

Legend:

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

    r73930 r73933  
    295295        kCollectionFormat_ssv,              /**< Space-separated list. */
    296296        kCollectionFormat_tsv,              /**< Tab-separated list. */
    297         kCollectionFormat_pipes             /**< Pipe-separated list. */
     297        kCollectionFormat_pipes,            /**< Pipe-separated list. */
     298        kCollectionFormat_Mask = 7          /**< Collection type mask. */
    298299    };
    299300
     
    328329     */
    329330    virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
    330                                   uint32_t a_fFlags = kCollectionFormat_Unspecified);
     331                           uint32_t a_fFlags = kCollectionFormat_Unspecified);
    331332
    332333    /**
     
    334335     */
    335336    virtual const char *getType(void) = 0;
     337
     338    /**
     339     * Factory method.
     340     * @returns Pointer to new object on success, NULL if out of memory.
     341     */
     342    typedef DECLCALLBACK(RTCRestObjectBase *) FNCREATEINSTANCE(void);
     343    /** Pointer to factory method. */
     344    typedef FNCREATEINSTANCE *PFNCREATEINSTANCE;
    336345};
    337346
     
    363372    virtual const char *getType(void) RT_OVERRIDE;
    364373
     374    /** Factory method. */
     375    static DECLCALLBACK(RTCRestObjectBase *) createInstance(void);
     376
    365377public:
    366378    /** The value. */
     
    395407    virtual const char *getType(void) RT_OVERRIDE;
    396408
     409    /** Factory method. */
     410    static DECLCALLBACK(RTCRestObjectBase *) createInstance(void);
     411
    397412public:
    398413    /** The value. */
     
    427442    virtual const char *getType(void) RT_OVERRIDE;
    428443
     444    /** Factory method. */
     445    static DECLCALLBACK(RTCRestObjectBase *) createInstance(void);
     446
    429447public:
    430448    /** The value. */
     
    459477    virtual const char *getType(void) RT_OVERRIDE;
    460478
     479    /** Factory method. */
     480    static DECLCALLBACK(RTCRestObjectBase *) createInstance(void);
     481
    461482public:
    462483    /** The value. */
     
    491512    virtual const char *getType(void) RT_OVERRIDE;
    492513
     514    /** Factory method. */
     515    static DECLCALLBACK(RTCRestObjectBase *) createInstance(void);
     516
    493517public:
    494518    /** The value. */
     
    522546                           uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
    523547    virtual const char *getType(void) RT_OVERRIDE;
     548
     549    /** Factory method. */
     550    static DECLCALLBACK(RTCRestObjectBase *) createInstance(void);
    524551};
    525552
     
    528555 * Limited array class.
    529556 */
    530 template<class Type> class RTCRestArray : public RTCRestObjectBase
     557template<class ElementType> class RTCRestArray : public RTCRestObjectBase
    531558{
    532559public:
     
    535562/** @todo more later. */
    536563
    537     virtual void resetToDefault() RT_OVERRIDE;
    538     virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_OVERRIDE;
     564    virtual void resetToDefault() RT_OVERRIDE
     565    {
     566    }
     567
     568    virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_OVERRIDE
     569    {
     570        RT_NOREF(a_rDst);
     571        return a_rDst;
     572    }
     573
    539574    virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_OVERRIDE
    540575    {
     
    542577        return VERR_NOT_IMPLEMENTED;
    543578    }
     579
    544580    virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
    545                            uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
    546     virtual const char *getType(void) RT_OVERRIDE;
     581                           uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE
     582    {
     583        RT_NOREF(a_rValue, a_pszName, a_pErrInfo, a_fFlags);
     584        return VERR_NOT_IMPLEMENTED;
     585    }
     586
     587    virtual const char *getType(void) RT_OVERRIDE
     588    {
     589        return "RTCRestArray<ElementType>";
     590    }
     591
     592    /** Factory method. */
     593    static DECLCALLBACK(RTCRestObjectBase *) createInstance(void)
     594    {
     595        return new RTCRestArray<ElementType>();
     596    }
     597
     598    /** Factory method for elements. */
     599    static DECLCALLBACK(RTCRestObjectBase *) createElementInstance(void)
     600    {
     601        return new ElementType();
     602    }
    547603};
    548604
     
    551607 * Limited map class.
    552608 */
    553 template<class Type> class RTCRestStringMap : public RTCRestObjectBase
     609template<class ElementType> class RTCRestStringMap : public RTCRestObjectBase
    554610{
    555611public:
     
    558614/** @todo more later. */
    559615
    560     virtual void resetToDefault() RT_OVERRIDE;
    561     virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_OVERRIDE;
     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
    562626    virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_OVERRIDE
    563627    {
     
    565629        return VERR_NOT_IMPLEMENTED;
    566630    }
     631
    567632    virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
    568                            uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
    569     virtual const char *getType(void) RT_OVERRIDE;
     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;
     637    }
     638
     639    virtual const char *getType(void) RT_OVERRIDE
     640    {
     641        return "RTCRestStringMap<ElementType>";
     642    }
     643
     644    /** Factory method. */
     645    static DECLCALLBACK(RTCRestObjectBase *) createInstance(void)
     646    {
     647        return new RTCRestStringMap<ElementType>();
     648    }
     649
     650    /** Factory method for elements. */
     651    static DECLCALLBACK(RTCRestObjectBase *) createElementInstance(void)
     652    {
     653        return new ElementType();
     654    }
    570655};
    571656
     
    743828     * @param   ...         Message arguments.
    744829     */
    745     int         addError(int a_rc, const char *a_pszFormat, ...);
     830    int addError(int a_rc, const char *a_pszFormat, ...);
     831
     832    /** Field flags. */
     833    enum
     834    {
     835        /** Collection map, name is a prefix followed by '*'. */
     836        kHdrField_MapCollection   = RT_BIT_32(24),
     837        /** Array collection, i.e. the heade field may appear more than once. */
     838        kHdrField_ArrayCollection = RT_BIT_32(25),
     839    };
     840
     841    /** Header field descriptor. */
     842    typedef struct
     843    {
     844        /** The header field name. */
     845        const char *pszName;
     846        /** The length of the field name.*/
     847        uint32_t    cchName;
     848        /** Flags, TBD. */
     849        uint32_t    fFlags;
     850        /** Object factory. */
     851        RTCRestObjectBase::PFNCREATEINSTANCE pfnCreateInstance;
     852    } HEADERFIELDDESC;
     853
     854    /**
     855     * Helper that extracts fields from the HTTP headers.
     856     *
     857     * @param   a_paFieldsDesc      Pointer to an array of field descriptors.
     858     * @param   a_pappFieldValues   Pointer to a parallel array of value pointer pointers.
     859     * @param   a_cFields           Number of field descriptors..
     860     * @param   a_pchData           The header blob to search.
     861     * @param   a_cbData            The size of the header blob to search.
     862     */
     863    void extracHeaderFieldsFromBlob(HEADERFIELDDESC const *a_paFieldDescs, RTCRestObjectBase ***a_pappFieldValues,
     864                                    size_t a_cFields, const char *a_pchData, size_t a_cbData);
    746865
    747866    /**
  • trunk/include/iprt/err.h

    r73930 r73933  
    32143214/** Server response contains embedded zero character(s). */
    32153215#define VERR_REST_RESPONSE_EMBEDDED_ZERO_CHAR           (-25702)
     3216/** Server response contains unexpected repetitive header field. */
     3217#define VERR_REST_RESPONSE_REPEAT_HEADER_FIELD          (-25703)
    32163218/** @} */
    32173219
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