[Rinside-commits] r166 - in pkg: . inst/examples/standard inst/include src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Jun 16 11:10:11 CEST 2010
Author: romain
Date: 2010-06-16 11:10:10 +0200 (Wed, 16 Jun 2010)
New Revision: 166
Added:
pkg/inst/examples/standard/rinside_sample9.cpp
Modified:
pkg/DESCRIPTION
pkg/inst/include/MemBuf.h
pkg/src/MemBuf.cpp
Log:
add an example using InternalFunction
Modified: pkg/DESCRIPTION
===================================================================
--- pkg/DESCRIPTION 2010-06-15 16:22:41 UTC (rev 165)
+++ pkg/DESCRIPTION 2010-06-16 09:10:10 UTC (rev 166)
@@ -15,8 +15,8 @@
Several examples are provided below the examples/ directory of
the installed package, and Doxygen-generated documentation of
the C++ classes is included as well.
-Depends: R (>= 2.10.0), Rcpp (>= 0.8.2)
+Depends: R (>= 2.10.0), Rcpp (>= 0.8.2.6)
LinkingTo: Rcpp
-SystemRequirements: None
+SystemRequirements: GNU make
URL: http://dirk.eddelbuettel.com/code/rinside.html
License: GPL (>= 2)
Added: pkg/inst/examples/standard/rinside_sample9.cpp
===================================================================
--- pkg/inst/examples/standard/rinside_sample9.cpp (rev 0)
+++ pkg/inst/examples/standard/rinside_sample9.cpp 2010-06-16 09:10:10 UTC (rev 166)
@@ -0,0 +1,30 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8; -*-
+//
+// Simple example showing how expose a C++ function
+//
+// Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
+
+#include <RInside.h> // for the embedded R via RInside
+
+// a c++ function we wish to expose to R
+const char* hello( std::string who ){
+ std::string result( "hello " ) ;
+ result += who ;
+ return result.c_str() ;
+}
+
+int main(int argc, char *argv[]) {
+
+ // create an embedded R instance
+ RInside R(argc, argv);
+
+ // expose the "hello" function in the global environment
+ R["hello"] = Rcpp::InternalFunction( &hello ) ;
+
+ // call it and display the result
+ std::string result = R.parseEval("hello('world')") ;
+ std::cout << "hello( 'world') = " << result << std::endl ;
+
+ exit(0);
+}
+
Modified: pkg/inst/include/MemBuf.h
===================================================================
--- pkg/inst/include/MemBuf.h 2010-06-15 16:22:41 UTC (rev 165)
+++ pkg/inst/include/MemBuf.h 2010-06-16 09:10:10 UTC (rev 166)
@@ -29,6 +29,6 @@
~MemBuf();
void resize();
void rewind();
- void add(char *buf);
- const char* getBufPtr() { return buffer.c_str() ; };
+ void add(const std::string& );
+ inline const char* getBufPtr() { return buffer.c_str() ; };
};
Modified: pkg/src/MemBuf.cpp
===================================================================
--- pkg/src/MemBuf.cpp 2010-06-15 16:22:41 UTC (rev 165)
+++ pkg/src/MemBuf.cpp 2010-06-16 09:10:10 UTC (rev 166)
@@ -21,8 +21,7 @@
#include <iostream>
#include <cstdlib>
-#include <cstring>
-#include <string.h> // Solaris seems to need this for strlen
+#include <string>
#include <MemBuf.h>
@@ -45,8 +44,8 @@
buffer.clear() ;
}
-void MemBuf::add(char *buf){
- int buflen = strlen(buf);
+void MemBuf::add(const std::string& buf){
+ int buflen = buf.size() ;
while ( ( buflen + buffer.size() ) >= buffer.capacity() ) {
resize();
}
More information about the Rinside-commits
mailing list