Rcpp Version 1.0.9
Module.h
Go to the documentation of this file.
1 //
2 // Module.h: Rcpp R/C++ interface class library -- Rcpp modules
3 //
4 // Copyright (C) 2012 - 2020 Dirk Eddelbuettel and Romain Francois
5 //
6 // This file is part of Rcpp.
7 //
8 // Rcpp is free software: you can redistribute it and/or modify it
9 // under the terms of the GNU General Public License as published by
10 // the Free Software Foundation, either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // Rcpp is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
20 
21 #ifndef Rcpp_Module_Module_h
22 #define Rcpp_Module_Module_h
23 
24 namespace Rcpp {
25 
29  class Module {
30  public:
31  typedef std::map<std::string,CppFunction*> MAP ;
32  typedef std::pair<const std::string,CppFunction*> FUNCTION_PAIR ;
33 
34  typedef std::map<std::string,class_Base*> CLASS_MAP ;
35  typedef std::pair<const std::string,class_Base*> CLASS_PAIR ;
36  typedef CLASS_MAP::iterator CLASS_ITERATOR ;
37 
38  Module() :
39  name(), functions(), classes(), prefix() {}
40 
41  Module(const char* name_) :
42  name(name_), functions(), classes(), prefix("Rcpp_module_"){
43  prefix += name ;
44  }
45 
53  inline SEXP invoke( const std::string& name_, SEXP* args, int nargs){ // #nocov start
54  MAP::iterator it = functions.find( name_ );
55  if( it == functions.end() ){
56  throw std::range_error( "no such function" ) ;
57  }
58  CppFunction* fun = it->second ;
59  if( fun->nargs() > nargs ){
60  throw std::range_error( "incorrect number of arguments" ) ;
61  }
62 
63  return List::create(
64  _["result"] = fun->operator()( args ),
65  _["void"] = fun->is_void()
66  ) ;
67  }
68 
73  size_t n = functions.size() ;
74  IntegerVector x( n ) ;
75  CharacterVector names( n );
76  MAP::iterator it = functions.begin() ;
77  for( size_t i=0; i<n; i++, ++it){
78  x[i] = (it->second)->nargs() ;
79  names[i] = it->first ;
80  }
81  x.names() = names ;
82  return x ;
83  } // #nocov end
84 
89  size_t n = functions.size() ;
90  CharacterVector names( n );
91  MAP::iterator it = functions.begin() ;
92  for( size_t i=0; i<n; i++, ++it){
93  names[i] = it->first ;
94  }
95  return names ;
96  }
97 
102  size_t n = classes.size() ;
103  CharacterVector names( n );
104  CLASS_MAP::iterator it = classes.begin() ;
105  for( size_t i=0; i<n; i++, ++it){
106  names[i] = it->first ;
107  }
108  return names ;
109  }
110 
114  List classes_info() ;
115 
119  CharacterVector complete(){ // #nocov start
120  size_t nf = functions.size() ;
121  size_t nc = classes.size() ;
122  size_t n = nf + nc ;
123  CharacterVector res( n ) ;
124  size_t i=0;
125  MAP::iterator it = functions.begin();
126  std::string buffer ;
127  for( ; i<nf; i++, ++it) {
128  buffer = it->first ;
129  if( (it->second)->nargs() == 0 ) {
130  buffer += "() " ;
131  } else {
132  buffer += "( " ;
133  }
134  res[i] = buffer ;
135  }
136  CLASS_MAP::iterator cit = classes.begin() ;
137  for( size_t j=0; j<nc; j++, i++, ++cit){
138  res[i] = cit->first ;
139  }
140  return res ;
141  } // #nocov end
142 
154  inline SEXP get_function( const std::string& name_ ){
155  MAP::iterator it = functions.begin() ;
156  size_t n = functions.size() ;
157  CppFunction* fun = 0 ;
158  for( size_t i=0; i<n; i++, ++it){
159  if( name_.compare( it->first ) == 0){
160  fun = it->second ;
161  break ;
162  }
163  }
164  std::string sign ;
165  fun->signature( sign, name_.data() ) ;
166  return List::create(
167  XPtr<CppFunction>( fun, false ),
168  fun->is_void(),
169  fun->docstring,
170  sign,
171  fun->get_formals(),
172  fun->nargs()
173  ) ;
174  }
175 
179  inline DL_FUNC get_function_ptr( const std::string& name_ ){
180  MAP::iterator it = functions.begin() ;
181  size_t n = functions.size() ;
182  CppFunction* fun = 0 ;
183  for( size_t i=0; i<n; i++, ++it){
184  if( name_.compare( it->first ) == 0){
185  fun = it->second ;
186  break ;
187  }
188  }
189  return fun->get_function_ptr() ;
190  }
191 
192  inline void Add( const char* name_ , CppFunction* ptr){
193  R_RegisterCCallable( prefix.c_str(), name_, ptr->get_function_ptr() ) ;
194  functions.insert( FUNCTION_PAIR( name_ , ptr ) ) ;
195  }
196 
197  inline void AddClass(const char* name_ , class_Base* cptr){
198  classes.insert( CLASS_PAIR( name_ , cptr ) ) ;
199  }
200 
201  inline bool has_function( const std::string& m){ // #nocov start
202  return functions.find(m) != functions.end() ;
203  }
204 
205  inline bool has_class( const std::string& m){
206  return classes.find(m) != classes.end() ;
207  } // #nocov end
208 
209  CppClass get_class( const std::string& cl ) ;
210 
211  class_Base* get_class_pointer(const std::string& cl){
212  CLASS_MAP::iterator it = classes.find(cl) ;
213  if( it == classes.end() ) throw std::range_error( "no such class" ) ;
214  return it->second ;
215  }
216 
217  std::string name ;
218 
219  void add_enum( const std::string& parent_class_typeinfo_name, const std::string& enum_name, const std::map<std::string, int>& value ){
220  // find the parent class
221  CLASS_ITERATOR it ;
222  class_Base* target_class = NULL;
223  for( it = classes.begin(); it != classes.end(); it++){
224  if( it->second->has_typeinfo_name(parent_class_typeinfo_name) ){
225  target_class = it->second ;
226  }
227  }
228 
229  // TODO: add the enum to the class
230  target_class->add_enum( enum_name, value ) ;
231  }
232 
233  private:
236  std::string prefix ;
237 
238  };
239 
240 }
241 
242 #endif
virtual int nargs()
Definition: CppFunction.h:60
virtual DL_FUNC get_function_ptr()=0
virtual SEXP get_formals()
Definition: CppFunction.h:75
std::string docstring
Definition: CppFunction.h:86
virtual void signature(std::string &, const char *)
Definition: CppFunction.h:70
virtual bool is_void()
Definition: CppFunction.h:65
void AddClass(const char *name_, class_Base *cptr)
Definition: Module.h:197
IntegerVector functions_arity()
Definition: Module.h:72
void Add(const char *name_, CppFunction *ptr)
Definition: Module.h:192
Module(const char *name_)
Definition: Module.h:41
std::pair< const std::string, class_Base * > CLASS_PAIR
Definition: Module.h:35
class_Base * get_class_pointer(const std::string &cl)
Definition: Module.h:211
std::map< std::string, CppFunction * > MAP
Definition: Module.h:31
SEXP invoke(const std::string &name_, SEXP *args, int nargs)
Definition: Module.h:53
std::map< std::string, class_Base * > CLASS_MAP
Definition: Module.h:34
bool has_function(const std::string &m)
Definition: Module.h:201
void add_enum(const std::string &parent_class_typeinfo_name, const std::string &enum_name, const std::map< std::string, int > &value)
Definition: Module.h:219
bool has_class(const std::string &m)
Definition: Module.h:205
CLASS_MAP classes
Definition: Module.h:235
std::pair< const std::string, CppFunction * > FUNCTION_PAIR
Definition: Module.h:32
CppClass get_class(const std::string &cl)
Definition: Module.h:39
SEXP get_function(const std::string &name_)
Definition: Module.h:154
CharacterVector functions_names()
Definition: Module.h:88
MAP functions
Definition: Module.h:234
CharacterVector class_names()
Definition: Module.h:101
std::string prefix
Definition: Module.h:236
CharacterVector complete()
Definition: Module.h:119
CLASS_MAP::iterator CLASS_ITERATOR
Definition: Module.h:36
DL_FUNC get_function_ptr(const std::string &name_)
Definition: Module.h:179
List classes_info()
Definition: Module.h:25
std::string name
Definition: Module.h:217
NamesProxy names()
Definition: NamesProxy.h:82
static Vector create()
Definition: Vector.h:1122
void add_enum(const std::string &enum_name, const std::map< std::string, int > &value)
Definition: class_Base.h:81
Rcpp API.
Definition: algo.h:28
sugar::Sign< INTSXP, NA, T > sign(const VectorBase< INTSXP, NA, T > &t)
Definition: sign.h:70
static internal::NamedPlaceHolder _
Definition: Named.h:64