Skip to content

Commit e16f375

Browse files
committed
More fixing of code smells, related to #146
1 parent 6414323 commit e16f375

File tree

4 files changed

+59
-48
lines changed

4 files changed

+59
-48
lines changed

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
2014-05-18 Kevin Ushey <[email protected]>
22

33
* inst/include/Rcpp/vector/Vector.h: Safer casting to fix #146
4+
* inst/include/Rcpp/Environment.h: Idem
5+
* inst/include/Rcpp/api/meat/Environment.h: Idem
6+
* inst/include/Rcpp/vector/Vector.h: Idem
47

58
2014-05-10 Dirk Eddelbuettel <[email protected]>
69

inst/include/Rcpp/Environment.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ namespace Rcpp{
5252
* if the SEXP is not an environment, and exception is thrown
5353
*/
5454
Environment_Impl(SEXP x) {
55-
Storage::set__( as_environment(x) ) ;
55+
Shield<SEXP> env(as_environment(x));
56+
Storage::set__(env) ;
5657
}
5758

5859
/**

inst/include/Rcpp/api/meat/Environment.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,16 @@ bool Environment_Impl<StoragePolicy>::assign( const std::string& name, const WRA
3232

3333
template <template <class> class StoragePolicy>
3434
Environment_Impl<StoragePolicy>::Environment_Impl( const std::string& name ){
35-
Storage::set__( as_environment( wrap(name) ) ) ;
35+
Shield<SEXP> wrapped(wrap(name));
36+
Shield<SEXP> env(as_environment(wrapped));
37+
Storage::set__(env) ;
3638
}
3739

3840
template <template <class> class StoragePolicy>
3941
Environment_Impl<StoragePolicy>::Environment_Impl( int pos ){
40-
Storage::set__( as_environment( wrap(pos) ) ) ;
42+
Shield<SEXP> wrapped(wrap(pos));
43+
Shield<SEXP> env(as_environment(wrapped));
44+
Storage::set__(env) ;
4145
}
4246

4347

inst/include/Rcpp/vector/Vector.h

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Vector :
4949
typedef typename traits::init_type<RTYPE>::type init_type ;
5050
typedef typename traits::r_vector_element_converter<RTYPE>::type converter_type ;
5151
typedef typename traits::storage_type<RTYPE>::type stored_type ;
52-
52+
5353
/**
5454
* Default constructor. Creates a vector of the appropriate type
5555
* and 0 length
@@ -96,7 +96,7 @@ class Vector :
9696
RCPP_DEBUG_2( "Vector<%d>( const char* = %s )", RTYPE, st )
9797
Storage::set__(internal::vector_from_string<RTYPE>(st) ) ;
9898
}
99-
99+
100100
Vector( const int& siz, stored_type (*gen)(void) ) {
101101
RCPP_DEBUG_2( "Vector<%d>( const int& siz = %s, stored_type (*gen)(void) )", RTYPE, siz )
102102
Storage::set__( Rf_allocVector( RTYPE, siz) ) ;
@@ -134,7 +134,7 @@ class Vector :
134134
Vector( const int& size, const U& u) {
135135
RCPP_DEBUG_2( "Vector<%d>( const int& size, const U& u )", RTYPE, size )
136136
Storage::set__( Rf_allocVector( RTYPE, size) ) ;
137-
fill_or_generate( u ) ;
137+
fill_or_generate( u ) ;
138138
}
139139
template <bool NA, typename T>
140140
Vector( const sugar::SingleLogicalResult<NA,T>& obj ) {
@@ -199,13 +199,13 @@ class Vector :
199199
assign( list.begin() , list.end() ) ;
200200
}
201201
#endif
202-
202+
203203
template <typename T>
204204
Vector& operator=( const T& x) {
205205
assign_object( x, typename traits::is_sugar_expression<T>::type() ) ;
206206
return *this ;
207207
}
208-
208+
209209
static inline stored_type get_na() {
210210
return traits::get_na<RTYPE>();
211211
}
@@ -279,7 +279,7 @@ class Vector :
279279
inline iterator end() { return cache.get() + size() ; }
280280
inline const_iterator begin() const{ return cache.get_const() ; }
281281
inline const_iterator end() const{ return cache.get_const() + size() ; }
282-
282+
283283
inline Proxy operator[]( int i ){ return cache.ref(i) ; }
284284
inline const_Proxy operator[]( int i ) const { return cache.ref(i) ; }
285285

@@ -296,14 +296,14 @@ class Vector :
296296
inline const_Proxy operator()( const size_t& i, const size_t& j) const {
297297
return cache.ref( offset(i,j) ) ;
298298
}
299-
299+
300300
inline NameProxy operator[]( const std::string& name ){
301301
return NameProxy( *this, name ) ;
302302
}
303303
inline NameProxy operator()( const std::string& name ){
304304
return NameProxy( *this, name ) ;
305305
}
306-
306+
307307
inline NameProxy operator[]( const std::string& name ) const {
308308
return NameProxy( const_cast<Vector&>(*this), name ) ;
309309
}
@@ -349,8 +349,9 @@ class Vector :
349349
/* FIXME: we can do better than this r_cast to avoid
350350
allocating an unnecessary temporary object
351351
*/
352-
Shield<SEXP> x( r_cast<RTYPE>( wrap( first, last ) ) );
353-
Storage::set__( x) ;
352+
Shield<SEXP> wrapped(wrap(first, last));
353+
Shield<SEXP> casted(r_cast<RTYPE>(wrapped));
354+
Storage::set__(casted) ;
354355
}
355356

