You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2020/11/03 13:44:00 UTC

[GitHub] [arrow] romainfrancois commented on pull request #8256: ARROW-9001: [R] Box outputs as correct type in call_function

romainfrancois commented on pull request #8256:
URL: https://github.com/apache/arrow/pull/8256#issuecomment-720361840


   Last commit goes in that direction with: 
   
   ```cpp
   namespace cpp11 {
   
   template <typename T>
   std::string r6_class_name(const std::shared_ptr<T>& x) ;
   
   template <typename T>
   SEXP to_r6(const std::shared_ptr<T>& x) {
     if (x == nullptr) return R_NilValue;
   
     auto r_class_name = cpp11::r6_class_name<T>(x);
     cpp11::external_pointer<std::shared_ptr<T>> xp(new std::shared_ptr<T>(x));
     SEXP r6_class = Rf_install(r_class_name.c_str());
   
     // make call:  <symbol>$new(<x>)
     SEXP call = PROTECT(Rf_lang3(R_DollarSymbol, r6_class, arrow::r::symbols::new_));
     SEXP call2 = PROTECT(Rf_lang2(call, xp));
   
     // and then eval in arrow::
     SEXP r6 = PROTECT(Rf_eval(call2, arrow::r::ns::arrow));
   
     UNPROTECT(3);
     return r6;
   }
   }
   
   class R6 {
   public:
   
     template <typename T>
     R6(const std::shared_ptr<T>& x) : data_(cpp11::to_r6<T>(x)){}
   
     template <typename T>
     R6(std::unique_ptr<T> x) : data_(cpp11::to_r6<T>(std::shared_ptr<T>(x.release()))){}
   
     R6(SEXP data) : data_(data){}
   
     operator SEXP() const {
       return data_;
     }
   
   private:
     SEXP data_;
   };
   ```
   
   so that the functions "just" return `R6` and the the `R6` constructor finds the right R6 class name by calling one of the `r6_class_name` methods. 
   
   This however means defining many of these, most of them being trivial: 
   
   ```cpp
   template <> inline std::string r6_class_name<arrow::Field>(const std::shared_ptr<arrow::Field>& array_data) {
     return "Field";
   }
   ```
   
   but some of them with some notion of dispatch: 
   
   ```cpptemplate <> std::string r6_class_name<ds::Dataset>(const std::shared_ptr<ds::Dataset>& dataset) {
     auto type_name = dataset->type_name();
   
     if (type_name == "union") {
       return "UnionDataset";
     } else if (type_name == "filesystem") {
       return "FileSystemDataset";
     } else if(type_name == "in-memory"){
       return "InMemoryDataset";
     } else {
       return "Dataset";
     }
   }
   ```
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org