如何将类对象从 Rcpp 模块传递回 C++?

Hon*_*Ooi 5 r rcpp c++14

我有一个 C++ 代码库,我使用 Rcpp 模块将其暴露给 R。具体来说,我使用一种接口模式,其中我公开的类实际上是底层对象(即实现)之上的抽象层。

我正在处理的类也相互交互,并且具有将对象的共享指针作为参数的方法。我无法找出向 R 公开这些方法的正确方法。

例如,这是一些代码。该TestClass::combine方法接受一个指向另一个TestClass对象的指针并用它做一些事情。当我尝试编译此代码时,当我ITestClass::combine向模块添加相应的接口方法时,出现编译器错误(见下文)。

执行:

class TestClass
{
public:
    TestClass(int const& n, double const& x)
        : n(n), x(x)
    {}

    const double get_x() {
        return x;
    }

    double combine(std::shared_ptr<TestClass> obj) {
        return x + obj->get_x();
    }

protected:
    int n;
    double x;
};
Run Code Online (Sandbox Code Playgroud)

界面:

//' @export ITestClass
class ITestClass
{
public:
    ITestClass(int const& in_n, double const& in_x)
        : impl(in_n, in_x)
    {}

    double get_x() {
        return impl.get_x();
    }

    double combine(ITestClass obj) {
        return impl.combine(obj.get_object_ptr());
    }

    std::shared_ptr<TestClass> get_object_ptr() {
        std::shared_ptr<TestClass> ptr(&impl);
        return ptr;
    }

private:
    TestClass impl;
};

RCPP_MODULE(RTestClassModule)
{
    class_<ITestClass>("ITestClass")
        .constructor<int, double>()
        .method("get_x", &ITestClass::get_x, "get_x")
        .method("combine", &ITestClass::combine, "combine"); // this line errors out
}
Run Code Online (Sandbox Code Playgroud)

我收到的错误示例:

    In file included from C:/Rlib/Rcpp/include/Rcpp/as.h:25,
                    from C:/Rlib/Rcpp/include/RcppCommon.h:168,
                    from C:/Rlib/Rcpp/include/Rcpp.h:27,
                    from interface1.cpp:2:
   C:/Rlib/Rcpp/include/Rcpp/internal/Exporter.h: In instantiation of 'Rcpp::traits::Exporter<T>::Exporter(SEXP) [with T = testpkg::ITestClass; SEXP = SEXPREC*]':
   C:/Rlib/Rcpp/include/Rcpp/as.h:87:41:   required from 'T Rcpp::internal::as(SEXP, Rcpp::traits::r_type_generic_tag) [with T = testpkg::ITestClass; SEXP = SEXPREC*]'
   C:/Rlib/Rcpp/include/Rcpp/as.h:152:31:   required from 'T Rcpp::as(SEXP) [with T = testpkg::ITestClass; SEXP = SEXPREC*]'
   C:/Rlib/Rcpp/include/Rcpp/InputParameter.h:34:43:   required from 'Rcpp::InputParameter<T>::operator T() [with T = testpkg::ITestClass]'
   C:/Rlib/Rcpp/include/Rcpp/module/Module_generated_CppMethod.h:111:69:   required from 'SEXPREC* Rcpp::CppMethod1<Class, RESULT_TYPE, U0>::operator()(Class*, SEXPREC**) [with Class = testpkg::ITestClass; RESULT_TYPE = double; U0 = testpkg::ITestClass; SEXP = SEXPREC*]'
   C:/Rlib/Rcpp/include/Rcpp/module/Module_generated_CppMethod.h:109:10:   required from here
   C:/Rlib/Rcpp/include/Rcpp/internal/Exporter.h:31:31: error: no matching function for 
call to 'testpkg::ITestClass::ITestClass(SEXPREC*&)'
          Exporter( SEXP x ) : t(x){}
                                  ^
   interface1.cpp:17:5: note: candidate: 'testpkg::ITestClass::ITestClass(SEXP, const int&, const double&)'
        ITestClass(SEXP in_date, int const& in_n, double const& in_x)
        ^~~~~~~~~~
   interface1.cpp:17:5: note:   candidate expects 3 arguments, 1 provided
   interface1.cpp:14:7: note: candidate: 'constexpr testpkg::ITestClass::ITestClass(const testpkg::ITestClass&)'
    class ITestClass
          ^~~~~~~~~~
   interface1.cpp:14:7: note:   no known conversion for argument 1 from 'SEXP' {aka 'SEXPREC*'} to 'const testpkg::ITestClass&'
   interface1.cpp:14:7: note: candidate: 'constexpr testpkg::ITestClass::ITestClass(testpkg::ITestClass&&)'
   interface1.cpp:14:7: note:   no known conversion for argument 1 from 'SEXP' {aka 'SEXPREC*'} to 'testpkg::ITestClass&&'
Run Code Online (Sandbox Code Playgroud)

我如何定义ITestClass::combine以便可以从 R 调用它?

Hon*_*Ooi 1

我找到了一种更好的解决方案,该解决方案具有首选接口combine并且似乎不会遇到垃圾收集问题。

有几点:

  • 因为底层 API 广泛使用共享指针,所以我没有将TestClass对象存储在 中impl,而是存储了std::shared_ptr<TestClass>. 这是直接引用的,而不是从头开始创建新的共享指针(当它们被销毁时,这会导致 R 崩溃)。
  • 我利用 Rcpp 模块返回的 refclass 对象的内部结构。特别是,它有一个.pointer成员,它是指向底层 C++ 对象的指针。所以我可以取消引用它来获取impl成员。

新界面:

//' @export ITestClass2
class ITestClass2
{
public:
    ITestClass2(int const& in_n, double const& in_x)
        : impl(in_n, in_x))
    {}

    double get_x()
    {
        return impl->get_x();
    }

    double combine(Environment obj)
    {
        SEXP objptr = obj[".pointer"];
        ITestClass2* ptr = (ITestClass2*) R_ExternalPtrAddr(objptr);
        return impl->combine(ptr->get_object_ptr());
    }

// this doesn't need to be seen from R
protected:
    std::shared_ptr<TestClass> get_object_ptr()
    {
        return impl;
    }

private:
    std::shared_ptr<TestClass> impl;
};

RCPP_MODULE(RTestClassModule2)
{
    class_<ITestClass2>("ITestClass2")
        .constructor<int, double>()
        .method("get_x", &ITestClass2::get_x, "get_x")
        .method("combine", &ITestClass2::combine, "combine")
    ;
}
Run Code Online (Sandbox Code Playgroud)

在 R 中调用如下:

obj <- new(ITestClass2, 1, pi)
obj2 <- new(ITestClass2, 2, exp(1))
obj$combine(obj2)
Run Code Online (Sandbox Code Playgroud)