VirtualBox

Changeset 74040 in vbox for trunk/include/iprt


Ignore:
Timestamp:
Sep 3, 2018 12:25:52 PM (6 years ago)
Author:
vboxsync
Message:

IPRT/rest: Hacked up string <-> enum translation. bugref:9167

File:
1 edited

Legend:

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

    r74025 r74040  
    369369        kTypeClass_Object,              /**< Object (any kind of data model object). */
    370370        kTypeClass_Array,               /**< Array (containing any kind of object). */
    371         kTypeClass_StringMap            /**< String map (containing any kind of object). */
     371        kTypeClass_StringMap,           /**< String map (containing any kind of object). */
     372        kTypeClass_StringEnum           /**< String enum. */
    372373    } kTypeClass;
    373374
     
    389390    /** Pointer to factory method. */
    390391    typedef FNCREATEINSTANCE *PFNCREATEINSTANCE;
    391 
    392392
    393393protected:
     
    409409{
    410410public:
    411     /** Default destructor. */
     411    /** Default constructor. */
    412412    RTCRestBool();
    413413    /** Copy constructor. */
     
    449449{
    450450public:
    451     /** Default destructor. */
     451    /** Default constructor. */
    452452    RTCRestInt64();
    453453    /** Copy constructor. */
     
    489489{
    490490public:
    491     /** Default destructor. */
     491    /** Default constructor. */
    492492    RTCRestInt32();
    493493    /** Copy constructor. */
     
    529529{
    530530public:
    531     /** Default destructor. */
     531    /** Default constructor. */
    532532    RTCRestInt16();
    533533    /** Copy constructor. */
     
    569569{
    570570public:
    571     /** Default destructor. */
     571    /** Default constructor. */
    572572    RTCRestDouble();
    573573    /** Copy constructor. */
     
    609609{
    610610public:
    611     /** Default destructor. */
     611    /** Default constructor. */
    612612    RTCRestString();
    613613    /** Destructor. */
     
    644644
    645645/**
     646 * String enum base class.
     647 */
     648class RT_DECL_CLASS RTCRestStringEnumBase : public RTCRestObjectBase
     649{
     650public:
     651    /** Enum map entry. */
     652    typedef struct ENUMMAPENTRY
     653    {
     654        const char *pszName;
     655        uint32_t    cchName;
     656        int32_t     iValue;
     657    } ENUMMAPENTRY;
     658
     659    /** Default constructor. */
     660    RTCRestStringEnumBase();
     661    /** Destructor. */
     662    virtual ~RTCRestStringEnumBase();
     663
     664    /** Copy constructor. */
     665    RTCRestStringEnumBase(RTCRestStringEnumBase const &a_rThat);
     666    /** Copy assignment operator. */
     667    RTCRestStringEnumBase &operator=(RTCRestStringEnumBase const &a_rThat);
     668
     669    /** Safe copy assignment method. */
     670    int assignCopy(RTCRestStringEnumBase const &a_rThat);
     671    /** Safe copy assignment method. */
     672    int assignCopy(RTCString const &a_rThat)    { return setByString(a_rThat); }
     673    /** Safe copy assignment method. */
     674    int assignCopy(const char *a_pszThat)       { return setByString(a_pszThat); }
     675
     676    /* Overridden methods: */
     677    virtual int resetToDefault() RT_OVERRIDE;
     678    virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_OVERRIDE;
     679    virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_OVERRIDE;
     680    virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = kCollectionFormat_Unspecified) const RT_OVERRIDE;
     681    virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
     682                           uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
     683    virtual kTypeClass typeClass(void) const RT_OVERRIDE;
     684
     685    /**
     686     * Sets the value given a C-string value.
     687     *
     688     * @retval VINF_SUCCESS on success.
     689     * @retval VWRN_NOT_FOUND if not mappable to enum value.
     690     * @retval VERR_NO_STR_MEMORY if not mappable and we're out of memory.
     691     * @param   a_pszValue      The string value.
     692     * @param   a_cchValue      The string value length.  Optional.
     693     */
     694    int setByString(const char *a_pszValue, size_t a_cchValue = RTSTR_MAX);
     695
     696    /**
     697     * Sets the value given a string value.
     698     *
     699     * @retval VINF_SUCCESS on success.
     700     * @retval VWRN_NOT_FOUND if not mappable to enum value.
     701     * @retval VERR_NO_STR_MEMORY if not mappable and we're out of memory.
     702     * @param   a_pszValue      The string value.
     703     */
     704    int setByString(RTCString const &a_rValue);
     705
     706    /**
     707     * Gets the string value.
     708     */
     709    const char *getString() const;
     710
     711    /** Maps the given string value to an enum. */
     712    int stringToEnum(const char *a_pszValue, size_t a_cchValue = RTSTR_MAX);
     713    /** Maps the given string value to an enum. */
     714    int stringToEnum(RTCString const &a_rStrValue);
     715    /** Maps the given string value to an enum. */
     716    const char *enumToString(int a_iEnumValue, size_t *a_pcchString);
     717
     718
     719protected:
     720    /** The enum value. */
     721    int                 m_iEnumValue;
     722    /** The string value if not a match. */
     723    RTCString           m_strValue;
     724
     725    /**
     726     * Worker for setting the object to the given enum value.
     727     *
     728     * @retval  true on success.
     729     * @retval  false if a_iEnumValue can't be translated.
     730     * @param   a_iEnumValue    The enum value to set.
     731     */
     732    bool                setWorker(int a_iEnumValue);
     733
     734    /**
     735     * Gets the mapping table.
     736     *
     737     * @returns Pointer to the translation table.
     738     * @param   pcEntries   Where to return the translation table size.
     739     */
     740    virtual ENUMMAPENTRY const *getMappingTable(size_t *pcEntries) const = 0;
     741};
     742
     743
     744/**
    646745 * Dynamic REST object.
    647746 *
     
    652751{
    653752public:
    654     /** Default destructor. */
     753    /** Default constructor. */
    655754    RTCRestObject();
    656755    /** Destructor. */
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