[Rcpp-commits] r3812 - in pkg/Rcpp: R inst/include/Rcpp man src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Oct 23 13:57:42 CEST 2012


Author: romain
Date: 2012-10-23 13:57:42 +0200 (Tue, 23 Oct 2012)
New Revision: 3812

Modified:
   pkg/Rcpp/R/00_classes.R
   pkg/Rcpp/inst/include/Rcpp/Module.h
   pkg/Rcpp/man/CppClass-class.Rd
   pkg/Rcpp/src/Module.cpp
Log:
starting to add enum support

Modified: pkg/Rcpp/R/00_classes.R
===================================================================
--- pkg/Rcpp/R/00_classes.R	2012-10-23 03:01:13 UTC (rev 3811)
+++ pkg/Rcpp/R/00_classes.R	2012-10-23 11:57:42 UTC (rev 3812)
@@ -73,7 +73,8 @@
 	    constructors = "list",
 	    generator    = "refObjectGenerator", 
 	    docstring    = "character", 
-	    typeid       = "character"
+	    typeid       = "character", 
+	    enums        = "list"
 	), 
 	contains = "character"
 	)

Modified: pkg/Rcpp/inst/include/Rcpp/Module.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/Module.h	2012-10-23 03:01:13 UTC (rev 3811)
+++ pkg/Rcpp/inst/include/Rcpp/Module.h	2012-10-23 11:57:42 UTC (rev 3812)
@@ -83,7 +83,7 @@
     public:
         class_Base() : name(), docstring() {} ;
         class_Base(const char* name_, const char* doc) : 
-            name(name_), docstring( doc == 0 ? "" : doc ){} ;
+            name(name_), docstring( doc == 0 ? "" : doc ), enums() {} ;
         
         virtual Rcpp::List fields(SEXP){ return Rcpp::List(0); }
         virtual Rcpp::List getMethods(SEXP, std::string&){ return Rcpp::List(0); }
@@ -129,9 +129,19 @@
             throw std::range_error( "cannot set property" ) ;
         }
         virtual std::string get_typeinfo_name(){ return "" ; }
+        bool has_typeinfo_name( const std::string& name_ ){
+            return get_typeinfo_name().compare(name_) == 0;   
+        }
+        void add_enum( const std::string& enum_name, const std::map<std::string, int>& value ) ;
         
         std::string name ;
         std::string docstring ;
+        
+        typedef std::map< std::string, int > ENUM ;
+        typedef std::map< std::string, ENUM > ENUM_MAP ;
+        typedef ENUM_MAP::value_type ENUM_MAP_PAIR ;
+        ENUM_MAP enums ;
+        
     } ;
 
     class Module {
@@ -141,6 +151,7 @@
                 
         typedef std::map<std::string,class_Base*> CLASS_MAP ;
         typedef std::pair<const std::string,class_Base*> CLASS_PAIR ;
+        typedef CLASS_MAP::iterator CLASS_ITERATOR ;
         
         Module()  ;
         Module(const char* name_)  ;
@@ -174,7 +185,9 @@
         Rcpp::CppClass get_class(const std::string& ) ;
                 
         std::string name ;
-                
+           
+        void add_enum( const std::string& parent_class_typeinfo_name, const std::string& enum_name, const std::map<std::string, int>& value ) ;
+        
     private:
         MAP functions ;
         CLASS_MAP classes ;
@@ -745,6 +758,34 @@
         
     } ;   
 
+    template <typename Enum, typename Parent>
+    class enum_ {
+        public:
+            typedef enum_<Enum,Parent> self ;
+            
+            enum_( const char* name_ ) : 
+                name(name_), values(), parent_typeinfo_name( typeid(Parent).name() ){ 
+                } 
+            ~enum_(){
+                Rcpp::Module* module = getCurrentScope() ;
+                module->add_enum( parent_typeinfo_name, name, values ) ;
+            }
+            
+            self& value( const char* name_, Enum value_ ){
+                values.insert( PAIR( name_, static_cast<int>( value_ ) ) ) ;
+                return *this ;
+            }
+                
+        private:
+            
+            std::string name ;
+            typedef std::map< std::string, int > MAP ;
+            typedef MAP::value_type PAIR ;
+            MAP values ;
+            std::string parent_typeinfo_name ;
+            
+    } ;
+    
     // function factories
 #include <Rcpp/module/Module_generated_function.h>
 

Modified: pkg/Rcpp/man/CppClass-class.Rd
===================================================================
--- pkg/Rcpp/man/CppClass-class.Rd	2012-10-23 03:01:13 UTC (rev 3811)
+++ pkg/Rcpp/man/CppClass-class.Rd	2012-10-23 11:57:42 UTC (rev 3812)
@@ -24,7 +24,7 @@
     \item{generator}{the generator object for the class}
     \item{docstring}{description of the class}
     \item{typeid}{unmangled typeid of the class}
-
+    \item{enums}{enums of the class}
   }
 }
 \section{Methods}{

Modified: pkg/Rcpp/src/Module.cpp
===================================================================
--- pkg/Rcpp/src/Module.cpp	2012-10-23 03:01:13 UTC (rev 3811)
+++ pkg/Rcpp/src/Module.cpp	2012-10-23 11:57:42 UTC (rev 3812)
@@ -382,6 +382,24 @@
 		return res ;
 	}
 	
+	void Module::add_enum( const std::string& parent_class_typeinfo_name, const std::string& enum_name, const std::map<std::string, int>& value ){
+	    // find the parent class
+	    CLASS_ITERATOR it ;
+	    class_Base* target_class ;
+	    for( it = classes.begin(); it != classes.end(); it++){
+	        if( it->second->has_typeinfo_name(parent_class_typeinfo_name) ){
+	            target_class = it->second ;
+	        }
+	    }
+	    
+	    // TODO: add the enum to the class
+	    target_class->add_enum( enum_name, value ) ;
+	}
+	
+	void class_Base::add_enum( const std::string& enum_name, const std::map<std::string, int>& value ){
+	    enums.insert( ENUM_MAP_PAIR( enum_name, value ) ) ;
+	}
+        
 	CppClass::CppClass( SEXP x) : S4(x){}
 	
 	CppClass::CppClass( Module* p, class_Base* cl, std::string& buffer ) : S4("C++Class") {
@@ -399,6 +417,7 @@
 		slot( "constructors") = cl->getConstructors( clxp.asSexp(), buffer ) ;
 		slot( "docstring"   ) = cl->docstring ;
 		slot( "typeid" )      = cl->get_typeinfo_name() ;
+		slot( "enums"  )      = cl->enums ;
 	}
 
 	CppObject::CppObject( Module* p, class_Base* clazz, SEXP xp ) : S4("C++Object") {



More information about the Rcpp-commits mailing list