VirtualBox

Changeset 74390 in vbox


Ignore:
Timestamp:
Sep 20, 2018 4:28:29 PM (6 years ago)
Author:
vboxsync
Message:

IPRT/rest: Replaced thrice duplicate enum wrapping code with a template class. bugref:9167

File:
1 edited

Legend:

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

    r74387 r74390  
    950950
    951951/**
     952 * String enum template class.
     953 *
     954 * Takes the enum type as argument.
     955 */
     956template <typename EnumType>
     957class RTCRestStringEnum : public RTCRestStringEnumBase
     958{
     959public:
     960    typedef EnumType Type;  /**< The enum type. */
     961
     962    /** Default constructor */
     963    RTCRestStringEnum() : RTCRestStringEnumBase() { }
     964    /** Constructor with initial enum value. */
     965    RTCRestStringEnum(Type a_enmValue) : RTCRestStringEnumBase() { set(a_enmValue); }
     966    /** Constructor with string default. */
     967    RTCRestStringEnum(const char *a_pszDefault) : RTCRestStringEnumBase() { setByString(a_pszDefault); }
     968    /** Copy constructor */
     969    RTCRestStringEnum(RTCRestStringEnum const &a_rThat) : RTCRestStringEnumBase(a_rThat) { }
     970    /** Make a clone of this object. */
     971    inline RTCRestStringEnum *clone() const RT_OVERRIDE { return (RTCRestStringEnum *)baseClone(); }
     972
     973    virtual RTCRestObjectBase *baseClone() const RT_OVERRIDE { return cloneWorker(new (std::nothrow) RTCRestStringEnum()); }
     974
     975    /**
     976     * Gets the enum value.
     977     * @returns enum value.
     978     * @retval  kXxxxInvalid means there was no mapping for the string, or that
     979     *          no value has been assigned yet.
     980     */
     981    Type get() const { return (Type)m_iEnumValue; }
     982
     983    /**
     984     * Sets the object value to @a a_enmType
     985     *
     986     * @returns true if a_enmType is valid, false if not.
     987     * @param   a_enmType   The new value.
     988     */
     989    bool set(Type a_enmType) { return setWorker((int)a_enmType); }
     990
     991    virtual const char *typeName(void) const RT_OVERRIDE { return "RTCRestStringEnum<EnumType>"; }
     992
     993    /** Factory method. */
     994    static DECLCALLBACK(RTCRestObjectBase *) createInstance(void) { return new (std::nothrow) RTCRestStringEnum(); }
     995
     996protected:
     997    /** Enum mapping table. */
     998    static const ENUMMAPENTRY s_aMappingTable[];
     999    /** Enum mapping table size. */
     1000    static const size_t       s_cMappingTable;
     1001
     1002    virtual ENUMMAPENTRY const *getMappingTable(size_t *pcEntries) const RT_OVERRIDE
     1003    {
     1004        *pcEntries = s_cMappingTable;
     1005        return s_aMappingTable;
     1006    }
     1007};
     1008
     1009
     1010/**
    9521011 * Class for handling binary blobs (strings).
    9531012 *
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