{"id":16692900,"url":"https://github.com/seleznevae/smart_enum","last_synced_at":"2025-04-10T01:14:34.205Z","repository":{"id":93769106,"uuid":"86222917","full_name":"seleznevae/smart_enum","owner":"seleznevae","description":"Smart enums for c++","archived":false,"fork":false,"pushed_at":"2017-05-06T14:18:02.000Z","size":931,"stargazers_count":12,"open_issues_count":2,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-10T01:14:26.797Z","etag":null,"topics":["cpp14"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/seleznevae.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-26T09:58:47.000Z","updated_at":"2024-10-06T23:26:56.000Z","dependencies_parsed_at":"2023-03-08T12:00:30.160Z","dependency_job_id":null,"html_url":"https://github.com/seleznevae/smart_enum","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seleznevae%2Fsmart_enum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seleznevae%2Fsmart_enum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seleznevae%2Fsmart_enum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seleznevae%2Fsmart_enum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/seleznevae","download_url":"https://codeload.github.com/seleznevae/smart_enum/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248137891,"owners_count":21053775,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp14"],"created_at":"2024-10-12T16:28:46.529Z","updated_at":"2025-04-10T01:14:34.186Z","avatar_url":"https://github.com/seleznevae.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Smart enum\n[![Build Status](https://travis-ci.org/seleznevae/smart_enum.svg?branch=master)](https://travis-ci.org/seleznevae/smart_enum)\n [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n- [Design goals](#design-goals)\n- [Examples](#examples)\n- [License](#license)\n\nHeader-only library for compile-time C++ enum introspection.\n\n## Design goals\n\nC++ doesn't have reflection. It is a common technique to use macros to add compile time information about enumeration like the following:\n\n```c++\nenum class Animal {\n  Dog = 1,\n  Cat = 2,\n  Lion = 4,\n  Horse = 5,\n};\n\nDECLARE_ENUM(Animal, Animal::Dog, Animal::Cat, Animal::Lion, Animal::Horse);\n```\nThis code violates **DRY** (don't repeat yourself) principle. The aim of **smart enum** library is to provide a user with macros that can be used in enum declaration and the user will not need to repeat enum elements.\n\n## Highlights\n\n1. Smart enums can be declared in namespaces and classes.\n2. Smart enum to and from string conversions.\n\n\n## Examples\n\nDeclaration of smart enum looks like this:\n```c++\n#include \"smart_enum.h\"\n\nSMART_ENUM(Animal, int) {\n    SM_ENUM_ELEM(Dog,    1,  \"dog\"  , \"dog_description\");\n    SM_ENUM_ELEM(Cat,    2,  \"cat\"  );\n    SM_ENUM_ELEM(Lion,   5);\n    SM_ENUM_ELEM(Horse);\n};\n```\n\nFor each enum constant string representation and string description is generated. When macro with 1 or 2 arguments is used\n( like SM_ENUM_ELEM(Lion,   5) and SM_ENUM_ELEM(Horse) ) string representation and description will match enum constant name.\nIf you want to specify custom string representation and string description macro with 3 or 4 arguments should be used.\n\nDefinitions of variables:\n```c++\nAnimal pet_cat   ={Animal::Cat};\nAnimal pet_dog   = Animal::Dog;\nAnimal pet_lion   (Animal::Lion);\nAnimal pet_horse  {Animal::Horse};\n```\n\nIt is possible to convert smart enums to strings:\n```c++\nstd::cout \u003c\u003c \"pet name: \" \u003c\u003c pet_cat.to_string() \u003c\u003c std::endl;\n//pet name: cat\nstd::cout \u003c\u003c \"pet name: \" \u003c\u003c pet_lion.to_string() \u003c\u003c std::endl;\n//pet name: Lion\n```\n\nIt is possible get descriptions for smart enum values:\n```c++\nstd::cout \u003c\u003c \"descr.: \" \u003c\u003c pet_dog.description() \u003c\u003c std::endl;\n//descr.: dog_description\nstd::cout \u003c\u003c \"descr.: \" \u003c\u003c pet_horse.description() \u003c\u003c std::endl;\n//descr.: hOrse\n```\n\nSmart enums can be used in switch statements like ordinal enums:\n```c++\nswitch (pet_cat) {\n     case Animal::Cat:\n         assert(true);\n         break;\n     case Animal::Horse:\n         assert(false);\n         break;\n     default:\n         assert(false);\n         break;\n }\n```\nIt is possible to get information about number of elements declared in smart enum:\n```c++\nstd::cout \u003c\u003c \"Elements in smart enum: \" \u003c\u003c Animal::size() \u003c\u003c std::endl;\n//Elements in smart enum: 4\n```\n\nIt is possible to get all values, names and descriptions of smart enum:\n```c++\nstd::cout \u003c\u003c \"smart enum names: \";\nfor (const auto \u0026enum_item : Animal::values())\n    std::cout \u003c\u003c enum_item.to_string() \u003c\u003c \" \";\nstd::cout \u003c\u003c std::endl;\n//smart enum names: dog cat Lion Horse\n\nstd::cout \u003c\u003c \"smart enum names: \";\nfor (const auto \u0026name : Animal::names())\n    std::cout \u003c\u003c name \u003c\u003c \" \";\nstd::cout \u003c\u003c std::endl;\n//smart enum names: dog cat Lion Horse\n\nstd::cout \u003c\u003c \"smart enum descriptions: \";\nfor (const auto \u0026name : Animal::descriptions())\n    std::cout \u003c\u003c name \u003c\u003c \" \";\nstd::cout \u003c\u003c std::endl;\n//smart enum descriptions: dog_description cat Lion Horse\n\n\n\n```\n\nIt is possible to check if smart enum value is valid:\n```c++\nAnimal invalid;\n*(reinterpret_cast\u003cint*\u003e(\u0026invalid)) = 6666;\nassert(invalid.is_valid() == false);\n```\n\n## Installation\n\nInclude smart_enum.h in your source code.\n\n## License\nThe MIT license\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseleznevae%2Fsmart_enum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fseleznevae%2Fsmart_enum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseleznevae%2Fsmart_enum/lists"}