[Rcpp-devel] vector<int>

Romain François francoisromain at free.fr
Mon Dec 21 20:17:59 CET 2009


On 12/21/2009 04:02 PM, Romain François wrote:
>> |
>> | The hairy bit is probably the first line of the code :
>> |
>> | std::vector<int>   *p = (std::vector<int>
>> | *)EXTPTR_PTR(R_do_slot(v,Rf_install("pointer"))) ;
>>
>> Yikes :)   That doesn't exactly hide the C/C++ complexity from users :)
>
> Yes, I know. Reading the books, I was kind of thinking about doing some
> sort of "smart external pointer".
>
> SEXP fun( SEXP x){
> 	external_ptr<  vector<int>  >  p = x ;
> 	p->size() ; /* ... */
> }
>
> but I'm not sure how to do this just yet, or if it is even possible.

I've now got a template class external_pointer that looks like this:


template <class T>
class external_pointer {
	public:
   		explicit external_pointer(SEXP xp) ;
   		
   		T& operator*() const ;
   		T* operator->() const ;
   		T* pointer ;
   	
   	private:
   		external_pointer() ;


with this implementation :


template<class T>
external_pointer<T>::external_pointer(SEXP xp){
	Rprintf( "external_pointer(SEXP)\n" ) ;
	pointer = (T*)EXTPTR_PTR(xp) ;
}

template<class T>
external_pointer<T>::external_pointer(){
	Rprintf( "external_pointer()\n" ) ;
	pointer = (T*)0 ;
}

template<class T>
T& external_pointer<T>::operator*() const {
	return *pointer ;
}

template<class T>
T* external_pointer<T>::operator->() const {
	return pointer ;
}



so the idea would be to grab the SEXP like this:

SEXP bar( SEXP xp ){
	external_pointer< std::vector<int> > p(xp) ;
	// trying the -> operator
	Rprintf( "size() = %d\n", p->size()  ) ;
	
	// trying the * operator
	std::vector<int>& v = *p ;
	Rprintf( "size() = %d\n", v.size()  ) ;
	
	return R_NilValue ;
}

I wanted to get some implicit conversion going so that I could declare 
the function like this :

SEXP bar( external_pointer< std::vector<int> > )

but I don't understand how to do it ... nevermind.

Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/HlX9 : new package : bibtex
|- http://tr.im/Gq7i : ohloh
`- http://tr.im/FtUu : new package : highlight





More information about the Rcpp-devel mailing list