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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Apr 1 16:04:54 CEST 2010


Author: romain
Date: 2010-04-01 16:04:54 +0200 (Thu, 01 Apr 2010)
New Revision: 145

Modified:
   pkg/inst/examples/standard/rinside_callbacks0.cpp
   pkg/src/Callbacks.h
   pkg/src/RInside.cpp
   pkg/src/RInside.h
Log:
better callbacks code

Modified: pkg/inst/examples/standard/rinside_callbacks0.cpp
===================================================================
--- pkg/inst/examples/standard/rinside_callbacks0.cpp	2010-04-01 12:07:02 UTC (rev 144)
+++ pkg/inst/examples/standard/rinside_callbacks0.cpp	2010-04-01 14:04:54 UTC (rev 145)
@@ -12,9 +12,8 @@
 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' )" ) ;
+    R.set_callbacks( new Callbacks() );
+    R.repl() ;
     exit(0);
 }
 

Modified: pkg/src/Callbacks.h
===================================================================
--- pkg/src/Callbacks.h	2010-04-01 12:07:02 UTC (rev 144)
+++ pkg/src/Callbacks.h	2010-04-01 14:04:54 UTC (rev 145)
@@ -27,62 +27,40 @@
 class Callbacks {
 public:
 	
-	Callbacks() : R_is_busy(false){} ;
+	Callbacks() : R_is_busy(false), buffer() {} ;
 	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 ;
+	virtual void ShowMessage(const char* message) {} ;
+	virtual void Suicide(const char* message) {};
+	virtual std::string ReadConsole( const char* prompt, bool addtohistory ) { return ""; };
+	virtual void WriteConsole( const std::string& line, int type ) {};
+	virtual void FlushConsole() {};
+	virtual void ResetConsole() {};
+	virtual void CleanerrConsole(){} ;
+	virtual void Busy( bool is_busy ) {} ;
 	
-	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 ) ;
+	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                                                                                      
 	
+	virtual bool has_ShowMessage() { return false ; } ;
+	virtual bool has_Suicide() { return false ; } ;
+	virtual bool has_ReadConsole() { return false ; } ;
+	virtual bool has_WriteConsole() { return false ; } ;
+	virtual bool has_ResetConsole() { return false ; } ;
+	virtual bool has_CleanerrConsole() { return false ; } ;
+	virtual bool has_Busy() { return false ; } ;
+	virtual bool has_FlushConsole(){ return false; } ;
+	
 private:
 	bool R_is_busy ;
-	             
+	std::string buffer ;
+	
 } ;                                       
 
-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/RInside.cpp
===================================================================
--- pkg/src/RInside.cpp	2010-04-01 12:07:02 UTC (rev 144)
+++ pkg/src/RInside.cpp	2010-04-01 14:04:54 UTC (rev 145)
@@ -322,14 +322,14 @@
 
 /* callbacks */
 
-void Callbacks::busy_( int which ){
+void Callbacks::Busy_( int which ){
 	R_is_busy = static_cast<bool>( which ) ;
-	busy( R_is_busy ) ;	
+	Busy( R_is_busy ) ;	
 }
 
-int Callbacks::readConsole_( const char* prompt, unsigned char* buf, int len, int addtohistory ){
+int Callbacks::ReadConsole_( const char* prompt, unsigned char* buf, int len, int addtohistory ){
 	try {
-		std::string res( readConsole( prompt, static_cast<bool>(addtohistory) ) ) ;
+		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. */
@@ -345,83 +345,76 @@
 }
 
 
-void Callbacks::writeConsole_( const char* buf, int len, int oType ){
-	std::string line ;
-	line.assign( buf, buf + len ) ;
-	writeConsole( line, oType) ;
+void Callbacks::WriteConsole_( const char* buf, int len, int oType ){
+	if( len ){
+		buffer.assign( buf, buf + len - 1 ) ;
+		WriteConsole( buffer, oType) ;
+	}
 }
 
 void RInside_ShowMessage( const char* message ){
-	RInside::instance().callbacks->showMessage( message ) ;	
+	RInside::instance().callbacks->ShowMessage( message ) ;	
 }
 
 void RInside_WriteConsoleEx( const char* message, int len, int oType ){
-	RInside::instance().callbacks->writeConsole_( message, len, 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 ) ;
+	return RInside::instance().callbacks->ReadConsole_( prompt, buf, len, addtohistory ) ;
 }
 
 void RInside_ResetConsole(){
-	RInside::instance().callbacks->resetConsole() ;
+	RInside::instance().callbacks->ResetConsole() ;
 }
 
 void RInside_FlushConsole(){                                       
-	RInside::instance().callbacks->flushConsole() ;
+	RInside::instance().callbacks->FlushConsole() ;
 }
 
 void RInside_ClearerrConsole(){
-	RInside::instance().callbacks->cleanerrConsole() ;
+	RInside::instance().callbacks->CleanerrConsole() ;
 }
 
 void RInside_Busy( int which ){
-	RInside::instance().callbacks->busy_(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 ) ;
+	callbacks = callbacks_ ;
+	
+	/* short circuit the callback function pointers */
+	if( callbacks->has_ShowMessage() ){
 		ptr_R_ShowMessage = RInside_ShowMessage ;
-		
-		Rprintf( "ptr_R_ReadConsole : <%p> = <%p>\n", ptr_R_ReadConsole, RInside_ReadConsole ) ;
+	}
+	if( callbacks->has_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 ) ;
+	}
+    if( callbacks->has_WriteConsole() ){
+    	ptr_R_WriteConsoleEx = RInside_WriteConsoleEx ;
+    	ptr_R_WriteConsole = NULL;
+	}
+	if( callbacks->has_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" ) ;
-	
+    if( callbacks->has_FlushConsole() ){
+    	ptr_R_FlushConsole = RInside_FlushConsole;
+    }
+    if( callbacks->has_CleanerrConsole() ){
+    	ptr_R_ClearerrConsole = RInside_ClearerrConsole;
+    }
+    if( callbacks->has_Busy() ){
+    	ptr_R_Busy = RInside_Busy;
+    }
+    
+    R_Outputfile = NULL;
+    R_Consolefile = NULL;    
+    
 }
 
+void RInside::repl(){
+	R_ReplDLLinit();
+	while( R_ReplDLLdo1() > 0 ){}
+}
+
+

Modified: pkg/src/RInside.h
===================================================================
--- pkg/src/RInside.h	2010-04-01 12:07:02 UTC (rev 144)
+++ pkg/src/RInside.h	2010-04-01 14:04:54 UTC (rev 145)
@@ -52,7 +52,8 @@
     
 public:
 	void set_callbacks(Callbacks* callbacks_) ;
-    
+	void repl() ;
+	
     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)
 



More information about the Rinside-commits mailing list