[Rinside-commits] r144 - in pkg: inst/examples/standard src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Apr 1 14:07:03 CEST 2010


Author: romain
Date: 2010-04-01 14:07:02 +0200 (Thu, 01 Apr 2010)
New Revision: 144

Added:
   pkg/inst/examples/standard/rinside_callbacks0.cpp
   pkg/src/Callbacks.h
   pkg/src/RInsideCommon.h
Modified:
   pkg/inst/examples/standard/Makefile
   pkg/src/Makefile
   pkg/src/RInside.cpp
   pkg/src/RInside.h
Log:
start to add some code for callbacks (does not work yet)

Modified: pkg/inst/examples/standard/Makefile
===================================================================
--- pkg/inst/examples/standard/Makefile	2010-03-30 19:00:12 UTC (rev 143)
+++ pkg/inst/examples/standard/Makefile	2010-04-01 12:07:02 UTC (rev 144)
@@ -45,6 +45,7 @@
 
 clean:
 			rm -vf $(programs)
+			rm -vrf *.dSYM
 
 runAll:
 			for p in $(programs); do echo "Running $$p"; ./$$p; done

Added: pkg/inst/examples/standard/rinside_callbacks0.cpp
===================================================================
--- pkg/inst/examples/standard/rinside_callbacks0.cpp	                        (rev 0)
+++ pkg/inst/examples/standard/rinside_callbacks0.cpp	2010-04-01 12:07:02 UTC (rev 144)
@@ -0,0 +1,20 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4;  tab-width: 8; -*-
+//
+// Simple example showing how to do the standard 'hello, world' using embedded R
+//
+// Copyright (C) 2009 Dirk Eddelbuettel 
+// Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
+//
+// GPL'ed 
+
+#include <RInside.h>                    // for the embedded R via RInside
+
+int main(int argc, char *argv[]) {
+
+    RInside R(argc, argv);              // create an embedded R instance 
+    DummyCallbacks* callbacks = new DummyCallbacks() ;
+    R.set_callbacks( callbacks );
+    R.parseEval( "print( 'foof' )" ) ;
+    exit(0);
+}
+

Added: pkg/src/Callbacks.h
===================================================================
--- pkg/src/Callbacks.h	                        (rev 0)
+++ pkg/src/Callbacks.h	2010-04-01 12:07:02 UTC (rev 144)
@@ -0,0 +1,88 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// Callbacks.h: R/C++ interface class library -- Easier R embedding into C++
+//
+// Copyright (C) 2010        Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of RInside.
+//
+// RInside is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// RInside is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with RInside.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef RINSIDE_CALLBACKS_H
+#define RINSIDE_CALLBACKS_H
+
+#include "RInsideCommon.h"
+
+class Callbacks {
+public:
+	
+	Callbacks() : R_is_busy(false){} ;
+	virtual ~Callbacks(){} ;
+	
+	virtual void showMessage(const char* message) = 0;
+	virtual void suicide(const char* message) = 0;
+	virtual std::string readConsole( const char* prompt, bool addtohistory ) = 0;
+	virtual void writeConsole( const std::string& line, int type ) = 0;
+	virtual void flushConsole() = 0;
+	virtual void resetConsole() = 0;
+	virtual void cleanerrConsole() = 0;
+	virtual void busy( bool is_busy ) = 0 ;
+	
+	void busy_( int which ) ;
+	int readConsole_( const char* prompt, unsigned char* buf, int len, int addtohistory ) ;
+	void writeConsole_( const char* buf, int len, int oType ) ;
+	
+	// TODO: ShowFiles
+	// TODO: ChooseFile
+	// TODO: loadHistory
+	// TODO: SaveHistory                                                                                      
+	
+private:
+	bool R_is_busy ;
+	             
+} ;                                       
+
+class DummyCallbacks : public Callbacks{
+public:
+	DummyCallbacks() : Callbacks(){}
+	
+	void showMessage(const char* message){
+		Rprintf( ">> showMessage('%s')\n", message ) ;	
+	}
+	void suicide(const char* message){
+		Rprintf( ">> suicide('%s')\n", message ) ;
+	}
+	std::string readConsole(const char* prompt, bool addtohistory){ 
+		Rprintf( ">> readConsole('%s', %d)\n", prompt, addtohistory ) ;
+		return " " ; 
+	}
+	void writeConsole( const std::string& line, int type){
+		Rprintf( ">> writeConsole('%s', %d)\n", line.c_str(), type ) ;
+	}
+	void flushConsole(){
+		Rprintf( ">> flushConsole()\n" ) ;
+	}
+	void resetConsole(){
+		Rprintf( ">> resetConsole()\n" ) ;
+	}
+	void cleanerrConsole(){
+		Rprintf( ">> cleanerrConsole()\n" ) ;
+	}
+	void busy( bool is_busy ){
+		Rprintf( ">> busy(%d) \n", is_busy ) ;
+	}
+	
+} ;
+
+#endif

