[Rcpp-commits] r4138 - in pkg/Rcpp: . inst/include/Rcpp inst/include/Rcpp/api/meat src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Dec 10 17:46:35 CET 2012


Author: romain
Date: 2012-12-10 17:46:35 +0100 (Mon, 10 Dec 2012)
New Revision: 4138

Added:
   pkg/Rcpp/inst/include/Rcpp/api/meat/Reference.h
Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/Reference.h
   pkg/Rcpp/inst/include/Rcpp/api/meat/meat.h
   pkg/Rcpp/src/api.cpp
Log:
move meat of Reference in own file

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2012-12-10 16:19:37 UTC (rev 4137)
+++ pkg/Rcpp/ChangeLog	2012-12-10 16:46:35 UTC (rev 4138)
@@ -15,6 +15,8 @@
         * include/Rcpp/DataFrame.h: just have declaration for DataFrame
         * include/Rcpp/Benchmark/Timer.h: move it into the Rcpp:: namespace
         * include/Rcpp/cache.h: clean
+        * include/Rcpp/Reference.h : field was declared const and should not be. 
+        added a ConstFieldProxy class for read only use
         
 2012-12-09 Romain Francois <romain at r-enthusiasts.com>
 

Modified: pkg/Rcpp/inst/include/Rcpp/Reference.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/Reference.h	2012-12-10 16:19:37 UTC (rev 4137)
+++ pkg/Rcpp/inst/include/Rcpp/Reference.h	2012-12-10 16:46:35 UTC (rev 4138)
@@ -61,8 +61,6 @@
          */
         Reference( const std::string& klass ) ;
 
-        
-        // TODO: perhaps I should move this to RObject (do the same as SlotProxy, etc ...)
         /**
          * Proxy for objects slots. 
          */
@@ -74,7 +72,7 @@
              * @param v parent object of which we get/set a field
              * @param name field name
              */
-            FieldProxy( const Reference& v, const std::string& name) ;
+            FieldProxy( Reference& v, const std::string& name) ;
 
             /**
              * lhs use. Assigns the target slot using the current 
@@ -89,30 +87,54 @@
              *
              * @param rhs wrappable object
              */
-            template <typename T> FieldProxy& operator=(const T& rhs){
-                set( wrap(rhs) ) ;
-                return *this ;
-            }
+            template <typename T> FieldProxy& operator=(const T& rhs) ;
                 
             /**
              * rhs use. Retrieves the current value of the slot
              * and structures it as a T object. This only works 
              * when as<T> makes sense
              */ 
-            template <typename T> operator T() const {
-                return as<T>(get()) ;
-            }
+            template <typename T> operator T() const  ;
                 
         private:
-            const Reference& parent; 
+            Reference& parent; 
             std::string field_name ;
                 
             SEXP get() const ;
-            void set(SEXP x ) const;
+            void set(SEXP x ) ;
         } ;
         friend class FieldProxy ;   
+        
+        /**
+         * Proxy for objects slots. 
+         */
+        class ConstFieldProxy {
+        public:
+            /**
+             * Creates a field proxy. 
+             *
+             * @param v parent object of which we get/set a field
+             * @param name field name
+             */
+            ConstFieldProxy( const Reference& v, const std::string& name) ;
+
+            /**
+             * rhs use. Retrieves the current value of the slot
+             * and structures it as a T object. This only works 
+             * when as<T> makes sense
+             */ 
+            template <typename T> operator T() const  ;
                 
-        FieldProxy field( const std::string& name) const  ;
+        private:
+            const Reference& parent; 
+            std::string field_name ;
+                
+            SEXP get() const ;
+        } ;
+        friend class ConstFieldProxy ;   
+        
+        FieldProxy field( const std::string& name)  ;
+        ConstFieldProxy field( const std::string& name) const ;
     
     
     private:

