|
52 | 52 | typedef std::pair<const std::string,prop_class*> PROP_PAIR ; |
53 | 53 |
|
54 | 54 | class_( const char* name_, const char* doc = 0) : |
55 | | - class_Base(name_, 0), |
| 55 | + class_Base(name_, doc), |
56 | 56 | vec_methods(), |
57 | 57 | properties(), |
58 | 58 | finalizer_pointer(0), |
|
62 | 62 | class_pointer(0), |
63 | 63 | typeinfo_name("") |
64 | 64 | { |
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 { |
67 | 79 | 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 ; |
70 | 82 | class_pointer->finalizer_pointer = new finalizer_class ; |
71 | 83 | 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 ; |
74 | 87 | } |
75 | | - |
| 88 | + |
| 89 | + public: |
| 90 | + |
76 | 91 | ~class_(){} |
77 | 92 |
|
78 | 93 | self& AddConstructor( constructor_class* ctor, ValidConstructor valid, const char* docstring = 0 ){ |
|
216 | 231 |
|
217 | 232 | self& AddMethod( const char* name_, method_class* m, ValidMethod valid = &yes, const char* docstring = 0){ |
218 | 233 | 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 ; |
222 | 238 | } |
223 | 239 | (it->second)->push_back( new signed_method_class(m, valid, docstring ) ) ; |
224 | | - if( *name_ == '[' ) class_pointer->specials++ ; |
| 240 | + if( *name_ == '[' ) ptr->specials++ ; |
225 | 241 | return *this ; |
226 | 242 | } |
227 | 243 |
|
228 | 244 | 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 ) ) ; |
230 | 246 | return *this ; |
231 | 247 | } |
232 | 248 |
|
|
439 | 455 | } |
440 | 456 |
|
441 | 457 | 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 ; |
444 | 461 | } |
445 | 462 |
|
446 | 463 | map_vec_signed_method vec_methods ; |
|
505 | 522 | } |
506 | 523 |
|
507 | 524 | std::string buffer( "Rcpp_" ) ; buffer += parent ; |
508 | | - class_pointer->parents.push_back( buffer.c_str() ) ; |
| 525 | + get_instance()->parents.push_back( buffer.c_str() ) ; |
509 | 526 | return *this ; |
510 | 527 | } |
511 | 528 |
|
|
0 commit comments