Modified: pkg/src/Makefile
===================================================================
--- pkg/src/Makefile	2010-03-30 19:00:12 UTC (rev 143)
+++ pkg/src/Makefile	2010-04-01 12:07:02 UTC (rev 144)
@@ -61,7 +61,7 @@
 userLibrary : 		$(USERLIB) $(USERLIBST)
 			- at if test ! -e $(USERDIR)$(R_ARCH); then mkdir -p $(USERDIR)$(R_ARCH); fi
 			cp $(USERLIB) $(USERDIR)$(R_ARCH)
-			cp RInside.h MemBuf.h $(USERDIR)$(R_ARCH)
+			cp RInside.h MemBuf.h RInsideCommon.h Callbacks.h $(USERDIR)$(R_ARCH)
 			cp $(USERLIBST) $(USERDIR)$(R_ARCH)
 			rm $(USERLIB) $(USERLIBST)
 

Modified: pkg/src/RInside.cpp
===================================================================
--- pkg/src/RInside.cpp	2010-03-30 19:00:12 UTC (rev 143)
+++ pkg/src/RInside.cpp	2010-04-01 12:07:02 UTC (rev 144)
@@ -21,6 +21,10 @@
 // along with RInside.  If not, see <http://www.gnu.org/licenses/>.
 
 #include "RInside.h"
+#include "Callbacks.h"
+
+RInside* RInside::instance_ = 0 ;
+
 #include <sys/time.h>		// gettimeofday
 
 bool verbose = false;
@@ -46,11 +50,11 @@
     instance_ = 0 ;
 }
 
