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