[Rcpp-devel] Rcpp attributes, default values for enums

Gábor Csárdi csardi.gabor at gmail.com
Sat Mar 1 15:39:06 CET 2014


On Sat, Mar 1, 2014 at 9:30 AM, JJ Allaire <jj.allaire at gmail.com> wrote:

> Gabor,
>
> The issue is that we don't know how to translate C++ enums into R (since R
> has no enum construct). Since R typically uses a character vector for
> enumerated values, another way to approach this would be use a std::string
> and then just convert it to enum within the C++ implementation.
>

Yes, I guess it could be translated to a character vector. This is what I
am doing currently as a hack:

https://github.com/gaborcsardi/Rcpp/commit/028f9ac9ab032c8eb196005f0a481f0562387397

I don't see how std::string could be a good solution, because I have
separate enums and want to create as<> conversions for them, so if all of
them are std::string, then they cannot have separate as<> conversions.

It would be great to use the R convention, e.g.

myfun <- function(enum=c("foo", "bar", "foobar")) {
  enum <- match.arg(enum)
  .Call(...)
}

but I guess this is hard to do, because the code generator in
attributes.cpp does not have type information. So every "solution" is
necessarily hacky. If you have other ideas, please let me know.

Best,
Gabor


> J.J.
>
>
> On Fri, Feb 28, 2014 at 11:32 PM, Gábor Csárdi <csardi.gabor at gmail.com>wrote:
>
>> Hi All,
>>
>> it seems that this is currently not supported.
>>
>> For this:
>>
>> // [[Rcpp::export("L_tree")]]
>> NumericMatrix rogdf_tree_layout(GraphAttributes graph,
>>  double siblingDistance=20, double subtreeDistance=20,
>>  double levelDistance=50, double treeDistance=50,
>>  bool orthogonalLayout=false,
>>  Orientation orientation=topToBottom,
>>  TreeLayout::RootSelectionType selectRoot=TreeLayout::rootIsSource) {
>> ...
>>
>> where the last two arguments are enums, I get:
>>
>> Warning messages:
>> 1: Unable to parse C++ default value 'topToBottom' for argument
>> orientation of function rogdf_tree_layout
>> 2: Unable to parse C++ default value 'TreeLayout::rootIsSource' for
>> argument selectRoot of function rogdf_tree_layout
>>
>> and there are no default values in the generated R code.
>>
>> Is there a workaround? Any chance of adding support for it? How hard do
>> you think it is to support it?
>>
>> Thanks, Best,
>> Gabor
>>
>>
>> _______________________________________________
>> Rcpp-devel mailing list
>> Rcpp-devel at lists.r-forge.r-project.org
>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140301/8cd4c571/attachment.html>


More information about the Rcpp-devel mailing list