-RInside::RInside() {
+RInside::RInside() : callbacks(0) {
 	initialize( 0, 0 );
 }
 
-RInside::RInside(const int argc, const char* const argv[]) {
+RInside::RInside(const int argc, const char* const argv[]) : callbacks(0) {
     initialize( argc, argv ); 
 }
 
@@ -61,7 +65,7 @@
     if( instance_ ){
     	throw std::runtime_error( "can only have one RInside instance" ) ;
     } else {
-    	instance_ = *this ;	
+    	instance_ = this ;	
     }
     
     verbose_m = false; 		// Default is false
@@ -312,3 +316,112 @@
     return global_env[name]; 
 }
 
+RInside& RInside::instance(){
+	return *instance_ ;
+}
+
+/* callbacks */
+
+void Callbacks::busy_( int which ){
+	R_is_busy = static_cast<bool>( which ) ;
+	busy( R_is_busy ) ;	
+}
+
+int Callbacks::readConsole_( const char* prompt, unsigned char* buf, int len, int addtohistory ){
+	try {
+		std::string res( readConsole( prompt, static_cast<bool>(addtohistory) ) ) ;
+		
+		/* At some point we need to figure out what to do if the result is
+		 * longer than "len"... For now, just truncate. */
+		 
+		int l = res.size() ;
+		int last = (l>len-1)?len-1:l ;
+		strncpy( (char*)buf, res.c_str(), last ) ;
+		buf[last] = 0 ;
+		return 1 ;
+	} catch( const std::exception& ex){
+		return -1 ;	
+	}
+}
+
+
+void Callbacks::writeConsole_( const char* buf, int len, int oType ){
+	std::string line ;
+	line.assign( buf, buf + len ) ;
+	writeConsole( line, oType) ;
+}
+
+void RInside_ShowMessage( const char* message ){
+	RInside::instance().callbacks->showMessage( message ) ;	
+}
+
+void RInside_WriteConsoleEx( const char* message, int len, int oType ){
+	RInside::instance().callbacks->writeConsole_( message, len, oType ) ;		
+}
+
+int RInside_ReadConsole(const char *prompt, unsigned char *buf, int len, int addtohistory){
+	return RInside::instance().callbacks->readConsole_( prompt, buf, len, addtohistory ) ;
+}
+
+void RInside_ResetConsole(){
+	RInside::instance().callbacks->resetConsole() ;
+}
+
+void RInside_FlushConsole(){                                       
+	RInside::instance().callbacks->flushConsole() ;
+}
+
+void RInside_ClearerrConsole(){
+	RInside::instance().callbacks->cleanerrConsole() ;
+}
+
+void RInside_Busy( int which ){
+	RInside::instance().callbacks->busy_(which) ;
+}
+
+void RInside::set_callbacks(Callbacks* callbacks_){
+	Rprintf( "<set_callbacks>\n" ) ;
+	if( ! callbacks ){
+		/* short circuit the callback function pointers */
+		Rprintf( "ptr_R_ShowMessage : <%p> = <%p>\n", ptr_R_ShowMessage, RInside_ShowMessage ) ;
+		ptr_R_ShowMessage = RInside_ShowMessage ;
+		
+		Rprintf( "ptr_R_ReadConsole : <%p> = <%p>\n", ptr_R_ReadConsole, RInside_ReadConsole ) ;
+		ptr_R_ReadConsole = RInside_ReadConsole;
+    	
+		// Rprintf( "ptr_R_WriteConsoleEx : <%p> = <%p>\n", ptr_R_WriteConsoleEx, RInside_WriteConsoleEx ) ;
+		// ptr_R_WriteConsoleEx = RInside_WriteConsoleEx;
+		// ptr_R_WriteConsole = NULL;
+		Rprintf( "after\n") ;
+		
+		
+		Rprintf( "ptr_R_FlushConsole : <%p> = <%p>\n", ptr_R_FlushConsole, RInside_FlushConsole ) ;
+		ptr_R_FlushConsole = RInside_FlushConsole;
+    	
+		Rprintf( "ptr_R_ClearerrConsole : <%p> = <%p>\n", ptr_R_ClearerrConsole, RInside_ClearerrConsole ) ;
+		ptr_R_ClearerrConsole = RInside_ClearerrConsole;
+    	
+		Rprintf( "ptr_R_Busy : <%p> = <%p>\n", ptr_R_Busy, RInside_Busy ) ;
+		ptr_R_Busy = RInside_Busy;
+    	
+		Rprintf( "ptr_R_ResetConsole : <%p> = <%p>\n", ptr_R_ResetConsole, RInside_ResetConsole ) ;
+		ptr_R_ResetConsole = RInside_ResetConsole;
+    	
+    	R_Outputfile = NULL;
+    	R_Consolefile = NULL;    
+    	
+    	// setup_Rmainloop()
+    	// this gives this message: 
+    	// Erreur dans .Call("R_isMethodsDispatchOn", onOff, PACKAGE = "base") : 
+		//   Nombre d'arguments incorrect (2), attendu 1 pour R_isMethodsDispatchOn
+		// Segmentation fault
+
+    	
+    }
+    // callbacks = callbacks_ ;
+	// this gives a segfauylt
+	
+	Rprintf( "</set_callbacks>\n" ) ;
+	
+}
+

Modified: pkg/src/RInside.h
===================================================================
--- pkg/src/RInside.h	2010-03-30 19:00:12 UTC (rev 143)
+++ pkg/src/RInside.h	2010-04-01 12:07:02 UTC (rev 144)
@@ -20,25 +20,17 @@
 // You should have received a copy of the GNU General Public License
 // along with RInside.  If not, see <http://www.gnu.org/licenses/>.
 
-#include <string>
-#include <vector>
-#include <iostream>
+#ifndef RINSIDE_RINSIDE_H
+#define RINSIDE_RINSIDE_H
 
-#include <Rcpp.h>
+#include "RInsideCommon.h"
+#include "Callbacks.h"
 
-#include <Rembedded.h>
-#ifndef WIN32
-#define R_INTERFACE_PTRS
-#include <Rinterface.h>
-#endif
-#include <R_ext/RStartup.h>
-
-#include "MemBuf.h"
-
 class RInside {
 private:
     MemBuf mb_m;
     Rcpp::Environment global_env ;
+    Callbacks* callbacks ;
     
     bool verbose_m;				// private switch
                                                   
@@ -50,7 +42,17 @@
 
     static RInside* instance_ ;
     
+    friend void RInside_ShowMessage( const char* message) ;
+    friend void RInside_WriteConsoleEx( const char* message, int len, int oType ) ;
+    friend int RInside_ReadConsole(const char *prompt, unsigned char *buf, int len, int addtohistory) ;
+    friend void RInside_ResetConsole() ;
+    friend void RInside_FlushConsole() ;
+    friend void RInside_ClearerrConsole() ;
+    friend void RInside_Busy(int which) ;
+    
 public:
+	void set_callbacks(Callbacks* callbacks_) ;
+    
     int  parseEval(const std::string & line, SEXP &ans); // parse line, return in ans; error code rc
     void parseEvalQ(const std::string & line);		 // parse line, no return (throws on error)
 
@@ -82,17 +84,4 @@
     static RInside& instance() ;
 };
 
-RInside* RInside::instance_ = 0 ;
-
-// simple logging help
-inline void logTxtFunction(const char* file, const int line, const char* expression, const bool verbose) {
-    if (verbose) {
-	std::cout << file << ":" << line << " expression: " << expression << std::endl;
-    }
-}
-
-#ifdef logTxt
-#undef logTxt
 #endif
-//#define logTxt(x, b) logTxtFunction(__FILE__, __LINE__, x, b);
-#define logTxt(x, b)

Added: pkg/src/RInsideCommon.h
===================================================================
--- pkg/src/RInsideCommon.h	                        (rev 0)
+++ pkg/src/RInsideCommon.h	2010-04-01 12:07:02 UTC (rev 144)
@@ -0,0 +1,51 @@
+// RInsideCommon.h: R/C++ interface class library -- Easier R embedding into C++
+//
+// Copyright (C) 2010        Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of RInside.
+//
+// RInside is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// RInside is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with RInside.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef RINSIDE_RINSIDECOMMON_H
+#define RINSIDE_RINSIDECOMMON_H
+
+#include <string>
+#include <vector>
+#include <iostream>
+
+#include <Rcpp.h>
+
+#include <Rembedded.h>
+#ifndef WIN32
+#define R_INTERFACE_PTRS
+#include <Rinterface.h>
+#endif
+#include <R_ext/RStartup.h>
+
+#include "MemBuf.h"
+
+// simple logging help
+inline void logTxtFunction(const char* file, const int line, const char* expression, const bool verbose) {
+    if (verbose) {
+	std::cout << file << ":" << line << " expression: " << expression << std::endl;
+    }
+}
+
+#ifdef logTxt
+#undef logTxt
+#endif
+//#define logTxt(x, b) logTxtFunction(__FILE__, __LINE__, x, b);
+#define logTxt(x, b)
+
+#endif



More information about the Rinside-commits mailing list