如何使用 Rcpp 公开抽象 class 的指针?
How to expose a pointer of an abstract class using Rcpp?
我正在使用 Rcpp 为 C++ 库实现 R 包。该库从抽象 class 实现了几个派生的 classes。一个函数初始化新派生的 classes 和 returns 它的一个指针作为抽象 class。是否可以在不更改 C++ 库的情况下使用 Rcpp 为 R 获得类似的构造?
这是一个简化的可重现示例:
#include <iostream>
#include <string>
using namespace std;
// abstract class
class Base {
public:
virtual ~Base() {}
virtual std::string name() const = 0;
virtual Base* getNew() const = 0;
};
// derived class
class Derived: public Base {
public:
Derived() : Base() {}
virtual std::string name() const { return "Derived"; }
virtual Derived* getNew() const { return new Derived(); };
};
Base *newDerived( const std::string &name ) {
if ( name == "d1" ){
return new Derived ;
} else {
return 0 ;
}
};
int main( void ) {
Base* b = newDerived( "d1" ) ;
cout << b->name() << endl ;
return(1);
}
compiled代码的输出是:Derived
我当前的版本使用 Rcpp::RCPP_MODULE
和 Rcpp::XPtr
。但是,这个版本不能像 C++ 实现那样使用:
#include <Rcpp.h>
using namespace Rcpp;
//... Base and Derived class and newDerived function implementations ... //
// wrapper function for pointer
typedef Base* (*newDerivedPtr)(const std::string& name);
//[[Rcpp::export]]
Rcpp::XPtr< newDerivedPtr > getNewDerived(const std::string type) {
return(Rcpp::XPtr< newDerivedPtr >(new newDerivedPtr(&newDerived)));
}
RCPP_MODULE(mod) {
Rcpp::class_< Base >("Base")
;
Rcpp::class_< Derived >("Derived")
.derives<Base>("Base")
.default_constructor()
.method("name", &Derived::name)
;
}
执行示例:
(dv = new(Derived))
# C++ object <0x101c0ce20> of class 'Derived' <0x101b51e00>
dv$name()
# [1] "Derived"
(dvptr = getNewDerived("d1"))
# pointer: 0x101c82770> // WANTED: C++ Object <0x...> of class 'Base' <0x...>
让我重新表述一下问题:您有一个 class Derived
和一个非 public 构造函数。创建此 class 实例的唯一方法是通过工厂方法 newDerived
。此工厂方法不是 return 指向 Derived
的指针,而是指向 Base
的指针,从中派生 Derived
。
如果这是正确的,那么您可以使用 expose class in Rcpp - factory instead of constructor 中显示的技术。不过,您必须确保公开 Base
class:
#include <Rcpp.h>
using namespace Rcpp;
// abstract class
class Base {
public:
virtual ~Base() {}
virtual std::string name() const = 0;
};
// derived class
class Derived1: public Base {
public:
Derived1() : Base() {}
virtual std::string name() const { return "Derived1"; }
};
// derived class
class Derived2: public Base {
public:
Derived2() : Base() {}
virtual std::string name() const { return "Derived2"; }
};
Base *newBase( const std::string &name ) {
if ( name == "d1" ){
return new Derived1 ;
} else if ( name == "d2" ){
return new Derived2 ;
} else {
return 0 ;
}
}
RCPP_MODULE(mod) {
Rcpp::class_< Base >("Base")
.factory<const std::string&>(newBase)
.method("name", &Base::name)
;
}
/*** R
(dv1 <- new(Base, "d1"))
dv1$name()
(dv2 <- new(Base, "d2"))
dv2$name()
*/
输出:
> (dv1 <- new(Base, "d1"))
C++ object <0x1cd3e60> of class 'Base' <0x1ea3560>
> dv1$name()
[1] "Derived1"
> (dv2 <- new(Base, "d2"))
C++ object <0x1fbb9f0> of class 'Base' <0x1ea3560>
> dv2$name()
[1] "Derived2"
请注意,我添加了第二个派生 class,删除了未使用的 getNew
方法,并重命名了因子方法。这种方式对我来说更现实。
原始答案,现在可以作为替代方案:我担心您将不得不手动完成 Rcpp 模块提供的所有自动化,即在 R 级别生成 class 并使用工厂方法初始化。以下是 Rcpp 模块小插图中的代码改编版以及 Rcpp 属性,以便于编码:
#include <Rcpp.h>
using namespace Rcpp;
// abstract class
class Base {
public:
virtual ~Base() {}
virtual std::string name() const = 0;
virtual Base* getNew() const = 0;
};
// derived class
class Derived1: public Base {
public:
Derived1() : Base() {}
virtual std::string name() const { return "Derived1"; }
virtual Derived1* getNew() const { return new Derived1(); };
};
// derived class
class Derived2: public Base {
public:
Derived2() : Base() {}
virtual std::string name() const { return "Derived2"; }
virtual Derived2* getNew() const { return new Derived2(); };
};
Base *newBase( const std::string &name ) {
if ( name == "d1" ){
return new Derived1 ;
} else if ( name == "d2" ){
return new Derived2 ;
} else {
return 0 ;
}
}
//[[Rcpp::export]]
Rcpp::XPtr< Base > getNewBase(const std::string type) {
return(Rcpp::XPtr< Base >(newBase(type)));
}
//[[Rcpp::export]]
std::string Base__name(Rcpp::XPtr< Base > ptr) {
return ptr->name();
}
/*** R
setClass("Base", representation( pointer = "externalptr"))
Base_method <- function(name) {
paste("Base", name, sep = "__")
}
setMethod("$", "Base", function(x, name) {
function(...) do.call(Base_method(name), list(x@pointer, ...))
})
setMethod("initialize", "Base", function(.Object, ...) {
.Object@pointer <- getNewBase(...)
.Object
})
(dv <- new("Base", "d1"))
dv$name()
(dv <- new("Base", "d2"))
dv$name()
*/
有趣的输出:
> (dv <- new("Base", "d1"))
An object of class "Base"
Slot "pointer":
<pointer: 0x19b83e0>
> dv$name()
[1] "Derived1"
> (dv <- new("Base", "d2"))
An object of class "Base"
Slot "pointer":
<pointer: 0x1c02600>
> dv$name()
[1] "Derived2"
请注意,我在这里使用的是 S4 class,而 RCPP_MODULE
创建了一个参考 Class。人们也可能为此使用 RC。将是一个有趣的学习练习,因为到目前为止我还没有直接使用过 RC。另请注意,我使用的 XPtr
与您的非常不同。我不确定你想在那里包装什么。
我正在使用 Rcpp 为 C++ 库实现 R 包。该库从抽象 class 实现了几个派生的 classes。一个函数初始化新派生的 classes 和 returns 它的一个指针作为抽象 class。是否可以在不更改 C++ 库的情况下使用 Rcpp 为 R 获得类似的构造?
这是一个简化的可重现示例:
#include <iostream>
#include <string>
using namespace std;
// abstract class
class Base {
public:
virtual ~Base() {}
virtual std::string name() const = 0;
virtual Base* getNew() const = 0;
};
// derived class
class Derived: public Base {
public:
Derived() : Base() {}
virtual std::string name() const { return "Derived"; }
virtual Derived* getNew() const { return new Derived(); };
};
Base *newDerived( const std::string &name ) {
if ( name == "d1" ){
return new Derived ;
} else {
return 0 ;
}
};
int main( void ) {
Base* b = newDerived( "d1" ) ;
cout << b->name() << endl ;
return(1);
}
compiled代码的输出是:Derived
我当前的版本使用 Rcpp::RCPP_MODULE
和 Rcpp::XPtr
。但是,这个版本不能像 C++ 实现那样使用:
#include <Rcpp.h>
using namespace Rcpp;
//... Base and Derived class and newDerived function implementations ... //
// wrapper function for pointer
typedef Base* (*newDerivedPtr)(const std::string& name);
//[[Rcpp::export]]
Rcpp::XPtr< newDerivedPtr > getNewDerived(const std::string type) {
return(Rcpp::XPtr< newDerivedPtr >(new newDerivedPtr(&newDerived)));
}
RCPP_MODULE(mod) {
Rcpp::class_< Base >("Base")
;
Rcpp::class_< Derived >("Derived")
.derives<Base>("Base")
.default_constructor()
.method("name", &Derived::name)
;
}
执行示例:
(dv = new(Derived))
# C++ object <0x101c0ce20> of class 'Derived' <0x101b51e00>
dv$name()
# [1] "Derived"
(dvptr = getNewDerived("d1"))
# pointer: 0x101c82770> // WANTED: C++ Object <0x...> of class 'Base' <0x...>
让我重新表述一下问题:您有一个 class Derived
和一个非 public 构造函数。创建此 class 实例的唯一方法是通过工厂方法 newDerived
。此工厂方法不是 return 指向 Derived
的指针,而是指向 Base
的指针,从中派生 Derived
。
如果这是正确的,那么您可以使用 expose class in Rcpp - factory instead of constructor 中显示的技术。不过,您必须确保公开 Base
class:
#include <Rcpp.h>
using namespace Rcpp;
// abstract class
class Base {
public:
virtual ~Base() {}
virtual std::string name() const = 0;
};
// derived class
class Derived1: public Base {
public:
Derived1() : Base() {}
virtual std::string name() const { return "Derived1"; }
};
// derived class
class Derived2: public Base {
public:
Derived2() : Base() {}
virtual std::string name() const { return "Derived2"; }
};
Base *newBase( const std::string &name ) {
if ( name == "d1" ){
return new Derived1 ;
} else if ( name == "d2" ){
return new Derived2 ;
} else {
return 0 ;
}
}
RCPP_MODULE(mod) {
Rcpp::class_< Base >("Base")
.factory<const std::string&>(newBase)
.method("name", &Base::name)
;
}
/*** R
(dv1 <- new(Base, "d1"))
dv1$name()
(dv2 <- new(Base, "d2"))
dv2$name()
*/
输出:
> (dv1 <- new(Base, "d1"))
C++ object <0x1cd3e60> of class 'Base' <0x1ea3560>
> dv1$name()
[1] "Derived1"
> (dv2 <- new(Base, "d2"))
C++ object <0x1fbb9f0> of class 'Base' <0x1ea3560>
> dv2$name()
[1] "Derived2"
请注意,我添加了第二个派生 class,删除了未使用的 getNew
方法,并重命名了因子方法。这种方式对我来说更现实。
原始答案,现在可以作为替代方案:我担心您将不得不手动完成 Rcpp 模块提供的所有自动化,即在 R 级别生成 class 并使用工厂方法初始化。以下是 Rcpp 模块小插图中的代码改编版以及 Rcpp 属性,以便于编码:
#include <Rcpp.h>
using namespace Rcpp;
// abstract class
class Base {
public:
virtual ~Base() {}
virtual std::string name() const = 0;
virtual Base* getNew() const = 0;
};
// derived class
class Derived1: public Base {
public:
Derived1() : Base() {}
virtual std::string name() const { return "Derived1"; }
virtual Derived1* getNew() const { return new Derived1(); };
};
// derived class
class Derived2: public Base {
public:
Derived2() : Base() {}
virtual std::string name() const { return "Derived2"; }
virtual Derived2* getNew() const { return new Derived2(); };
};
Base *newBase( const std::string &name ) {
if ( name == "d1" ){
return new Derived1 ;
} else if ( name == "d2" ){
return new Derived2 ;
} else {
return 0 ;
}
}
//[[Rcpp::export]]
Rcpp::XPtr< Base > getNewBase(const std::string type) {
return(Rcpp::XPtr< Base >(newBase(type)));
}
//[[Rcpp::export]]
std::string Base__name(Rcpp::XPtr< Base > ptr) {
return ptr->name();
}
/*** R
setClass("Base", representation( pointer = "externalptr"))
Base_method <- function(name) {
paste("Base", name, sep = "__")
}
setMethod("$", "Base", function(x, name) {
function(...) do.call(Base_method(name), list(x@pointer, ...))
})
setMethod("initialize", "Base", function(.Object, ...) {
.Object@pointer <- getNewBase(...)
.Object
})
(dv <- new("Base", "d1"))
dv$name()
(dv <- new("Base", "d2"))
dv$name()
*/
有趣的输出:
> (dv <- new("Base", "d1"))
An object of class "Base"
Slot "pointer":
<pointer: 0x19b83e0>
> dv$name()
[1] "Derived1"
> (dv <- new("Base", "d2"))
An object of class "Base"
Slot "pointer":
<pointer: 0x1c02600>
> dv$name()
[1] "Derived2"
请注意,我在这里使用的是 S4 class,而 RCPP_MODULE
创建了一个参考 Class。人们也可能为此使用 RC。将是一个有趣的学习练习,因为到目前为止我还没有直接使用过 RC。另请注意,我使用的 XPtr
与您的非常不同。我不确定你想在那里包装什么。