From sfalcon at fhcrc.org  Thu Mar 16 02:06:21 2006
From: sfalcon at fhcrc.org (Seth Falcon)
Date: Wed, 15 Mar 2006 17:06:21 -0800
Subject: [Rd] multiple packages using the same native code.
In-Reply-To: <4418AA0C.1040703@berkeley.edu> (James Bullard's message of "Wed,
	15 Mar 2006 15:58:04 -0800")
References: <4418AA0C.1040703@berkeley.edu>
Message-ID: <m2wtevp59u.fsf@ziti.local>

Hi Jim,

James Bullard <bullard at berkeley.edu> writes:
> I would like to construct two packages (A, B) which utilize a number of 
> common C functions. The most straightforward way to do this is just copy 
> the relevant .c and .h files from one src directory to the next, but 
> this is tedious especially in the face of multiple developers and
> changes.

I'm not sure I understand what you are after.  One possible solution
would be to create a third package 'C' that contains the common C
code.  This would allow you to call C function defined in 'C' from the
C code in 'A' or 'B'.

Using a .onLoad hook and getNativeSymbolInfo(), you can pass C
function pointers to the code in packages A and B.

Suppose in 'C' you have a C function foo() that is registered in the
usual manner so that it can be called by .Call or .C.

Then in 'A' you could have (all untested, sorry, but hopefully it
sketches the idea for you):

A/src/A.c

   static DL_FUNC C_foo;

   void init_funcs_from_C(SEXP foo_info) {
       C_foo = R_ExternalPtrAddr(foo_info);
   }

   void bar(int *x) {
       ...
       z = C_foo();
       ...
   }


A/R/zzz.R

   .onLoad <- function(libname, pkgname) {
       foo_info <- getNativeSymbolInfo("foo", PACKAGE="C")
       .Call("init_funcs_from_C", foo_info$address)
   }


+ seth



[Rd] multiple packages using the same native code.
Seth Falcon