[Rcpp-commits] r4085 - in pkg/Rcpp: . inst/include/Rcpp/iostream src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Dec 5 20:22:07 CET 2012


Author: romain
Date: 2012-12-05 20:22:07 +0100 (Wed, 05 Dec 2012)
New Revision: 4085

Removed:
   pkg/Rcpp/src/Rstreambuf.cpp
Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/iostream/Rostream.h
   pkg/Rcpp/inst/include/Rcpp/iostream/Rstreambuf.h
   pkg/Rcpp/src/AttributesGen.cpp
   pkg/Rcpp/src/Rostream.cpp
Log:
rework Rstreambuf / Rostream

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2012-12-05 18:10:09 UTC (rev 4084)
+++ pkg/Rcpp/ChangeLog	2012-12-05 19:22:07 UTC (rev 4085)
@@ -3,6 +3,10 @@
         * src/cache.cpp: added get_cache
         * include/Rcpp/hash/IndexHash.h: use a cached integer vector for
         the hash table payload. The cache vector increases as needed. 
+        * include/Rcpp/iostream/Rostream.h: make Rostream a template
+        * include/Rcpp/iostream/Rstreambuf.h: make Rstreambuf a template
+        * src/AttributesGen.cpp : include exceptions.h which was included 
+        implicitely by Rostream.h before
         
 2012-12-05  JJ Allaire <jj at rstudio.org>
 

Modified: pkg/Rcpp/inst/include/Rcpp/iostream/Rostream.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/iostream/Rostream.h	2012-12-05 18:10:09 UTC (rev 4084)
+++ pkg/Rcpp/inst/include/Rcpp/iostream/Rostream.h	2012-12-05 19:22:07 UTC (rev 4085)
@@ -19,24 +19,25 @@
 // You should have received a copy of the GNU General Public License
 // along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
 
-#ifndef __ROSTREAM_H__
-#define __ROSTREAM_H__
+#ifndef RCPP__IOSTREAM__ROSTREAM_H
+#define RCPP__IOSTREAM__ROSTREAM_H
 
-#include <iomanip>				// USES setw
-
-#include "Rstreambuf.h"         		// to permit direct inclusion of Rostream header
-
 // modified from 
 // http://stackoverflow.com/questions/243696/correctly-over-loading-a-stringbuf-to-replace-cout-in-a-matlab-mex-file
 
