RcppMagicEnum: Rcpp Bindings to Magic Enum Library

CI License CRAN Dependencies Last Commit

Motivation

Even Modern C++ does not have proper reflection (yet). The Magic Enum library by Daniil Goncharov fills this void for enum types, and does so in a header-only fashion that is eay to deploy (provided recent-enough compilers).

A compelling example is included in the package, more examples are at the Magic Enum repository.

Example

The following file can be passed to Rcpp::sourceCpp() as is:

// [[Rcpp::depends(RcppMagicEnum)]]

#include <RcppMagicEnum>

// define a simple enum class, it uses optional typing as well as optional assigned values
enum class Color : int { RED = -10, BLUE = 0, GREEN = 10 };

// [[Rcpp::export]]
void example() {
    // instantiate an enum value in variable 'val'
    auto val = Color::RED;

    // show the current value on stdout
    Rcpp::Rcout << "Name of enum: " << magic_enum::enum_name(val) << std::endl;
    Rcpp::Rcout << "Integer value of enum: " << magic_enum::enum_integer(val) << std::endl;
}

/*** R
example()
*/

It produces the following output (where the ‘meta-comment’ at the end ensure the included and created-by-sourcing function example() is also called):

> Rcpp::sourceCpp("miniex.cpp")

> example()
Name of enum: RED
Integer value of enum: -10
>

Status

The package is complete and contains a mature version of Magic Enum.

Contributing

Any problems, bug reports, or features requests for the package can be submitted and handled most conveniently as Github issues in the repository.

Before submitting pull requests, it is frequently preferable to first discuss need and scope in such an issue ticket. See the file Contributing.md (in the Rcpp repo) for a brief discussion.

Authors

For the R package, Dirk Eddelbuettel.

For everything pertaining to Magic Enum: Daniil Goncharov.

Initially created: Tue Aug 20 04:46:22 PM CDT 2024
Last modified: Tue Aug 20 16:48:31 CDT 2024