Added: pkg/Rcpp/inst/include/Rcpp/api/meat/Reference.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/api/meat/Reference.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/api/meat/Reference.h	2012-12-10 16:46:35 UTC (rev 4138)
@@ -0,0 +1,44 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
+//
+// backward.h: Rcpp R/C++ interface class library -- 
+//
+// Copyright (C) 2012    Dirk Eddelbuettel and Romain Francois
+//
+// 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/>.
+
+#ifndef Rcpp_api_meat_Reference_h
+#define Rcpp_api_meat_Reference_h
+
+namespace Rcpp{
+
+    template <typename T>
+    Reference::FieldProxy& Reference::FieldProxy::operator=( const T& rhs){
+         set( wrap(rhs) ) ;
+         return *this ;
+    }
+    
+    template <typename T>
+    Reference::FieldProxy::operator T() const {
+        return as<T>(get());    
+    }
+    
+    template <typename T>
+    Reference::ConstFieldProxy::operator T() const {
+        return as<T>(get());    
+    }
+    
+}
+#endif

Modified: pkg/Rcpp/inst/include/Rcpp/api/meat/meat.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/api/meat/meat.h	2012-12-10 16:19:37 UTC (rev 4137)
+++ pkg/Rcpp/inst/include/Rcpp/api/meat/meat.h	2012-12-10 16:46:35 UTC (rev 4138)
@@ -27,5 +27,6 @@
 #include <Rcpp/api/meat/DottedPair.h>
 #include <Rcpp/api/meat/Vector.h>
 #include <Rcpp/api/meat/Matrix.h>
+#include <Rcpp/api/meat/Reference.h>
 
 #endif

Modified: pkg/Rcpp/src/api.cpp
===================================================================
--- pkg/Rcpp/src/api.cpp	2012-12-10 16:19:37 UTC (rev 4137)
+++ pkg/Rcpp/src/api.cpp	2012-12-10 16:46:35 UTC (rev 4138)
@@ -756,7 +756,7 @@
         }
     }
     
-    Reference::FieldProxy::FieldProxy( const Reference& v, const std::string& name) : 
+    Reference::FieldProxy::FieldProxy( Reference& v, const std::string& name) : 
         parent(v), field_name(name) {}
 
     Reference::FieldProxy& Reference::FieldProxy::operator=(const FieldProxy& rhs){
@@ -766,33 +766,49 @@
     
     
     SEXP Reference::FieldProxy::get() const {
-        // TODO: get the field  
-        
         SEXP call = PROTECT( Rf_lang3( 
 									  R_DollarSymbol, 
-									  const_cast<Reference&>(parent).asSexp(), 
+									  parent.asSexp(), 
 									  Rf_mkString( field_name.c_str() )
 									   ) ) ;
         return Rcpp::internal::try_catch( call ) ;
         UNPROTECT(1) ;
     }
     
-    void Reference::FieldProxy::set( SEXP x) const {
+    void Reference::FieldProxy::set( SEXP x) {
         PROTECT(x);
         SEXP dollarGetsSym = Rf_install( "$<-");
         SEXP call = PROTECT( Rf_lang4( 
 									  dollarGetsSym,
-									  const_cast<Reference&>(parent).asSexp(), 
+									  parent.asSexp(), 
 									  Rf_mkString( field_name.c_str() ), 
 									  x
 									   ) ) ;
-        const_cast<Reference&>(parent).setSEXP( Rf_eval( call, R_GlobalEnv ) ); 
+        parent.setSEXP( Rf_eval( call, R_GlobalEnv ) ); 
         UNPROTECT(2) ;
     }
 
-    Reference::FieldProxy Reference::field( const std::string& name) const {
+    Reference::FieldProxy Reference::field( const std::string& name) {
         return FieldProxy( *this, name );
     }
+    
+    Reference::ConstFieldProxy Reference::field( const std::string& name) const {
+        return ConstFieldProxy( *this, name );
+    }
+    
+    Reference::ConstFieldProxy::ConstFieldProxy( const Reference& v, const std::string& name) : 
+        parent(v), field_name(name) {}
+
+    SEXP Reference::ConstFieldProxy::get() const {
+        SEXP call = PROTECT( Rf_lang3( 
+									  R_DollarSymbol, 
+									  const_cast<Reference&>(parent).asSexp(), 
+									  Rf_mkString( field_name.c_str() )
+									   ) ) ;
+        return Rcpp::internal::try_catch( call ) ;
+        UNPROTECT(1) ;
+    }
+    
     // }}}
  
     // {{{ Environment



More information about the Rcpp-commits mailing list