Skip to content

Commit 16f62cf

Browse files
more safe about class_pointer. #67
1 parent e1e885f commit 16f62cf

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

inst/include/Rcpp/module/class.h

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
typedef std::pair<const std::string,prop_class*> PROP_PAIR ;
5353

5454
class_( const char* name_, const char* doc = 0) :
55-
class_Base(name_, 0),
55+
class_Base(name_, doc),
5656
vec_methods(),
5757
properties(),
5858
finalizer_pointer(0),
@@ -62,17 +62,32 @@
6262
class_pointer(0),
6363
typeinfo_name("")
6464
{
65-
Rcpp::Module* module = getCurrentScope() ;
66-
if( ! module->has_class(name_) ){
65+
class_pointer = get_instance() ;
66+
}
67+
68+
private:
69+
70+
self* get_instance() {
71+
// check if we already have it
72+
if( class_pointer ) return class_pointer ;
73+
74+
// check if it exists in the module
75+
Module* module = getCurrentScope() ;
76+
if( module->has_class(name) ){
77+
class_pointer = dynamic_cast<self*>( module->get_class_pointer(name) ) ;
78+
} else {
6779
class_pointer = new self ;
68-
class_pointer->name = name_ ;
69-
class_pointer->docstring = std::string( doc == 0 ? "" : doc );
80+
class_pointer->name = name ;
81+
class_pointer->docstring = docstring ;
7082
class_pointer->finalizer_pointer = new finalizer_class ;
7183
class_pointer->typeinfo_name = typeid(Class).name() ;
72-
module->AddClass( name_, class_pointer ) ;
73-
}
84+
module->AddClass( name.c_str(), class_pointer ) ;
85+
}
86+
return class_pointer ;
7487
}
75-
88+
89+
public:
90+
7691
~class_(){}
7792

7893
self& AddConstructor( constructor_class* ctor, ValidConstructor valid, const char* docstring = 0 ){
@@ -216,17 +231,18 @@
216231

217232
self& AddMethod( const char* name_, method_class* m, ValidMethod valid = &yes, const char* docstring = 0){
218233
RCPP_DEBUG_1( "AddMethod( %s, method_class* m, ValidMethod valid = &yes, const char* docstring = 0", name_ )
219-
typename map_vec_signed_method::iterator it = class_pointer->vec_methods.find( name_ ) ;
220-
if( it == class_pointer->vec_methods.end() ){
221-
it = class_pointer->vec_methods.insert( vec_signed_method_pair( name_, new vec_signed_method() ) ).first ;
234+
self* ptr = get_instance() ;
235+
typename map_vec_signed_method::iterator it = ptr->vec_methods.find( name_ ) ;
236+
if( it == ptr->vec_methods.end() ){
237+
it = ptr->vec_methods.insert( vec_signed_method_pair( name_, new vec_signed_method() ) ).first ;
222238
}
223239
(it->second)->push_back( new signed_method_class(m, valid, docstring ) ) ;
224-
if( *name_ == '[' ) class_pointer->specials++ ;
240+
if( *name_ == '[' ) ptr->specials++ ;
225241
return *this ;
226242
}
227243

228244
self& AddProperty( const char* name_, prop_class* p){
229-
class_pointer->properties.insert( PROP_PAIR( name_, p ) ) ;
245+
get_instance()->properties.insert( PROP_PAIR( name_, p ) ) ;
230246
return *this ;
231247
}
232248

@@ -439,8 +455,9 @@
439455
}
440456

441457
void SetFinalizer( finalizer_class* f ){
442-
if( class_pointer->finalizer_pointer ) delete class_pointer->finalizer_pointer ;
443-
class_pointer->finalizer_pointer = f ;
458+
self* ptr = get_instance() ;
459+
if( ptr->finalizer_pointer ) delete ptr->finalizer_pointer ;
460+
ptr->finalizer_pointer = f ;
444461
}
445462

446463
map_vec_signed_method vec_methods ;
@@ -505,7 +522,7 @@
505522
}
506523

507524
std::string buffer( "Rcpp_" ) ; buffer += parent ;
508-
class_pointer->parents.push_back( buffer.c_str() ) ;
525+
get_instance()->parents.push_back( buffer.c_str() ) ;
509526
return *this ;
510527
}
511528

0 commit comments

Comments
 (0)