Rcpp 枚举支持
Rcpp Enum Support
我正在使用 Rcpp 模块导出 class 方法。其中一些方法具有 return 枚举类型。例如:
#include "Enum.h"
#include <Rcpp.h>
using namespace Rcpp;
class EnumTest{
public:
EnumTest(){}
void setP(Polarization pol){p = pol;}
Polarization getP(){return p;}
private:
Polarization p;
};
RCPP_EXPOSED_CLASS(EnumTest)
RCPP_MODULE(EnumTest){
class_<EnumTest>("EnumTest")
.property("p", &EnumTest::getP, &EnumTest::setP)
;
}
Polarization是枚举,定义如下:
enum Polarization{
HORIZONTAL_POL = 0,
VERTICAL_POL = 1
};
当我尝试构建代码时,出现以下错误。
cannot convert 'SEXP' to 'Polarization' in initialization
是否有向 Rcpp 公开枚举的方法,类似于公开 classes 的方式?我在 Rcpp 模块小插图中注意到枚举类型列在未来的扩展中。这是否意味着没有办法做到这一点?如果是这样,是否有任何可能的解决方法?
如文档所述,仅接口from/to R(我们使用/应该使用)是
SEXP .Call(yourFunction, SEXP a, SEXP b, SEXP c, ...)
Rcpp 为您的转换器提供 to/from 基本类型。但是您的 enum 不是这些 base 之一,因此您必须编写自己的转换器,或者只传递会传递的整数值。
Rcpp 中对枚举的支持有限,由宏 here 提供。
所以你可以这样做:
#include <Rcpp.h>
using namespace Rcpp;
enum Polarization{
HORIZONTAL_POL = 0,
VERTICAL_POL = 1
};
RCPP_EXPOSED_ENUM_NODECL(Polarization)
class EnumTest{
public:
EnumTest(){}
void setP(Polarization pol){p = pol;}
Polarization getP(){return p;}
private:
Polarization p;
};
RCPP_MODULE(Bla){
class_<EnumTest>("EnumTest")
.constructor()
.property("p", &EnumTest::getP, &EnumTest::setP)
;
}
支持非常基本,本质上允许您将枚举作为整数传递,从而丢失 class 和所有内容。有时这就足够了。我建议在 R 端有一个简单的命名列表以使代码更清晰,例如
Polarization <- list( HORIZONTAL_POL = 0L, VERTICAL_POL = 1L )
这样你就可以做,例如
test <- new( EnumTest )
test$p <- Polarization$HORIZONTAL_POL
names(which( test$p == Polarization ))
## [1] "HORIZONTAL_POL"
我正在使用 Rcpp 模块导出 class 方法。其中一些方法具有 return 枚举类型。例如:
#include "Enum.h"
#include <Rcpp.h>
using namespace Rcpp;
class EnumTest{
public:
EnumTest(){}
void setP(Polarization pol){p = pol;}
Polarization getP(){return p;}
private:
Polarization p;
};
RCPP_EXPOSED_CLASS(EnumTest)
RCPP_MODULE(EnumTest){
class_<EnumTest>("EnumTest")
.property("p", &EnumTest::getP, &EnumTest::setP)
;
}
Polarization是枚举,定义如下:
enum Polarization{
HORIZONTAL_POL = 0,
VERTICAL_POL = 1
};
当我尝试构建代码时,出现以下错误。
cannot convert 'SEXP' to 'Polarization' in initialization
是否有向 Rcpp 公开枚举的方法,类似于公开 classes 的方式?我在 Rcpp 模块小插图中注意到枚举类型列在未来的扩展中。这是否意味着没有办法做到这一点?如果是这样,是否有任何可能的解决方法?
如文档所述,仅接口from/to R(我们使用/应该使用)是
SEXP .Call(yourFunction, SEXP a, SEXP b, SEXP c, ...)
Rcpp 为您的转换器提供 to/from 基本类型。但是您的 enum 不是这些 base 之一,因此您必须编写自己的转换器,或者只传递会传递的整数值。
Rcpp 中对枚举的支持有限,由宏 here 提供。
所以你可以这样做:
#include <Rcpp.h>
using namespace Rcpp;
enum Polarization{
HORIZONTAL_POL = 0,
VERTICAL_POL = 1
};
RCPP_EXPOSED_ENUM_NODECL(Polarization)
class EnumTest{
public:
EnumTest(){}
void setP(Polarization pol){p = pol;}
Polarization getP(){return p;}
private:
Polarization p;
};
RCPP_MODULE(Bla){
class_<EnumTest>("EnumTest")
.constructor()
.property("p", &EnumTest::getP, &EnumTest::setP)
;
}
支持非常基本,本质上允许您将枚举作为整数传递,从而丢失 class 和所有内容。有时这就足够了。我建议在 R 端有一个简单的命名列表以使代码更清晰,例如
Polarization <- list( HORIZONTAL_POL = 0L, VERTICAL_POL = 1L )
这样你就可以做,例如
test <- new( EnumTest )
test$p <- Polarization$HORIZONTAL_POL
names(which( test$p == Polarization ))
## [1] "HORIZONTAL_POL"