356357
template <typename InputIterator>
@@ -364,80 +365,80 @@ class Vector :
364365
static Vector import_transform( InputIterator first, InputIterator last, F f){
365366
return Vector( first, last, f) ;
366367
}
367-
368+
368369
template <typename T>
369370
void push_back( const T& object){
370371
push_back__impl( converter_type::get(object),
371372
typename traits::same_type<stored_type,SEXP>()
372373
) ;
373374
}
374-
375+
375376
template <typename T>
376377
void push_back( const T& object, const std::string& name ){
377378
push_back_name__impl( converter_type::get(object), name,
378379
typename traits::same_type<stored_type,SEXP>()
379380
) ;
380381
}
381-
382+
382383
template <typename T>
383384
void push_front( const T& object){
384385
push_front__impl( converter_type::get(object),
385386
typename traits::same_type<stored_type,SEXP>() ) ;
386387
}
387-
388+
388389
template <typename T>
389390
void push_front( const T& object, const std::string& name){
390391
push_front_name__impl( converter_type::get(object), name,
391392
typename traits::same_type<stored_type,SEXP>() ) ;
392393
}
393-
394-
394+
395+
395396
template <typename T>
396397
iterator insert( iterator position, const T& object){
397398
return insert__impl( position, converter_type::get(object),
398399
typename traits::same_type<stored_type,SEXP>()
399400
) ;
400401
}
401-
402+
402403
template <typename T>
403404
iterator insert( int position, const T& object){
404405
return insert__impl( cache.get() + position, converter_type::get(object),
405406
typename traits::same_type<stored_type,SEXP>()
406407
);
407408
}
408-
409+
409410
iterator erase( int position){
410411
return erase_single__impl( cache.get() + position) ;
411412
}
412-
413+
413414
iterator erase( iterator position){
414415
return erase_single__impl( position ) ;
415416
}
416-
417+
417418
iterator erase( int first, int last){
418419
iterator start = cache.get() ;
419420
return erase_range__impl( start + first, start + last ) ;
420421
}
421-
422+
422423
iterator erase( iterator first, iterator last){
423424
return erase_range__impl( first, last ) ;
424425
}
425-
426+
426427
void update(SEXP){
427428
cache.update(*this) ;
428429
}
429-
430+
430431
template <typename U>
431432
static void replace_element( iterator it, SEXP names, int index, const U& u){
432433
replace_element__dispatch( typename traits::is_named<U>::type(),
433434
it, names, index, u ) ;
434435
}
435-
436+
436437
template <typename U>
437438
static void replace_element__dispatch( traits::false_type, iterator it, SEXP names, int index, const U& u){
438439
*it = converter_type::get(u);
439440
}
440-
441+
441442
template <typename U>
442443
static void replace_element__dispatch( traits::true_type, iterator it, SEXP names, int index, const U& u){
443444
replace_element__dispatch__isArgument( typename traits::same_type<U,Argument>(), it, names, index, u ) ;
@@ -446,21 +447,21 @@ class Vector :
446447
template <typename U>
447448
static void replace_element__dispatch__isArgument( traits::false_type, iterator it, SEXP names, int index, const U& u){
448449
RCPP_DEBUG_2( " Vector::replace_element__dispatch<%s>(true, index= %d) ", DEMANGLE(U), index ) ;
449-
450+
450451
*it = converter_type::get(u.object ) ;
451452
SET_STRING_ELT( names, index, ::Rf_mkChar( u.name.c_str() ) ) ;
452453
}
453-
454+
454455
template <typename U>
455456
static void replace_element__dispatch__isArgument( traits::true_type, iterator it, SEXP names, int index, const U& u){
456457
RCPP_DEBUG_2( " Vector::replace_element__dispatch<%s>(true, index= %d) ", DEMANGLE(U), index ) ;
457-
458+
458459
*it = R_MissingArg ;
459460
SET_STRING_ELT( names, index, ::Rf_mkChar( u.name.c_str() ) ) ;
460461
}
461462

462463
typedef internal::RangeIndexer<RTYPE,true,Vector> Indexer ;
463-
464+
464465
inline Indexer operator[]( const Range& range ){
465466
return Indexer( const_cast<Vector&>(*this), range );
466467
}
@@ -589,7 +590,7 @@ class Vector :
589590
*target_it = object;
590591
Storage::set__( target.get__() ) ;
591592
}
592-
593+
593594
void push_back_name__impl(const stored_type& object, const std::string& name, traits::true_type ) {
594595
Shield<SEXP> object_sexp( object ) ;
595596
int n = size() ;
@@ -613,7 +614,7 @@ class Vector :
613614
}
614615
SET_STRING_ELT( newnames, i, Rf_mkChar( name.c_str() ) );
615616
target.attr("names") = newnames ;
616-
617+
617618
*target_it = object_sexp;
618619
Storage::set__( target.get__() ) ;
619620
}
@@ -640,11 +641,11 @@ class Vector :
640641
}
641642
SET_STRING_ELT( newnames, i, Rf_mkChar( name.c_str() ) );
642643
target.attr("names") = newnames ;
643-
644+
644645
*target_it = object;
645646
Storage::set__( target.get__() ) ;
646647
}
647-
648+
648649
void push_front__impl(const stored_type& object, traits::true_type ) {
649650
Shield<SEXP> object_sexp( object ) ;
650651
int n = size() ;
@@ -698,7 +699,7 @@ class Vector :
698699
Storage::set__( target.get__() ) ;
699700

700701
}
701-
702+
702703
void push_front_name__impl(const stored_type& object, const std::string& name, traits::true_type ) {
703704
Shield<SEXP> object_sexp(object) ;
704705
int n = size() ;
@@ -712,7 +713,7 @@ class Vector :
712713
SET_STRING_ELT( newnames, 0, Rf_mkChar( name.c_str() ) );
713714
*target_it = object_sexp;
714715
++target_it ;
715-
716+
716717
if( Rf_isNull(names) ){
717718
for( ; it < this_end; ++it, ++target_it,i++ ){
718719
*target_it = *it ;
@@ -740,7 +741,7 @@ class Vector :
740741
SET_STRING_ELT( newnames, 0, Rf_mkChar( name.c_str() ) );
741742
*target_it = object;
742743
++target_it ;
743-
744+
744745
if( Rf_isNull(names) ){
745746
for( ; it < this_end; ++it, ++target_it,i++ ){
746747
*target_it = *it ;
@@ -753,11 +754,11 @@ class Vector :
753754
}
754755
}
755756
target.attr("names") = newnames ;
756-
757+
757758
Storage::set__( target.get__() ) ;
758759

759760
}
760-
761+
761762
iterator insert__impl( iterator position, const stored_type& object_, traits::true_type ) {
762763
Shield<SEXP> object( object_ ) ;
763764
int n = size() ;
@@ -838,7 +839,7 @@ class Vector :
838839
Storage::set__( target.get__() ) ;
839840
return result ;
840841
}
841-
842+
842843
iterator erase_single__impl( iterator position ) {
843844
if( position < begin() || position > end() ) throw index_out_of_bounds( ) ;
844845
int n = size() ;
@@ -877,18 +878,18 @@ class Vector :
877878
return begin()+result ;
878879
}
879880
}
880-
881+
881882
iterator erase_range__impl( iterator first, iterator last ) {
882883
if( first > last ) throw std::range_error("invalid range") ;
883884
if( last > end() || first < begin() ) throw index_out_of_bounds() ;
884-
885+
885886
iterator it = begin() ;
886887
iterator this_end = end() ;
887888
int nremoved = std::distance(first,last) ;
888889
int target_size = size() - nremoved ;
889890
Vector target( target_size ) ;
890891
iterator target_it = target.begin() ;
891-
892+
892893
SEXP names = RCPP_GET_NAMES(Storage::get__()) ;
893894
iterator result ;
894895
if( Rf_isNull(names) ){
@@ -928,7 +929,9 @@ class Vector :
928929
import_expression<T>(x, n ) ;
929930
} else{
930931
// different size, so we change the memory
931-
Storage::set__( r_cast<RTYPE>( wrap(x) ) );
932+
Shield<SEXP> wrapped(wrap(x));
933+
Shield<SEXP> casted(r_cast<RTYPE>(wrapped));
934+
Storage::set__(casted);
932935
}
933936
}
934937

@@ -945,7 +948,7 @@ class Vector :
945948
Shield<SEXP> casted(r_cast<RTYPE>(wrapped));
946949
Storage::set__(casted);
947950
}
948-
951+
949952
// we are importing a real sugar expression, i.e. not a vector
950953
template <bool NA, typename VEC>
951954
inline void import_sugar_expression( const Rcpp::VectorBase<RTYPE,NA,VEC>& other, traits::false_type ) {
@@ -995,7 +998,7 @@ class Vector :
995998
*it = ::Rf_duplicate( elem ) ;
996999
}
9971000
}
998-
1001+
9991002
template <typename U>
10001003
void fill__dispatch( traits::true_type, const U& u){
10011004
std::fill( begin(), end(), converter_type::get( u ) ) ;

0 commit comments

Comments
 (0)