[Rcpp-devel] Exporting C++ enums to R

Romain Francois romain at r-enthusiasts.com
Sat Aug 10 15:30:28 CEST 2013


Le 05/08/13 22:40, Sameer D'Costa a écrit :
> Hi,
>
> I have an enum defined in a C++ library that I would like to export to
> R. I saw some references to enums in
> pkg/Rcpp/inst/include/Rcpp/module/Module.h but I could not find any
> examples of enums being used. Through trial and error I figured out that
> I could create a dummy class and add the enum to that (see code below).
> Can I export the enum directly? I saw references to RCPP_EXPOSED_ENUM
> but I could not figure out how to use that macro. Any help or pointers
> to examples where enums are used would be appreciated.
>
> Regards,
> Sameer
>
> #include "Rcpp.h"
>
> using namespace Rcpp;
>
> class Dummy {
>    int x;
>    int get_x() {return x;}
> };
>
> enum MyEnum {ENUM0, ENUM1, ENUM2};
>
>
> RCPP_MODULE(EnumMod) {
>      class_<Dummy>("EnumMod")
>      .default_constructor()
>      ;
>
>      enum_<MyEnum, Dummy>("EnumType")
>          .value("Enum0", ENUM0)
>          .value("Enum1", ENUM1)
>          .value("Enum2", ENUM2)
>          ;
> }

Unfortunately, I think you found the workaround. We need to work on a 
way to expose enums that are outside of a class definition.

Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30

R Graph Gallery: http://gallery.r-enthusiasts.com

blog:            http://blog.r-enthusiasts.com
|- http://bit.ly/13SrjxO : highlight 0.4.2
`- http://bit.ly/10X94UM : Mobile version of the graph gallery



More information about the Rcpp-devel mailing list