[Rcpp-devel] wrap returned pointer to class

Sebastian Weber sebastian.weber at frias.uni-freiburg.de
Mon Aug 1 18:35:59 CEST 2011


Hi!

To answer my question: The issue was with constness of the pointer (there are a lot of variations of const pointers, saying wif the address or the object is const, but never got right behind that declaration mess...). So what did it in the end was to declare a wrap function which wraps the type B* const&, i.e. a declaration and definition of

template <> SEXP wrap( B* const& )

in between of RcppCommon.h and Rcpp.h and another declaration of

  template <>
  SEXP wrap( B* const& mb ) {
    return XPtr<B>(mb, false);
  }

did the job. BTW, shouldn't this be a behavior which is enabled by default for all pointers?

Concerning the bug: Just try to wrap a function taking a char as its argument. On my platform this has size 1 (one byte), while const char* has size 5 (larger than 1 byte). The respective automatic const char* -> char conversion then fails. Declaring this:

  template <>
  char as(SEXP s) {
    std::string str = as<std::string>(s);
    char c;
    std::strcpy( &c, str.c_str() );
    return c;
  }

cures the problem for me.

Just as a side note: Is it thinkable to enhance Boost.Python to handle Python and R? If Rcpp and Boost.Python would be source compatible *all* stuff written for Boost.Python would become immediately available to R and vice versa...

Cheers,

Sebastian

Am 01.08.2011 um 18:26 schrieb Dirk Eddelbuettel:

> 
> Sebastuan,
> 
> On 1 August 2011 at 18:07, Sebastian Weber wrote:
> | first of all thank you, Dirk and Romain, so much for this great library! I was stunned when I saw the examples.
> | 
> | Hence, I started immediately a quite involved project which interfaces another library to R via C++. 
> 
> That should work.  The number of packages on CRAN which use Rcpp just that
> keeps increasing -- we added a few more last week -- and several of those
> interface external libraries or code.
> 
> | Currently I have the problem that I want to expose a function from a class which returns a pointer to a class. When exposing via the
> | 
> | class_<A>.method("fun", &A::fun);
> | 
> | mechanism, the compiler fails with a message saying that he cannot wrap the return type of function fun. The returned object itself is a pointer to another class B. How to solve this? I tried providing the wrap call with no success.
> | 
> | Here is some code which might not work, but explains more detailed:
> | 
> | class B {};
> | 
> | class A {
> | public:
> | 	B* fun();
> | };
> | 
> | which gives, when compiled:
> | 
> | /Library/Frameworks/R.framework/Versions/2.13/Resources/library/Rcpp/include/Rcpp/internal/wrap.h:428: error: cannot convert ‘B* const’ to ‘SEXPREC*’ in initialization
> | 
> | Any ideas how to fix this?
> 
> It seems to me that the issue is not so much that you have a pointer to B, as
> it is that Rcpp simply does not what B is -- in the sense of having wrap()
> and as<> operators for objects of class "B".  
> 
> You would have to provide those, see the Rcpp-package vignette.
> 
> | Thanks,
> | 
> | PS: Is there some bug tracking tool? I got some weird problem when the function wrapped expects a char as input. I fixed it somehow, but I am not sure if it is the right way...
> 
> Not really. The R-Forge project site has it, but the reports there get mostly
> ignored / forgotten.  The best idea is to post a reproducible example of some
> exciting bug here.  To more exciting the bug the higher the chance it will
> get addressed :)
> 
> Cheers, Dirk
> 
> 
> | -- 
> | Sebastian Weber
> | Group of Cell Communication and Control
> | Freiburg Institute for Advanced Studies - FRIAS
> | School of Life Sciences - LIFENET
> | Albert-Ludwigs-Universität Freiburg
> | Albertstr. 19
> | 79104 Freiburg
> | T.: +49-761-203-97237
> | Fax:+49-761-203-97334
> | 
> | _______________________________________________
> | 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
> 
> -- 
> Gauss once played himself in a zero-sum game and won $50.
>                      -- #11 at http://www.gaussfacts.com

-- 
Sebastian Weber
Group of Cell Communication and Control
Freiburg Institute for Advanced Studies - FRIAS
School of Life Sciences - LIFENET
Albert-Ludwigs-Universität Freiburg
Albertstr. 19
79104 Freiburg
T.: +49-761-203-97237
Fax:+49-761-203-97334



More information about the Rcpp-devel mailing list