- Notifications
You must be signed in to change notification settings - Fork2
Tiny library for C++ enum introspection and more!
License
krabicezpapundeklu/smart_enum
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
#smart_enumTiny library for C++enum introspection... and more!
(Sorry for this readme being incomplete, I'm working on updating it. For now, please seeexamples andtests.cpp. Thanks!)
##Motivating exampleCommand-line processing:
#include"smart_enum.hpp"#include<iostream>voidfunction_1();voidfunction_2();voidfunction_3();// define "enum class" (use "SMART_ENUM" to define "enum") ...SMART_ENUM_CLASS(// ... in namespace "examples" ... examples,// ... with name "options" having "char" type ("... enum class options : char ...") ... (options,char), (// ... with member "run_1" having data "runs 'function_1'" and pointer to "function_1" ... (run_1, ("runs 'function_1'", &function_1)),// ... with member "run_2" having data "runs 'function_2'" and pointer to "function_2" ... (run_2, ("runs 'function_2'", &function_2)),// ... with member "run_1" having data "runs 'function_3'" and pointer to "function_3" ... (run_3, ("runs 'function_3'", &function_3)) ))voidfunction_1(){ std::cout <<"In 'function_1'" << std::endl;}voidfunction_2(){ std::cout <<"In 'function_2'" << std::endl;}voidfunction_3(){ std::cout <<"In 'function_3'" << std::endl;}intmain(int argc,char **argv){usingnamespaceexamples;usingnamespacesmart_enum;if(argc ==1) { std::cout <<"Available options:" <<"(using '" << full_name<options>() <<"' enum having" << count<options>() <<" values)" << std::endl << std::endl;// for each option ...for(auto option : range<options>()) {// ... get its data and ...auto option_data =data(option); std::cout// ... print option name ... <<to_string(option) <<" ==>"// ... and its description << std::get<0>(option_data) << std::endl; }return0; }for(auto i =1; i < argc; ++i) {auto arg = argv[i];try {// find option based on its name ...auto option = from_string<options>(arg);// ... and get its dataauto option_data =data(option); std::get<1>(option_data)(); }catch(const std::invalid_argument &) { std::cout <<"Invalid option" << arg << std::endl; } }return0;}
Running this code without arguments shows:
Available options: (using 'examples::options' enum having 3 values)run_1 ==> runs 'function_1'run_2 ==> runs 'function_2'run_3 ==> runs 'function_3'Running it with argumentsrun_1 xxx run_3 shows:
In 'function_1'Invalid option xxxIn 'function_3'##Library usageIf you are usingBoost then just addsmart_enum.hpp to your project and you're done.If not then also add includedboost directory which contains subset ofBoost.
##Supported compilersTested with Clang (3.5.2, 3.7.0), g++ (4.7.3, 5.3.0) and MSVC 2015, but any compiler with C++ 11 support should be fine.
##LicenseUses standardBoost license.
If you find this library useful I'll be glad if you star this repo :-) Any feedback is welcome!
About
Tiny library for C++ enum introspection and more!
Resources
License
Uh oh!
There was an error while loading.Please reload this page.