22 #ifndef Rcpp_Module_CLASS_h
23 #define Rcpp_Module_CLASS_h
25 template <
typename Class>
37 typedef std::pair<const std::string,method_class*>
PAIR ;
52 typedef std::pair<const std::string,prop_class*>
PROP_PAIR ;
54 class_(
const char* name_,
const char* doc = 0) :
55 class_Base(name_, doc),
80 if( module->has_class(name) ){
111 return constructor( docstring, valid ) ;
114 #include <Rcpp/module/Module_generated_class_constructor.h>
115 #include <Rcpp/module/Module_generated_class_factory.h>
127 for(
int i=0; i<n; i++ ){
129 bool ok = (p->valid)(args, nargs) ;
131 Class* ptr = p->ctor->get_new( args, nargs ) ;
132 return XP( ptr,
true ) ;
138 for(
int i=0; i<n; i++){
140 bool ok = (pfact->valid)(args, nargs) ;
142 Class* ptr = pfact->fact->get_new( args, nargs ) ;
143 return XP( ptr,
true ) ;
147 throw std::range_error(
"no valid constructor available for the argument list" ) ;
154 for(
int i=0; i<n; i++ ){
156 if( p->nargs() == 0 )
return true ;
160 for(
int i=0; i<n; i++ ){
162 if( pfact->nargs() == 0 )
return true ;
167 SEXP
invoke( SEXP method_xp, SEXP
object, SEXP *args,
int nargs ){
171 typename vec_signed_method::iterator it = mets->begin() ;
172 int n = mets->size() ;
175 for(
int i=0; i<n; i++, ++it ){
176 if( ( (*it)->valid )( args, nargs) ){
183 throw std::range_error(
"could not find valid method" ) ;
186 m->operator()(
XP(
object), args );
194 SEXP
invoke_void( SEXP method_xp, SEXP
object, SEXP *args,
int nargs ){
198 typename vec_signed_method::iterator it = mets->begin() ;
199 int n = mets->size() ;
202 for(
int i=0; i<n; i++, ++it ){
203 if( ( (*it)->valid )( args, nargs) ){
210 throw std::range_error(
"could not find valid method" ) ;
212 m->operator()(
XP(
object), args );
220 typename vec_signed_method::iterator it = mets->begin() ;
221 int n = mets->size() ;
224 for(
int i=0; i<n; i++, ++it ){
225 if( ( (*it)->valid )( args, nargs) ){
232 throw std::range_error(
"could not find valid method" ) ;
234 return m->operator()(
XP(
object), args ) ;
240 RCPP_DEBUG_MODULE_1(
"AddMethod( %s, method_class* m, ValidMethod valid = &yes, const char* docstring = 0", name_ )
242 typename map_vec_signed_method::iterator it = ptr->vec_methods.find( name_ ) ;
243 if( it == ptr->vec_methods.end() ){
247 if( *name_ ==
'[' ) ptr->specials++ ;
256 #include <Rcpp/module/Module_generated_method.h>
257 #include <Rcpp/module/Module_generated_Pointer_method.h>
266 typename PROPERTY_MAP::iterator it =
properties.find( p ) ;
267 if( it ==
properties.end() )
throw std::range_error(
"no such property" ) ;
268 return it->second->is_readonly() ;
271 typename PROPERTY_MAP::iterator it =
properties.find( p ) ;
272 if( it ==
properties.end() )
throw std::range_error(
"no such property" ) ;
273 return it->second->get_class() ;
279 typename map_vec_signed_method::iterator it =
vec_methods.begin( ) ;
280 for(
int i=0; i<s; i++, ++it){
281 n += (it->second)->size() ;
286 for(
int i=0; i<s; i++, ++it){
287 n = (it->second)->size() ;
288 std::string name = it->first ;
289 for(
int j=0; j<n; j++, k++){
299 typename map_vec_signed_method::iterator it =
vec_methods.begin( ) ;
300 for(
int i=0; i<s; i++, ++it){
301 n += (it->second)->size() ;
307 for(
int i=0; i<s; i++, ++it){
308 n = (it->second)->size() ;
309 std::string name = it->first ;
310 typename vec_signed_method::iterator m_it = (it->second)->begin() ;
311 for(
int j=0; j<n; j++, k++, ++m_it){
313 res[k] = (*m_it)->nargs() ;
316 res.
names( ) = mnames ;
323 typename map_vec_signed_method::iterator it =
vec_methods.begin( ) ;
324 for(
int i=0; i<s; i++, ++it){
325 n += (it->second)->size() ;
331 for(
int i=0; i<s; i++, ++it){
332 n = (it->second)->size() ;
333 std::string name = it->first ;
334 typename vec_signed_method::iterator m_it = (it->second)->begin() ;
335 for(
int j=0; j<n; j++, k++, ++m_it){
337 res[k] = (*m_it)->is_void() ;
340 res.
names( ) = mnames ;
348 typename PROPERTY_MAP::iterator it =
properties.begin( ) ;
349 for(
int i=0; i<n; i++, ++it){
359 typename PROPERTY_MAP::iterator it =
properties.begin( ) ;
360 for(
int i=0; i<n; i++, ++it){
361 pnames[i] = it->first ;
362 out[i] = it->second->get_class() ;
364 out.
names() = pnames ;
372 typename map_vec_signed_method::iterator it =
vec_methods.begin( ) ;
377 if( buffer[0] ==
'[' ) continue ;
387 typename PROPERTY_MAP::iterator prop_it =
properties.begin();
388 for( ; i<ntotal; i++, ++prop_it){
389 out[i] = prop_it->first ;
397 return prop->get(
XP(
object) );
404 return prop->set(
XP(
object), value );
413 typename PROPERTY_MAP::iterator it =
properties.begin( ) ;
414 for(
int i=0; i<n; i++, ++it){
415 pnames[i] = it->first ;
416 out[i] = S4_field<Class>( it->second, class_xp ) ;
418 out.
names() = pnames ;
423 RCPP_DEBUG_MODULE(
"Rcpp::List getMethods( const XP_Class& class_xp, std::string& buffer" )
424 #if RCPP_DEBUG_LEVEL > 0
425 Rf_PrintValue( class_xp ) ;
430 typename map_vec_signed_method::iterator it =
vec_methods.begin( ) ;
432 for(
int i=0; i<n; i++, ++it){
433 mnames[i] = it->first ;
435 res[i] = S4_CppOverloadedMethods<Class>( v , class_xp, it->first.c_str(), buffer ) ;
437 res.
names() = mnames ;
444 typename vec_signed_constructor::iterator it =
constructors.begin( ) ;
445 for(
int i=0; i<n; i++, ++it){
446 out[i] = S4_CppConstructor<Class>( *it , class_xp, name, buffer ) ;
466 if( ptr->finalizer_pointer )
delete ptr->finalizer_pointer ;
467 ptr->finalizer_pointer = f ;
486 template <
typename PARENT>
489 typedef typename parent_class_::signed_method_class parent_signed_method_class ;
490 typedef typename parent_class_::method_class parent_method_class ;
493 parent_class_* parent_class_pointer =
reinterpret_cast< parent_class_*
> ( scope->get_class_pointer(
parent ) );
496 typename parent_class_::map_vec_signed_method::iterator parent_vec_methods_iterator = parent_class_pointer->vec_methods.begin() ;
497 typename parent_class_::map_vec_signed_method::iterator parent_vec_methods_end = parent_class_pointer->vec_methods.end() ;
498 std::string method_name ;
499 for( ; parent_vec_methods_iterator != parent_vec_methods_end; parent_vec_methods_iterator++){
500 method_name = parent_vec_methods_iterator->first ;
502 typedef typename parent_class_::vec_signed_method parent_vec_signed_method ;
503 parent_vec_signed_method* p_methods = parent_vec_methods_iterator->second ;
504 typename parent_vec_signed_method::iterator methods_it = p_methods->begin() ;
505 typename parent_vec_signed_method::iterator methods_end = p_methods->end() ;
507 for( ; methods_it != methods_end; methods_it++){
508 parent_signed_method_class* signed_method = *methods_it ;
509 parent_method_class* parent_method = signed_method->method ;
511 CppMethod<Class>* method =
new CppInheritedMethod<Class,PARENT>( parent_method ) ;
513 AddMethod( method_name.c_str(), method, signed_method->valid , signed_method->docstring.c_str() ) ;
519 typedef typename parent_class_::PROPERTY_MAP parent_PROPERTY_MAP ;
520 typedef typename parent_PROPERTY_MAP::iterator parent_PROPERTY_MAP_iterator ;
522 parent_PROPERTY_MAP_iterator parent_property_it = parent_class_pointer->properties.begin() ;
523 parent_PROPERTY_MAP_iterator parent_property_end = parent_class_pointer->properties.end() ;
524 for( ; parent_property_it != parent_property_end; parent_property_it++){
526 parent_property_it->first.c_str(),
527 new CppInheritedProperty<Class,PARENT>( parent_property_it->second )
531 std::string buffer(
"Rcpp_" ) ; buffer +=
parent ;
void SetFinalizer(finalizer_class *f)
SignedMethod< Class > signed_method_class
std::map< std::string, prop_class * > PROPERTY_MAP
self & finalizer(void(*f)(Class *))
vec_signed_constructor constructors
SEXP invoke_notvoid(SEXP method_xp, SEXP object, SEXP *args, int nargs)
SignedFactory< Class > signed_factory_class
CppFinalizer< Class > finalizer_class
Factory_Base< Class > factory_class
bool has_method(const std::string &m)
std::map< std::string, method_class * > METHOD_MAP
Rcpp::List property_classes()
std::map< std::string, vec_signed_method * > map_vec_signed_method
void setProperty(SEXP field_xp, SEXP object, SEXP value)
std::vector< signed_method_class * > vec_signed_method
self & AddMethod(const char *name_, method_class *m, ValidMethod valid=&yes, const char *docstring=0)
Rcpp::List getConstructors(const XP_Class &class_xp, std::string &buffer)
CppMethod< Class > method_class
virtual void run_finalizer(SEXP object)
Rcpp::List fields(const XP_Class &class_xp)
class_(const char *name_, const char *doc=0)
std::pair< const std::string, method_class * > PAIR
self & derives(const char *parent)
std::string property_class(const std::string &p)
std::pair< std::string, vec_signed_method * > vec_signed_method_pair
Constructor_Base< Class > constructor_class
Rcpp::CharacterVector property_names()
finalizer_class * finalizer_pointer
SEXP invoke(SEXP method_xp, SEXP object, SEXP *args, int nargs)
self & AddConstructor(constructor_class *ctor, ValidConstructor valid, const char *docstring=0)
self & AddProperty(const char *name_, prop_class *p)
std::string typeinfo_name
self & default_constructor(const char *docstring=0, ValidConstructor valid=&yes_arity< 0 >)
Rcpp::CharacterVector complete()
Rcpp::LogicalVector methods_voidness()
Rcpp::CharacterVector method_names()
vec_signed_factory factories
SEXP newInstance(SEXP *args, int nargs)
self & AddFactory(factory_class *fact, ValidConstructor valid, const char *docstring=0)
SEXP invoke_void(SEXP method_xp, SEXP object, SEXP *args, int nargs)
std::string get_typeinfo_name()
Rcpp::List getMethods(const XP_Class &class_xp, std::string &buffer)
std::pair< const std::string, prop_class * > PROP_PAIR
SEXP getProperty(SEXP field_xp, SEXP object)
bool property_is_readonly(const std::string &p)
bool has_property(const std::string &m)
CppProperty< Class > prop_class
map_vec_signed_method vec_methods
SignedConstructor< Class > signed_constructor_class
std::vector< signed_constructor_class * > vec_signed_constructor
std::vector< signed_factory_class * > vec_signed_factory
Rcpp::IntegerVector methods_arity()
bool has_default_constructor()
#define RCPP_DEBUG_MODULE_1(fmt, MSG)
#define RCPP_DEBUG_MODULE(MSG)
#define RCPP_DEBUG_MODULE_2(fmt, M1, M2)
#define DEMANGLE(__TYPE__)
bool(* ValidConstructor)(SEXP *, int)
bool(* ValidMethod)(SEXP *, int)
StretchyList_Impl & push_back(const T &obj)
Environment_Impl parent() const
attribute_hidden Rcpp::Module * getCurrentScope()