Skip to content

Extensible Enum #100

Closed
Closed

Description

Pattern proposal for extensible enum: https://godbolt.org/z/BauE8C

#include <string>

char AsciiToUpper(const char c) noexcept {
    class AsciiToUpperTable {
        char m_map[256];
    public:
        constexpr AsciiToUpperTable() noexcept : m_map() {
            for (int idx = 0; idx < 256; ++idx) {
                m_map[idx] = static_cast<char>(static_cast<unsigned char>(idx));
            }
 
            for (int idx = 'a'; idx < 'z'; ++idx) {
                m_map[idx] = idx + ('a' - 'A');
            }
        } 
        char operator[](const char c) const noexcept { return m_map[static_cast<unsigned char>(c)]; }
    };
 
    static constexpr AsciiToUpperTable toUpperTable;
    return toUpperTable[c];
}
 
inline bool CaseInSensStringCompareCpp11(const std::string& str1, const std::string &str2) noexcept {
    return std::equal(str1.begin(), str1.end(), str2.begin(), str2.end(),
        [](const char lhs, const char rhs) noexcept { return AsciiToUpper(lhs) == AsciiToUpper(rhs); });
}


// This goes in a .cpp file:
    
/// <summary>
/// <see cref="JsonWebKey"/> key types.
/// </summary>
class KeyType {
    std::string mValue;

public:
    /// <summary>
    /// Initializes a new instance of the <see cref="KeyType"/> structure.
    /// </summary>
    /// <param name="value"></param>
    KeyType(const std::string& value) : mValue(value) { }

    KeyType(const char* value) : mValue(value) { }

    /// <summary>
    /// Determines if two <see cref="KeyType"/> values are the same.
    /// </summary>
    /// <param name="a">The first <see cref="KeyType"/> to compare.</param>
    /// <param name="b">The second <see cref="KeyType"/> to compare.</param>
    /// <returns>True if <paramref name="a"/> and <paramref name="b"/> are the same; otherwise, false.</returns>
    bool operator==(const KeyType& other) const noexcept { return CaseInSensStringCompareCpp11(mValue, other.mValue); }

    /// <summary>
    /// Determines if two <see cref="KeyType"/> values are different.
    /// </summary>
    /// <param name="a">The first <see cref="KeyType"/> to compare.</param>
    /// <param name="b">The second <see cref="KeyType"/> to compare.</param>
    /// <returns>True if <paramref name="a"/> and <paramref name="b"/> are different; otherwise, false.</returns>
    bool operator!=(const KeyType& other) const noexcept { return !(*this == other); }

    const std::string& Get() const noexcept { return mValue; }

    /// <summary>
    /// Elliptic curve cryptographic algorithm.
    /// </summary>
    const static KeyType Ec;

    /// <summary>
    /// Elliptic curve cryptographic algorithm backed by HSM.
    /// </summary>
    const static KeyType EcHsm;

    /// <summary>
    /// RSA cryptographic algorithm.
    /// </summary>
    const static KeyType Rsa;

    /// <summary>
    /// RSA cryptographic algorithm backed by HSM.
    /// </summary>
    const static KeyType RsaHsm;

    /// <summary>
    /// AES cryptographic algorithm.
    /// </summary>
    const static KeyType Oct;    
};

// These go in a .cpp file:
const KeyType KeyType::Ec = "EC";
const KeyType KeyType::EcHsm = "EC-HSM";
const KeyType KeyType::Rsa = "RSA";
const KeyType KeyType::RsaHsm = "RSA-HSM";
const KeyType KeyType::Oct = "oct";

Activity

  1. ghost added
    needs-triageWorkflow: This is a new issue that needs to be triaged to the appropriate team.
    on May 12, 2020
  2. changed the title [-][FEATURE REQ] Extensible Enum[/-] [+]Extensible Enum[/+] on May 13, 2020
  3. added
    ClientThis issue points to a problem in the data-plane of the library.
    feature-requestThis issue requires a new behavior in the product in order be resolved.
    and removed
    needs-triageWorkflow: This is a new issue that needs to be triaged to the appropriate team.
    on May 13, 2020
  4. RickWinter commented on Jul 23, 2020

    @RickWinter
    Member

    Anton Kolesnyk (@antkmsft) you mentioned you were adding an extensible enum with your logging work. Here is a proposed pattern for the extensible enum

  5. RickWinter commented on Dec 15, 2020

    @RickWinter
    Member

    Guidelines are updated with the design

  6. locked and limited conversation to collaborators on Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Azure.CoreClientThis issue points to a problem in the data-plane of the library.feature-requestThis issue requires a new behavior in the product in order be resolved.

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions