Rcpp Version 0.12.12
NamesProxy.h
Go to the documentation of this file.
1 // Copyright (C) 2013 Romain Francois
2 //
3 // This file is part of Rcpp.
4 //
5 // Rcpp is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // Rcpp is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
17 
18 #ifndef Rcpp_proxy_NamesProxy_h
19 #define Rcpp_proxy_NamesProxy_h
20 
21 namespace Rcpp{
22 
23 template <typename CLASS>
25 public:
26 
27  class NamesProxy : public GenericProxy<NamesProxy> {
28  public:
29  NamesProxy( CLASS& v) : parent(v){} ;
30 
31  /* lvalue uses */
33  if( this != &rhs) set( rhs.get() ) ;
34  return *this ;
35  }
36 
37  template <typename T>
38  NamesProxy& operator=(const T& rhs);
39 
40  template <typename T> operator T() const;
41 
42  private:
43  CLASS& parent;
44 
45  SEXP get() const {
46  return RCPP_GET_NAMES(parent.get__()) ;
47  }
48 
49  void set(SEXP x) {
50  /* check if we can use a fast version */
51  if( TYPEOF(x) == STRSXP && parent.size() == Rf_length(x) ){
52  SEXP y = parent.get__() ;
53  Rf_setAttrib( y, R_NamesSymbol, x ) ;
54  } else {
55  /* use the slower and more flexible version (callback to R) */
56  SEXP namesSym = Rf_install( "names<-" );
57  Shield<SEXP> new_vec( Rcpp_eval(Rf_lang3( namesSym, parent, x ))) ;
58  parent.set__(new_vec);
59  }
60 
61  }
62 
63  } ;
64 
65  class const_NamesProxy : public GenericProxy<const_NamesProxy>{
66  public:
67  const_NamesProxy( const CLASS& v) : parent(v){} ;
68 
69  template <typename T> operator T() const;
70 
71  private:
72  const CLASS& parent;
73 
74  SEXP get() const {
75  return RCPP_GET_NAMES(parent.get__()) ;
76  }
77 
78  } ;
79 
81  return NamesProxy( static_cast<CLASS&>(*this) ) ;
82  }
83 
84  const_NamesProxy names() const {
85  return const_NamesProxy(static_cast<const CLASS&>(*this) ) ;
86  }
87 
88 
89 } ;
90 
91 }
92 
93 #endif
NamesProxy names()
Definition: NamesProxy.h:80
SEXP Rcpp_eval(SEXP expr, SEXP env)
Definition: Rcpp_eval.h:25
const_NamesProxy names() const
Definition: NamesProxy.h:84
Rcpp API.
Definition: algo.h:28
#define RCPP_GET_NAMES(x)
Definition: macros.h:26
NamesProxy & operator=(const NamesProxy &rhs)
Definition: NamesProxy.h:32