Skip to content

C++ static support for converting strings to associated enumeration items (header only)

License

Notifications You must be signed in to change notification settings

cflaviu/decl_enum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 

Repository files navigation

decl-enum

C++ support for enumerations with textual representation (header only). The enumeration is sequential, starting from zero. It supports conversions enumeration-to-text and text-to-enumeration. The implementation is based on static hash maps (std::unordered_map).

Example

#include <decl_enum.hpp>

decl_enum(currency,  euro, dollar, pound, renminbi);

This will lead to

namespace currency
{
  enum id
  {
    euro,
    dollar,
    pound,
    renminbi
  };

  namespace meta
  {
    constexpr unsigned item_count = 4;
    
    constexpr const char* const texts[item_count] = { "euro", "dollar", "pound", "renminbi" };
    
    constexpr const char* text_of(const id id_item) noexcept;
    
    std::pair<id, bool> parse(const char* text_item);
    std::pair<id, bool> parse(const char* text_item, const size_t text_item_length);
  }
}

Usage

auto data = currency::meta::parse(text, text_length);
if (data.second)
{
   currency::id value = data.first;
}
auto text = currency::meta::text_of(currency::euro);

About

C++ static support for converting strings to associated enumeration items (header only)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages