Tiny library for C++ enum introspection and more!

Overview

#smart_enum Tiny library for C++ enum introspection... and more!

(Sorry for this readme being incomplete, I'm working on updating it. For now, please see examples and tests.cpp. Thanks!)

##Motivating example Command-line processing:

#include "smart_enum.hpp"

#include <iostream>

void function_1();
void function_2();
void function_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))
    )
)

void function_1()
{
    std::cout << "In 'function_1'" << std::endl;
}

void function_2()
{
    std::cout << "In 'function_2'" << std::endl;
}

void function_3()
{
    std::cout << "In 'function_3'" << std::endl;
}

int main(int argc, char **argv)
{
    using namespace examples;
    using namespace smart_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;
        }

        return 0;
    }

    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 data
            auto option_data = data(option);

            std::get<1>(option_data)();
        }
        catch(const std::invalid_argument &)
        {
            std::cout << "Invalid option " << arg << std::endl;
        }
    }

    return 0;
}

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 arguments run_1 xxx run_3 shows:

In 'function_1'
Invalid option xxx
In 'function_3'

##Library usage If you are using Boost then just add smart_enum.hpp to your project and you're done. If not then also add included boost directory which contains subset of Boost.

##Supported compilers Tested 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.

##License Uses standard Boost license.

If you find this library useful I'll be glad if you star this repo :-) Any feedback is welcome!

You might also like...
 Tiny CUDA Neural Networks
Tiny CUDA Neural Networks

This is a small, self-contained framework for training and querying neural networks. Most notably, it contains a lightning fast "fully fused" multi-layer perceptron as well as support for various advanced input encodings, losses, and optimizers.

Minctest - tiny unit testing framework for ANSI C

Minctest Minctest is a very minimal unit-testing "framework" written in ANSI C and implemented in a single header file. It's handy when you want some

The purpose of this project is to apply mediapipe to more AI chips.
The purpose of this project is to apply mediapipe to more AI chips.

1.About This Project Our Official Website: www.houmo.ai Who We Are: We are Houmo - A Great AI Company. We wish to change the world with unlimited comp

The dgSPARSE Library (Deep Graph Sparse Library) is a high performance library for sparse kernel acceleration on GPUs based on CUDA.

dgSPARSE Library Introdution The dgSPARSE Library (Deep Graph Sparse Library) is a high performance library for sparse kernel acceleration on GPUs bas

The Robotics Library (RL) is a self-contained C++ library for rigid body kinematics and dynamics, motion planning, and control.

Robotics Library The Robotics Library (RL) is a self-contained C++ library for rigid body kinematics and dynamics, motion planning, and control. It co

ORB-SLAM3 is the first real-time SLAM library able to perform Visual, Visual-Inertial and Multi-Map SLAM with monocular, stereo and RGB-D cameras, using pin-hole and fisheye lens models.
ORB-SLAM3 is the first real-time SLAM library able to perform Visual, Visual-Inertial and Multi-Map SLAM with monocular, stereo and RGB-D cameras, using pin-hole and fisheye lens models.

Just to test for my research, and I add coordinate transformation to evaluate the ORB_SLAM3. Only applied in research, and respect the authors' all work.

C-based/Cached/Core Computer Vision Library, A Modern Computer Vision Library

Build Status Travis CI VM: Linux x64: Raspberry Pi 3: Jetson TX2: Backstory I set to build ccv with a minimalism inspiration. That was back in 2010, o

 Edge ML Library - High-performance Compute Library for On-device Machine Learning Inference
Edge ML Library - High-performance Compute Library for On-device Machine Learning Inference

Edge ML Library (EMLL) offers optimized basic routines like general matrix multiplications (GEMM) and quantizations, to speed up machine learning (ML) inference on ARM-based devices. EMLL supports fp32, fp16 and int8 data types. EMLL accelerates on-device NMT, ASR and OCR engines of Youdao, Inc.

A fast, scalable, high performance Gradient Boosting on Decision Trees library, used for ranking, classification, regression and other machine learning tasks for Python, R, Java, C++. Supports computation on CPU and GPU.
A fast, scalable, high performance Gradient Boosting on Decision Trees library, used for ranking, classification, regression and other machine learning tasks for Python, R, Java, C++. Supports computation on CPU and GPU.

Website | Documentation | Tutorials | Installation | Release Notes CatBoost is a machine learning method based on gradient boosting over decision tree

Comments
  • Usage inside namespaces/structs

    Usage inside namespaces/structs

    Hello,

    Unfortunately, it seems like the current approach cannot play nice with declaring an enum inside a namespace or struct. The reason being is that there is no way to do the template specialization of the traits class while nested in that namespace. Since most enums (like all things) are declared within a namespace, this seems like major issue.

    The preferred solution, IMHO (and the only clean way to get it to work with structs) is to replace the proper templated traits struct, with a simple identified based struct. So instead of generating an enum/enum class called e_1, and a specialization of smart_enum::enum_traits<e_1>, you would instead just generate a struct e_1_traits. It can still inherit from the templated base to gain some of the functionality. I've tried changing some things in the macros to make this change; I don't think it's that hard but it's not trivial either.

    What do you think? It's less elegant, but IMHO much more practical.

    Other than that it looks fantastic, I took it for a spin today and it works perfectly.

    opened by quicknir 7
  • To/from string support

    To/from string support

    There's a couple of related points here. First, it would be good to have some kind of conversion function that goes from string to an enum. Second, a couple of thoughts on the to-string interface, currently given by description. The name: could description be changed to something more idiomatic like to_string? Also, this is possibly related: what's the use case for allowing a description different from the name of the enum? Since the name of the enum doesn't actually affect the code at all, it seems like this adds more complexity and potential confusion for little benefit. The only application I see is to handle either "scoped" or unscoped names, but this could be handled by a trait or different to_string functions (or booleans).

    opened by quicknir 3
  • Support For Iteration

    Support For Iteration

    Hi, very nice library. I'm curious, do you have an appetite for adding support for iteration? I would suggest supporting two syntaxes:

    using e1_traits = smart_enum::enum_traits<e_1>;
    
    std::for_each(e1_traits.begin(), e1_traits.end(), ...);
    
    for ( auto e : e1_traits::range() ) { 
    ...
    }
    

    range() would just return a trivial object with begin and end methods. Obviously doing this properly is a bit of work (need cbegin and rbegin variants ideally)

    opened by quicknir 2
Releases(v3.0)
Owner
Jarda
Jarda
Scalable, Portable and Distributed Gradient Boosting (GBDT, GBRT or GBM) Library, for Python, R, Java, Scala, C++ and more. Runs on single machine, Hadoop, Spark, Dask, Flink and DataFlow

eXtreme Gradient Boosting Community | Documentation | Resources | Contributors | Release Notes XGBoost is an optimized distributed gradient boosting l

Distributed (Deep) Machine Learning Community 23.6k Jan 3, 2023
C++ trainable detection library based on libtorch (or pytorch c++). Yolov4 tiny provided now.

C++ Library with Neural Networks for Object Detection Based on LibTorch. ?? Libtorch Tutorials ?? Visit Libtorch Tutorials Project if you want to know

null 62 Dec 29, 2022
Tiny OpenEXR image loader/saver library

Tiny OpenEXR image library. tinyexr is a small, single header-only library to load and save OpenEXR (.exr) images. tinyexr is written in portable C++

Syoyo Fujita 556 Dec 28, 2022
A tiny C++11 library for reading BVH motion capture data

bvh11 A tiny C++11 library for reading (and writing) BVH motion capture data. Dependencies C++11 standard library Eigen 3 http://eigen.tuxfamily.org/

Yuki Koyama 33 Dec 19, 2022
Training and fine-tuning YOLOv4 Tiny on custom object detection dataset for Taiwanese traffic

Object Detection on Taiwanese Traffic using YOLOv4 Tiny Exploration of YOLOv4 Tiny on custom Taiwanese traffic dataset Trained and tested AlexeyAB's D

Andrew Chen 5 Dec 14, 2022
Lightweight, Portable, Flexible Distributed/Mobile Deep Learning with Dynamic, Mutation-aware Dataflow Dep Scheduler; for Python, R, Julia, Scala, Go, Javascript and more

Apache MXNet (incubating) for Deep Learning Apache MXNet is a deep learning framework designed for both efficiency and flexibility. It allows you to m

The Apache Software Foundation 20.2k Dec 31, 2022
heuristically and dynamically sample (more) uniformly from large decision trees of unknown shape

PROBLEM STATEMENT When writing a randomized generator for some file format in a general-purpose programming language, we can view the resulting progra

John Regehr 4 Feb 15, 2022
Pipy is a tiny, high performance, highly stable, programmable proxy.

Pipy Pipy is a tiny, high performance, highly stable, programmable proxy. Written in C++, built on top of Asio asynchronous I/O library, Pipy is extre

null 539 Dec 28, 2022
YOLOV4 tiny + lane detection on Android with 8 FPS!

YOLOV4 Tiny + Ultra fast lane detection on Android with 8 FPS! Tested with HONOR 20PRO Kirin 980

yq-pan 3 Dec 28, 2022
In this tutorial, we will use machine learning to build a gesture recognition system that runs on a tiny microcontroller, the RP2040.

Pico-Motion-Recognition This Repository has the code used on the 2 parts tutorial TinyML - Motion Recognition Using Raspberry Pi Pico The first part i

Marcelo Rovai 19 Nov 3, 2022