-namespace Rcpp {
+#include <Rcpp/iostream/Rstreambuf.h>
+#include <iostream>
 
+namespace Rcpp {   
+
+    template <bool OUTPUT>
     class Rostream : public std::ostream {
-        Rstreambuf* buf ;
+        typedef Rstreambuf<OUTPUT> Buffer ; 
+        Buffer* buf ;
     public:
-        Rostream(bool output) : 
-            std::ostream( new Rstreambuf(output) ), 
-            buf( static_cast<Rstreambuf*>( rdbuf() ) )
+        Rostream() : 
+            std::ostream( new Buffer ), 
+            buf( static_cast<Buffer*>( rdbuf() ) )
         {}
         ~Rostream() { 
             if (buf != NULL) {
@@ -47,10 +48,10 @@
     };
     
     // declare global variable
-    extern Rostream Rcout;
+    extern Rostream<true> Rcout;
 
     // declare global variable
-    extern Rostream Rcerr;
+    extern Rostream<false> Rcerr;
 }
 
 #endif

Modified: pkg/Rcpp/inst/include/Rcpp/iostream/Rstreambuf.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/iostream/Rstreambuf.h	2012-12-05 18:10:09 UTC (rev 4084)
+++ pkg/Rcpp/inst/include/Rcpp/iostream/Rstreambuf.h	2012-12-05 19:22:07 UTC (rev 4085)
@@ -1,6 +1,6 @@
 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
 //
-// Rostream.h: Rcpp R/C++ interface class library -- stream buffer
+// Rstreambuf.h: Rcpp R/C++ interface class library -- stream buffer
 //
 // Copyright (C) 2011 - 2012    Dirk Eddelbuettel, Romain Francois and Jelmer Ypma
 //
@@ -19,31 +19,30 @@
 // You should have received a copy of the GNU General Public License
 // along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
 
-#ifndef __RSTREAMBUF_H__
-#define __RSTREAMBUF_H__
+#ifndef RCPP__IOSTREAM__RSTREAMBUF_H
+#define RCPP__IOSTREAM__RSTREAMBUF_H
 
 #include <streambuf>
-#include <RcppCommon.h>
 
 // modified from 
 // http://stackoverflow.com/questions/243696/correctly-over-loading-a-stringbuf-to-replace-cout-in-a-matlab-mex-file
 
 namespace Rcpp {
 
+    template <bool OUTPUT>
     class Rstreambuf : public std::streambuf {
     public:
-        Rstreambuf(bool output_): output(output_){}
-		
+        Rstreambuf(){}
+    	
     protected:
         virtual std::streamsize xsputn(const char *s, std::streamsize n );
         
         virtual int overflow(int c = EOF );
         
-        virtual int sync() ;
-    private:
-        bool output ;
-  };
+        virtual int sync()  ;
+    };
 
+  
 }
 
 #endif

Modified: pkg/Rcpp/src/AttributesGen.cpp
===================================================================
--- pkg/Rcpp/src/AttributesGen.cpp	2012-12-05 18:10:09 UTC (rev 4084)
+++ pkg/Rcpp/src/AttributesGen.cpp	2012-12-05 19:22:07 UTC (rev 4085)
@@ -26,6 +26,7 @@
 #include <fstream>
 
 #include <Rcpp/iostream/Rostream.h>
+#include <Rcpp/exceptions.h>
 
 namespace Rcpp {
 namespace attributes {

Modified: pkg/Rcpp/src/Rostream.cpp
===================================================================
--- pkg/Rcpp/src/Rostream.cpp	2012-12-05 18:10:09 UTC (rev 4084)
+++ pkg/Rcpp/src/Rostream.cpp	2012-12-05 19:22:07 UTC (rev 4085)
@@ -2,7 +2,7 @@
 //
 // Rostream.cpp: Rcpp R/C++ interface class library -- output stream
 //
-// Copyright (C) 2011           Dirk Eddelbuettel, Romain Francois and Jelmer Ypma
+// Copyright (C) 2011 - 2012    Dirk Eddelbuettel, Romain Francois and Jelmer Ypma
 //
 // This file is part of Rcpp.
 //
@@ -19,10 +19,42 @@
 // You should have received a copy of the GNU General Public License
 // along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
 
-#include <RcppCommon.h>
+#include <Rcpp/iostream/Rstreambuf.h>
 #include <Rcpp/iostream/Rostream.h>
+#include <R.h>
 
+namespace Rcpp{
+  template <> inline std::streamsize Rstreambuf<true>::xsputn(const char *s, std::streamsize num ) {
+      Rprintf( "%.*s", num, s ) ;
+      return num ;
+  }
+  template <> inline std::streamsize Rstreambuf<false>::xsputn(const char *s, std::streamsize num ) {
+      REprintf( "%.*s", num, s ) ; 
+      return num ;
+  }
+
+  template <> inline int Rstreambuf<true>::overflow(int c ) {
+    if (c != EOF) Rprintf( "%.1s", &c ) ;
+    return c ;
+  }
+  template <> inline int Rstreambuf<false>::overflow(int c ) {
+    if (c != EOF) REprintf( "%.1s", &c ) ;
+    return c ;
+  }
+      
+  template <> inline int Rstreambuf<true>::sync(){
+      ::R_FlushConsole() ;
+      return 0 ;
+  }
+  template <> inline int Rstreambuf<false>::sync(){
+      ::R_FlushConsole() ;
+      return 0 ;
+  }
+  
+  
+}
+
 // define global variable Rcout
-Rcpp::Rostream Rcpp::Rcout(true);
-Rcpp::Rostream Rcpp::Rcerr(false);
+Rcpp::Rostream<true>  Rcpp::Rcout;
+Rcpp::Rostream<false> Rcpp::Rcerr;
 

Deleted: pkg/Rcpp/src/Rstreambuf.cpp
===================================================================
--- pkg/Rcpp/src/Rstreambuf.cpp	2012-12-05 18:10:09 UTC (rev 4084)
+++ pkg/Rcpp/src/Rstreambuf.cpp	2012-12-05 19:22:07 UTC (rev 4085)
@@ -1,48 +0,0 @@
-// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
-//
-// Rostream.h: Rcpp R/C++ interface class library -- stream buffer
-//
-// Copyright (C) 2011 - 2012    Dirk Eddelbuettel, Romain Francois and Jelmer Ypma
-//
-// This file is part of Rcpp.
-//
-// Rcpp 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.
-//
-// Rcpp 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 Rcpp.  If not, see <http://www.gnu.org/licenses/>.
-
-#include <RcppCommon.h>
-#include <Rcpp/iostream/Rstreambuf.h>
-
-std::streamsize Rcpp::Rstreambuf::xsputn(const char *s, std::streamsize num ) {
-    if(output) {
-        Rprintf( "%.*s", num, s ) ; 
-    } else {
-        REprintf( "%.*s", num, s) ;
-    }
-    return num;
-}
-
-int Rcpp::Rstreambuf::overflow(int c ) {
-    if (c != EOF) {
-        if( output ) {
-            Rprintf( "%.1s", &c ) ;
-        } else {
-            REprintf( "%.1s", &c ) ;
-        }
-    }
-    return c;
-}
-
-int Rcpp::Rstreambuf::sync(){
-    R_FlushConsole() ;
-    return 0 ;    
-}



More information about the Rcpp-commits mailing list