[Rd] A 'true' R-wrapper for C++ classes

Ali - saveez at hotmail.com
Mon Apr 18 10:25:52 CEST 2005


Hello

I am trying to wrap some C++ classes into R.

(1) Comparing the OOP and methods packages, I have came to this conclusion 
that OOP works much better for this wrapper -- please correct me if I am 
wrong. One question is why this useful package (OOP) is not included in the 
official release of R?

(2) Choosing the OOP package way, I have carried out the following steps to 
wrap the C++ classes:

  (2.1) A C-wrapper is created to convert the C++ class to some C-style 
code.
  (2.2) An R-wrapper wraps the C-wrapper.

Here is a rough example to demonstrate the above:

---------------------
C++ class:

class foo
{
public:
  foo();
  ~foo();

  fun();
}

---------------------
C-wrapper:

extern "C" SEXP R_foo_init()
{
  foo* obj = new foo();

  SEXP res;
  PROTECT(res = R_MakeExternalPtr(obj, R_NilValue, R_NilValue));
  UNPROTECT(1);

  return res;
}

extern "C" SEXP R_foo_fun(SEXP ptr)
{
  foo *ptr = Hello

I am trying to wrap some C++ classes into R.

(1) Comparing the OOP and methods packages, I have came to this conclusion 
that OOP works much better for this wrapper -- please correct me if I am 
wrong. One question is why this useful package (OOP) is not included in the 
official release of R?

(2) Choosing the OOP package way, I have carried out the following steps to 
wrap the C++ classes:

  (2.1) A C-wrapper is created to convert the C++ class to some C-style 
code.
  (2.2) An R-wrapper wraps the C-wrapper.

Here is a rough example to demonstrate the above:

---------------------
C++ class:

class foo
{
public:
  foo();
  ~foo();

  fun();
}

---------------------
C-wrapper:

extern "C" SEXP R_foo_init()
{
  foo* obj = new foo();

  SEXP res;
  PROTECT(res = R_MakeExternalPtr(obj, R_NilValue, R_NilValue));
  UNPROTECT(1);

  return res;
}

extern "C" SEXP R_foo_fun(SEXP ptr)
{
  foo *= Hello

I am trying to wrap some C++ classes into R.

(1) Comparing the OOP and methods packages, I have came to this conclusion 
that OOP works much better for this wrapper -- please correct me if I am 
wrong. One question is why this useful package (OOP) is not included in the 
official release of R?

(2) Choosing the OOP package way, I have carried out the following steps to 
wrap the C++ classes:

  (2.1) A C-wrapper is created to convert the C++ class to some C-style 
code.
  (2.2) An R-wrapper wraps the C-wrapper.

Here is a rough example to demonstrate the above:

---------------------
C++ class:

class foo
{
public:
  foo();
  ~foo();

  fun();
}

---------------------
C-wrapper:

extern "C" SEXP R_foo_init()
{
  foo* ptr= new foo();

  SEXP res;
  PROTECT(res = R_MakeExternalPtr(ptr, R_NilValue, R_NilValue));
  UNPROTECT(1);

  return res;
}

extern "C" SEXP R_foo_fun(SEXP obj)
{
  foo *ptr= (foo *) R_ExternalPtrAddr(obj);
  ptr->fun();

  return  R_NilValue;
}

---------------------
R-wrapper:

defineClass(className = "foo");

vtkObject$defineFields(ptr = "externalptr");

vtkObject$defineMethod(
	"initialize",
	function(){
		ptr <- .Call("R_foo_init")
	}
);

vtkObject$defineMethod(
    "fun",
    function()
    {
        .Call("R_foo_fun", ptr);
    }
);
--------------------

(3) The above model lacks something like an 'environment' for the pointer to 
the C++ object to live in it. Assume we create the foo class in R like:

  obj <- foo$new()

Now, the following would return an error:

  obj$fun()

and the reason is that the pointer created in the initialize method is lost.

(4) The question is how to assign an environment to the pointers. A well 
described answer, rather than some abstract hints, is well-appreciated. Also 
I am curious to know why there is no standard method for R to wrap C++ 
classes, something like JNI.

Thanks,

Ali



More information about the R-devel mailing list